Commit 36112f2d authored by Barry Warsaw's avatar Barry Warsaw

Update to Python 2.3, getting rid of backward compatiblity crud. Get rid of a

bunch of module globals that aren't used.

__maxheaderlen -> _maxheaderlen

_handle_multipart(): This should be more RFC compliant now, and does match the
updated/fixed semantics for preamble and epilogue.
parent 418101fd
# Copyright (C) 2001,2002 Python Software Foundation # Copyright (C) 2001-2004 Python Software Foundation
# Author: barry@zope.com (Barry Warsaw) # Author: barry@python.org (Barry Warsaw)
"""Classes to generate plain text from a message object tree. """Classes to generate plain text from a message object tree.
""" """
...@@ -7,39 +7,18 @@ ...@@ -7,39 +7,18 @@
import re import re
import sys import sys
import time import time
import locale
import random import random
from types import ListType, StringType
from cStringIO import StringIO from cStringIO import StringIO
from email.Header import Header from email.Header import Header
from email.Parser import NLCRE
try:
from email._compat22 import _isstring
except SyntaxError:
from email._compat21 import _isstring
try:
True, False
except NameError:
True = 1
False = 0
EMPTYSTRING = ''
SEMISPACE = '; '
BAR = '|'
UNDERSCORE = '_' UNDERSCORE = '_'
NL = '\n' NL = '\n'
NLTAB = '\n\t'
SEMINLTAB = ';\n\t'
SPACE8 = ' ' * 8
fcre = re.compile(r'^From ', re.MULTILINE) fcre = re.compile(r'^From ', re.MULTILINE)
def _is8bitstring(s): def _is8bitstring(s):
if isinstance(s, StringType): if isinstance(s, str):
try: try:
unicode(s, 'us-ascii') unicode(s, 'us-ascii')
except UnicodeError: except UnicodeError:
...@@ -77,7 +56,7 @@ class Generator: ...@@ -77,7 +56,7 @@ class Generator:
""" """
self._fp = outfp self._fp = outfp
self._mangle_from_ = mangle_from_ self._mangle_from_ = mangle_from_
self.__maxheaderlen = maxheaderlen self._maxheaderlen = maxheaderlen
def write(self, s): def write(self, s):
# Just delegate to the file object # Just delegate to the file object
...@@ -106,7 +85,7 @@ class Generator: ...@@ -106,7 +85,7 @@ class Generator:
def clone(self, fp): def clone(self, fp):
"""Clone this generator with the exact same options.""" """Clone this generator with the exact same options."""
return self.__class__(fp, self._mangle_from_, self.__maxheaderlen) return self.__class__(fp, self._mangle_from_, self._maxheaderlen)
# #
# Protected interface - undocumented ;/ # Protected interface - undocumented ;/
...@@ -162,7 +141,7 @@ class Generator: ...@@ -162,7 +141,7 @@ class Generator:
def _write_headers(self, msg): def _write_headers(self, msg):
for h, v in msg.items(): for h, v in msg.items():
print >> self._fp, '%s:' % h, print >> self._fp, '%s:' % h,
if self.__maxheaderlen == 0: if self._maxheaderlen == 0:
# Explicit no-wrapping # Explicit no-wrapping
print >> self._fp, v print >> self._fp, v
elif isinstance(v, Header): elif isinstance(v, Header):
...@@ -179,7 +158,7 @@ class Generator: ...@@ -179,7 +158,7 @@ class Generator:
else: else:
# Header's got lots of smarts, so use it. # Header's got lots of smarts, so use it.
print >> self._fp, Header( print >> self._fp, Header(
v, maxlinelen=self.__maxheaderlen, v, maxlinelen=self._maxheaderlen,
header_name=h, continuation_ws='\t').encode() header_name=h, continuation_ws='\t').encode()
# A blank line always separates headers from body # A blank line always separates headers from body
print >> self._fp print >> self._fp
...@@ -195,7 +174,7 @@ class Generator: ...@@ -195,7 +174,7 @@ class Generator:
cset = msg.get_charset() cset = msg.get_charset()
if cset is not None: if cset is not None:
payload = cset.body_encode(payload) payload = cset.body_encode(payload)
if not _isstring(payload): if not isinstance(payload, basestring):
raise TypeError, 'string payload expected: %s' % type(payload) raise TypeError, 'string payload expected: %s' % type(payload)
if self._mangle_from_: if self._mangle_from_:
payload = fcre.sub('>From ', payload) payload = fcre.sub('>From ', payload)
...@@ -211,17 +190,12 @@ class Generator: ...@@ -211,17 +190,12 @@ class Generator:
msgtexts = [] msgtexts = []
subparts = msg.get_payload() subparts = msg.get_payload()
if subparts is None: if subparts is None:
# Nothing has ever been attached subparts = []
boundary = msg.get_boundary(failobj=_make_boundary()) elif isinstance(subparts, basestring):
print >> self._fp, '--' + boundary
print >> self._fp, '\n'
print >> self._fp, '--' + boundary + '--'
return
elif _isstring(subparts):
# e.g. a non-strict parse of a message with no starting boundary. # e.g. a non-strict parse of a message with no starting boundary.
self._fp.write(subparts) self._fp.write(subparts)
return return
elif not isinstance(subparts, ListType): elif not isinstance(subparts, list):
# Scalar payload # Scalar payload
subparts = [subparts] subparts = [subparts]
for part in subparts: for part in subparts:
...@@ -242,28 +216,26 @@ class Generator: ...@@ -242,28 +216,26 @@ class Generator:
# suite. # suite.
if msg.get_boundary() <> boundary: if msg.get_boundary() <> boundary:
msg.set_boundary(boundary) msg.set_boundary(boundary)
# Write out any preamble # If there's a preamble, write it out, with a trailing CRLF
if msg.preamble is not None: if msg.preamble is not None:
self._fp.write(msg.preamble) print >> self._fp, msg.preamble
# If preamble is the empty string, the length of the split will be # dash-boundary transport-padding CRLF
# 1, but the last element will be the empty string. If it's
# anything else but does not end in a line separator, the length
# will be > 1 and not end in an empty string. We need to
# guarantee a newline after the preamble, but don't add too many.
plines = NLCRE.split(msg.preamble)
if plines <> [''] and plines[-1] <> '':
self._fp.write('\n')
# First boundary is a bit different; it doesn't have a leading extra
# newline.
print >> self._fp, '--' + boundary print >> self._fp, '--' + boundary
# Join and write the individual parts # body-part
joiner = '\n--' + boundary + '\n' if msgtexts:
self._fp.write(joiner.join(msgtexts)) self._fp.write(msgtexts.pop(0))
print >> self._fp, '\n--' + boundary + '--', # *encapsulation
# Write out any epilogue # --> delimiter transport-padding
# --> CRLF body-part
for body_part in msgtexts:
# delimiter transport-padding CRLF
print >> self._fp, '\n--' + boundary
# body-part
self._fp.write(body_part)
# close-delimiter transport-padding
self._fp.write('\n--' + boundary + '--')
if msg.epilogue is not None: if msg.epilogue is not None:
if not msg.epilogue.startswith('\n'): print >> self._fp
print >> self._fp
self._fp.write(msg.epilogue) self._fp.write(msg.epilogue)
def _handle_message_delivery_status(self, msg): def _handle_message_delivery_status(self, msg):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment