annotate MoinMoin/support/HeaderFixed.py @ 1079:029754c52b11

Changed file format of meta file (\r\n line endings). Enhanced docstrings.
author Alexander Schremmer <alex AT alexanderweb DOT de>
date Fri, 28 Jul 2006 16:01:19 +0200
parents 7a6e256dd074
children 6dd2e29acffe
rev   line source
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
1 # copied from email.Header because the original is broken
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
2
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
3 # Copyright (C) 2002-2004 Python Software Foundation
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
4 # Author: Ben Gertzfield, Barry Warsaw
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
5 # Contact: email-sig@python.org
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
6
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
7 import sys
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
8
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
9 from email.Header import ecre
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
10
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
11 import email.quopriMIME
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
12 import email.base64MIME
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
13 from email.Errors import HeaderParseError
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
14 from email.Charset import Charset
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
15
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
16 if sys.version_info[:3] < (2, 9, 0): # insert the version number
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
17 # of a fixed python here
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
18
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
19 def decode_header(header):
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
20 """Decode a message header value without converting charset.
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
21
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
22 Returns a list of (decoded_string, charset) pairs containing each of the
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
23 decoded parts of the header. Charset is None for non-encoded parts of the
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
24 header, otherwise a lower-case string containing the name of the character
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
25 set specified in the encoded string.
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
26
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
27 An email.Errors.HeaderParseError may be raised when certain decoding error
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
28 occurs (e.g. a base64 decoding exception).
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
29 """
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
30 # If no encoding, just return the header
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
31 header = str(header)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
32 if not ecre.search(header):
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
33 return [(header, None)]
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
34 decoded = []
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
35 dec = ''
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
36 for line in header.splitlines():
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
37 # This line might not have an encoding in it
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
38 if not ecre.search(line):
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
39 decoded.append((line, None))
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
40 continue
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
41 parts = ecre.split(line)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
42 while parts:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
43 unenc = parts.pop(0).rstrip()
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
44 if unenc:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
45 # Should we continue a long line?
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
46 if decoded and decoded[-1][1] is None:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
47 decoded[-1] = (decoded[-1][0] + SPACE + unenc, None)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
48 else:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
49 decoded.append((unenc, None))
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
50 if parts:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
51 charset, encoding = [s.lower() for s in parts[0:2]]
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
52 encoded = parts[2]
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
53 dec = None
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
54 if encoding == 'q':
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
55 dec = email.quopriMIME.header_decode(encoded)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
56 elif encoding == 'b':
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
57 try:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
58 dec = email.base64MIME.decode(encoded)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
59 except binascii.Error:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
60 # Turn this into a higher level exception. BAW: Right
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
61 # now we throw the lower level exception away but
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
62 # when/if we get exception chaining, we'll preserve it.
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
63 raise HeaderParseError
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
64 if dec is None:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
65 dec = encoded
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
66
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
67 if decoded and decoded[-1][1] == charset:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
68 decoded[-1] = (decoded[-1][0] + dec, decoded[-1][1])
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
69 else:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
70 decoded.append((dec, charset))
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
71 del parts[0:3]
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
72 return decoded
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
73
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
74 else:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
75 from email.Header import decode_header