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