Kaydet (Commit) 56835dd9 authored tarafından Barry Warsaw's avatar Barry Warsaw

Use True/False everywhere.

üst 5932c9be
...@@ -18,6 +18,11 @@ try: ...@@ -18,6 +18,11 @@ try:
except SyntaxError: except SyntaxError:
from email._compat21 import _isstring from email._compat21 import _isstring
try:
True, False
except NameError:
True = 1
False = 0
EMPTYSTRING = '' EMPTYSTRING = ''
SEMISPACE = '; ' SEMISPACE = '; '
...@@ -42,14 +47,15 @@ class Generator: ...@@ -42,14 +47,15 @@ class Generator:
# Public interface # Public interface
# #
def __init__(self, outfp, mangle_from_=1, maxheaderlen=78): def __init__(self, outfp, mangle_from_=True, maxheaderlen=78):
"""Create the generator for message flattening. """Create the generator for message flattening.
outfp is the output file-like object for writing the message to. It outfp is the output file-like object for writing the message to. It
must have a write() method. must have a write() method.
Optional mangle_from_ is a flag that, when true, escapes From_ lines Optional mangle_from_ is a flag that, when True (the default), escapes
in the body of the message by putting a `>' in front of them. From_ lines in the body of the message by putting a `>' in front of
them.
Optional maxheaderlen specifies the longest length for a non-continued Optional maxheaderlen specifies the longest length for a non-continued
header. When a header line is longer (in characters, with tabs header. When a header line is longer (in characters, with tabs
...@@ -61,21 +67,20 @@ class Generator: ...@@ -61,21 +67,20 @@ class Generator:
""" """
self._fp = outfp self._fp = outfp
self._mangle_from_ = mangle_from_ self._mangle_from_ = mangle_from_
self.__first = 1
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
self._fp.write(s) self._fp.write(s)
def flatten(self, msg, unixfrom=0): def flatten(self, msg, unixfrom=False):
"""Print the message object tree rooted at msg to the output file """Print the message object tree rooted at msg to the output file
specified when the Generator instance was created. specified when the Generator instance was created.
unixfrom is a flag that forces the printing of a Unix From_ delimiter unixfrom is a flag that forces the printing of a Unix From_ delimiter
before the first object in the message tree. If the original message before the first object in the message tree. If the original message
has no From_ delimiter, a `standard' one is crafted. By default, this has no From_ delimiter, a `standard' one is crafted. By default, this
is 0 to inhibit the printing of any From_ delimiter. is False to inhibit the printing of any From_ delimiter.
Note that for subobjects, no From_ line is printed. Note that for subobjects, no From_ line is printed.
""" """
...@@ -146,23 +151,17 @@ class Generator: ...@@ -146,23 +151,17 @@ class Generator:
def _write_headers(self, msg): def _write_headers(self, msg):
for h, v in msg.items(): for h, v in msg.items():
# We only write the MIME-Version: header for the outermost
# container message. Unfortunately, we can't use same technique
# as for the Unix-From above because we don't know when
# MIME-Version: will occur.
if h.lower() == 'mime-version' and not self.__first:
continue
# RFC 2822 says that lines SHOULD be no more than maxheaderlen # RFC 2822 says that lines SHOULD be no more than maxheaderlen
# characters wide, so we're well within our rights to split long # characters wide, so we're well within our rights to split long
# headers. # headers.
text = '%s: %s' % (h, v) text = '%s: %s' % (h, v)
if self.__maxheaderlen > 0 and len(text) > self.__maxheaderlen: if self.__maxheaderlen > 0 and len(text) > self.__maxheaderlen:
text = self._split_header(h, text) text = self._split_header(text)
print >> self._fp, text print >> self._fp, text
# A blank line always separates headers from body # A blank line always separates headers from body
print >> self._fp print >> self._fp
def _split_header(self, name, text): def _split_header(self, text):
maxheaderlen = self.__maxheaderlen maxheaderlen = self.__maxheaderlen
# Find out whether any lines in the header are really longer than # Find out whether any lines in the header are really longer than
# maxheaderlen characters wide. There could be continuation lines # maxheaderlen characters wide. There could be continuation lines
...@@ -225,7 +224,7 @@ class Generator: ...@@ -225,7 +224,7 @@ class Generator:
for part in subparts: for part in subparts:
s = StringIO() s = StringIO()
g = self.clone(s) g = self.clone(s)
g.flatten(part, unixfrom=0) g.flatten(part, unixfrom=False)
msgtexts.append(s.getvalue()) msgtexts.append(s.getvalue())
# Now make sure the boundary we've selected doesn't appear in any of # Now make sure the boundary we've selected doesn't appear in any of
# the message texts. # the message texts.
...@@ -264,7 +263,7 @@ class Generator: ...@@ -264,7 +263,7 @@ class Generator:
for part in msg.get_payload(): for part in msg.get_payload():
s = StringIO() s = StringIO()
g = self.clone(s) g = self.clone(s)
g.flatten(part, unixfrom=0) g.flatten(part, unixfrom=False)
text = s.getvalue() text = s.getvalue()
lines = text.split('\n') lines = text.split('\n')
# Strip off the unnecessary trailing empty line # Strip off the unnecessary trailing empty line
...@@ -284,7 +283,7 @@ class Generator: ...@@ -284,7 +283,7 @@ class Generator:
# of length 1. The zeroth element of the list should be the Message # of length 1. The zeroth element of the list should be the Message
# object for the subpart. Extract that object, stringify it, and # object for the subpart. Extract that object, stringify it, and
# write it out. # write it out.
g.flatten(msg.get_payload(0), unixfrom=0) g.flatten(msg.get_payload(0), unixfrom=False)
self._fp.write(s.getvalue()) self._fp.write(s.getvalue())
...@@ -295,7 +294,7 @@ class DecodedGenerator(Generator): ...@@ -295,7 +294,7 @@ class DecodedGenerator(Generator):
Like the Generator base class, except that non-text parts are substituted Like the Generator base class, except that non-text parts are substituted
with a format string representing the part. with a format string representing the part.
""" """
def __init__(self, outfp, mangle_from_=1, maxheaderlen=78, fmt=None): def __init__(self, outfp, mangle_from_=True, maxheaderlen=78, fmt=None):
"""Like Generator.__init__() except that an additional optional """Like Generator.__init__() except that an additional optional
argument is allowed. argument is allowed.
...@@ -327,7 +326,7 @@ class DecodedGenerator(Generator): ...@@ -327,7 +326,7 @@ class DecodedGenerator(Generator):
for part in msg.walk(): for part in msg.walk():
maintype = part.get_main_type('text') maintype = part.get_main_type('text')
if maintype == 'text': if maintype == 'text':
print >> self, part.get_payload(decode=1) print >> self, part.get_payload(decode=True)
elif maintype == 'multipart': elif maintype == 'multipart':
# Just skip this # Just skip this
pass pass
...@@ -354,7 +353,7 @@ def _make_boundary(text=None): ...@@ -354,7 +353,7 @@ def _make_boundary(text=None):
return boundary return boundary
b = boundary b = boundary
counter = 0 counter = 0
while 1: while True:
cre = re.compile('^--' + re.escape(b) + '(--)?$', re.MULTILINE) cre = re.compile('^--' + re.escape(b) + '(--)?$', re.MULTILINE)
if not cre.search(text): if not cre.search(text):
break break
......
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