0c05224489e2df45b0f517308cde6158a3fadd4e
# Copyright (C) 2001-2004 Python Software Foundation
# Author: Barry Warsaw, Thomas Wouters, Anthony Baxter
# Contact: email-sig@python.org
"""A parser of RFC 2822 and MIME email messages."""
from cStringIO
import StringIO
from email
.FeedParser
import FeedParser
from email
.Message
import Message
def __init__(self
, *args
, **kws
):
"""Parser of RFC 2822 and MIME email messages.
Creates an in-memory object tree representing the email message, which
can then be manipulated and turned over to a Generator to return the
textual representation of the message.
The string must be formatted as a block of RFC 2822 headers and header
continuation lines, optionally preceeded by a `Unix-from' header. The
header block is terminated either by the end of the string or by a
_class is the class to instantiate for new message objects when they
must be created. This class must have a constructor that can take
zero arguments. Default is Message.Message.
raise TypeError("Multiple values for keyword arg '_class'")
raise TypeError("Multiple values for keyword arg 'strict'")
raise TypeError('Too many arguments')
self
._class
= kws
['_class']
warnings
.warn("'strict' argument is deprecated (and ignored)",
raise TypeError('Unexpected keyword arguments')
def parse(self
, fp
, headersonly
=False):
"""Create a message structure from the data in a file.
Reads all the data from the file and returns the root of the message
structure. Optional headersonly is a flag specifying whether to stop
parsing after reading the headers or not. The default is False,
meaning it parses the entire contents of the file.
feedparser
= FeedParser(self
._class
)
feedparser
._set
_headersonly
()
return feedparser
.close()
def parsestr(self
, text
, headersonly
=False):
"""Create a message structure from a string.
Returns the root of the message structure. Optional headersonly is a
flag specifying whether to stop parsing after reading the headers or
not. The default is False, meaning it parses the entire contents of
return self
.parse(StringIO(text
), headersonly
=headersonly
)
class HeaderParser(Parser
):
def parse(self
, fp
, headersonly
=True):
return Parser
.parse(self
, fp
, True)
def parsestr(self
, text
, headersonly
=True):
return Parser
.parsestr(self
, text
, True)