PyXR

c:\python24\lib \ email \ Parser.py



0001 # Copyright (C) 2001-2004 Python Software Foundation
0002 # Author: Barry Warsaw, Thomas Wouters, Anthony Baxter
0003 # Contact: email-sig@python.org
0004 
0005 """A parser of RFC 2822 and MIME email messages."""
0006 
0007 import warnings
0008 from cStringIO import StringIO
0009 from email.FeedParser import FeedParser
0010 from email.Message import Message
0011 
0012 
0013 
0014 class Parser:
0015     def __init__(self, *args, **kws):
0016         """Parser of RFC 2822 and MIME email messages.
0017 
0018         Creates an in-memory object tree representing the email message, which
0019         can then be manipulated and turned over to a Generator to return the
0020         textual representation of the message.
0021 
0022         The string must be formatted as a block of RFC 2822 headers and header
0023         continuation lines, optionally preceeded by a `Unix-from' header.  The
0024         header block is terminated either by the end of the string or by a
0025         blank line.
0026 
0027         _class is the class to instantiate for new message objects when they
0028         must be created.  This class must have a constructor that can take
0029         zero arguments.  Default is Message.Message.
0030         """
0031         if len(args) >= 1:
0032             if '_class' in kws:
0033                 raise TypeError("Multiple values for keyword arg '_class'")
0034             kws['_class'] = args[0]
0035         if len(args) == 2:
0036             if 'strict' in kws:
0037                 raise TypeError("Multiple values for keyword arg 'strict'")
0038             kws['strict'] = args[1]
0039         if len(args) > 2:
0040             raise TypeError('Too many arguments')
0041         if '_class' in kws:
0042             self._class = kws['_class']
0043             del kws['_class']
0044         else:
0045             self._class = Message
0046         if 'strict' in kws:
0047             warnings.warn("'strict' argument is deprecated (and ignored)",
0048                           DeprecationWarning, 2)
0049             del kws['strict']
0050         if kws:
0051             raise TypeError('Unexpected keyword arguments')
0052 
0053     def parse(self, fp, headersonly=False):
0054         """Create a message structure from the data in a file.
0055 
0056         Reads all the data from the file and returns the root of the message
0057         structure.  Optional headersonly is a flag specifying whether to stop
0058         parsing after reading the headers or not.  The default is False,
0059         meaning it parses the entire contents of the file.
0060         """
0061         feedparser = FeedParser(self._class)
0062         if headersonly:
0063             feedparser._set_headersonly()
0064         while True:
0065             data = fp.read(8192)
0066             if not data:
0067                 break
0068             feedparser.feed(data)
0069         return feedparser.close()
0070 
0071     def parsestr(self, text, headersonly=False):
0072         """Create a message structure from a string.
0073 
0074         Returns the root of the message structure.  Optional headersonly is a
0075         flag specifying whether to stop parsing after reading the headers or
0076         not.  The default is False, meaning it parses the entire contents of
0077         the file.
0078         """
0079         return self.parse(StringIO(text), headersonly=headersonly)
0080 
0081 
0082 
0083 class HeaderParser(Parser):
0084     def parse(self, fp, headersonly=True):
0085         return Parser.parse(self, fp, True)
0086 
0087     def parsestr(self, text, headersonly=True):
0088         return Parser.parsestr(self, text, True)
0089 

Generated by PyXR 0.9.4
SourceForge.net Logo