Benjamin Peterson | 46a9900 | 2010-01-09 18:45:30 +0000 | [diff] [blame] | 1 | # Copyright (C) 2001-2010 Python Software Foundation |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2 | # Author: Barry Warsaw |
| 3 | # Contact: email-sig@python.org |
| 4 | |
| 5 | """Classes to generate plain text from a message object tree.""" |
| 6 | |
| 7 | __all__ = ['Generator', 'DecodedGenerator'] |
| 8 | |
| 9 | import re |
| 10 | import sys |
| 11 | import time |
| 12 | import random |
| 13 | import warnings |
| 14 | |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 15 | from io import StringIO, BytesIO |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 16 | from email.header import Header |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 17 | from email.message import _has_surrogates |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 18 | |
| 19 | UNDERSCORE = '_' |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 20 | NL = '\n' # XXX: no longer used by the code below. |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 21 | |
| 22 | fcre = re.compile(r'^From ', re.MULTILINE) |
| 23 | |
| 24 | |
| 25 | |
| 26 | class Generator: |
| 27 | """Generates output from a Message object tree. |
| 28 | |
| 29 | This basic generator writes the message to the given file object as plain |
| 30 | text. |
| 31 | """ |
| 32 | # |
| 33 | # Public interface |
| 34 | # |
| 35 | |
| 36 | def __init__(self, outfp, mangle_from_=True, maxheaderlen=78): |
| 37 | """Create the generator for message flattening. |
| 38 | |
| 39 | outfp is the output file-like object for writing the message to. It |
| 40 | must have a write() method. |
| 41 | |
| 42 | Optional mangle_from_ is a flag that, when True (the default), escapes |
| 43 | From_ lines in the body of the message by putting a `>' in front of |
| 44 | them. |
| 45 | |
| 46 | Optional maxheaderlen specifies the longest length for a non-continued |
| 47 | header. When a header line is longer (in characters, with tabs |
| 48 | expanded to 8 spaces) than maxheaderlen, the header will split as |
| 49 | defined in the Header class. Set maxheaderlen to zero to disable |
| 50 | header wrapping. The default is 78, as recommended (but not required) |
| 51 | by RFC 2822. |
| 52 | """ |
| 53 | self._fp = outfp |
| 54 | self._mangle_from_ = mangle_from_ |
| 55 | self._maxheaderlen = maxheaderlen |
| 56 | |
| 57 | def write(self, s): |
| 58 | # Just delegate to the file object |
| 59 | self._fp.write(s) |
| 60 | |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 61 | def flatten(self, msg, unixfrom=False, linesep='\n'): |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 62 | """Print the message object tree rooted at msg to the output file |
| 63 | specified when the Generator instance was created. |
| 64 | |
| 65 | unixfrom is a flag that forces the printing of a Unix From_ delimiter |
| 66 | before the first object in the message tree. If the original message |
| 67 | has no From_ delimiter, a `standard' one is crafted. By default, this |
| 68 | is False to inhibit the printing of any From_ delimiter. |
| 69 | |
| 70 | Note that for subobjects, no From_ line is printed. |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 71 | |
| 72 | linesep specifies the characters used to indicate a new line in |
| 73 | the output. |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 74 | """ |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 75 | # We use the _XXX constants for operating on data that comes directly |
| 76 | # from the msg, and _encoded_XXX constants for operating on data that |
| 77 | # has already been converted (to bytes in the BytesGenerator) and |
| 78 | # inserted into a temporary buffer. |
| 79 | self._NL = linesep |
| 80 | self._encoded_NL = self._encode(linesep) |
| 81 | self._EMPTY = '' |
| 82 | self._encoded_EMTPY = self._encode('') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 83 | if unixfrom: |
| 84 | ufrom = msg.get_unixfrom() |
| 85 | if not ufrom: |
| 86 | ufrom = 'From nobody ' + time.ctime(time.time()) |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 87 | self.write(ufrom + self._NL) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 88 | self._write(msg) |
| 89 | |
| 90 | def clone(self, fp): |
| 91 | """Clone this generator with the exact same options.""" |
| 92 | return self.__class__(fp, self._mangle_from_, self._maxheaderlen) |
| 93 | |
| 94 | # |
| 95 | # Protected interface - undocumented ;/ |
| 96 | # |
| 97 | |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 98 | # Note that we use 'self.write' when what we are writing is coming from |
| 99 | # the source, and self._fp.write when what we are writing is coming from a |
| 100 | # buffer (because the Bytes subclass has already had a chance to transform |
| 101 | # the data in its write method in that case). This is an entirely |
| 102 | # pragmatic split determined by experiment; we could be more general by |
| 103 | # always using write and having the Bytes subclass write method detect when |
| 104 | # it has already transformed the input; but, since this whole thing is a |
| 105 | # hack anyway this seems good enough. |
| 106 | |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 107 | # Similarly, we have _XXX and _encoded_XXX attributes that are used on |
| 108 | # source and buffer data, respectively. |
| 109 | _encoded_EMPTY = '' |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 110 | |
| 111 | def _new_buffer(self): |
| 112 | # BytesGenerator overrides this to return BytesIO. |
| 113 | return StringIO() |
| 114 | |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 115 | def _encode(self, s): |
| 116 | # BytesGenerator overrides this to encode strings to bytes. |
| 117 | return s |
| 118 | |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 119 | def _write(self, msg): |
| 120 | # We can't write the headers yet because of the following scenario: |
| 121 | # say a multipart message includes the boundary string somewhere in |
| 122 | # its body. We'd have to calculate the new boundary /before/ we write |
| 123 | # the headers so that we can write the correct Content-Type: |
| 124 | # parameter. |
| 125 | # |
| 126 | # The way we do this, so as to make the _handle_*() methods simpler, |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 127 | # is to cache any subpart writes into a buffer. The we write the |
| 128 | # headers and the buffer contents. That way, subpart handlers can |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 129 | # Do The Right Thing, and can still modify the Content-Type: header if |
| 130 | # necessary. |
| 131 | oldfp = self._fp |
| 132 | try: |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 133 | self._fp = sfp = self._new_buffer() |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 134 | self._dispatch(msg) |
| 135 | finally: |
| 136 | self._fp = oldfp |
| 137 | # Write the headers. First we see if the message object wants to |
| 138 | # handle that itself. If not, we'll do it generically. |
| 139 | meth = getattr(msg, '_write_headers', None) |
| 140 | if meth is None: |
| 141 | self._write_headers(msg) |
| 142 | else: |
| 143 | meth(self) |
| 144 | self._fp.write(sfp.getvalue()) |
| 145 | |
| 146 | def _dispatch(self, msg): |
| 147 | # Get the Content-Type: for the message, then try to dispatch to |
| 148 | # self._handle_<maintype>_<subtype>(). If there's no handler for the |
| 149 | # full MIME type, then dispatch to self._handle_<maintype>(). If |
| 150 | # that's missing too, then dispatch to self._writeBody(). |
| 151 | main = msg.get_content_maintype() |
| 152 | sub = msg.get_content_subtype() |
| 153 | specific = UNDERSCORE.join((main, sub)).replace('-', '_') |
| 154 | meth = getattr(self, '_handle_' + specific, None) |
| 155 | if meth is None: |
| 156 | generic = main.replace('-', '_') |
| 157 | meth = getattr(self, '_handle_' + generic, None) |
| 158 | if meth is None: |
| 159 | meth = self._writeBody |
| 160 | meth(msg) |
| 161 | |
| 162 | # |
| 163 | # Default handlers |
| 164 | # |
| 165 | |
| 166 | def _write_headers(self, msg): |
| 167 | for h, v in msg.items(): |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 168 | self.write('%s: ' % h) |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 169 | if isinstance(v, Header): |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 170 | self.write(v.encode( |
| 171 | maxlinelen=self._maxheaderlen, linesep=self._NL)+self._NL) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 172 | else: |
| 173 | # Header's got lots of smarts, so use it. |
| 174 | header = Header(v, maxlinelen=self._maxheaderlen, |
Barry Warsaw | 70d61ce | 2009-03-30 23:12:30 +0000 | [diff] [blame] | 175 | header_name=h) |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 176 | self.write(header.encode(linesep=self._NL)+self._NL) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 177 | # A blank line always separates headers from body |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 178 | self.write(self._NL) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 179 | |
| 180 | # |
| 181 | # Handlers for writing types and subtypes |
| 182 | # |
| 183 | |
| 184 | def _handle_text(self, msg): |
| 185 | payload = msg.get_payload() |
| 186 | if payload is None: |
| 187 | return |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 188 | if not isinstance(payload, str): |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 189 | raise TypeError('string payload expected: %s' % type(payload)) |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 190 | if _has_surrogates(msg._payload): |
| 191 | charset = msg.get_param('charset') |
| 192 | if charset is not None: |
| 193 | del msg['content-transfer-encoding'] |
| 194 | msg.set_payload(payload, charset) |
| 195 | payload = msg.get_payload() |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 196 | if self._mangle_from_: |
| 197 | payload = fcre.sub('>From ', payload) |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 198 | self.write(payload) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 199 | |
| 200 | # Default body handler |
| 201 | _writeBody = _handle_text |
| 202 | |
| 203 | def _handle_multipart(self, msg): |
| 204 | # The trick here is to write out each part separately, merge them all |
| 205 | # together, and then make sure that the boundary we've chosen isn't |
| 206 | # present in the payload. |
| 207 | msgtexts = [] |
| 208 | subparts = msg.get_payload() |
| 209 | if subparts is None: |
| 210 | subparts = [] |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 211 | elif isinstance(subparts, str): |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 212 | # e.g. a non-strict parse of a message with no starting boundary. |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 213 | self.write(subparts) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 214 | return |
| 215 | elif not isinstance(subparts, list): |
| 216 | # Scalar payload |
| 217 | subparts = [subparts] |
| 218 | for part in subparts: |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 219 | s = self._new_buffer() |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 220 | g = self.clone(s) |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 221 | g.flatten(part, unixfrom=False, linesep=self._NL) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 222 | msgtexts.append(s.getvalue()) |
| 223 | # Now make sure the boundary we've selected doesn't appear in any of |
| 224 | # the message texts. |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 225 | alltext = self._encoded_NL.join(msgtexts) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 226 | # BAW: What about boundaries that are wrapped in double-quotes? |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 227 | boundary = msg.get_boundary(failobj=self._make_boundary(alltext)) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 228 | # If we had to calculate a new boundary because the body text |
| 229 | # contained that string, set the new boundary. We don't do it |
| 230 | # unconditionally because, while set_boundary() preserves order, it |
| 231 | # doesn't preserve newlines/continuations in headers. This is no big |
| 232 | # deal in practice, but turns out to be inconvenient for the unittest |
| 233 | # suite. |
| 234 | if msg.get_boundary() != boundary: |
| 235 | msg.set_boundary(boundary) |
| 236 | # If there's a preamble, write it out, with a trailing CRLF |
| 237 | if msg.preamble is not None: |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 238 | self.write(msg.preamble + self._NL) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 239 | # dash-boundary transport-padding CRLF |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 240 | self.write('--' + boundary + self._NL) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 241 | # body-part |
| 242 | if msgtexts: |
| 243 | self._fp.write(msgtexts.pop(0)) |
| 244 | # *encapsulation |
| 245 | # --> delimiter transport-padding |
| 246 | # --> CRLF body-part |
| 247 | for body_part in msgtexts: |
| 248 | # delimiter transport-padding CRLF |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 249 | self.write(self._NL + '--' + boundary + self._NL) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 250 | # body-part |
| 251 | self._fp.write(body_part) |
| 252 | # close-delimiter transport-padding |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 253 | self.write(self._NL + '--' + boundary + '--') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 254 | if msg.epilogue is not None: |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 255 | self.write(self._NL) |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 256 | self.write(msg.epilogue) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 257 | |
R. David Murray | a8f480f | 2010-01-16 18:30:03 +0000 | [diff] [blame] | 258 | def _handle_multipart_signed(self, msg): |
| 259 | # The contents of signed parts has to stay unmodified in order to keep |
| 260 | # the signature intact per RFC1847 2.1, so we disable header wrapping. |
| 261 | # RDM: This isn't enough to completely preserve the part, but it helps. |
| 262 | old_maxheaderlen = self._maxheaderlen |
| 263 | try: |
| 264 | self._maxheaderlen = 0 |
| 265 | self._handle_multipart(msg) |
| 266 | finally: |
| 267 | self._maxheaderlen = old_maxheaderlen |
| 268 | |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 269 | def _handle_message_delivery_status(self, msg): |
| 270 | # We can't just write the headers directly to self's file object |
| 271 | # because this will leave an extra newline between the last header |
| 272 | # block and the boundary. Sigh. |
| 273 | blocks = [] |
| 274 | for part in msg.get_payload(): |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 275 | s = self._new_buffer() |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 276 | g = self.clone(s) |
R. David Murray | 719a449 | 2010-11-21 16:53:48 +0000 | [diff] [blame] | 277 | g.flatten(part, unixfrom=False, linesep=self._NL) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 278 | text = s.getvalue() |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 279 | lines = text.split(self._encoded_NL) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 280 | # Strip off the unnecessary trailing empty line |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 281 | if lines and lines[-1] == self._encoded_EMPTY: |
| 282 | blocks.append(self._encoded_NL.join(lines[:-1])) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 283 | else: |
| 284 | blocks.append(text) |
| 285 | # Now join all the blocks with an empty line. This has the lovely |
| 286 | # effect of separating each block with an empty line, but not adding |
| 287 | # an extra one after the last one. |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 288 | self._fp.write(self._encoded_NL.join(blocks)) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 289 | |
| 290 | def _handle_message(self, msg): |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 291 | s = self._new_buffer() |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 292 | g = self.clone(s) |
| 293 | # The payload of a message/rfc822 part should be a multipart sequence |
| 294 | # of length 1. The zeroth element of the list should be the Message |
| 295 | # object for the subpart. Extract that object, stringify it, and |
| 296 | # write it out. |
R. David Murray | 57c45ac | 2010-02-21 04:39:40 +0000 | [diff] [blame] | 297 | # Except, it turns out, when it's a string instead, which happens when |
| 298 | # and only when HeaderParser is used on a message of mime type |
| 299 | # message/rfc822. Such messages are generated by, for example, |
| 300 | # Groupwise when forwarding unadorned messages. (Issue 7970.) So |
| 301 | # in that case we just emit the string body. |
| 302 | payload = msg.get_payload() |
| 303 | if isinstance(payload, list): |
R. David Murray | 719a449 | 2010-11-21 16:53:48 +0000 | [diff] [blame] | 304 | g.flatten(msg.get_payload(0), unixfrom=False, linesep=self._NL) |
R. David Murray | 57c45ac | 2010-02-21 04:39:40 +0000 | [diff] [blame] | 305 | payload = s.getvalue() |
| 306 | self._fp.write(payload) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 307 | |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 308 | # This used to be a module level function; we use a classmethod for this |
| 309 | # and _compile_re so we can continue to provide the module level function |
| 310 | # for backward compatibility by doing |
| 311 | # _make_boudary = Generator._make_boundary |
| 312 | # at the end of the module. It *is* internal, so we could drop that... |
| 313 | @classmethod |
| 314 | def _make_boundary(cls, text=None): |
| 315 | # Craft a random boundary. If text is given, ensure that the chosen |
| 316 | # boundary doesn't appear in the text. |
| 317 | token = random.randrange(sys.maxsize) |
| 318 | boundary = ('=' * 15) + (_fmt % token) + '==' |
| 319 | if text is None: |
| 320 | return boundary |
| 321 | b = boundary |
| 322 | counter = 0 |
| 323 | while True: |
| 324 | cre = cls._compile_re('^--' + re.escape(b) + '(--)?$', re.MULTILINE) |
| 325 | if not cre.search(text): |
| 326 | break |
| 327 | b = boundary + '.' + str(counter) |
| 328 | counter += 1 |
| 329 | return b |
| 330 | |
| 331 | @classmethod |
| 332 | def _compile_re(cls, s, flags): |
| 333 | return re.compile(s, flags) |
| 334 | |
| 335 | |
| 336 | class BytesGenerator(Generator): |
| 337 | """Generates a bytes version of a Message object tree. |
| 338 | |
| 339 | Functionally identical to the base Generator except that the output is |
| 340 | bytes and not string. When surrogates were used in the input to encode |
| 341 | bytes, these are decoded back to bytes for output. |
| 342 | |
| 343 | The outfp object must accept bytes in its write method. |
| 344 | """ |
| 345 | |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 346 | # Bytes versions of this constant for use in manipulating data from |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 347 | # the BytesIO buffer. |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 348 | _encoded_EMPTY = b'' |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 349 | |
| 350 | def write(self, s): |
| 351 | self._fp.write(s.encode('ascii', 'surrogateescape')) |
| 352 | |
| 353 | def _new_buffer(self): |
| 354 | return BytesIO() |
| 355 | |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 356 | def _encode(self, s): |
| 357 | return s.encode('ascii') |
| 358 | |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 359 | def _write_headers(self, msg): |
| 360 | # This is almost the same as the string version, except for handling |
| 361 | # strings with 8bit bytes. |
| 362 | for h, v in msg._headers: |
| 363 | self.write('%s: ' % h) |
| 364 | if isinstance(v, Header): |
| 365 | self.write(v.encode(maxlinelen=self._maxheaderlen)+NL) |
| 366 | elif _has_surrogates(v): |
| 367 | # If we have raw 8bit data in a byte string, we have no idea |
| 368 | # what the encoding is. There is no safe way to split this |
| 369 | # string. If it's ascii-subset, then we could do a normal |
| 370 | # ascii split, but if it's multibyte then we could break the |
| 371 | # string. There's no way to know so the least harm seems to |
| 372 | # be to not split the string and risk it being too long. |
| 373 | self.write(v+NL) |
| 374 | else: |
| 375 | # Header's got lots of smarts and this string is safe... |
| 376 | header = Header(v, maxlinelen=self._maxheaderlen, |
| 377 | header_name=h) |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 378 | self.write(header.encode(linesep=self._NL)+self._NL) |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 379 | # A blank line always separates headers from body |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 380 | self.write(self._NL) |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 381 | |
| 382 | def _handle_text(self, msg): |
| 383 | # If the string has surrogates the original source was bytes, so |
| 384 | # just write it back out. |
| 385 | if _has_surrogates(msg._payload): |
| 386 | self.write(msg._payload) |
| 387 | else: |
| 388 | super(BytesGenerator,self)._handle_text(msg) |
| 389 | |
| 390 | @classmethod |
| 391 | def _compile_re(cls, s, flags): |
| 392 | return re.compile(s.encode('ascii'), flags) |
| 393 | |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 394 | |
| 395 | |
| 396 | _FMT = '[Non-text (%(type)s) part of message omitted, filename %(filename)s]' |
| 397 | |
| 398 | class DecodedGenerator(Generator): |
R. David Murray | 70a9993 | 2010-10-01 20:38:33 +0000 | [diff] [blame] | 399 | """Generates a text representation of a message. |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 400 | |
| 401 | Like the Generator base class, except that non-text parts are substituted |
| 402 | with a format string representing the part. |
| 403 | """ |
| 404 | def __init__(self, outfp, mangle_from_=True, maxheaderlen=78, fmt=None): |
| 405 | """Like Generator.__init__() except that an additional optional |
| 406 | argument is allowed. |
| 407 | |
| 408 | Walks through all subparts of a message. If the subpart is of main |
| 409 | type `text', then it prints the decoded payload of the subpart. |
| 410 | |
| 411 | Otherwise, fmt is a format string that is used instead of the message |
| 412 | payload. fmt is expanded with the following keywords (in |
| 413 | %(keyword)s format): |
| 414 | |
| 415 | type : Full MIME type of the non-text part |
| 416 | maintype : Main MIME type of the non-text part |
| 417 | subtype : Sub-MIME type of the non-text part |
| 418 | filename : Filename of the non-text part |
| 419 | description: Description associated with the non-text part |
| 420 | encoding : Content transfer encoding of the non-text part |
| 421 | |
| 422 | The default value for fmt is None, meaning |
| 423 | |
| 424 | [Non-text (%(type)s) part of message omitted, filename %(filename)s] |
| 425 | """ |
| 426 | Generator.__init__(self, outfp, mangle_from_, maxheaderlen) |
| 427 | if fmt is None: |
| 428 | self._fmt = _FMT |
| 429 | else: |
| 430 | self._fmt = fmt |
| 431 | |
| 432 | def _dispatch(self, msg): |
| 433 | for part in msg.walk(): |
| 434 | maintype = part.get_content_maintype() |
| 435 | if maintype == 'text': |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 436 | print(part.get_payload(decode=False), file=self) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 437 | elif maintype == 'multipart': |
| 438 | # Just skip this |
| 439 | pass |
| 440 | else: |
| 441 | print(self._fmt % { |
| 442 | 'type' : part.get_content_type(), |
| 443 | 'maintype' : part.get_content_maintype(), |
| 444 | 'subtype' : part.get_content_subtype(), |
| 445 | 'filename' : part.get_filename('[no filename]'), |
| 446 | 'description': part.get('Content-Description', |
| 447 | '[no description]'), |
| 448 | 'encoding' : part.get('Content-Transfer-Encoding', |
| 449 | '[no encoding]'), |
| 450 | }, file=self) |
| 451 | |
| 452 | |
| 453 | |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 454 | # Helper used by Generator._make_boundary |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 455 | _width = len(repr(sys.maxsize-1)) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 456 | _fmt = '%%0%dd' % _width |
| 457 | |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 458 | # Backward compatibility |
| 459 | _make_boundary = Generator._make_boundary |