Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame^] | 1 | \section{\module{email.Parser} --- |
| 2 | Parsing flat text email messages} |
| 3 | |
| 4 | \declaremodule{standard}{email.Parser} |
| 5 | \modulesynopsis{Parse flat text email messages to produce a message |
| 6 | object tree.} |
| 7 | \sectionauthor{Barry A. Warsaw}{barry@zope.com} |
| 8 | |
| 9 | \versionadded{2.2} |
| 10 | |
| 11 | The \module{Parser} module provides a single class, the \class{Parser} |
| 12 | class, which is used to take a message in flat text form and create |
| 13 | the associated object model. The resulting object tree can then be |
| 14 | manipulated using the \class{Message} class interface as described in |
| 15 | \refmodule{email.Message}, and turned over |
| 16 | to a generator (as described in \refmodule{emamil.Generator}) to |
| 17 | return the textual representation of the message. It is intended that |
| 18 | the \class{Parser} to \class{Generator} path be idempotent if the |
| 19 | object model isn't modified in between. |
| 20 | |
| 21 | \subsection{Parser class API} |
| 22 | |
| 23 | \begin{classdesc}{Parser}{\optional{_class}} |
| 24 | The constructor for the \class{Parser} class takes a single optional |
| 25 | argument \var{_class}. This must be callable factory (i.e. a function |
| 26 | or a class), and it is used whenever a sub-message object needs to be |
| 27 | created. It defaults to \class{Message} (see |
| 28 | \refmodule{email.Message}). \var{_class} will be called with zero |
| 29 | arguments. |
| 30 | \end{classdesc} |
| 31 | |
| 32 | The other public \class{Parser} methods are: |
| 33 | |
| 34 | \begin{methoddesc}[Parser]{parse}{fp} |
| 35 | Read all the data from the file-like object \var{fp}, parse the |
| 36 | resulting text, and return the root message object. \var{fp} must |
| 37 | support both the \method{readline()} and the \method{read()} methods |
| 38 | on file-like objects. |
| 39 | |
| 40 | The text contained in \var{fp} must be formatted as a block of \rfc{2822} |
| 41 | style headers and header continuation lines, optionally preceeded by a |
| 42 | \emph{Unix-From} header. The header block is terminated either by the |
| 43 | end of the data or by a blank line. Following the header block is the |
| 44 | body of the message (which may contain MIME-encoded subparts). |
| 45 | \end{methoddesc} |
| 46 | |
| 47 | \begin{methoddesc}[Parser]{parsestr}{text} |
| 48 | Similar to the \method{parse()} method, except it takes a string |
| 49 | object instead of a file-like object. Calling this method on a string |
| 50 | is exactly equivalent to wrapping \var{text} in a \class{StringIO} |
| 51 | instance first and calling \method{parse()}. |
| 52 | \end{methoddesc} |
| 53 | |
| 54 | Since creating a message object tree from a string or a file object is |
| 55 | such a common task, two functions are provided as a convenience. They |
| 56 | are available in the top-level \module{email} package namespace. |
| 57 | |
| 58 | \begin{funcdesc}{message_from_string}{s\optional{, _class}} |
| 59 | Return a message object tree from a string. This is exactly |
| 60 | equivalent to \code{Parser().parsestr(s)}. Optional \var{_class} is |
| 61 | interpreted as with the \class{Parser} class constructor. |
| 62 | \end{funcdesc} |
| 63 | |
| 64 | \begin{funcdesc}{message_from_file}{fp\optional{, _class}} |
| 65 | Return a message object tree from an open file object. This is exactly |
| 66 | equivalent to \code{Parser().parse(fp)}. Optional \var{_class} is |
| 67 | interpreted as with the \class{Parser} class constructor. |
| 68 | \end{funcdesc} |
| 69 | |
| 70 | Here's an example of how you might use this at an interactive Python |
| 71 | prompt: |
| 72 | |
| 73 | \begin{verbatim} |
| 74 | >>> import email |
| 75 | >>> msg = email.message_from_string(myString) |
| 76 | \end{verbatim} |
| 77 | |
| 78 | \subsection{Additional notes} |
| 79 | |
| 80 | Here are some notes on the parsing semantics: |
| 81 | |
| 82 | \begin{itemize} |
| 83 | \item Most non-\code{multipart} type messages are parsed as a single |
| 84 | message object with a string payload. These objects will return |
| 85 | 0 for \method{is_multipart()}. |
| 86 | \item One exception is for \code{message/delivery-status} type |
| 87 | messages. Because such the body of such messages consist of |
| 88 | blocks of headers, \class{Parser} will create a non-multipart |
| 89 | object containing non-multipart subobjects for each header |
| 90 | block. |
| 91 | \item Another exception is for \code{message/*} types (i.e. more |
| 92 | general than \code{message/delivery-status}. These are |
| 93 | typically \code{message/rfc822} type messages, represented as a |
| 94 | non-multipart object containing a singleton payload, another |
| 95 | non-multipart \class{Message} instance. |
| 96 | \end{itemize} |