symbian-qemu-0.9.1-12/python-win32-2.6.1/lib/email/parser.py
changeset 1 2fb8b9db1c86
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     1 # Copyright (C) 2001-2006 Python Software Foundation
       
     2 # Author: Barry Warsaw, Thomas Wouters, Anthony Baxter
       
     3 # Contact: email-sig@python.org
       
     4 
       
     5 """A parser of RFC 2822 and MIME email messages."""
       
     6 
       
     7 __all__ = ['Parser', 'HeaderParser']
       
     8 
       
     9 import warnings
       
    10 from cStringIO import StringIO
       
    11 
       
    12 from email.feedparser import FeedParser
       
    13 from email.message import Message
       
    14 
       
    15 
       
    16 
       
    17 class Parser:
       
    18     def __init__(self, *args, **kws):
       
    19         """Parser of RFC 2822 and MIME email messages.
       
    20 
       
    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.
       
    24 
       
    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
       
    28         blank line.
       
    29 
       
    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.
       
    33         """
       
    34         if len(args) >= 1:
       
    35             if '_class' in kws:
       
    36                 raise TypeError("Multiple values for keyword arg '_class'")
       
    37             kws['_class'] = args[0]
       
    38         if len(args) == 2:
       
    39             if 'strict' in kws:
       
    40                 raise TypeError("Multiple values for keyword arg 'strict'")
       
    41             kws['strict'] = args[1]
       
    42         if len(args) > 2:
       
    43             raise TypeError('Too many arguments')
       
    44         if '_class' in kws:
       
    45             self._class = kws['_class']
       
    46             del kws['_class']
       
    47         else:
       
    48             self._class = Message
       
    49         if 'strict' in kws:
       
    50             warnings.warn("'strict' argument is deprecated (and ignored)",
       
    51                           DeprecationWarning, 2)
       
    52             del kws['strict']
       
    53         if kws:
       
    54             raise TypeError('Unexpected keyword arguments')
       
    55 
       
    56     def parse(self, fp, headersonly=False):
       
    57         """Create a message structure from the data in a file.
       
    58 
       
    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.
       
    63         """
       
    64         feedparser = FeedParser(self._class)
       
    65         if headersonly:
       
    66             feedparser._set_headersonly()
       
    67         while True:
       
    68             data = fp.read(8192)
       
    69             if not data:
       
    70                 break
       
    71             feedparser.feed(data)
       
    72         return feedparser.close()
       
    73 
       
    74     def parsestr(self, text, headersonly=False):
       
    75         """Create a message structure from a string.
       
    76 
       
    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
       
    80         the file.
       
    81         """
       
    82         return self.parse(StringIO(text), headersonly=headersonly)
       
    83 
       
    84 
       
    85 
       
    86 class HeaderParser(Parser):
       
    87     def parse(self, fp, headersonly=True):
       
    88         return Parser.parse(self, fp, True)
       
    89 
       
    90     def parsestr(self, text, headersonly=True):
       
    91         return Parser.parsestr(self, text, True)