12.3.2 Parsing MIME documents

The mimelib.Parser class (hereafter referred to as Parser is used to take a message in flat text form and create the associated object model. The resulting object model can then be manipulated using the Message class interface and turned over to a mimelib.Generator class to return the textual representation of the message. It is intended that the Parser to Generator path be idempotent if the object model isn't edited in between.

The constructor for the Parser class takes a single optional argument _class. This must be callable factory (i.e. a function or a class), and it is used whenever a sub-message object needs to be created. It defaults to Message. _class will be called with zero arguments.

The other public Parser methods are:

parse (fp)
Read all the data from the file-like object fp, parse the resulting text, and return the root message object. fp must support both the readline() and the read() methods on file-like objects.

The text contained in fp must be formatted as a block of RFC 822 style headers and header continuation lines, optionally preceeded by a ``Unix-From'' header. The header block is terminated either by the end of the data or by a blank line. Following the header block is the body of the message (which may contain MIME encoded sub-messages).

parsestr (text)
Similar to the parse() method, except it takes a string object instead of a file-like object. Calling this method on a string is exactly equivalent to wrapping text in a StringIO instance first and calling parse().

See About this document... for information on suggesting changes.