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

Use True/False everywhere, and other code cleanups.

üst f776e692
...@@ -41,6 +41,12 @@ EMPTYSTRING = '' ...@@ -41,6 +41,12 @@ EMPTYSTRING = ''
# See also Charset.py # See also Charset.py
MISC_LEN = 7 MISC_LEN = 7
try:
True, False
except NameError:
True = 1
False = 0
# Helpers # Helpers
...@@ -56,8 +62,8 @@ def base64_len(s): ...@@ -56,8 +62,8 @@ def base64_len(s):
def header_encode(header, charset='iso-8859-1', keep_eols=0, maxlinelen=76, def header_encode(header, charset='iso-8859-1', keep_eols=False,
eol=NL): maxlinelen=76, eol=NL):
"""Encode a single header line with Base64 encoding in a given charset. """Encode a single header line with Base64 encoding in a given charset.
Defined in RFC 2045, this Base64 encoding is identical to normal Base64 Defined in RFC 2045, this Base64 encoding is identical to normal Base64
...@@ -69,7 +75,7 @@ def header_encode(header, charset='iso-8859-1', keep_eols=0, maxlinelen=76, ...@@ -69,7 +75,7 @@ def header_encode(header, charset='iso-8859-1', keep_eols=0, maxlinelen=76,
End-of-line characters (\\r, \\n, \\r\\n) will be automatically converted End-of-line characters (\\r, \\n, \\r\\n) will be automatically converted
to the canonical email line separator \\r\\n unless the keep_eols to the canonical email line separator \\r\\n unless the keep_eols
parameter is set to true (the default is false). parameter is True (the default is False).
Each line of the header will be terminated in the value of eol, which Each line of the header will be terminated in the value of eol, which
defaults to "\\n". Set this to "\\r\\n" if you are using the result of defaults to "\\n". Set this to "\\r\\n" if you are using the result of
...@@ -106,7 +112,7 @@ def header_encode(header, charset='iso-8859-1', keep_eols=0, maxlinelen=76, ...@@ -106,7 +112,7 @@ def header_encode(header, charset='iso-8859-1', keep_eols=0, maxlinelen=76,
lines = [] lines = []
for line in base64ed: for line in base64ed:
# Ignore the last character of each line if it is a newline # Ignore the last character of each line if it is a newline
if line[-1] == NL: if line.endswith(NL):
line = line[:-1] line = line[:-1]
# Add the chrome # Add the chrome
lines.append('=?%s?b?%s?=' % (charset, line)) lines.append('=?%s?b?%s?=' % (charset, line))
...@@ -117,13 +123,13 @@ def header_encode(header, charset='iso-8859-1', keep_eols=0, maxlinelen=76, ...@@ -117,13 +123,13 @@ def header_encode(header, charset='iso-8859-1', keep_eols=0, maxlinelen=76,
def encode(s, binary=1, maxlinelen=76, eol=NL): def encode(s, binary=True, maxlinelen=76, eol=NL):
"""Encode a string with base64. """Encode a string with base64.
Each line will be wrapped at, at most, maxlinelen characters (defaults to Each line will be wrapped at, at most, maxlinelen characters (defaults to
76 characters). 76 characters).
If binary is false, end-of-line characters will be converted to the If binary is False, end-of-line characters will be converted to the
canonical email end-of-line sequence \\r\\n. Otherwise they will be left canonical email end-of-line sequence \\r\\n. Otherwise they will be left
verbatim (this is the default). verbatim (this is the default).
...@@ -143,7 +149,7 @@ def encode(s, binary=1, maxlinelen=76, eol=NL): ...@@ -143,7 +149,7 @@ def encode(s, binary=1, maxlinelen=76, eol=NL):
# BAW: should encode() inherit b2a_base64()'s dubious behavior in # BAW: should encode() inherit b2a_base64()'s dubious behavior in
# adding a newline to the encoded string? # adding a newline to the encoded string?
enc = b2a_base64(s[i:i + max_unencoded]) enc = b2a_base64(s[i:i + max_unencoded])
if enc[-1] == NL and eol <> NL: if enc.endswith(NL) and eol <> NL:
enc = enc[:-1] + eol enc = enc[:-1] + eol
encvec.append(enc) encvec.append(enc)
return EMPTYSTRING.join(encvec) return EMPTYSTRING.join(encvec)
......
...@@ -38,17 +38,23 @@ MISC_LEN = 7 ...@@ -38,17 +38,23 @@ MISC_LEN = 7
hqre = re.compile(r'[^-a-zA-Z0-9!*+/ ]') hqre = re.compile(r'[^-a-zA-Z0-9!*+/ ]')
bqre = re.compile(r'[^ !-<>-~\t]') bqre = re.compile(r'[^ !-<>-~\t]')
try:
True, False
except NameError:
True = 1
False = 0
# Helpers # Helpers
def header_quopri_check(c): def header_quopri_check(c):
"""Return true if the character should be escaped with header quopri.""" """Return True if the character should be escaped with header quopri."""
return hqre.match(c) and 1 return hqre.match(c) and True
def body_quopri_check(c): def body_quopri_check(c):
"""Return true if the character should be escaped with body quopri.""" """Return True if the character should be escaped with body quopri."""
return bqre.match(c) and 1 return bqre.match(c) and True
def header_quopri_len(s): def header_quopri_len(s):
...@@ -92,8 +98,8 @@ def quote(c): ...@@ -92,8 +98,8 @@ def quote(c):
def header_encode(header, charset="iso-8859-1", keep_eols=0, maxlinelen=76, def header_encode(header, charset="iso-8859-1", keep_eols=False,
eol=NL): maxlinelen=76, eol=NL):
"""Encode a single header line with quoted-printable (like) encoding. """Encode a single header line with quoted-printable (like) encoding.
Defined in RFC 2045, this `Q' encoding is similar to quoted-printable, but Defined in RFC 2045, this `Q' encoding is similar to quoted-printable, but
...@@ -114,7 +120,7 @@ def header_encode(header, charset="iso-8859-1", keep_eols=0, maxlinelen=76, ...@@ -114,7 +120,7 @@ def header_encode(header, charset="iso-8859-1", keep_eols=0, maxlinelen=76,
End-of-line characters (\\r, \\n, \\r\\n) will be automatically converted End-of-line characters (\\r, \\n, \\r\\n) will be automatically converted
to the canonical email line separator \\r\\n unless the keep_eols to the canonical email line separator \\r\\n unless the keep_eols
parameter is set to true (the default is false). parameter is True (the default is False).
Each line of the header will be terminated in the value of eol, which Each line of the header will be terminated in the value of eol, which
defaults to "\\n". Set this to "\\r\\n" if you are using the result of defaults to "\\n". Set this to "\\r\\n" if you are using the result of
...@@ -151,10 +157,10 @@ def header_encode(header, charset="iso-8859-1", keep_eols=0, maxlinelen=76, ...@@ -151,10 +157,10 @@ def header_encode(header, charset="iso-8859-1", keep_eols=0, maxlinelen=76,
def encode(body, binary=0, maxlinelen=76, eol=NL): def encode(body, binary=False, maxlinelen=76, eol=NL):
"""Encode with quoted-printable, wrapping at maxlinelen characters. """Encode with quoted-printable, wrapping at maxlinelen characters.
If binary is false (the default), end-of-line characters will be converted If binary is False (the default), end-of-line characters will be converted
to the canonical email end-of-line sequence \\r\\n. Otherwise they will to the canonical email end-of-line sequence \\r\\n. Otherwise they will
be left verbatim. be left verbatim.
...@@ -213,7 +219,7 @@ def encode(body, binary=0, maxlinelen=76, eol=NL): ...@@ -213,7 +219,7 @@ def encode(body, binary=0, maxlinelen=76, eol=NL):
# Now at end of line.. # Now at end of line..
if prev and prev in ' \t': if prev and prev in ' \t':
# Special case for whitespace at end of file # Special case for whitespace at end of file
if lineno+1 == len(lines): if lineno + 1 == len(lines):
prev = quote(prev) prev = quote(prev)
if len(encoded_line) + len(prev) > maxlinelen: if len(encoded_line) + len(prev) > maxlinelen:
encoded_body += encoded_line + '=' + eol + prev encoded_body += encoded_line + '=' + eol + prev
...@@ -283,7 +289,7 @@ def decode(encoded, eol=NL): ...@@ -283,7 +289,7 @@ def decode(encoded, eol=NL):
if i == n: if i == n:
decoded += eol decoded += eol
# Special case if original string did not end with eol # Special case if original string did not end with eol
if encoded[-1] <> eol and decoded[-1] == eol: if not encoded.endswith(eol) and decoded.endswith(eol):
decoded = decoded[:-1] decoded = decoded[:-1]
return decoded return decoded
......
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