Back to index

python3.2  3.2.2
Classes | Functions | Variables
email.quoprimime Namespace Reference

Classes

class  _body_accumulator

Functions

def header_check
def body_check
def header_length
def body_length
def _max_append
def unquote
def quote
def header_encode
def body_encode
def decode
def _unquote_match
def header_decode

Variables

list __all__
string CRLF = '\r\n'
string NL = '\n'
string EMPTYSTRING = ''
tuple _QUOPRI_HEADER_MAP = dict((c, '=%02X' % c) for c in range(256))
tuple _QUOPRI_BODY_MAP = _QUOPRI_HEADER_MAP.copy()
 body_decode = decode
 decodestring = decode

Function Documentation

def email.quoprimime._max_append (   L,
  s,
  maxlen,
  extra = '' 
) [private]

Definition at line 107 of file quoprimime.py.

00107 
00108 def _max_append(L, s, maxlen, extra=''):
00109     if not isinstance(s, str):
00110         s = chr(s)
00111     if not L:
00112         L.append(s.lstrip())
00113     elif len(L[-1]) + len(s) <= maxlen:
00114         L[-1] += extra + s
00115     else:
00116         L.append(s.lstrip())
00117 

Here is the call graph for this function:

def email.quoprimime._unquote_match (   match) [private]
Turn a match in the form =AB to the ASCII character with value 0xab

Definition at line 307 of file quoprimime.py.

00307 
00308 def _unquote_match(match):
00309     """Turn a match in the form =AB to the ASCII character with value 0xab"""
00310     s = match.group(0)
00311     return unquote(s)
00312 
00313 
# Header decoding is done a bit differently

Here is the call graph for this function:

def email.quoprimime.body_check (   octet)
Return True if the octet should be escaped with body quopri.

Definition at line 79 of file quoprimime.py.

00079 
00080 def body_check(octet):
00081     """Return True if the octet should be escaped with body quopri."""
00082     return chr(octet) != _QUOPRI_BODY_MAP[octet]
00083 

Here is the call graph for this function:

Here is the caller graph for this function:

def email.quoprimime.body_encode (   body,
  maxlinelen = 76,
  eol = NL 
)
Encode with quoted-printable, wrapping at maxlinelen characters.

Each line of encoded text will end with eol, which defaults to "\\n".  Set
this to "\\r\\n" if you will be using the result of this function directly
in an email.

Each line will be wrapped at, at most, maxlinelen characters before the
eol string (maxlinelen defaults to 76 characters, the maximum value
permitted by RFC 2045).  Long lines will have the 'soft line break'
quoted-printable character "=" appended to them, so the decoded text will
be identical to the original text.

The minimum maxlinelen is 4 to have room for a quoted character ("=XX")
followed by a soft line break.  Smaller values will generate a
ValueError.

Definition at line 205 of file quoprimime.py.

00205 
00206 def body_encode(body, maxlinelen=76, eol=NL):
00207     """Encode with quoted-printable, wrapping at maxlinelen characters.
00208 
00209     Each line of encoded text will end with eol, which defaults to "\\n".  Set
00210     this to "\\r\\n" if you will be using the result of this function directly
00211     in an email.
00212 
00213     Each line will be wrapped at, at most, maxlinelen characters before the
00214     eol string (maxlinelen defaults to 76 characters, the maximum value
00215     permitted by RFC 2045).  Long lines will have the 'soft line break'
00216     quoted-printable character "=" appended to them, so the decoded text will
00217     be identical to the original text.
00218 
00219     The minimum maxlinelen is 4 to have room for a quoted character ("=XX")
00220     followed by a soft line break.  Smaller values will generate a
00221     ValueError.
00222 
00223     """
00224 
00225     if maxlinelen < 4:
00226         raise ValueError("maxlinelen must be at least 4")
00227     if not body:
00228         return body
00229 
00230     # The last line may or may not end in eol, but all other lines do.
00231     last_has_eol = (body[-1] in '\r\n')
00232 
00233     # This accumulator will make it easier to build the encoded body.
00234     encoded_body = _body_accumulator(maxlinelen, eol)
00235 
00236     lines = body.splitlines()
00237     last_line_no = len(lines) - 1
00238     for line_no, line in enumerate(lines):
00239         last_char_index = len(line) - 1
00240         for i, c in enumerate(line):
00241             if body_check(ord(c)):
00242                 c = quote(c)
00243             encoded_body.write_char(c, i==last_char_index)
00244         # Add an eol if input line had eol.  All input lines have eol except
00245         # possibly the last one.
00246         if line_no < last_line_no or last_has_eol:
00247             encoded_body.newline()
00248 
00249     return encoded_body.getvalue()
00250 
00251 
00252 
00253 # BAW: I'm not sure if the intent was for the signature of this function to be
# the same as base64MIME.decode() or not...

