1
# Copyright (C) 2001-2004 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."""
8
from cStringIO import StringIO
9
from email.FeedParser import FeedParser
10
from email.Message import Message
15
def __init__(self, *args, **kws):
16
"""Parser of RFC 2822 and MIME email messages.
18
Creates an in-memory object tree representing the email message, which
19
can then be manipulated and turned over to a Generator to return the
20
textual representation of the message.
22
The string must be formatted as a block of RFC 2822 headers and header
23
continuation lines, optionally preceeded by a `Unix-from' header. The
24
header block is terminated either by the end of the string or by a
27
_class is the class to instantiate for new message objects when they
28
must be created. This class must have a constructor that can take
29
zero arguments. Default is Message.Message.
33
raise TypeError("Multiple values for keyword arg '_class'")
34
kws['_class'] = args[0]
37
raise TypeError("Multiple values for keyword arg 'strict'")
38
kws['strict'] = args[1]
40
raise TypeError('Too many arguments')
42
self._class = kws['_class']
47
warnings.warn("'strict' argument is deprecated (and ignored)",
48
DeprecationWarning, 2)
51
raise TypeError('Unexpected keyword arguments')
53
def parse(self, fp, headersonly=False):
54
"""Create a message structure from the data in a file.
56
Reads all the data from the file and returns the root of the message
57
structure. Optional headersonly is a flag specifying whether to stop
58
parsing after reading the headers or not. The default is False,
59
meaning it parses the entire contents of the file.
61
feedparser = FeedParser(self._class)
63
feedparser._set_headersonly()
69
return feedparser.close()
71
def parsestr(self, text, headersonly=False):
72
"""Create a message structure from a string.
74
Returns the root of the message structure. Optional headersonly is a
75
flag specifying whether to stop parsing after reading the headers or
76
not. The default is False, meaning it parses the entire contents of
79
return self.parse(StringIO(text), headersonly=headersonly)
83
class HeaderParser(Parser):
84
def parse(self, fp, headersonly=True):
85
return Parser.parse(self, fp, True)
87
def parsestr(self, text, headersonly=True):
88
return Parser.parsestr(self, text, True)