Back to index

moin  1.9.0~rc2
HeaderFixed.py
Go to the documentation of this file.
00001 # copied from email.Header because the original is broken
00002 
00003 # Copyright (C) 2002-2004 Python Software Foundation
00004 # Author: Ben Gertzfield, Barry Warsaw
00005 # Contact: email-sig@python.org
00006 
00007 import sys, binascii
00008 
00009 from email.Header import ecre
00010 
00011 import email.quopriMIME
00012 import email.base64MIME
00013 from email.Errors import HeaderParseError
00014 from email.Charset import Charset
00015 
00016 SPACE = ' '
00017 
00018 if sys.version_info[:3] < (2, 9, 0): # insert the version number
00019                                      # of a fixed python here
00020 
00021     def decode_header(header):
00022         """Decode a message header value without converting charset.
00023     
00024         Returns a list of (decoded_string, charset) pairs containing each of the
00025         decoded parts of the header.  Charset is None for non-encoded parts of the
00026         header, otherwise a lower-case string containing the name of the character
00027         set specified in the encoded string.
00028     
00029         An email.Errors.HeaderParseError may be raised when certain decoding error
00030         occurs (e.g. a base64 decoding exception).
00031         """
00032         # If no encoding, just return the header
00033         header = str(header)
00034         if not ecre.search(header):
00035             return [(header, None)]
00036         decoded = []
00037         dec = ''
00038         for line in header.splitlines():
00039             # This line might not have an encoding in it
00040             if not ecre.search(line):
00041                 decoded.append((line, None))
00042                 continue
00043             parts = ecre.split(line)
00044             while parts:
00045                 unenc = parts.pop(0).rstrip()
00046                 if unenc:
00047                     # Should we continue a long line?
00048                     if decoded and decoded[-1][1] is None:
00049                         decoded[-1] = (decoded[-1][0] + SPACE + unenc, None)
00050                     else:
00051                         decoded.append((unenc, None))
00052                 if parts:
00053                     charset, encoding = [s.lower() for s in parts[0:2]]
00054                     encoded = parts[2]
00055                     dec = None
00056                     if encoding == 'q':
00057                         dec = email.quopriMIME.header_decode(encoded)
00058                     elif encoding == 'b':
00059                         try:
00060                             dec = email.base64MIME.decode(encoded)
00061                         except binascii.Error:
00062                             # Turn this into a higher level exception.  BAW: Right
00063                             # now we throw the lower level exception away but
00064                             # when/if we get exception chaining, we'll preserve it.
00065                             raise HeaderParseError
00066                     if dec is None:
00067                         dec = encoded
00068     
00069                     if decoded and decoded[-1][1] == charset:
00070                         decoded[-1] = (decoded[-1][0] + dec, decoded[-1][1])
00071                     else:
00072                         decoded.append((dec, charset))
00073                 del parts[0:3]
00074         return decoded
00075 
00076 else:
00077     from email.Header import decode_header