Here is the call graph for this function:

Here is the caller graph for this function:

def email.quoprimime.body_length (   bytearray)
Return a body quoted-printable encoding length.

:param bytearray: An array of bytes (a.k.a. octets).
:return: The length in bytes of the byte array when it is encoded with
    quoted-printable for bodies.

Definition at line 97 of file quoprimime.py.

00097 
00098 def body_length(bytearray):
00099     """Return a body quoted-printable encoding length.
00100 
00101     :param bytearray: An array of bytes (a.k.a. octets).
00102     :return: The length in bytes of the byte array when it is encoded with
00103         quoted-printable for bodies.
00104     """
00105     return sum(len(_QUOPRI_BODY_MAP[octet]) for octet in bytearray)
00106 

Here is the call graph for this function:

def email.quoprimime.decode (   encoded,
  eol = NL 
)
Decode a quoted-printable string.

Lines are separated with eol, which defaults to \\n.

Definition at line 254 of file quoprimime.py.

00254 
00255 def decode(encoded, eol=NL):
00256     """Decode a quoted-printable string.
00257 
00258     Lines are separated with eol, which defaults to \\n.
00259     """
00260     if not encoded:
00261         return encoded
00262     # BAW: see comment in encode() above.  Again, we're building up the
00263     # decoded string with string concatenation, which could be done much more
00264     # efficiently.
00265     decoded = ''
00266 
00267     for line in encoded.splitlines():
00268         line = line.rstrip()
00269         if not line:
00270             decoded += eol
00271             continue
00272 
00273         i = 0
00274         n = len(line)
00275         while i < n:
00276             c = line[i]
00277             if c != '=':
00278                 decoded += c
00279                 i += 1
00280             # Otherwise, c == "=".  Are we at the end of the line?  If so, add
00281             # a soft line break.
00282             elif i+1 == n:
00283                 i += 1
00284                 continue
00285             # Decode if in form =AB
00286             elif i+2 < n and line[i+1] in hexdigits and line[i+2] in hexdigits:
00287                 decoded += unquote(line[i:i+3])
00288                 i += 3
00289             # Otherwise, not in form =AB, pass literally
00290             else:
00291                 decoded += c
00292                 i += 1
00293 
00294             if i == n:
00295                 decoded += eol
00296     # Special case if original string did not end with eol
00297     if encoded[-1] not in '\r\n' and decoded.endswith(eol):
00298         decoded = decoded[:-1]
00299     return decoded
00300 
00301 
# For convenience and backwards compatibility w/ standard base64 module

Here is the call graph for this function:

Return True if the octet should be escaped with header quopri.

Definition at line 74 of file quoprimime.py.

00074 
00075 def header_check(octet):
00076     """Return True if the octet should be escaped with header quopri."""
00077     return chr(octet) != _QUOPRI_HEADER_MAP[octet]
00078 

Here is the call graph for this function:

