Back to index

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

Classes

class  Header
class  _ValueFormatter
class  _Accumulator

Functions

def decode_header
def make_header

Variables

list __all__
 Charset = _charset.Charset
string NL = '\n'
string SPACE = ' '
string BSPACE = ' '
string SPACE8 = ' '
string EMPTYSTRING = ''
int MAXLINELEN = 78
string FWS = ' \t'
tuple USASCII = Charset('us-ascii')
tuple UTF8 = Charset('utf-8')
tuple ecre
tuple fcre = re.compile(r'[\041-\176]+:$')
tuple _embeded_header = re.compile(r'\n[^ \t]+:')
 _max_append = email.quoprimime._max_append

Function Documentation

def email.header.decode_header (   header)
Decode a message header value without converting charset.

Returns a list of (string, charset) pairs containing each of the decoded
parts of the header.  Charset is None for non-encoded parts of the header,
otherwise a lower-case string containing the name of the character set
specified in the encoded string.

header may be a string that may or may not contain RFC2047 encoded words,
or it may be a Header object.

An email.errors.HeaderParseError may be raised when certain decoding error
occurs (e.g. a base64 decoding exception).

Definition at line 62 of file header.py.

00062 
00063 def decode_header(header):
00064     """Decode a message header value without converting charset.
00065 
00066     Returns a list of (string, charset) pairs containing each of the decoded
00067     parts of the header.  Charset is None for non-encoded parts of the header,
00068     otherwise a lower-case string containing the name of the character set
00069     specified in the encoded string.
00070 
00071     header may be a string that may or may not contain RFC2047 encoded words,
00072     or it may be a Header object.
00073 
00074     An email.errors.HeaderParseError may be raised when certain decoding error
00075     occurs (e.g. a base64 decoding exception).
00076     """
00077     # If it is a Header object, we can just return the encoded chunks.
00078     if hasattr(header, '_chunks'):
00079         return [(_charset._encode(string, str(charset)), str(charset))
00080                     for string, charset in header._chunks]
00081     # If no encoding, just return the header with no charset.
00082     if not ecre.search(header):
00083         return [(header, None)]
00084     # First step is to parse all the encoded parts into triplets of the form
00085     # (encoded_string, encoding, charset).  For unencoded strings, the last
00086     # two parts will be None.
00087     words = []
00088     for line in header.splitlines():
00089         parts = ecre.split(line)
00090         while parts:
00091             unencoded = parts.pop(0).strip()
00092             if unencoded:
00093                 words.append((unencoded, None, None))
00094             if parts:
00095                 charset = parts.pop(0).lower()
00096                 encoding = parts.pop(0).lower()
00097                 encoded = parts.pop(0)
00098                 words.append((encoded, encoding, charset))
00099     # The next step is to decode each encoded word by applying the reverse
00100     # base64 or quopri transformation.  decoded_words is now a list of the
00101     # form (decoded_word, charset).
00102     decoded_words = []
00103     for encoded_string, encoding, charset in words:
00104         if encoding is None:
00105             # This is an unencoded word.
00106             decoded_words.append((encoded_string, charset))
00107         elif encoding == 'q':
00108             word = email.quoprimime.header_decode(encoded_string)
00109             decoded_words.append((word, charset))
00110         elif encoding == 'b':
00111             paderr = len(encoded_string) % 4   # Postel's law: add missing padding
00112             if paderr:
00113                 encoded_string += '==='[:4 - paderr]
00114             try:
00115                 word = email.base64mime.decode(encoded_string)
00116             except binascii.Error:
00117                 raise HeaderParseError('Base64 decoding error')
00118             else:
00119                 decoded_words.append((word, charset))
00120         else:
00121             raise AssertionError('Unexpected encoding: ' + encoding)
00122     # Now convert all words to bytes and collapse consecutive runs of
00123     # similarly encoded words.
00124     collapsed = []
00125     last_word = last_charset = None
00126     for word, charset in decoded_words:
00127         if isinstance(word, str):
00128             word = bytes(word, 'raw-unicode-escape')
00129         if last_word is None:
00130             last_word = word
00131             last_charset = charset
00132         elif charset != last_charset:
00133             collapsed.append((last_word, last_charset))
00134             last_word = word
00135             last_charset = charset
00136         elif last_charset is None:
00137             last_word += BSPACE + word
00138         else:
00139             last_word += word
00140     collapsed.append((last_word, last_charset))
00141     return collapsed
00142 
00143 


Here is the call graph for this function:

Here is the caller graph for this function:

def email.header.make_header (   decoded_seq,
  maxlinelen = None,
  header_name = None,
  continuation_ws = ' ' 
)
Create a Header from a sequence of pairs as returned by decode_header()

decode_header() takes a header value string and returns a sequence of
pairs of the format (decoded_string, charset) where charset is the string
name of the character set.

This function takes one of those sequence of pairs and returns a Header
instance.  Optional maxlinelen, header_name, and continuation_ws are as in
the Header constructor.

Definition at line 145 of file header.py.

00145 
00146                 continuation_ws=' '):
00147     """Create a Header from a sequence of pairs as returned by decode_header()
00148 
00149     decode_header() takes a header value string and returns a sequence of
00150     pairs of the format (decoded_string, charset) where charset is the string
00151     name of the character set.
00152 
00153     This function takes one of those sequence of pairs and returns a Header
00154     instance.  Optional maxlinelen, header_name, and continuation_ws are as in
00155     the Header constructor.
00156     """
00157     h = Header(maxlinelen=maxlinelen, header_name=header_name,
00158                continuation_ws=continuation_ws)
00159     for s, charset in decoded_seq:
00160         # None means us-ascii but we can simply pass it on to h.append()
00161         if charset is not None and not isinstance(charset, Charset):
00162             charset = Charset(charset)
00163         h.append(s, charset)
00164     return h
00165 
00166 


Here is the caller graph for this function:


Variable Documentation

Initial value:
00001 [
00002     'Header',
00003     'decode_header',
00004     'make_header',
00005     ]

Definition at line 7 of file header.py.

Definition at line 53 of file header.py.

Definition at line 58 of file header.py.

Definition at line 25 of file header.py.

email.header.Charset = _charset.Charset

Definition at line 21 of file header.py.

Initial value:
00001 re.compile(r'''
00002   =\?                   # literal =?
00003   (?P<charset>[^?]*?)   # non-greedy up to the next ? is the charset
00004   \?                    # literal ?
00005   (?P<encoding>[qb])    # either a "q" or a "b", case insensitive
00006   \?                    # literal ?
00007   (?P<encoded>.*?)      # non-greedy up to the next ?= is the encoded string
00008   \?=                   # literal ?=
00009   (?=[ \t]|$)           # whitespace or the end of the string
00010   ''', re.VERBOSE | re.IGNORECASE | re.MULTILINE)

Definition at line 35 of file header.py.

Definition at line 27 of file header.py.

tuple email.header.fcre = re.compile(r'[\041-\176]+:$')

Definition at line 49 of file header.py.

Definition at line 29 of file header.py.

Definition at line 28 of file header.py.

Definition at line 23 of file header.py.

Definition at line 24 of file header.py.

Definition at line 26 of file header.py.

tuple email.header.USASCII = Charset('us-ascii')

Definition at line 31 of file header.py.

tuple email.header.UTF8 = Charset('utf-8')

Definition at line 32 of file header.py.