annotate Lib/email/Header.py @ 27998:e27f58a50737 legacy-trunk

SF bug #690377: Dud footnote reference in distutils docs
author Raymond Hettinger <python@rcn.com>
date Fri, 21 Feb 2003 02:47:09 +0000
parents 3cf5a7852a2f
children 149a5772dd0c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
1 # Copyright (C) 2002 Python Software Foundation
25494
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
2 # Author: che@debian.org (Ben Gertzfield), barry@zope.com (Barry Warsaw)
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
3
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
4 """Header encoding and decoding functionality."""
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
5
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
6 import re
25494
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
7 from types import StringType, UnicodeType
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
8
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
9 import email.quopriMIME
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
10 import email.base64MIME
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
11 from email.Charset import Charset
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
12
23474
344ae531e6a7 Fixed a bug in the splitting of lines, and improved the splitting for
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
13 try:
23617
3e3b3c1770ac The _compat modules now export _floordiv() instead of _intdiv2() for
Barry Warsaw <barry@python.org>
parents: 23521
diff changeset
14 from email._compat22 import _floordiv
23474
344ae531e6a7 Fixed a bug in the splitting of lines, and improved the splitting for
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
15 except SyntaxError:
344ae531e6a7 Fixed a bug in the splitting of lines, and improved the splitting for
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
16 # Python 2.1 spells integer division differently
23617
3e3b3c1770ac The _compat modules now export _floordiv() instead of _intdiv2() for
Barry Warsaw <barry@python.org>
parents: 23521
diff changeset
17 from email._compat21 import _floordiv
23474
344ae531e6a7 Fixed a bug in the splitting of lines, and improved the splitting for
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
18
25494
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
19 try:
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
20 True, False
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
21 except NameError:
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
22 True = 1
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
23 False = 0
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
24
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
25 CRLFSPACE = '\r\n '
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
26 CRLF = '\r\n'
24044
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
27 NL = '\n'
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
28 SPACE8 = ' ' * 8
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
29 EMPTYSTRING = ''
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
30
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
31 MAXLINELEN = 76
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
32
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
33 ENCODE = 1
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
34 DECODE = 2
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
35
25494
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
36 USASCII = Charset('us-ascii')
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
37 UTF8 = Charset('utf-8')
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
38
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
39 # Match encoded-word strings in the form =?charset?q?Hello_World?=
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
40 ecre = re.compile(r'''
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
41 =\? # literal =?
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
42 (?P<charset>[^?]*?) # non-greedy up to the next ? is the charset
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
43 \? # literal ?
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
44 (?P<encoding>[qb]) # either a "q" or a "b", case insensitive
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
45 \? # literal ?
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
46 (?P<encoded>.*?) # non-greedy up to the next ?= is the encoded string
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
47 \?= # literal ?=
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
48 ''', re.VERBOSE | re.IGNORECASE)
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
49
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
50
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
51
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
52 # Helpers
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
53 _max_append = email.quopriMIME._max_append
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
54
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
55
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
56
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
57 def decode_header(header):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
58 """Decode a message header value without converting charset.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
59
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
60 Returns a list of (decoded_string, charset) pairs containing each of the
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
61 decoded parts of the header. Charset is None for non-encoded parts of the
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
62 header, otherwise a lower-case string containing the name of the character
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
63 set specified in the encoded string.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
64 """
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
65 # If no encoding, just return the header
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
66 header = str(header)
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
67 if not ecre.search(header):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
68 return [(header, None)]
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
69 decoded = []
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
70 dec = ''
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
71 for line in header.splitlines():
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
72 # This line might not have an encoding in it
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
73 if not ecre.search(line):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
74 decoded.append((line, None))
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
75 continue
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
76 parts = ecre.split(line)
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
77 while parts:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
78 unenc = parts.pop(0).strip()
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
79 if unenc:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
80 # Should we continue a long line?
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
81 if decoded and decoded[-1][1] is None:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
82 decoded[-1] = (decoded[-1][0] + dec, None)
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
83 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
84 decoded.append((unenc, None))
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
85 if parts:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
86 charset, encoding = [s.lower() for s in parts[0:2]]
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
87 encoded = parts[2]
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
88 dec = ''
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
89 if encoding == 'q':
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
90 dec = email.quopriMIME.header_decode(encoded)
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
91 elif encoding == 'b':
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
92 dec = email.base64MIME.decode(encoded)
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
93 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
94 dec = encoded
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
95
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
96 if decoded and decoded[-1][1] == charset:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
97 decoded[-1] = (decoded[-1][0] + dec, decoded[-1][1])
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
98 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
99 decoded.append((dec, charset))
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
100 del parts[0:3]
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
101 return decoded
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
102
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
103
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
104
24160
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
105 def make_header(decoded_seq, maxlinelen=None, header_name=None,
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
106 continuation_ws=' '):
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
107 """Create a Header from a sequence of pairs as returned by decode_header()
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
108
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
109 decode_header() takes a header value string and returns a sequence of
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
110 pairs of the format (decoded_string, charset) where charset is the string
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
111 name of the character set.
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
112
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
113 This function takes one of those sequence of pairs and returns a Header
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
114 instance. Optional maxlinelen, header_name, and continuation_ws are as in
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
115 the Header constructor.
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
116 """
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
117 h = Header(maxlinelen=maxlinelen, header_name=header_name,
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
118 continuation_ws=continuation_ws)
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
119 for s, charset in decoded_seq:
24361
e8fd5cab0db3 make_header(): Watch out for charset is None, which decode_header()
Barry Warsaw <barry@python.org>
parents: 24160
diff changeset
120 # None means us-ascii but we can simply pass it on to h.append()
e8fd5cab0db3 make_header(): Watch out for charset is None, which decode_header()
Barry Warsaw <barry@python.org>
parents: 24160
diff changeset
121 if charset is not None and not isinstance(charset, Charset):
24160
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
122 charset = Charset(charset)
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
123 h.append(s, charset)
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
124 return h
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
125
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
126
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
127
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
128 class Header:
24160
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
129 def __init__(self, s=None, charset=None, maxlinelen=None, header_name=None,
26860
3cf5a7852a2f Header.__init__(), .append(): Add an optional argument `errors' which
Barry Warsaw <barry@python.org>
parents: 25777
diff changeset
130 continuation_ws=' ', errors='strict'):
25494
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
131 """Create a MIME-compliant header that can contain many character sets.
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
132
25494
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
133 Optional s is the initial header value. If None, the initial header
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
134 value is not set. You can later append to the header with .append()
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
135 method calls. s may be a byte string or a Unicode string, but see the
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
136 .append() documentation for semantics.
24160
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
137
25494
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
138 Optional charset serves two purposes: it has the same meaning as the
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
139 charset argument to the .append() method. It also sets the default
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
140 character set for all subsequent .append() calls that omit the charset
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
141 argument. If charset is not provided in the constructor, the us-ascii
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
142 charset is used both as s's initial charset and as the default for
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
143 subsequent .append() calls.
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
144
24044
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
145 The maximum line length can be specified explicit via maxlinelen. For
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
146 splitting the first line to a shorter value (to account for the field
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
147 header which isn't included in s, e.g. `Subject') pass in the name of
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
148 the field in header_name. The default maxlinelen is 76.
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
149
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
150 continuation_ws must be RFC 2822 compliant folding whitespace (usually
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
151 either a space or a hard tab) which will be prepended to continuation
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
152 lines.
26860
3cf5a7852a2f Header.__init__(), .append(): Add an optional argument `errors' which
Barry Warsaw <barry@python.org>
parents: 25777
diff changeset
153
3cf5a7852a2f Header.__init__(), .append(): Add an optional argument `errors' which
Barry Warsaw <barry@python.org>
parents: 25777
diff changeset
154 errors is passed through to the .append() call.
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
155 """
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
156 if charset is None:
25494
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
157 charset = USASCII
25771
951e3fe7ed3a __init__(): Fix an invariant, that the charset item in a chunk tuple
Barry Warsaw <barry@python.org>
parents: 25753
diff changeset
158 if not isinstance(charset, Charset):
951e3fe7ed3a __init__(): Fix an invariant, that the charset item in a chunk tuple
Barry Warsaw <barry@python.org>
parents: 25753
diff changeset
159 charset = Charset(charset)
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
160 self._charset = charset
24044
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
161 self._continuation_ws = continuation_ws
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
162 cws_expanded_len = len(continuation_ws.replace('\t', SPACE8))
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
163 # BAW: I believe `chunks' and `maxlinelen' should be non-public.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
164 self._chunks = []
24160
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
165 if s is not None:
26860
3cf5a7852a2f Header.__init__(), .append(): Add an optional argument `errors' which
Barry Warsaw <barry@python.org>
parents: 25777
diff changeset
166 self.append(s, charset, errors)
23474
344ae531e6a7 Fixed a bug in the splitting of lines, and improved the splitting for
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
167 if maxlinelen is None:
24044
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
168 maxlinelen = MAXLINELEN
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
169 if header_name is None:
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
170 # We don't know anything about the field header so the first line
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
171 # is the same length as subsequent lines.
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
172 self._firstlinelen = maxlinelen
23474
344ae531e6a7 Fixed a bug in the splitting of lines, and improved the splitting for
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
173 else:
24044
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
174 # The first line should be shorter to take into account the field
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
175 # header. Also subtract off 2 extra for the colon and space.
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
176 self._firstlinelen = maxlinelen - len(header_name) - 2
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
177 # Second and subsequent lines should subtract off the length in
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
178 # columns of the continuation whitespace prefix.
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
179 self._maxlinelen = maxlinelen - cws_expanded_len
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
180
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
181 def __str__(self):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
182 """A synonym for self.encode()."""
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
183 return self.encode()
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
184
24050
71840a1bd82f __unicode__(): Patch # 541263 by Mikhail Zabaluev, implementation
Barry Warsaw <barry@python.org>
parents: 24044
diff changeset
185 def __unicode__(self):
71840a1bd82f __unicode__(): Patch # 541263 by Mikhail Zabaluev, implementation
Barry Warsaw <barry@python.org>
parents: 24044
diff changeset
186 """Helper for the built-in unicode function."""
71840a1bd82f __unicode__(): Patch # 541263 by Mikhail Zabaluev, implementation
Barry Warsaw <barry@python.org>
parents: 24044
diff changeset
187 # charset item is a Charset instance so we need to stringify it.
71840a1bd82f __unicode__(): Patch # 541263 by Mikhail Zabaluev, implementation
Barry Warsaw <barry@python.org>
parents: 24044
diff changeset
188 uchunks = [unicode(s, str(charset)) for s, charset in self._chunks]
71840a1bd82f __unicode__(): Patch # 541263 by Mikhail Zabaluev, implementation
Barry Warsaw <barry@python.org>
parents: 24044
diff changeset
189 return u''.join(uchunks)
71840a1bd82f __unicode__(): Patch # 541263 by Mikhail Zabaluev, implementation
Barry Warsaw <barry@python.org>
parents: 24044
diff changeset
190
24160
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
191 # Rich comparison operators for equality only. BAW: does it make sense to
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
192 # have or explicitly disable <, <=, >, >= operators?
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
193 def __eq__(self, other):
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
194 # other may be a Header or a string. Both are fine so coerce
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
195 # ourselves to a string, swap the args and do another comparison.
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
196 return other == self.encode()
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
197
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
198 def __ne__(self, other):
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
199 return not self == other
fd17394f6dce make_header(): New function to take the output of decode_header() and
Barry Warsaw <barry@python.org>
parents: 24106
diff changeset
200
26860
3cf5a7852a2f Header.__init__(), .append(): Add an optional argument `errors' which
Barry Warsaw <barry@python.org>
parents: 25777
diff changeset
201 def append(self, s, charset=None, errors='strict'):
25494
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
202 """Append a string to the MIME header.
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
203
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
204 Optional charset, if given, should be a Charset instance or the name
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
205 of a character set (which will be converted to a Charset instance). A
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
206 value of None (the default) means that the charset given in the
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
207 constructor is used.
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
208
25494
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
209 s may be a byte string or a Unicode string. If it is a byte string
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
210 (i.e. isinstance(s, StringType) is true), then charset is the encoding
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
211 of that byte string, and a UnicodeError will be raised if the string
25506
6694f0ccf8ea Docstring consistency with the updated .tex files.
Barry Warsaw <barry@python.org>
parents: 25494
diff changeset
212 cannot be decoded with that charset. If s is a Unicode string, then
25494
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
213 charset is a hint specifying the character set of the characters in
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
214 the string. In this case, when producing an RFC 2822 compliant header
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
215 using RFC 2047 rules, the Unicode string will be encoded using the
25506
6694f0ccf8ea Docstring consistency with the updated .tex files.
Barry Warsaw <barry@python.org>
parents: 25494
diff changeset
216 following charsets in order: us-ascii, the charset hint, utf-8. The
6694f0ccf8ea Docstring consistency with the updated .tex files.
Barry Warsaw <barry@python.org>
parents: 25494
diff changeset
217 first character set not to provoke a UnicodeError is used.
26860
3cf5a7852a2f Header.__init__(), .append(): Add an optional argument `errors' which
Barry Warsaw <barry@python.org>
parents: 25777
diff changeset
218
3cf5a7852a2f Header.__init__(), .append(): Add an optional argument `errors' which
Barry Warsaw <barry@python.org>
parents: 25777
diff changeset
219 Optional `errors' is passed as the third argument to any unicode() or
3cf5a7852a2f Header.__init__(), .append(): Add an optional argument `errors' which
Barry Warsaw <barry@python.org>
parents: 25777
diff changeset
220 ustr.encode() call.
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
221 """
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
222 if charset is None:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
223 charset = self._charset
24362
713ad7e0406a append(): Bite the bullet and let charset be the string name of a
Barry Warsaw <barry@python.org>
parents: 24361
diff changeset
224 elif not isinstance(charset, Charset):
713ad7e0406a append(): Bite the bullet and let charset be the string name of a
Barry Warsaw <barry@python.org>
parents: 24361
diff changeset
225 charset = Charset(charset)
25777
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
226 # If the charset is our faux 8bit charset, leave the string unchanged
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
227 if charset <> '8bit':
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
228 # We need to test that the string can be converted to unicode and
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
229 # back to a byte string, given the input and output codecs of the
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
230 # charset.
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
231 if isinstance(s, StringType):
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
232 # Possibly raise UnicodeError if the byte string can't be
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
233 # converted to a unicode with the input codec of the charset.
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
234 incodec = charset.input_codec or 'us-ascii'
26860
3cf5a7852a2f Header.__init__(), .append(): Add an optional argument `errors' which
Barry Warsaw <barry@python.org>
parents: 25777
diff changeset
235 ustr = unicode(s, incodec, errors)
25777
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
236 # Now make sure that the unicode could be converted back to a
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
237 # byte string with the output codec, which may be different
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
238 # than the iput coded. Still, use the original byte string.
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
239 outcodec = charset.output_codec or 'us-ascii'
26860
3cf5a7852a2f Header.__init__(), .append(): Add an optional argument `errors' which
Barry Warsaw <barry@python.org>
parents: 25777
diff changeset
240 ustr.encode(outcodec, errors)
25777
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
241 elif isinstance(s, UnicodeType):
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
242 # Now we have to be sure the unicode string can be converted
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
243 # to a byte string with a reasonable output codec. We want to
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
244 # use the byte string in the chunk.
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
245 for charset in USASCII, charset, UTF8:
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
246 try:
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
247 outcodec = charset.output_codec or 'us-ascii'
26860
3cf5a7852a2f Header.__init__(), .append(): Add an optional argument `errors' which
Barry Warsaw <barry@python.org>
parents: 25777
diff changeset
248 s = s.encode(outcodec, errors)
25777
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
249 break
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
250 except UnicodeError:
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
251 pass
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
252 else:
84ee5a499743 append(): Fixing the test for convertability after consultation with
Barry Warsaw <barry@python.org>
parents: 25771
diff changeset
253 assert False, 'utf-8 conversion failed'
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
254 self._chunks.append((s, charset))
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23474
diff changeset
255
25494
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
256 def _split(self, s, charset, firstline=False):
25771
951e3fe7ed3a __init__(): Fix an invariant, that the charset item in a chunk tuple
Barry Warsaw <barry@python.org>
parents: 25753
diff changeset
257 # Split up a header safely for use with encode_chunks.
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
258 splittable = charset.to_splittable(s)
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
259 encoded = charset.from_splittable(splittable)
23474
344ae531e6a7 Fixed a bug in the splitting of lines, and improved the splitting for
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
260 elen = charset.encoded_header_len(encoded)
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23474
diff changeset
261
23474
344ae531e6a7 Fixed a bug in the splitting of lines, and improved the splitting for
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
262 if elen <= self._maxlinelen:
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
263 return [(encoded, charset)]
25771
951e3fe7ed3a __init__(): Fix an invariant, that the charset item in a chunk tuple
Barry Warsaw <barry@python.org>
parents: 25753
diff changeset
264 # If we have undetermined raw 8bit characters sitting in a byte
951e3fe7ed3a __init__(): Fix an invariant, that the charset item in a chunk tuple
Barry Warsaw <barry@python.org>
parents: 25753
diff changeset
265 # string, we really don't know what the right thing to do is. We
951e3fe7ed3a __init__(): Fix an invariant, that the charset item in a chunk tuple
Barry Warsaw <barry@python.org>
parents: 25753
diff changeset
266 # can't really split it because it might be multibyte data which we
951e3fe7ed3a __init__(): Fix an invariant, that the charset item in a chunk tuple
Barry Warsaw <barry@python.org>
parents: 25753
diff changeset
267 # could break if we split it between pairs. The least harm seems to
951e3fe7ed3a __init__(): Fix an invariant, that the charset item in a chunk tuple
Barry Warsaw <barry@python.org>
parents: 25753
diff changeset
268 # be to not split the header at all, but that means they could go out
951e3fe7ed3a __init__(): Fix an invariant, that the charset item in a chunk tuple
Barry Warsaw <barry@python.org>
parents: 25753
diff changeset
269 # longer than maxlinelen.
951e3fe7ed3a __init__(): Fix an invariant, that the charset item in a chunk tuple
Barry Warsaw <barry@python.org>
parents: 25753
diff changeset
270 elif charset == '8bit':
951e3fe7ed3a __init__(): Fix an invariant, that the charset item in a chunk tuple
Barry Warsaw <barry@python.org>
parents: 25753
diff changeset
271 return [(s, charset)]
24044
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
272 # BAW: I'm not sure what the right test here is. What we're trying to
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
273 # do is be faithful to RFC 2822's recommendation that ($2.2.3):
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
274 #
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
275 # "Note: Though structured field bodies are defined in such a way that
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
276 # folding can take place between many of the lexical tokens (and even
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
277 # within some of the lexical tokens), folding SHOULD be limited to
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
278 # placing the CRLF at higher-level syntactic breaks."
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
279 #
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
280 # For now, I can only imagine doing this when the charset is us-ascii,
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
281 # although it's possible that other charsets may also benefit from the
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
282 # higher-level syntactic breaks.
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
283 #
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
284 elif charset == 'us-ascii':
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
285 return self._ascii_split(s, charset, firstline)
23474
344ae531e6a7 Fixed a bug in the splitting of lines, and improved the splitting for
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
286 # BAW: should we use encoded?
344ae531e6a7 Fixed a bug in the splitting of lines, and improved the splitting for
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
287 elif elen == len(s):
344ae531e6a7 Fixed a bug in the splitting of lines, and improved the splitting for
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
288 # We can split on _maxlinelen boundaries because we know that the
344ae531e6a7 Fixed a bug in the splitting of lines, and improved the splitting for
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
289 # encoding won't change the size of the string
344ae531e6a7 Fixed a bug in the splitting of lines, and improved the splitting for
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
290 splitpnt = self._maxlinelen
25494
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
291 first = charset.from_splittable(splittable[:splitpnt], False)
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
292 last = charset.from_splittable(splittable[splitpnt:], False)
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
293 else:
23617
3e3b3c1770ac The _compat modules now export _floordiv() instead of _intdiv2() for
Barry Warsaw <barry@python.org>
parents: 23521
diff changeset
294 # Divide and conquer.
3e3b3c1770ac The _compat modules now export _floordiv() instead of _intdiv2() for
Barry Warsaw <barry@python.org>
parents: 23521
diff changeset
295 halfway = _floordiv(len(splittable), 2)
25494
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
296 first = charset.from_splittable(splittable[:halfway], False)
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
297 last = charset.from_splittable(splittable[halfway:], False)
24044
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
298 # Do the split
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
299 return self._split(first, charset, firstline) + \
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
300 self._split(last, charset)
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
301
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
302 def _ascii_split(self, s, charset, firstline):
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
303 # Attempt to split the line at the highest-level syntactic break
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
304 # possible. Note that we don't have a lot of smarts about field
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
305 # syntax; we just try to break on semi-colons, then whitespace.
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
306 rtn = []
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
307 lines = s.splitlines()
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
308 while lines:
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
309 line = lines.pop(0)
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
310 if firstline:
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
311 maxlinelen = self._firstlinelen
25494
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
312 firstline = False
24044
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
313 else:
25187
f1885b99a39c _ascii_split(): Don't lstrip continuation lines. Closes SF bug #601392.
Barry Warsaw <barry@python.org>
parents: 24362
diff changeset
314 #line = line.lstrip()
24044
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
315 maxlinelen = self._maxlinelen
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
316 # Short lines can remain unchanged
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
317 if len(line.replace('\t', SPACE8)) <= maxlinelen:
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
318 rtn.append(line)
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
319 else:
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
320 oldlen = len(line)
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
321 # Try to break the line on semicolons, but if that doesn't
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
322 # work, try to split on folding whitespace.
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
323 while len(line) > maxlinelen:
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
324 i = line.rfind(';', 0, maxlinelen)
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
325 if i < 0:
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
326 break
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
327 rtn.append(line[:i] + ';')
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
328 line = line[i+1:]
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
329 # Is the remaining stuff still longer than maxlinelen?
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
330 if len(line) <= maxlinelen:
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
331 # Splitting on semis worked
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
332 rtn.append(line)
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
333 continue
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
334 # Splitting on semis didn't finish the job. If it did any
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
335 # work at all, stick the remaining junk on the front of the
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
336 # `lines' sequence and let the next pass do its thing.
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
337 if len(line) <> oldlen:
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
338 lines.insert(0, line)
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
339 continue
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
340 # Otherwise, splitting on semis didn't help at all.
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
341 parts = re.split(r'(\s+)', line)
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
342 if len(parts) == 1 or (len(parts) == 3 and
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
343 parts[0].endswith(':')):
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
344 # This line can't be split on whitespace. There's now
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
345 # little we can do to get this into maxlinelen. BAW:
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
346 # We're still potentially breaking the RFC by possibly
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
347 # allowing lines longer than the absolute maximum of 998
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
348 # characters. For now, let it slide.
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
349 #
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
350 # len(parts) will be 1 if this line has no `Field: '
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
351 # prefix, otherwise it will be len(3).
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
352 rtn.append(line)
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
353 continue
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
354 # There is whitespace we can split on.
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
355 first = parts.pop(0)
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
356 sublines = [first]
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
357 acc = len(first)
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
358 while parts:
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
359 len0 = len(parts[0])
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
360 len1 = len(parts[1])
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
361 if acc + len0 + len1 <= maxlinelen:
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
362 sublines.append(parts.pop(0))
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
363 sublines.append(parts.pop(0))
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
364 acc += len0 + len1
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
365 else:
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
366 # Split it here, but don't forget to ignore the
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
367 # next whitespace-only part
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
368 if first <> '':
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
369 rtn.append(EMPTYSTRING.join(sublines))
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
370 del parts[0]
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
371 first = parts.pop(0)
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
372 sublines = [first]
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
373 acc = len(first)
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
374 rtn.append(EMPTYSTRING.join(sublines))
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
375 return [(chunk, charset) for chunk in rtn]
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
376
25753
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
377 def _encode_chunks(self, newchunks):
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
378 # MIME-encode a header with many different charsets and/or encodings.
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
379 #
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
380 # Given a list of pairs (string, charset), return a MIME-encoded
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
381 # string suitable for use in a header field. Each pair may have
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
382 # different charsets and/or encodings, and the resulting header will
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
383 # accurately reflect each setting.
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
384 #
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
385 # Each encoding can be email.Utils.QP (quoted-printable, for
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
386 # ASCII-like character sets like iso-8859-1), email.Utils.BASE64
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
387 # (Base64, for non-ASCII like character sets like KOI8-R and
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
388 # iso-2022-jp), or None (no encoding).
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
389 #
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
390 # Each pair will be represented on a separate line; the resulting
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
391 # string will be in the format:
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
392 #
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
393 # =?charset1?q?Mar=EDa_Gonz=E1lez_Alonso?=\n
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
394 # =?charset2?b?SvxyZ2VuIEL2aW5n?="
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
395 #
24044
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
396 chunks = []
25753
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
397 for header, charset in newchunks:
24044
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
398 if charset is None or charset.header_encoding is None:
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
399 # There's no encoding for this chunk's charsets
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
400 _max_append(chunks, header, self._maxlinelen)
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
401 else:
25494
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
402 _max_append(chunks, charset.header_encode(header),
24044
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
403 self._maxlinelen, ' ')
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
404 joiner = NL + self._continuation_ws
90cca857d203 Teach this class about "highest-level syntactic breaks" but only for
Barry Warsaw <barry@python.org>
parents: 23617
diff changeset
405 return joiner.join(chunks)
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
406
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
407 def encode(self):
25506
6694f0ccf8ea Docstring consistency with the updated .tex files.
Barry Warsaw <barry@python.org>
parents: 25494
diff changeset
408 """Encode a message header into an RFC-compliant format.
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
409
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
410 There are many issues involved in converting a given string for use in
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
411 an email header. Only certain character sets are readable in most
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
412 email clients, and as header strings can only contain a subset of
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
413 7-bit ASCII, care must be taken to properly convert and encode (with
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
414 Base64 or quoted-printable) header strings. In addition, there is a
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
415 75-character length limit on any given encoded header field, so
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
416 line-wrapping must be performed, even with double-byte character sets.
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23474
diff changeset
417
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
418 This method will do its best to convert the string to the correct
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
419 character set used in email, and encode and line wrap it safely with
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
420 the appropriate scheme for that character set.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
421
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
422 If the given charset is not known or an error occurs during
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
423 conversion, this function will return the header untouched.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
424 """
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
425 newchunks = []
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents:
diff changeset
426 for s, charset in self._chunks:
25494
c3eeaafe6265 With help from Martin v. Loewis, clarification is added for the
Barry Warsaw <barry@python.org>
parents: 25187
diff changeset
427 newchunks += self._split(s, charset, True)
25753
2d17e3cb774e _encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Barry Warsaw <barry@python.org>
parents: 25506
diff changeset
428 return self._encode_chunks(newchunks)