Decode a string encoded with RFC 2045 MIME header `Q' encoding.

This function does not parse a full MIME header value encoded with
quoted-printable (like =?iso-8895-1?q?Hello_World?=) -- please use
the high level email.header class for that functionality.

Definition at line 314 of file quoprimime.py.

00314 
00315 def header_decode(s):
00316     """Decode a string encoded with RFC 2045 MIME header `Q' encoding.
00317 
00318     This function does not parse a full MIME header value encoded with
00319     quoted-printable (like =?iso-8895-1?q?Hello_World?=) -- please use
00320     the high level email.header class for that functionality.
00321     """
00322     s = s.replace('_', ' ')
00323     return re.sub(r'=[a-fA-F0-9]{2}', _unquote_match, s, re.ASCII)

Here is the call graph for this function:

Here is the caller graph for this function:

def email.quoprimime.header_encode (   header_bytes,
  charset = 'iso-8859-1' 
)
Encode a single header line with quoted-printable (like) encoding.

Defined in RFC 2045, this `Q' encoding is similar to quoted-printable, but
used specifically for email header fields to allow charsets with mostly 7
bit characters (and some 8 bit) to remain more or less readable in non-RFC
2045 aware mail clients.

charset names the character set to use in the RFC 2046 header.  It
defaults to iso-8859-1.

Definition at line 128 of file quoprimime.py.

00128 
00129 def header_encode(header_bytes, charset='iso-8859-1'):
00130     """Encode a single header line with quoted-printable (like) encoding.
00131 
00132     Defined in RFC 2045, this `Q' encoding is similar to quoted-printable, but
00133     used specifically for email header fields to allow charsets with mostly 7
00134     bit characters (and some 8 bit) to remain more or less readable in non-RFC
00135     2045 aware mail clients.
00136 
00137     charset names the character set to use in the RFC 2046 header.  It
00138     defaults to iso-8859-1.
00139     """
00140     # Return empty headers as an empty string.
00141     if not header_bytes:
00142         return ''
00143     # Iterate over every byte, encoding if necessary.
00144     encoded = []
00145     for octet in header_bytes:
00146         encoded.append(_QUOPRI_HEADER_MAP[octet])
00147     # Now add the RFC chrome to each encoded chunk and glue the chunks
00148     # together.
00149     return '=?%s?q?%s?=' % (charset, EMPTYSTRING.join(encoded))
00150 

def email.quoprimime.header_length (   bytearray)
Return a header quoted-printable encoding length.

Note that this does not include any RFC 2047 chrome added by
`header_encode()`.

:param bytearray: An array of bytes (a.k.a. octets).
:return: The length in bytes of the byte array when it is encoded with
    quoted-printable for headers.

Definition at line 84 of file quoprimime.py.

00084 
00085 def header_length(bytearray):
00086     """Return a header quoted-printable encoding length.
00087 
00088     Note that this does not include any RFC 2047 chrome added by
00089     `header_encode()`.
00090 
00091     :param bytearray: An array of bytes (a.k.a. octets).
00092     :return: The length in bytes of the byte array when it is encoded with
00093         quoted-printable for headers.
00094     """
00095     return sum(len(_QUOPRI_HEADER_MAP[octet]) for octet in bytearray)
00096 

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 123 of file quoprimime.py.

00123 
00124 def quote(c):
00125     return '=%02X' % ord(c)
00126 
00127 

Here is the call graph for this function:

Here is the caller graph for this function:

Turn a string in the form =AB to the ASCII character with value 0xab

Definition at line 118 of file quoprimime.py.

00118 
00119 def unquote(s):
00120     """Turn a string in the form =AB to the ASCII character with value 0xab"""
00121     return chr(int(s[1:3], 16))
00122 

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Initial value:
00001 [
00002     'body_decode',
00003     'body_encode',
00004     'body_length',
00005     'decode',
00006     'decodestring',
00007     'header_decode',
00008     'header_encode',
00009     'header_length',
00010     'quote',
00011     'unquote',
00012     ]

Definition at line 29 of file quoprimime.py.

tuple email.quoprimime._QUOPRI_BODY_MAP = _QUOPRI_HEADER_MAP.copy()

Definition at line 57 of file quoprimime.py.

tuple email.quoprimime._QUOPRI_HEADER_MAP = dict((c, '=%02X' % c) for c in range(256))

Definition at line 56 of file quoprimime.py.

Definition at line 302 of file quoprimime.py.

Definition at line 47 of file quoprimime.py.

Definition at line 303 of file quoprimime.py.

Definition at line 49 of file quoprimime.py.

Definition at line 48 of file quoprimime.py.