Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 1 | # Copyright (C) 2001-2004 Python Software Foundation |
| 2 | # Author: barry@python.org (Barry Warsaw) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 3 | |
| 4 | """Classes to generate plain text from a message object tree. |
| 5 | """ |
| 6 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 7 | import re |
Barry Warsaw | db6888b | 2003-05-29 19:39:33 +0000 | [diff] [blame] | 8 | import sys |
Barry Warsaw | 5d384ef | 2003-03-06 05:22:02 +0000 | [diff] [blame] | 9 | import time |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 10 | import random |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 11 | from cStringIO import StringIO |
| 12 | |
Barry Warsaw | 062749a | 2002-06-28 23:41:42 +0000 | [diff] [blame] | 13 | from email.Header import Header |
| 14 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 15 | UNDERSCORE = '_' |
| 16 | NL = '\n' |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 17 | |
| 18 | fcre = re.compile(r'^From ', re.MULTILINE) |
| 19 | |
Barry Warsaw | 6c2bc46 | 2002-10-14 15:09:30 +0000 | [diff] [blame] | 20 | def _is8bitstring(s): |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 21 | if isinstance(s, str): |
Barry Warsaw | 6c2bc46 | 2002-10-14 15:09:30 +0000 | [diff] [blame] | 22 | try: |
| 23 | unicode(s, 'us-ascii') |
| 24 | except UnicodeError: |
| 25 | return True |
| 26 | return False |
| 27 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 28 | |
Barry Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 29 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 30 | class Generator: |
| 31 | """Generates output from a Message object tree. |
| 32 | |
| 33 | This basic generator writes the message to the given file object as plain |
| 34 | text. |
| 35 | """ |
| 36 | # |
| 37 | # Public interface |
| 38 | # |
| 39 | |
Barry Warsaw | 56835dd | 2002-09-28 18:04:55 +0000 | [diff] [blame] | 40 | def __init__(self, outfp, mangle_from_=True, maxheaderlen=78): |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 41 | """Create the generator for message flattening. |
| 42 | |
| 43 | outfp is the output file-like object for writing the message to. It |
| 44 | must have a write() method. |
| 45 | |
Barry Warsaw | 56835dd | 2002-09-28 18:04:55 +0000 | [diff] [blame] | 46 | Optional mangle_from_ is a flag that, when True (the default), escapes |
| 47 | From_ lines in the body of the message by putting a `>' in front of |
| 48 | them. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 49 | |
| 50 | Optional maxheaderlen specifies the longest length for a non-continued |
| 51 | header. When a header line is longer (in characters, with tabs |
Barry Warsaw | b03136a | 2003-11-19 02:23:01 +0000 | [diff] [blame] | 52 | expanded to 8 spaces) than maxheaderlen, the header will split as |
| 53 | defined in the Header class. Set maxheaderlen to zero to disable |
| 54 | header wrapping. The default is 78, as recommended (but not required) |
| 55 | by RFC 2822. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 56 | """ |
| 57 | self._fp = outfp |
| 58 | self._mangle_from_ = mangle_from_ |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 59 | self._maxheaderlen = maxheaderlen |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 60 | |
| 61 | def write(self, s): |
| 62 | # Just delegate to the file object |
| 63 | self._fp.write(s) |
| 64 | |
Barry Warsaw | 56835dd | 2002-09-28 18:04:55 +0000 | [diff] [blame] | 65 | def flatten(self, msg, unixfrom=False): |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 66 | """Print the message object tree rooted at msg to the output file |
| 67 | specified when the Generator instance was created. |
| 68 | |
| 69 | unixfrom is a flag that forces the printing of a Unix From_ delimiter |
| 70 | before the first object in the message tree. If the original message |
| 71 | has no From_ delimiter, a `standard' one is crafted. By default, this |
Barry Warsaw | 56835dd | 2002-09-28 18:04:55 +0000 | [diff] [blame] | 72 | is False to inhibit the printing of any From_ delimiter. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 73 | |
| 74 | Note that for subobjects, no From_ line is printed. |
| 75 | """ |
| 76 | if unixfrom: |
| 77 | ufrom = msg.get_unixfrom() |
| 78 | if not ufrom: |
| 79 | ufrom = 'From nobody ' + time.ctime(time.time()) |
| 80 | print >> self._fp, ufrom |
| 81 | self._write(msg) |
| 82 | |
Barry Warsaw | 7dc865a | 2002-06-02 19:02:37 +0000 | [diff] [blame] | 83 | # For backwards compatibility, but this is slower |
| 84 | __call__ = flatten |
| 85 | |
Barry Warsaw | 93c40f0 | 2002-07-09 02:43:47 +0000 | [diff] [blame] | 86 | def clone(self, fp): |
| 87 | """Clone this generator with the exact same options.""" |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 88 | return self.__class__(fp, self._mangle_from_, self._maxheaderlen) |
Barry Warsaw | 93c40f0 | 2002-07-09 02:43:47 +0000 | [diff] [blame] | 89 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 90 | # |
| 91 | # Protected interface - undocumented ;/ |
| 92 | # |
| 93 | |
| 94 | def _write(self, msg): |
| 95 | # We can't write the headers yet because of the following scenario: |
| 96 | # say a multipart message includes the boundary string somewhere in |
| 97 | # its body. We'd have to calculate the new boundary /before/ we write |
| 98 | # the headers so that we can write the correct Content-Type: |
| 99 | # parameter. |
| 100 | # |
| 101 | # The way we do this, so as to make the _handle_*() methods simpler, |
| 102 | # is to cache any subpart writes into a StringIO. The we write the |
| 103 | # headers and the StringIO contents. That way, subpart handlers can |
| 104 | # Do The Right Thing, and can still modify the Content-Type: header if |
| 105 | # necessary. |
| 106 | oldfp = self._fp |
| 107 | try: |
| 108 | self._fp = sfp = StringIO() |
| 109 | self._dispatch(msg) |
| 110 | finally: |
| 111 | self._fp = oldfp |
| 112 | # Write the headers. First we see if the message object wants to |
| 113 | # handle that itself. If not, we'll do it generically. |
| 114 | meth = getattr(msg, '_write_headers', None) |
| 115 | if meth is None: |
| 116 | self._write_headers(msg) |
| 117 | else: |
| 118 | meth(self) |
| 119 | self._fp.write(sfp.getvalue()) |
| 120 | |
| 121 | def _dispatch(self, msg): |
| 122 | # Get the Content-Type: for the message, then try to dispatch to |
Barry Warsaw | f488b2c | 2002-07-11 18:48:40 +0000 | [diff] [blame] | 123 | # self._handle_<maintype>_<subtype>(). If there's no handler for the |
| 124 | # full MIME type, then dispatch to self._handle_<maintype>(). If |
| 125 | # that's missing too, then dispatch to self._writeBody(). |
Barry Warsaw | dfea3b3 | 2002-08-20 14:47:30 +0000 | [diff] [blame] | 126 | main = msg.get_content_maintype() |
| 127 | sub = msg.get_content_subtype() |
Barry Warsaw | 93c40f0 | 2002-07-09 02:43:47 +0000 | [diff] [blame] | 128 | specific = UNDERSCORE.join((main, sub)).replace('-', '_') |
| 129 | meth = getattr(self, '_handle_' + specific, None) |
| 130 | if meth is None: |
| 131 | generic = main.replace('-', '_') |
| 132 | meth = getattr(self, '_handle_' + generic, None) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 133 | if meth is None: |
Barry Warsaw | 93c40f0 | 2002-07-09 02:43:47 +0000 | [diff] [blame] | 134 | meth = self._writeBody |
| 135 | meth(msg) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 136 | |
| 137 | # |
| 138 | # Default handlers |
| 139 | # |
| 140 | |
| 141 | def _write_headers(self, msg): |
| 142 | for h, v in msg.items(): |
Barry Warsaw | ce6bf59 | 2003-03-07 15:43:17 +0000 | [diff] [blame] | 143 | print >> self._fp, '%s:' % h, |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 144 | if self._maxheaderlen == 0: |
Barry Warsaw | ce6bf59 | 2003-03-07 15:43:17 +0000 | [diff] [blame] | 145 | # Explicit no-wrapping |
| 146 | print >> self._fp, v |
| 147 | elif isinstance(v, Header): |
| 148 | # Header instances know what to do |
| 149 | print >> self._fp, v.encode() |
| 150 | elif _is8bitstring(v): |
| 151 | # If we have raw 8bit data in a byte string, we have no idea |
| 152 | # what the encoding is. There is no safe way to split this |
| 153 | # string. If it's ascii-subset, then we could do a normal |
| 154 | # ascii split, but if it's multibyte then we could break the |
| 155 | # string. There's no way to know so the least harm seems to |
| 156 | # be to not split the string and risk it being too long. |
| 157 | print >> self._fp, v |
| 158 | else: |
| 159 | # Header's got lots of smarts, so use it. |
| 160 | print >> self._fp, Header( |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 161 | v, maxlinelen=self._maxheaderlen, |
Barry Warsaw | ce6bf59 | 2003-03-07 15:43:17 +0000 | [diff] [blame] | 162 | header_name=h, continuation_ws='\t').encode() |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 163 | # A blank line always separates headers from body |
| 164 | print >> self._fp |
| 165 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 166 | # |
| 167 | # Handlers for writing types and subtypes |
| 168 | # |
| 169 | |
| 170 | def _handle_text(self, msg): |
| 171 | payload = msg.get_payload() |
Barry Warsaw | b384e01 | 2001-09-26 05:32:41 +0000 | [diff] [blame] | 172 | if payload is None: |
| 173 | return |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 174 | cset = msg.get_charset() |
| 175 | if cset is not None: |
| 176 | payload = cset.body_encode(payload) |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 177 | if not isinstance(payload, basestring): |
Barry Warsaw | b384e01 | 2001-09-26 05:32:41 +0000 | [diff] [blame] | 178 | raise TypeError, 'string payload expected: %s' % type(payload) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 179 | if self._mangle_from_: |
| 180 | payload = fcre.sub('>From ', payload) |
| 181 | self._fp.write(payload) |
| 182 | |
| 183 | # Default body handler |
| 184 | _writeBody = _handle_text |
| 185 | |
Barry Warsaw | 93c40f0 | 2002-07-09 02:43:47 +0000 | [diff] [blame] | 186 | def _handle_multipart(self, msg): |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 187 | # The trick here is to write out each part separately, merge them all |
| 188 | # together, and then make sure that the boundary we've chosen isn't |
| 189 | # present in the payload. |
| 190 | msgtexts = [] |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 191 | subparts = msg.get_payload() |
| 192 | if subparts is None: |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 193 | subparts = [] |
| 194 | elif isinstance(subparts, basestring): |
Barry Warsaw | b1c1de3 | 2002-09-10 16:13:45 +0000 | [diff] [blame] | 195 | # e.g. a non-strict parse of a message with no starting boundary. |
| 196 | self._fp.write(subparts) |
| 197 | return |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 198 | elif not isinstance(subparts, list): |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 199 | # Scalar payload |
| 200 | subparts = [subparts] |
| 201 | for part in subparts: |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 202 | s = StringIO() |
Barry Warsaw | 93c40f0 | 2002-07-09 02:43:47 +0000 | [diff] [blame] | 203 | g = self.clone(s) |
Barry Warsaw | 56835dd | 2002-09-28 18:04:55 +0000 | [diff] [blame] | 204 | g.flatten(part, unixfrom=False) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 205 | msgtexts.append(s.getvalue()) |
| 206 | # Now make sure the boundary we've selected doesn't appear in any of |
| 207 | # the message texts. |
| 208 | alltext = NL.join(msgtexts) |
| 209 | # BAW: What about boundaries that are wrapped in double-quotes? |
| 210 | boundary = msg.get_boundary(failobj=_make_boundary(alltext)) |
| 211 | # If we had to calculate a new boundary because the body text |
| 212 | # contained that string, set the new boundary. We don't do it |
| 213 | # unconditionally because, while set_boundary() preserves order, it |
| 214 | # doesn't preserve newlines/continuations in headers. This is no big |
| 215 | # deal in practice, but turns out to be inconvenient for the unittest |
| 216 | # suite. |
| 217 | if msg.get_boundary() <> boundary: |
| 218 | msg.set_boundary(boundary) |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 219 | # If there's a preamble, write it out, with a trailing CRLF |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 220 | if msg.preamble is not None: |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 221 | print >> self._fp, msg.preamble |
| 222 | # dash-boundary transport-padding CRLF |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 223 | print >> self._fp, '--' + boundary |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 224 | # body-part |
| 225 | if msgtexts: |
| 226 | self._fp.write(msgtexts.pop(0)) |
| 227 | # *encapsulation |
| 228 | # --> delimiter transport-padding |
| 229 | # --> CRLF body-part |
| 230 | for body_part in msgtexts: |
| 231 | # delimiter transport-padding CRLF |
| 232 | print >> self._fp, '\n--' + boundary |
| 233 | # body-part |
| 234 | self._fp.write(body_part) |
| 235 | # close-delimiter transport-padding |
| 236 | self._fp.write('\n--' + boundary + '--') |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 237 | if msg.epilogue is not None: |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 238 | print >> self._fp |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 239 | self._fp.write(msg.epilogue) |
| 240 | |
Barry Warsaw | b384e01 | 2001-09-26 05:32:41 +0000 | [diff] [blame] | 241 | def _handle_message_delivery_status(self, msg): |
| 242 | # We can't just write the headers directly to self's file object |
| 243 | # because this will leave an extra newline between the last header |
| 244 | # block and the boundary. Sigh. |
| 245 | blocks = [] |
| 246 | for part in msg.get_payload(): |
| 247 | s = StringIO() |
Barry Warsaw | 93c40f0 | 2002-07-09 02:43:47 +0000 | [diff] [blame] | 248 | g = self.clone(s) |
Barry Warsaw | 56835dd | 2002-09-28 18:04:55 +0000 | [diff] [blame] | 249 | g.flatten(part, unixfrom=False) |
Barry Warsaw | b384e01 | 2001-09-26 05:32:41 +0000 | [diff] [blame] | 250 | text = s.getvalue() |
| 251 | lines = text.split('\n') |
| 252 | # Strip off the unnecessary trailing empty line |
| 253 | if lines and lines[-1] == '': |
| 254 | blocks.append(NL.join(lines[:-1])) |
| 255 | else: |
| 256 | blocks.append(text) |
| 257 | # Now join all the blocks with an empty line. This has the lovely |
| 258 | # effect of separating each block with an empty line, but not adding |
| 259 | # an extra one after the last one. |
| 260 | self._fp.write(NL.join(blocks)) |
| 261 | |
| 262 | def _handle_message(self, msg): |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 263 | s = StringIO() |
Barry Warsaw | 93c40f0 | 2002-07-09 02:43:47 +0000 | [diff] [blame] | 264 | g = self.clone(s) |
Barry Warsaw | 7dc865a | 2002-06-02 19:02:37 +0000 | [diff] [blame] | 265 | # The payload of a message/rfc822 part should be a multipart sequence |
| 266 | # of length 1. The zeroth element of the list should be the Message |
Barry Warsaw | 93c40f0 | 2002-07-09 02:43:47 +0000 | [diff] [blame] | 267 | # object for the subpart. Extract that object, stringify it, and |
| 268 | # write it out. |
Barry Warsaw | 56835dd | 2002-09-28 18:04:55 +0000 | [diff] [blame] | 269 | g.flatten(msg.get_payload(0), unixfrom=False) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 270 | self._fp.write(s.getvalue()) |
| 271 | |
| 272 | |
Barry Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 273 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 274 | class DecodedGenerator(Generator): |
| 275 | """Generator a text representation of a message. |
| 276 | |
| 277 | Like the Generator base class, except that non-text parts are substituted |
| 278 | with a format string representing the part. |
| 279 | """ |
Barry Warsaw | 56835dd | 2002-09-28 18:04:55 +0000 | [diff] [blame] | 280 | def __init__(self, outfp, mangle_from_=True, maxheaderlen=78, fmt=None): |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 281 | """Like Generator.__init__() except that an additional optional |
| 282 | argument is allowed. |
| 283 | |
| 284 | Walks through all subparts of a message. If the subpart is of main |
| 285 | type `text', then it prints the decoded payload of the subpart. |
| 286 | |
| 287 | Otherwise, fmt is a format string that is used instead of the message |
| 288 | payload. fmt is expanded with the following keywords (in |
| 289 | %(keyword)s format): |
| 290 | |
| 291 | type : Full MIME type of the non-text part |
| 292 | maintype : Main MIME type of the non-text part |
| 293 | subtype : Sub-MIME type of the non-text part |
| 294 | filename : Filename of the non-text part |
| 295 | description: Description associated with the non-text part |
| 296 | encoding : Content transfer encoding of the non-text part |
| 297 | |
| 298 | The default value for fmt is None, meaning |
| 299 | |
| 300 | [Non-text (%(type)s) part of message omitted, filename %(filename)s] |
| 301 | """ |
| 302 | Generator.__init__(self, outfp, mangle_from_, maxheaderlen) |
| 303 | if fmt is None: |
| 304 | fmt = ('[Non-text (%(type)s) part of message omitted, ' |
| 305 | 'filename %(filename)s]') |
| 306 | self._fmt = fmt |
| 307 | |
| 308 | def _dispatch(self, msg): |
| 309 | for part in msg.walk(): |
Barry Warsaw | b384e01 | 2001-09-26 05:32:41 +0000 | [diff] [blame] | 310 | maintype = part.get_main_type('text') |
| 311 | if maintype == 'text': |
Barry Warsaw | 56835dd | 2002-09-28 18:04:55 +0000 | [diff] [blame] | 312 | print >> self, part.get_payload(decode=True) |
Barry Warsaw | b384e01 | 2001-09-26 05:32:41 +0000 | [diff] [blame] | 313 | elif maintype == 'multipart': |
| 314 | # Just skip this |
| 315 | pass |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 316 | else: |
| 317 | print >> self, self._fmt % { |
| 318 | 'type' : part.get_type('[no MIME type]'), |
| 319 | 'maintype' : part.get_main_type('[no main MIME type]'), |
| 320 | 'subtype' : part.get_subtype('[no sub-MIME type]'), |
| 321 | 'filename' : part.get_filename('[no filename]'), |
| 322 | 'description': part.get('Content-Description', |
| 323 | '[no description]'), |
| 324 | 'encoding' : part.get('Content-Transfer-Encoding', |
| 325 | '[no encoding]'), |
| 326 | } |
| 327 | |
| 328 | |
Barry Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 329 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 330 | # Helper |
Barry Warsaw | db6888b | 2003-05-29 19:39:33 +0000 | [diff] [blame] | 331 | _width = len(repr(sys.maxint-1)) |
| 332 | _fmt = '%%0%dd' % _width |
| 333 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 334 | def _make_boundary(text=None): |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 335 | # Craft a random boundary. If text is given, ensure that the chosen |
| 336 | # boundary doesn't appear in the text. |
Barry Warsaw | 663219a | 2003-06-24 20:19:34 +0000 | [diff] [blame] | 337 | token = random.randrange(sys.maxint) |
Barry Warsaw | db6888b | 2003-05-29 19:39:33 +0000 | [diff] [blame] | 338 | boundary = ('=' * 15) + (_fmt % token) + '==' |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 339 | if text is None: |
| 340 | return boundary |
| 341 | b = boundary |
| 342 | counter = 0 |
Barry Warsaw | 56835dd | 2002-09-28 18:04:55 +0000 | [diff] [blame] | 343 | while True: |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 344 | cre = re.compile('^--' + re.escape(b) + '(--)?$', re.MULTILINE) |
| 345 | if not cre.search(text): |
| 346 | break |
| 347 | b = boundary + '.' + str(counter) |
| 348 | counter += 1 |
| 349 | return b |