annotate Lib/email/_parseaddr.py @ 64274:87bdf53ff4bb

#1194222: make parsedate always return RFC2822 four character years. Two character years are now converted to four character years using the Posix standard rule (<68 == 2000, >=68==1900). This makes the parsed date RFC2822 compliant even if the input is not. Patch and test by Jeffrey Finkelstein.
author R. David Murray <rdmurray@bitdance.com>
date Wed, 25 Aug 2010 00:45:55 +0000
parents d5f3c2f416f2
children ef5c57217b20
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43019
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Copyright (C) 2002-2007 Python Software Foundation
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # Contact: email-sig@python.org
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 """Email address parsing code.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 Lifted directly from rfc822.py. This should eventually be rewritten.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 """
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 __all__ = [
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 'mktime_tz',
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 'parsedate',
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 'parsedate_tz',
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 'quote',
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 ]
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 import time
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 SPACE = ' '
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 EMPTYSTRING = ''
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 COMMASPACE = ', '
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # Parse a date field
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 _monthnames = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul',
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 'aug', 'sep', 'oct', 'nov', 'dec',
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 'january', 'february', 'march', 'april', 'may', 'june', 'july',
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 'august', 'september', 'october', 'november', 'december']
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 _daynames = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 # The timezone table does not include the military time zones defined
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 # in RFC822, other than Z. According to RFC1123, the description in
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 # RFC822 gets the signs wrong, so we can't rely on any such time
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 # zones. RFC1123 recommends that numeric timezone indicators be used
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 # instead of timezone names.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 _timezones = {'UT':0, 'UTC':0, 'GMT':0, 'Z':0,
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 'AST': -400, 'ADT': -300, # Atlantic (used in Canada)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 'EST': -500, 'EDT': -400, # Eastern
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 'CST': -600, 'CDT': -500, # Central
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 'MST': -700, 'MDT': -600, # Mountain
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 'PST': -800, 'PDT': -700 # Pacific
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 }
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 def parsedate_tz(data):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 """Convert a date string to a time tuple.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 Accounts for military timezones.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 """
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 data = data.split()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 # The FWS after the comma after the day-of-week is optional, so search and
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 # adjust for this.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 if data[0].endswith(',') or data[0].lower() in _daynames:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 # There's a dayname here. Skip it
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 del data[0]
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 else:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 i = data[0].rfind(',')
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 if i >= 0:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 data[0] = data[0][i+1:]
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 if len(data) == 3: # RFC 850 date, deprecated
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 stuff = data[0].split('-')
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 if len(stuff) == 3:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 data = stuff + data[1:]
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 if len(data) == 4:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 s = data[3]
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 i = s.find('+')
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 if i > 0:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 data[3:] = [s[:i], s[i+1:]]
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 else:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 data.append('') # Dummy tz
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 if len(data) < 5:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 return None
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 data = data[:5]
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 [dd, mm, yy, tm, tz] = data
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 mm = mm.lower()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 if mm not in _monthnames:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 dd, mm = mm, dd.lower()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 if mm not in _monthnames:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 return None
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 mm = _monthnames.index(mm) + 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 if mm > 12:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 mm -= 12
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 if dd[-1] == ',':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 dd = dd[:-1]
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 i = yy.find(':')
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 if i > 0:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 yy, tm = tm, yy
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 if yy[-1] == ',':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 yy = yy[:-1]
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 if not yy[0].isdigit():
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 yy, tz = tz, yy
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 if tm[-1] == ',':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 tm = tm[:-1]
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 tm = tm.split(':')
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 if len(tm) == 2:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 [thh, tmm] = tm
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 tss = '0'
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 elif len(tm) == 3:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 [thh, tmm, tss] = tm
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 else:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 return None
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 try:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 yy = int(yy)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 dd = int(dd)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 thh = int(thh)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 tmm = int(tmm)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 tss = int(tss)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 except ValueError:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 return None
64274
87bdf53ff4bb #1194222: make parsedate always return RFC2822 four character years.
R. David Murray <rdmurray@bitdance.com>
parents: 43019
diff changeset
110 # Check for a yy specified in two-digit format, then convert it to the
87bdf53ff4bb #1194222: make parsedate always return RFC2822 four character years.
R. David Murray <rdmurray@bitdance.com>
parents: 43019
diff changeset
111 # appropriate four-digit format, according to the POSIX standard. RFC 822
87bdf53ff4bb #1194222: make parsedate always return RFC2822 four character years.
R. David Murray <rdmurray@bitdance.com>
parents: 43019
diff changeset
112 # calls for a two-digit yy, but RFC 2822 (which obsoletes RFC 822)
87bdf53ff4bb #1194222: make parsedate always return RFC2822 four character years.
R. David Murray <rdmurray@bitdance.com>
parents: 43019
diff changeset
113 # mandates a 4-digit yy. For more information, see the documentation for
87bdf53ff4bb #1194222: make parsedate always return RFC2822 four character years.
R. David Murray <rdmurray@bitdance.com>
parents: 43019
diff changeset
114 # the time module.
87bdf53ff4bb #1194222: make parsedate always return RFC2822 four character years.
R. David Murray <rdmurray@bitdance.com>
parents: 43019
diff changeset
115 if yy < 100:
87bdf53ff4bb #1194222: make parsedate always return RFC2822 four character years.
R. David Murray <rdmurray@bitdance.com>
parents: 43019
diff changeset
116 # The year is between 1969 and 1999 (inclusive).
87bdf53ff4bb #1194222: make parsedate always return RFC2822 four character years.
R. David Murray <rdmurray@bitdance.com>
parents: 43019
diff changeset
117 if yy > 68:
87bdf53ff4bb #1194222: make parsedate always return RFC2822 four character years.
R. David Murray <rdmurray@bitdance.com>
parents: 43019
diff changeset
118 yy += 1900
87bdf53ff4bb #1194222: make parsedate always return RFC2822 four character years.
R. David Murray <rdmurray@bitdance.com>
parents: 43019
diff changeset
119 # The year is between 2000 and 2068 (inclusive).
87bdf53ff4bb #1194222: make parsedate always return RFC2822 four character years.
R. David Murray <rdmurray@bitdance.com>
parents: 43019
diff changeset
120 else:
87bdf53ff4bb #1194222: make parsedate always return RFC2822 four character years.
R. David Murray <rdmurray@bitdance.com>
parents: 43019
diff changeset
121 yy += 2000
43019
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 tzoffset = None
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 tz = tz.upper()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 if tz in _timezones:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 tzoffset = _timezones[tz]
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 else:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 try:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 tzoffset = int(tz)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 except ValueError:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 pass
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 # Convert a timezone offset into seconds ; -0500 -> -18000
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 if tzoffset:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 if tzoffset < 0:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 tzsign = -1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 tzoffset = -tzoffset
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 else:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 tzsign = 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 tzoffset = tzsign * ( (tzoffset//100)*3600 + (tzoffset % 100)*60)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 # Daylight Saving Time flag is set to -1, since DST is unknown.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 return yy, mm, dd, thh, tmm, tss, 0, 1, -1, tzoffset
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 def parsedate(data):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 """Convert a time string to a time tuple."""
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 t = parsedate_tz(data)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 if isinstance(t, tuple):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 return t[:9]
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 else:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 return t
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 def mktime_tz(data):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 """Turn a 10-tuple as returned by parsedate_tz() into a UTC timestamp."""
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 if data[9] is None:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 # No zone info, so localtime is better assumption than GMT
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 return time.mktime(data[:8] + (-1,))
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 else:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 t = time.mktime(data[:8] + (0,))
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 return t - data[9] - time.timezone
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 def quote(str):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 """Add quotes around a string."""
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 return str.replace('\\', '\\\\').replace('"', '\\"')
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
165
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 class AddrlistClass:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 """Address parser class by Ben Escoto.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 To understand what this class does, it helps to have a copy of RFC 2822 in
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 front of you.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
172
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 Note: this class interface is deprecated and may be removed in the future.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 Use rfc822.AddressList instead.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 """
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 def __init__(self, field):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 """Initialize a new instance.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
179
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 `field' is an unparsed address header field, containing
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 one or more addresses.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 """
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 self.specials = '()<>@,:;.\"[]'
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 self.pos = 0
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 self.LWS = ' \t'
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 self.CR = '\r\n'
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 self.FWS = self.LWS + self.CR
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 self.atomends = self.specials + self.LWS + self.CR
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 # Note that RFC 2822 now specifies `.' as obs-phrase, meaning that it
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 # is obsolete syntax. RFC 2822 requires that we recognize obsolete
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 # syntax, so allow dots in phrases.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 self.phraseends = self.atomends.replace('.', '')
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 self.field = field
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 self.commentlist = []
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
195
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 def gotonext(self):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 """Parse up to the start of the next address."""
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 while self.pos < len(self.field):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 if self.field[self.pos] in self.LWS + '\n\r':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 self.pos += 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 elif self.field[self.pos] == '(':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 self.commentlist.append(self.getcomment())
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 else:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 break
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
205
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 def getaddrlist(self):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 """Parse all addresses.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
208
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 Returns a list containing all of the addresses.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 """
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 result = []
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 while self.pos < len(self.field):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 ad = self.getaddress()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 if ad:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 result += ad
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 else:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 result.append(('', ''))
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 return result
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 def getaddress(self):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 """Parse the next address."""
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 self.commentlist = []
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 self.gotonext()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
224
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 oldpos = self.pos
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 oldcl = self.commentlist
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 plist = self.getphraselist()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 self.gotonext()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 returnlist = []
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
231
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 if self.pos >= len(self.field):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 # Bad email address technically, no domain.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 if plist:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 returnlist = [(SPACE.join(self.commentlist), plist[0])]
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 elif self.field[self.pos] in '.@':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 # email address is just an addrspec
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 # this isn't very efficient since we start over
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 self.pos = oldpos
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 self.commentlist = oldcl
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 addrspec = self.getaddrspec()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 returnlist = [(SPACE.join(self.commentlist), addrspec)]
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 elif self.field[self.pos] == ':':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 # address is a group
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 returnlist = []
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
248
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 fieldlen = len(self.field)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 self.pos += 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 while self.pos < len(self.field):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 self.gotonext()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 if self.pos < fieldlen and self.field[self.pos] == ';':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 self.pos += 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 break
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 returnlist = returnlist + self.getaddress()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
257
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 elif self.field[self.pos] == '<':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 # Address is a phrase then a route addr
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 routeaddr = self.getrouteaddr()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
261
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 if self.commentlist:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 returnlist = [(SPACE.join(plist) + ' (' +
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 ' '.join(self.commentlist) + ')', routeaddr)]
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 else:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 returnlist = [(SPACE.join(plist), routeaddr)]
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
267
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 else:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 if plist:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 returnlist = [(SPACE.join(self.commentlist), plist[0])]
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 elif self.field[self.pos] in self.specials:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 self.pos += 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
273
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 self.gotonext()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 if self.pos < len(self.field) and self.field[self.pos] == ',':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 self.pos += 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 return returnlist
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
278
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 def getrouteaddr(self):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 """Parse a route address (Return-path value).
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
281
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 This method just skips all the route stuff and returns the addrspec.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 """
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 if self.field[self.pos] != '<':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 return
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
286
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 expectroute = False
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 self.pos += 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 self.gotonext()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 adlist = ''
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 while self.pos < len(self.field):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 if expectroute:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 self.getdomain()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 expectroute = False
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 elif self.field[self.pos] == '>':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 self.pos += 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 break
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 elif self.field[self.pos] == '@':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 self.pos += 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 expectroute = True
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 elif self.field[self.pos] == ':':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 self.pos += 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 else:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 adlist = self.getaddrspec()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 self.pos += 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 break
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 self.gotonext()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
308
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 return adlist
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
310
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 def getaddrspec(self):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 """Parse an RFC 2822 addr-spec."""
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 aslist = []
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
314
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 self.gotonext()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 while self.pos < len(self.field):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 if self.field[self.pos] == '.':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 aslist.append('.')
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 self.pos += 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 elif self.field[self.pos] == '"':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 aslist.append('"%s"' % self.getquote())
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 elif self.field[self.pos] in self.atomends:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 break
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 else:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 aslist.append(self.getatom())
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 self.gotonext()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
327
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 if self.pos >= len(self.field) or self.field[self.pos] != '@':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 return EMPTYSTRING.join(aslist)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
330
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 aslist.append('@')
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 self.pos += 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 self.gotonext()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 return EMPTYSTRING.join(aslist) + self.getdomain()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
335
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 def getdomain(self):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 """Get the complete domain name from an address."""
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 sdlist = []
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 while self.pos < len(self.field):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 if self.field[self.pos] in self.LWS:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 self.pos += 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 elif self.field[self.pos] == '(':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 self.commentlist.append(self.getcomment())
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 elif self.field[self.pos] == '[':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 sdlist.append(self.getdomainliteral())
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 elif self.field[self.pos] == '.':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 self.pos += 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 sdlist.append('.')
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 elif self.field[self.pos] in self.atomends:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 break
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 else:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 sdlist.append(self.getatom())
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 return EMPTYSTRING.join(sdlist)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
354
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 def getdelimited(self, beginchar, endchars, allowcomments=True):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 """Parse a header fragment delimited by special characters.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
357
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 `beginchar' is the start character for the fragment.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 If self is not looking at an instance of `beginchar' then
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 getdelimited returns the empty string.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
361
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 `endchars' is a sequence of allowable end-delimiting characters.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 Parsing stops when one of these is encountered.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
364
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 If `allowcomments' is non-zero, embedded RFC 2822 comments are allowed
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 within the parsed fragment.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 """
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 if self.field[self.pos] != beginchar:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 return ''
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
370
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 slist = ['']
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 quote = False
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 self.pos += 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 while self.pos < len(self.field):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 if quote:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 slist.append(self.field[self.pos])
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 quote = False
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 elif self.field[self.pos] in endchars:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 self.pos += 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 break
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 elif allowcomments and self.field[self.pos] == '(':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 slist.append(self.getcomment())
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 continue # have already advanced pos from getcomment
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 elif self.field[self.pos] == '\\':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 quote = True
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 else:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 slist.append(self.field[self.pos])
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 self.pos += 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
389
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 return EMPTYSTRING.join(slist)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
391
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 def getquote(self):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 """Get a quote-delimited fragment from self's field."""
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 return self.getdelimited('"', '"\r', False)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
395
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 def getcomment(self):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 """Get a parenthesis-delimited fragment from self's field."""
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 return self.getdelimited('(', ')\r', True)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
399
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 def getdomainliteral(self):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 """Parse an RFC 2822 domain-literal."""
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 return '[%s]' % self.getdelimited('[', ']\r', False)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
403
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 def getatom(self, atomends=None):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 """Parse an RFC 2822 atom.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
406
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 Optional atomends specifies a different set of end token delimiters
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 (the default is to use self.atomends). This is used e.g. in
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 getphraselist() since phrase endings must not include the `.' (which
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
410 is legal in phrases)."""
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 atomlist = ['']
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 if atomends is None:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 atomends = self.atomends
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
414
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 while self.pos < len(self.field):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 if self.field[self.pos] in atomends:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 break
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 else:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 atomlist.append(self.field[self.pos])
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 self.pos += 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
421
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 return EMPTYSTRING.join(atomlist)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
423
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 def getphraselist(self):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 """Parse a sequence of RFC 2822 phrases.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
426
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
427 A phrase is a sequence of words, which are in turn either RFC 2822
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 atoms or quoted-strings. Phrases are canonicalized by squeezing all
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 runs of continuous whitespace into one space.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 """
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 plist = []
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
432
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 while self.pos < len(self.field):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 if self.field[self.pos] in self.FWS:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 self.pos += 1
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 elif self.field[self.pos] == '"':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 plist.append(self.getquote())
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 elif self.field[self.pos] == '(':
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 self.commentlist.append(self.getcomment())
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 elif self.field[self.pos] in self.phraseends:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 break
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 else:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
443 plist.append(self.getatom(self.phraseends))
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
444
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 return plist
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
446
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 class AddressList(AddrlistClass):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 """An AddressList encapsulates a list of parsed RFC 2822 addresses."""
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
449 def __init__(self, field):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 AddrlistClass.__init__(self, field)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 if field:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 self.addresslist = self.getaddrlist()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 else:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 self.addresslist = []
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
455
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 def __len__(self):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 return len(self.addresslist)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
458
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
459 def __add__(self, other):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 # Set union
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 newaddr = AddressList(None)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 newaddr.addresslist = self.addresslist[:]
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 for x in other.addresslist:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 if not x in self.addresslist:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
465 newaddr.addresslist.append(x)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
466 return newaddr
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
467
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 def __iadd__(self, other):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 # Set union, in-place
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 for x in other.addresslist:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 if not x in self.addresslist:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 self.addresslist.append(x)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
473 return self
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
474
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 def __sub__(self, other):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
476 # Set difference
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
477 newaddr = AddressList(None)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
478 for x in self.addresslist:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
479 if not x in other.addresslist:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 newaddr.addresslist.append(x)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
481 return newaddr
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
482
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
483 def __isub__(self, other):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
484 # Set difference, in-place
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
485 for x in other.addresslist:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
486 if x in self.addresslist:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
487 self.addresslist.remove(x)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
488 return self
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
489
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
490 def __getitem__(self, index):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 # Make indexing, slices, and 'in' work
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
492 return self.addresslist[index]