summaryrefslogtreecommitdiffstats
path: root/Lib/email/Generator.py
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2002-09-28 18:04:55 (GMT)
committerBarry Warsaw <barry@python.org>2002-09-28 18:04:55 (GMT)
commit56835dd96110625a21bfe20af0af3f9755d86ef9 (patch)
tree77c0f82b06788ba3b7159ce105810f4782d5c171 /Lib/email/Generator.py
parent5932c9bedd959eb8a25b8d3ad40907142cbaa4f4 (diff)
downloadcpython-56835dd96110625a21bfe20af0af3f9755d86ef9.zip
cpython-56835dd96110625a21bfe20af0af3f9755d86ef9.tar.gz
cpython-56835dd96110625a21bfe20af0af3f9755d86ef9.tar.bz2
Use True/False everywhere.
Diffstat (limited to 'Lib/email/Generator.py')
-rw-r--r--Lib/email/Generator.py39
1 files changed, 19 insertions, 20 deletions
diff --git a/Lib/email/Generator.py b/Lib/email/Generator.py
index e8bce10..7f05218 100644
--- a/Lib/email/Generator.py
+++ b/Lib/email/Generator.py
@@ -18,6 +18,11 @@ try:
except SyntaxError:
from email._compat21 import _isstring
+try:
+ True, False
+except NameError:
+ True = 1
+ False = 0
EMPTYSTRING = ''
SEMISPACE = '; '
@@ -42,14 +47,15 @@ class Generator:
# 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.
outfp is the output file-like object for writing the message to. It
must have a write() method.
- Optional mangle_from_ is a flag that, when true, escapes From_ lines
- in the body of the message by putting a `>' in front of them.
+ Optional mangle_from_ is a flag that, when True (the default), escapes
+ 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
header. When a header line is longer (in characters, with tabs
@@ -61,21 +67,20 @@ class Generator:
"""
self._fp = outfp
self._mangle_from_ = mangle_from_
- self.__first = 1
self.__maxheaderlen = maxheaderlen
def write(self, s):
# Just delegate to the file object
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
specified when the Generator instance was created.
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
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.
"""
@@ -146,23 +151,17 @@ class Generator:
def _write_headers(self, msg):
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
# characters wide, so we're well within our rights to split long
# headers.
text = '%s: %s' % (h, v)
if self.__maxheaderlen > 0 and len(text) > self.__maxheaderlen:
- text = self._split_header(h, text)
+ text = self._split_header(text)
print >> self._fp, text
# A blank line always separates headers from body
print >> self._fp
- def _split_header(self, name, text):
+ def _split_header(self, text):
maxheaderlen = self.__maxheaderlen
# Find out whether any lines in the header are really longer than
# maxheaderlen characters wide. There could be continuation lines
@@ -225,7 +224,7 @@ class Generator:
for part in subparts:
s = StringIO()
g = self.clone(s)
- g.flatten(part, unixfrom=0)
+ g.flatten(part, unixfrom=False)
msgtexts.append(s.getvalue())
# Now make sure the boundary we've selected doesn't appear in any of
# the message texts.
@@ -264,7 +263,7 @@ class Generator:
for part in msg.get_payload():
s = StringIO()
g = self.clone(s)
- g.flatten(part, unixfrom=0)
+ g.flatten(part, unixfrom=False)
text = s.getvalue()
lines = text.split('\n')
# Strip off the unnecessary trailing empty line
@@ -284,7 +283,7 @@ class Generator:
# of length 1. The zeroth element of the list should be the Message
# object for the subpart. Extract that object, stringify it, and
# write it out.
- g.flatten(msg.get_payload(0), unixfrom=0)
+ g.flatten(msg.get_payload(0), unixfrom=False)
self._fp.write(s.getvalue())
@@ -295,7 +294,7 @@ class DecodedGenerator(Generator):
Like the Generator base class, except that non-text parts are substituted
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
argument is allowed.
@@ -327,7 +326,7 @@ class DecodedGenerator(Generator):
for part in msg.walk():
maintype = part.get_main_type('text')
if maintype == 'text':
- print >> self, part.get_payload(decode=1)
+ print >> self, part.get_payload(decode=True)
elif maintype == 'multipart':
# Just skip this
pass
@@ -354,7 +353,7 @@ def _make_boundary(text=None):
return boundary
b = boundary
counter = 0
- while 1:
+ while True:
cre = re.compile('^--' + re.escape(b) + '(--)?$', re.MULTILINE)
if not cre.search(text):
break