1 # Copyright (C) 2001-2006 Python Software Foundation
2 # Author: Barry Warsaw, Thomas Wouters, Anthony Baxter
3 # Contact: email-sig@python.org
5 """A parser of RFC 2822 and MIME email messages."""
7 __all__ = ['Parser', 'HeaderParser']
10 from cStringIO import StringIO
12 from email.feedparser import FeedParser
13 from email.message import Message
18 def __init__(self, *args, **kws):
19 """Parser of RFC 2822 and MIME email messages.
21 Creates an in-memory object tree representing the email message, which
22 can then be manipulated and turned over to a Generator to return the
23 textual representation of the message.
25 The string must be formatted as a block of RFC 2822 headers and header
26 continuation lines, optionally preceeded by a `Unix-from' header. The
27 header block is terminated either by the end of the string or by a
30 _class is the class to instantiate for new message objects when they
31 must be created. This class must have a constructor that can take
32 zero arguments. Default is Message.Message.
36 raise TypeError("Multiple values for keyword arg '_class'")
37 kws['_class'] = args[0]
40 raise TypeError("Multiple values for keyword arg 'strict'")
41 kws['strict'] = args[1]
43 raise TypeError('Too many arguments')
45 self._class = kws['_class']
50 warnings.warn("'strict' argument is deprecated (and ignored)",
51 DeprecationWarning, 2)
54 raise TypeError('Unexpected keyword arguments')
56 def parse(self, fp, headersonly=False):
57 """Create a message structure from the data in a file.
59 Reads all the data from the file and returns the root of the message
60 structure. Optional headersonly is a flag specifying whether to stop
61 parsing after reading the headers or not. The default is False,
62 meaning it parses the entire contents of the file.
64 feedparser = FeedParser(self._class)
66 feedparser._set_headersonly()
72 return feedparser.close()
74 def parsestr(self, text, headersonly=False):
75 """Create a message structure from a string.
77 Returns the root of the message structure. Optional headersonly is a
78 flag specifying whether to stop parsing after reading the headers or
79 not. The default is False, meaning it parses the entire contents of
82 return self.parse(StringIO(text), headersonly=headersonly)
86 class HeaderParser(Parser):
87 def parse(self, fp, headersonly=True):
88 return Parser.parse(self, fp, True)
90 def parsestr(self, text, headersonly=True):
91 return Parser.parsestr(self, text, True)