Back to index

moin  1.9.0~rc2
Functions | Variables
MoinMoin.support.HeaderFixed Namespace Reference

Functions

def decode_header

Variables

string SPACE = ' '

Function Documentation

Decode a message header value without converting charset.
    
Returns a list of (decoded_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.
    
An email.Errors.HeaderParseError may be raised when certain decoding error
occurs (e.g. a base64 decoding exception).

Definition at line 21 of file HeaderFixed.py.

00021 
00022     def decode_header(header):
00023         """Decode a message header value without converting charset.
00024     
00025         Returns a list of (decoded_string, charset) pairs containing each of the
00026         decoded parts of the header.  Charset is None for non-encoded parts of the
00027         header, otherwise a lower-case string containing the name of the character
00028         set specified in the encoded string.
00029     
00030         An email.Errors.HeaderParseError may be raised when certain decoding error
00031         occurs (e.g. a base64 decoding exception).
00032         """
00033         # If no encoding, just return the header
00034         header = str(header)
00035         if not ecre.search(header):
00036             return [(header, None)]
00037         decoded = []
00038         dec = ''
00039         for line in header.splitlines():
00040             # This line might not have an encoding in it
00041             if not ecre.search(line):
00042                 decoded.append((line, None))
00043                 continue
00044             parts = ecre.split(line)
00045             while parts:
00046                 unenc = parts.pop(0).rstrip()
00047                 if unenc:
00048                     # Should we continue a long line?
00049                     if decoded and decoded[-1][1] is None:
00050                         decoded[-1] = (decoded[-1][0] + SPACE + unenc, None)
00051                     else:
00052                         decoded.append((unenc, None))
00053                 if parts:
00054                     charset, encoding = [s.lower() for s in parts[0:2]]
00055                     encoded = parts[2]
00056                     dec = None
00057                     if encoding == 'q':
00058                         dec = email.quopriMIME.header_decode(encoded)
00059                     elif encoding == 'b':
00060                         try:
00061                             dec = email.base64MIME.decode(encoded)
00062                         except binascii.Error:
00063                             # Turn this into a higher level exception.  BAW: Right
00064                             # now we throw the lower level exception away but
00065                             # when/if we get exception chaining, we'll preserve it.
00066                             raise HeaderParseError
00067                     if dec is None:
00068                         dec = encoded
00069     
00070                     if decoded and decoded[-1][1] == charset:
00071                         decoded[-1] = (decoded[-1][0] + dec, decoded[-1][1])
00072                     else:
00073                         decoded.append((dec, charset))
00074                 del parts[0:3]
00075         return decoded
00076 
else:

Here is the caller graph for this function:


Variable Documentation

Definition at line 16 of file HeaderFixed.py.