+++ /dev/null
-# Copyright (C) 2001-2010 Python Software Foundation\r
-# Contact: email-sig@python.org\r
-\r
-"""Classes to generate plain text from a message object tree."""\r
-\r
-__all__ = ['Generator', 'DecodedGenerator']\r
-\r
-import re\r
-import sys\r
-import time\r
-import random\r
-import warnings\r
-\r
-from cStringIO import StringIO\r
-from email.header import Header\r
-\r
-UNDERSCORE = '_'\r
-NL = '\n'\r
-\r
-fcre = re.compile(r'^From ', re.MULTILINE)\r
-\r
-def _is8bitstring(s):\r
- if isinstance(s, str):\r
- try:\r
- unicode(s, 'us-ascii')\r
- except UnicodeError:\r
- return True\r
- return False\r
-\r
-\r
-\f\r
-class Generator:\r
- """Generates output from a Message object tree.\r
-\r
- This basic generator writes the message to the given file object as plain\r
- text.\r
- """\r
- #\r
- # Public interface\r
- #\r
-\r
- def __init__(self, outfp, mangle_from_=True, maxheaderlen=78):\r
- """Create the generator for message flattening.\r
-\r
- outfp is the output file-like object for writing the message to. It\r
- must have a write() method.\r
-\r
- Optional mangle_from_ is a flag that, when True (the default), escapes\r
- From_ lines in the body of the message by putting a `>' in front of\r
- them.\r
-\r
- Optional maxheaderlen specifies the longest length for a non-continued\r
- header. When a header line is longer (in characters, with tabs\r
- expanded to 8 spaces) than maxheaderlen, the header will split as\r
- defined in the Header class. Set maxheaderlen to zero to disable\r
- header wrapping. The default is 78, as recommended (but not required)\r
- by RFC 2822.\r
- """\r
- self._fp = outfp\r
- self._mangle_from_ = mangle_from_\r
- self._maxheaderlen = maxheaderlen\r
-\r
- def write(self, s):\r
- # Just delegate to the file object\r
- self._fp.write(s)\r
-\r
- def flatten(self, msg, unixfrom=False):\r
- """Print the message object tree rooted at msg to the output file\r
- specified when the Generator instance was created.\r
-\r
- unixfrom is a flag that forces the printing of a Unix From_ delimiter\r
- before the first object in the message tree. If the original message\r
- has no From_ delimiter, a `standard' one is crafted. By default, this\r
- is False to inhibit the printing of any From_ delimiter.\r
-\r
- Note that for subobjects, no From_ line is printed.\r
- """\r
- if unixfrom:\r
- ufrom = msg.get_unixfrom()\r
- if not ufrom:\r
- ufrom = 'From nobody ' + time.ctime(time.time())\r
- print >> self._fp, ufrom\r
- self._write(msg)\r
-\r
- def clone(self, fp):\r
- """Clone this generator with the exact same options."""\r
- return self.__class__(fp, self._mangle_from_, self._maxheaderlen)\r
-\r
- #\r
- # Protected interface - undocumented ;/\r
- #\r
-\r
- def _write(self, msg):\r
- # We can't write the headers yet because of the following scenario:\r
- # say a multipart message includes the boundary string somewhere in\r
- # its body. We'd have to calculate the new boundary /before/ we write\r
- # the headers so that we can write the correct Content-Type:\r
- # parameter.\r
- #\r
- # The way we do this, so as to make the _handle_*() methods simpler,\r
- # is to cache any subpart writes into a StringIO. The we write the\r
- # headers and the StringIO contents. That way, subpart handlers can\r
- # Do The Right Thing, and can still modify the Content-Type: header if\r
- # necessary.\r
- oldfp = self._fp\r
- try:\r
- self._fp = sfp = StringIO()\r
- self._dispatch(msg)\r
- finally:\r
- self._fp = oldfp\r
- # Write the headers. First we see if the message object wants to\r
- # handle that itself. If not, we'll do it generically.\r
- meth = getattr(msg, '_write_headers', None)\r
- if meth is None:\r
- self._write_headers(msg)\r
- else:\r
- meth(self)\r
- self._fp.write(sfp.getvalue())\r
-\r
- def _dispatch(self, msg):\r
- # Get the Content-Type: for the message, then try to dispatch to\r
- # self._handle_<maintype>_<subtype>(). If there's no handler for the\r
- # full MIME type, then dispatch to self._handle_<maintype>(). If\r
- # that's missing too, then dispatch to self._writeBody().\r
- main = msg.get_content_maintype()\r
- sub = msg.get_content_subtype()\r
- specific = UNDERSCORE.join((main, sub)).replace('-', '_')\r
- meth = getattr(self, '_handle_' + specific, None)\r
- if meth is None:\r
- generic = main.replace('-', '_')\r
- meth = getattr(self, '_handle_' + generic, None)\r
- if meth is None:\r
- meth = self._writeBody\r
- meth(msg)\r
-\r
- #\r
- # Default handlers\r
- #\r
-\r
- def _write_headers(self, msg):\r
- for h, v in msg.items():\r
- print >> self._fp, '%s:' % h,\r
- if self._maxheaderlen == 0:\r
- # Explicit no-wrapping\r
- print >> self._fp, v\r
- elif isinstance(v, Header):\r
- # Header instances know what to do\r
- print >> self._fp, v.encode()\r
- elif _is8bitstring(v):\r
- # If we have raw 8bit data in a byte string, we have no idea\r
- # what the encoding is. There is no safe way to split this\r
- # string. If it's ascii-subset, then we could do a normal\r
- # ascii split, but if it's multibyte then we could break the\r
- # string. There's no way to know so the least harm seems to\r
- # be to not split the string and risk it being too long.\r
- print >> self._fp, v\r
- else:\r
- # Header's got lots of smarts, so use it. Note that this is\r
- # fundamentally broken though because we lose idempotency when\r
- # the header string is continued with tabs. It will now be\r
- # continued with spaces. This was reversedly broken before we\r
- # fixed bug 1974. Either way, we lose.\r
- print >> self._fp, Header(\r
- v, maxlinelen=self._maxheaderlen, header_name=h).encode()\r
- # A blank line always separates headers from body\r
- print >> self._fp\r
-\r
- #\r
- # Handlers for writing types and subtypes\r
- #\r
-\r
- def _handle_text(self, msg):\r
- payload = msg.get_payload()\r
- if payload is None:\r
- return\r
- if not isinstance(payload, basestring):\r
- raise TypeError('string payload expected: %s' % type(payload))\r
- if self._mangle_from_:\r
- payload = fcre.sub('>From ', payload)\r
- self._fp.write(payload)\r
-\r
- # Default body handler\r
- _writeBody = _handle_text\r
-\r
- def _handle_multipart(self, msg):\r
- # The trick here is to write out each part separately, merge them all\r
- # together, and then make sure that the boundary we've chosen isn't\r
- # present in the payload.\r
- msgtexts = []\r
- subparts = msg.get_payload()\r
- if subparts is None:\r
- subparts = []\r
- elif isinstance(subparts, basestring):\r
- # e.g. a non-strict parse of a message with no starting boundary.\r
- self._fp.write(subparts)\r
- return\r
- elif not isinstance(subparts, list):\r
- # Scalar payload\r
- subparts = [subparts]\r
- for part in subparts:\r
- s = StringIO()\r
- g = self.clone(s)\r
- g.flatten(part, unixfrom=False)\r
- msgtexts.append(s.getvalue())\r
- # BAW: What about boundaries that are wrapped in double-quotes?\r
- boundary = msg.get_boundary()\r
- if not boundary:\r
- # Create a boundary that doesn't appear in any of the\r
- # message texts.\r
- alltext = NL.join(msgtexts)\r
- boundary = _make_boundary(alltext)\r
- msg.set_boundary(boundary)\r
- # If there's a preamble, write it out, with a trailing CRLF\r
- if msg.preamble is not None:\r
- print >> self._fp, msg.preamble\r
- # dash-boundary transport-padding CRLF\r
- print >> self._fp, '--' + boundary\r
- # body-part\r
- if msgtexts:\r
- self._fp.write(msgtexts.pop(0))\r
- # *encapsulation\r
- # --> delimiter transport-padding\r
- # --> CRLF body-part\r
- for body_part in msgtexts:\r
- # delimiter transport-padding CRLF\r
- print >> self._fp, '\n--' + boundary\r
- # body-part\r
- self._fp.write(body_part)\r
- # close-delimiter transport-padding\r
- self._fp.write('\n--' + boundary + '--')\r
- if msg.epilogue is not None:\r
- print >> self._fp\r
- self._fp.write(msg.epilogue)\r
-\r
- def _handle_multipart_signed(self, msg):\r
- # The contents of signed parts has to stay unmodified in order to keep\r
- # the signature intact per RFC1847 2.1, so we disable header wrapping.\r
- # RDM: This isn't enough to completely preserve the part, but it helps.\r
- old_maxheaderlen = self._maxheaderlen\r
- try:\r
- self._maxheaderlen = 0\r
- self._handle_multipart(msg)\r
- finally:\r
- self._maxheaderlen = old_maxheaderlen\r
-\r
- def _handle_message_delivery_status(self, msg):\r
- # We can't just write the headers directly to self's file object\r
- # because this will leave an extra newline between the last header\r
- # block and the boundary. Sigh.\r
- blocks = []\r
- for part in msg.get_payload():\r
- s = StringIO()\r
- g = self.clone(s)\r
- g.flatten(part, unixfrom=False)\r
- text = s.getvalue()\r
- lines = text.split('\n')\r
- # Strip off the unnecessary trailing empty line\r
- if lines and lines[-1] == '':\r
- blocks.append(NL.join(lines[:-1]))\r
- else:\r
- blocks.append(text)\r
- # Now join all the blocks with an empty line. This has the lovely\r
- # effect of separating each block with an empty line, but not adding\r
- # an extra one after the last one.\r
- self._fp.write(NL.join(blocks))\r
-\r
- def _handle_message(self, msg):\r
- s = StringIO()\r
- g = self.clone(s)\r
- # The payload of a message/rfc822 part should be a multipart sequence\r
- # of length 1. The zeroth element of the list should be the Message\r
- # object for the subpart. Extract that object, stringify it, and\r
- # write it out.\r
- # Except, it turns out, when it's a string instead, which happens when\r
- # and only when HeaderParser is used on a message of mime type\r
- # message/rfc822. Such messages are generated by, for example,\r
- # Groupwise when forwarding unadorned messages. (Issue 7970.) So\r
- # in that case we just emit the string body.\r
- payload = msg.get_payload()\r
- if isinstance(payload, list):\r
- g.flatten(msg.get_payload(0), unixfrom=False)\r
- payload = s.getvalue()\r
- self._fp.write(payload)\r
-\r
-\r
-\f\r
-_FMT = '[Non-text (%(type)s) part of message omitted, filename %(filename)s]'\r
-\r
-class DecodedGenerator(Generator):\r
- """Generates a text representation of a message.\r
-\r
- Like the Generator base class, except that non-text parts are substituted\r
- with a format string representing the part.\r
- """\r
- def __init__(self, outfp, mangle_from_=True, maxheaderlen=78, fmt=None):\r
- """Like Generator.__init__() except that an additional optional\r
- argument is allowed.\r
-\r
- Walks through all subparts of a message. If the subpart is of main\r
- type `text', then it prints the decoded payload of the subpart.\r
-\r
- Otherwise, fmt is a format string that is used instead of the message\r
- payload. fmt is expanded with the following keywords (in\r
- %(keyword)s format):\r
-\r
- type : Full MIME type of the non-text part\r
- maintype : Main MIME type of the non-text part\r
- subtype : Sub-MIME type of the non-text part\r
- filename : Filename of the non-text part\r
- description: Description associated with the non-text part\r
- encoding : Content transfer encoding of the non-text part\r
-\r
- The default value for fmt is None, meaning\r
-\r
- [Non-text (%(type)s) part of message omitted, filename %(filename)s]\r
- """\r
- Generator.__init__(self, outfp, mangle_from_, maxheaderlen)\r
- if fmt is None:\r
- self._fmt = _FMT\r
- else:\r
- self._fmt = fmt\r
-\r
- def _dispatch(self, msg):\r
- for part in msg.walk():\r
- maintype = part.get_content_maintype()\r
- if maintype == 'text':\r
- print >> self, part.get_payload(decode=True)\r
- elif maintype == 'multipart':\r
- # Just skip this\r
- pass\r
- else:\r
- print >> self, self._fmt % {\r
- 'type' : part.get_content_type(),\r
- 'maintype' : part.get_content_maintype(),\r
- 'subtype' : part.get_content_subtype(),\r
- 'filename' : part.get_filename('[no filename]'),\r
- 'description': part.get('Content-Description',\r
- '[no description]'),\r
- 'encoding' : part.get('Content-Transfer-Encoding',\r
- '[no encoding]'),\r
- }\r
-\r
-\r
-\f\r
-# Helper\r
-_width = len(repr(sys.maxint-1))\r
-_fmt = '%%0%dd' % _width\r
-\r
-def _make_boundary(text=None):\r
- # Craft a random boundary. If text is given, ensure that the chosen\r
- # boundary doesn't appear in the text.\r
- token = random.randrange(sys.maxint)\r
- boundary = ('=' * 15) + (_fmt % token) + '=='\r
- if text is None:\r
- return boundary\r
- b = boundary\r
- counter = 0\r
- while True:\r
- cre = re.compile('^--' + re.escape(b) + '(--)?$', re.MULTILINE)\r
- if not cre.search(text):\r
- break\r
- b = boundary + '.' + str(counter)\r
- counter += 1\r
- return b\r