Mercurial > cpython-lru_cache-weakref
annotate Lib/email/_parseaddr.py @ 79691:05c8928362ee
Merge __next__ method link fixes with 3.3.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 12 Oct 2012 13:46:37 +0300 |
| parents | 3b03d31f6a79 |
| children |
| 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 |
|
77560
ffc048f43a70
Issue #14653: email.utils.mktime_tz() no longer relies on system
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
73508
diff
changeset
|
16 import time, calendar |
|
43019
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 """ |
|
71439
5f7b03dcd523
#665194: support roundtripping RFC2822 date stamps in the email.utils module
R David Murray <rdmurray@bitdance.com>
parents:
68431
diff
changeset
|
50 res = _parsedate_tz(data) |
|
79096
3b03d31f6a79
#15925: fix regression: return None for null and non-date strings.
R David Murray <rdmurray@bitdance.com>
parents:
77561
diff
changeset
|
51 if not res: |
|
3b03d31f6a79
#15925: fix regression: return None for null and non-date strings.
R David Murray <rdmurray@bitdance.com>
parents:
77561
diff
changeset
|
52 return |
|
71439
5f7b03dcd523
#665194: support roundtripping RFC2822 date stamps in the email.utils module
R David Murray <rdmurray@bitdance.com>
parents:
68431
diff
changeset
|
53 if res[9] is None: |
|
5f7b03dcd523
#665194: support roundtripping RFC2822 date stamps in the email.utils module
R David Murray <rdmurray@bitdance.com>
parents:
68431
diff
changeset
|
54 res[9] = 0 |
|
5f7b03dcd523
#665194: support roundtripping RFC2822 date stamps in the email.utils module
R David Murray <rdmurray@bitdance.com>
parents:
68431
diff
changeset
|
55 return tuple(res) |
|
5f7b03dcd523
#665194: support roundtripping RFC2822 date stamps in the email.utils module
R David Murray <rdmurray@bitdance.com>
parents:
68431
diff
changeset
|
56 |
|
5f7b03dcd523
#665194: support roundtripping RFC2822 date stamps in the email.utils module
R David Murray <rdmurray@bitdance.com>
parents:
68431
diff
changeset
|
57 def _parsedate_tz(data): |
|
5f7b03dcd523
#665194: support roundtripping RFC2822 date stamps in the email.utils module
R David Murray <rdmurray@bitdance.com>
parents:
68431
diff
changeset
|
58 """Convert date to extended time tuple. |
|
5f7b03dcd523
#665194: support roundtripping RFC2822 date stamps in the email.utils module
R David Murray <rdmurray@bitdance.com>
parents:
68431
diff
changeset
|
59 |
|
5f7b03dcd523
#665194: support roundtripping RFC2822 date stamps in the email.utils module
R David Murray <rdmurray@bitdance.com>
parents:
68431
diff
changeset
|
60 The last (additional) element is the time zone offset in seconds, except if |
|
5f7b03dcd523
#665194: support roundtripping RFC2822 date stamps in the email.utils module
R David Murray <rdmurray@bitdance.com>
parents:
68431
diff
changeset
|
61 the timezone was specified as -0000. In that case the last element is |
|
5f7b03dcd523
#665194: support roundtripping RFC2822 date stamps in the email.utils module
R David Murray <rdmurray@bitdance.com>
parents:
68431
diff
changeset
|
62 None. This indicates a UTC timestamp that explicitly declaims knowledge of |
|
5f7b03dcd523
#665194: support roundtripping RFC2822 date stamps in the email.utils module
R David Murray <rdmurray@bitdance.com>
parents:
68431
diff
changeset
|
63 the source timezone, as opposed to a +0000 timestamp that indicates the |
|
5f7b03dcd523
#665194: support roundtripping RFC2822 date stamps in the email.utils module
R David Murray <rdmurray@bitdance.com>
parents:
68431
diff
changeset
|
64 source timezone really was UTC. |
|
5f7b03dcd523
#665194: support roundtripping RFC2822 date stamps in the email.utils module
R David Murray <rdmurray@bitdance.com>
parents:
68431
diff
changeset
|
65 |
|
5f7b03dcd523
#665194: support roundtripping RFC2822 date stamps in the email.utils module
R David Murray <rdmurray@bitdance.com>
parents:
68431
diff
changeset
|
66 """ |
|
79096
3b03d31f6a79
#15925: fix regression: return None for null and non-date strings.
R David Murray <rdmurray@bitdance.com>
parents:
77561
diff
changeset
|
67 if not data: |
|
3b03d31f6a79
#15925: fix regression: return None for null and non-date strings.
R David Murray <rdmurray@bitdance.com>
parents:
77561
diff
changeset
|
68 return |
|
43019
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 data = data.split() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 # 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
|
71 # adjust for this. |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 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
|
73 # 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
|
74 del data[0] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 else: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 i = data[0].rfind(',') |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 if i >= 0: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 data[0] = data[0][i+1:] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 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
|
80 stuff = data[0].split('-') |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 if len(stuff) == 3: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 data = stuff + data[1:] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 if len(data) == 4: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 s = data[3] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 i = s.find('+') |
|
67097
765796a34abe
#1155362: allow hh:mm:ss-uuuu like we allow hh:mm:ss+uuuu in parsedate_tz
R. David Murray <rdmurray@bitdance.com>
parents:
67031
diff
changeset
|
86 if i == -1: |
|
765796a34abe
#1155362: allow hh:mm:ss-uuuu like we allow hh:mm:ss+uuuu in parsedate_tz
R. David Murray <rdmurray@bitdance.com>
parents:
67031
diff
changeset
|
87 i = s.find('-') |
|
43019
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 if i > 0: |
|
67097
765796a34abe
#1155362: allow hh:mm:ss-uuuu like we allow hh:mm:ss+uuuu in parsedate_tz
R. David Murray <rdmurray@bitdance.com>
parents:
67031
diff
changeset
|
89 data[3:] = [s[:i], s[i:]] |
|
43019
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 else: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 data.append('') # Dummy tz |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 if len(data) < 5: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 return None |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 data = data[:5] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 [dd, mm, yy, tm, tz] = data |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 mm = mm.lower() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 if mm not in _monthnames: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 dd, mm = mm, dd.lower() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 if mm not in _monthnames: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 return None |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 mm = _monthnames.index(mm) + 1 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 if mm > 12: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 mm -= 12 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 if dd[-1] == ',': |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 dd = dd[:-1] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 i = yy.find(':') |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 if i > 0: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 yy, tm = tm, yy |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 if yy[-1] == ',': |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 yy = yy[:-1] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 if not yy[0].isdigit(): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 yy, tz = tz, yy |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 if tm[-1] == ',': |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 tm = tm[:-1] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 tm = tm.split(':') |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 if len(tm) == 2: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 [thh, tmm] = tm |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 tss = '0' |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 elif len(tm) == 3: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 [thh, tmm, tss] = tm |
|
68431
9d7a83654870
#1162477: accept '.' in addition to ':' when parsing time in date header.
R David Murray <rdmurray@bitdance.com>
parents:
67097
diff
changeset
|
121 elif len(tm) == 1 and '.' in tm[0]: |
|
9d7a83654870
#1162477: accept '.' in addition to ':' when parsing time in date header.
R David Murray <rdmurray@bitdance.com>
parents:
67097
diff
changeset
|
122 # Some non-compliant MUAs use '.' to separate time elements. |
|
9d7a83654870
#1162477: accept '.' in addition to ':' when parsing time in date header.
R David Murray <rdmurray@bitdance.com>
parents:
67097
diff
changeset
|
123 tm = tm[0].split('.') |
|
9d7a83654870
#1162477: accept '.' in addition to ':' when parsing time in date header.
R David Murray <rdmurray@bitdance.com>
parents:
67097
diff
changeset
|
124 if len(tm) == 2: |
|
9d7a83654870
#1162477: accept '.' in addition to ':' when parsing time in date header.
R David Murray <rdmurray@bitdance.com>
parents:
67097
diff
changeset
|
125 [thh, tmm] = tm |
|
9d7a83654870
#1162477: accept '.' in addition to ':' when parsing time in date header.
R David Murray <rdmurray@bitdance.com>
parents:
67097
diff
changeset
|
126 tss = 0 |
|
9d7a83654870
#1162477: accept '.' in addition to ':' when parsing time in date header.
R David Murray <rdmurray@bitdance.com>
parents:
67097
diff
changeset
|
127 elif len(tm) == 3: |
|
9d7a83654870
#1162477: accept '.' in addition to ':' when parsing time in date header.
R David Murray <rdmurray@bitdance.com>
parents:
67097
diff
changeset
|
128 [thh, tmm, tss] = tm |
|
43019
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 else: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 return None |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 try: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 yy = int(yy) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 dd = int(dd) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 thh = int(thh) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 tmm = int(tmm) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 tss = int(tss) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 except ValueError: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 return None |
|
64274
87bdf53ff4bb
#1194222: make parsedate always return RFC2822 four character years.
R. David Murray <rdmurray@bitdance.com>
parents:
43019
diff
changeset
|
139 # 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
|
140 # 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
|
141 # 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
|
142 # 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
|
143 # the time module. |
|
87bdf53ff4bb
#1194222: make parsedate always return RFC2822 four character years.
R. David Murray <rdmurray@bitdance.com>
parents:
43019
diff
changeset
|
144 if yy < 100: |
|
87bdf53ff4bb
#1194222: make parsedate always return RFC2822 four character years.
R. David Murray <rdmurray@bitdance.com>
parents:
43019
diff
changeset
|
145 # 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
|
146 if yy > 68: |
|
87bdf53ff4bb
#1194222: make parsedate always return RFC2822 four character years.
R. David Murray <rdmurray@bitdance.com>
parents:
43019
diff
changeset
|
147 yy += 1900 |
|
87bdf53ff4bb
#1194222: make parsedate always return RFC2822 four character years.
R. David Murray <rdmurray@bitdance.com>
parents:
43019
diff
changeset
|
148 # 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
|
149 else: |
|
87bdf53ff4bb
#1194222: make parsedate always return RFC2822 four character years.
R. David Murray <rdmurray@bitdance.com>
parents:
43019
diff
changeset
|
150 yy += 2000 |
|
43019
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 tzoffset = None |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 tz = tz.upper() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 if tz in _timezones: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 tzoffset = _timezones[tz] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 else: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 try: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 tzoffset = int(tz) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 except ValueError: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 pass |
|
71439
5f7b03dcd523
#665194: support roundtripping RFC2822 date stamps in the email.utils module
R David Murray <rdmurray@bitdance.com>
parents:
68431
diff
changeset
|
160 if tzoffset==0 and tz.startswith('-'): |
|
5f7b03dcd523
#665194: support roundtripping RFC2822 date stamps in the email.utils module
R David Murray <rdmurray@bitdance.com>
parents:
68431
diff
changeset
|
161 tzoffset = None |
|
43019
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 # 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
|
163 if tzoffset: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 if tzoffset < 0: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 tzsign = -1 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 tzoffset = -tzoffset |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 else: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 tzsign = 1 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 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
|
170 # Daylight Saving Time flag is set to -1, since DST is unknown. |
|
71439
5f7b03dcd523
#665194: support roundtripping RFC2822 date stamps in the email.utils module
R David Murray <rdmurray@bitdance.com>
parents:
68431
diff
changeset
|
171 return [yy, mm, dd, thh, tmm, tss, 0, 1, -1, tzoffset] |
|
43019
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 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 def parsedate(data): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 """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
|
176 t = parsedate_tz(data) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 if isinstance(t, tuple): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 return t[:9] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 else: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 return t |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 |
|
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 def mktime_tz(data): |
|
77560
ffc048f43a70
Issue #14653: email.utils.mktime_tz() no longer relies on system
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
73508
diff
changeset
|
184 """Turn a 10-tuple as returned by parsedate_tz() into a POSIX timestamp.""" |
|
43019
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 if data[9] is None: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 # 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
|
187 return time.mktime(data[:8] + (-1,)) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 else: |
|
77560
ffc048f43a70
Issue #14653: email.utils.mktime_tz() no longer relies on system
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
73508
diff
changeset
|
189 t = calendar.timegm(data) |
|
ffc048f43a70
Issue #14653: email.utils.mktime_tz() no longer relies on system
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
73508
diff
changeset
|
190 return t - data[9] |
|
43019
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 def quote(str): |
|
65062
ef5c57217b20
#1050268: make parseaddr 'quote' the contents of quoted strings in addresses.
R. David Murray <rdmurray@bitdance.com>
parents:
64274
diff
changeset
|
194 """Prepare string to be used in a quoted string. |
|
ef5c57217b20
#1050268: make parseaddr 'quote' the contents of quoted strings in addresses.
R. David Murray <rdmurray@bitdance.com>
parents:
64274
diff
changeset
|
195 |
|
ef5c57217b20
#1050268: make parseaddr 'quote' the contents of quoted strings in addresses.
R. David Murray <rdmurray@bitdance.com>
parents:
64274
diff
changeset
|
196 Turns backslash and double quote characters into quoted pairs. These |
|
ef5c57217b20
#1050268: make parseaddr 'quote' the contents of quoted strings in addresses.
R. David Murray <rdmurray@bitdance.com>
parents:
64274
diff
changeset
|
197 are the only characters that need to be quoted inside a quoted string. |
|
ef5c57217b20
#1050268: make parseaddr 'quote' the contents of quoted strings in addresses.
R. David Murray <rdmurray@bitdance.com>
parents:
64274
diff
changeset
|
198 Does not add the surrounding double quotes. |
|
ef5c57217b20
#1050268: make parseaddr 'quote' the contents of quoted strings in addresses.
R. David Murray <rdmurray@bitdance.com>
parents:
64274
diff
changeset
|
199 """ |
|
43019
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 return str.replace('\\', '\\\\').replace('"', '\\"') |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 class AddrlistClass: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 """Address parser class by Ben Escoto. |
|
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 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
|
207 front of you. |
|
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 Note: this class interface is deprecated and may be removed in the future. |
|
73508
27fe09ff14b7
Fix few typos.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
67097
diff
changeset
|
210 Use email.utils.AddressList instead. |
|
43019
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
211 """ |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 def __init__(self, field): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
214 """Initialize a new instance. |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
216 `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
|
217 one or more addresses. |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 """ |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 self.specials = '()<>@,:;.\"[]' |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 self.pos = 0 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 self.LWS = ' \t' |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 self.CR = '\r\n' |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 self.FWS = self.LWS + self.CR |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 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
|
225 # 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
|
226 # 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
|
227 # syntax, so allow dots in phrases. |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 self.phraseends = self.atomends.replace('.', '') |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 self.field = field |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 self.commentlist = [] |
|
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 def gotonext(self): |
|
67031
9385c536f454
#9286: Fix the rfc822 parser to preserve whitespace in address local part.
R. David Murray <rdmurray@bitdance.com>
parents:
65062
diff
changeset
|
233 """Skip white space and extract comments.""" |
|
9385c536f454
#9286: Fix the rfc822 parser to preserve whitespace in address local part.
R. David Murray <rdmurray@bitdance.com>
parents:
65062
diff
changeset
|
234 wslist = [] |
|
43019
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 while self.pos < len(self.field): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 if self.field[self.pos] in self.LWS + '\n\r': |
|
67031
9385c536f454
#9286: Fix the rfc822 parser to preserve whitespace in address local part.
R. David Murray <rdmurray@bitdance.com>
parents:
65062
diff
changeset
|
237 if self.field[self.pos] not in '\n\r': |
|
9385c536f454
#9286: Fix the rfc822 parser to preserve whitespace in address local part.
R. David Murray <rdmurray@bitdance.com>
parents:
65062
diff
changeset
|
238 wslist.append(self.field[self.pos]) |
|
43019
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 self.pos += 1 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 elif self.field[self.pos] == '(': |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 self.commentlist.append(self.getcomment()) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 else: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 break |
|
67031
9385c536f454
#9286: Fix the rfc822 parser to preserve whitespace in address local part.
R. David Murray <rdmurray@bitdance.com>
parents:
65062
diff
changeset
|
244 return EMPTYSTRING.join(wslist) |
|
43019
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 def getaddrlist(self): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 """Parse all addresses. |
|
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 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
|
250 """ |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
251 result = [] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
252 while self.pos < len(self.field): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 ad = self.getaddress() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 if ad: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 result += ad |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 else: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 result.append(('', '')) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 return result |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 def getaddress(self): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 """Parse the next address.""" |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
262 self.commentlist = [] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 self.gotonext() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
264 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
265 oldpos = self.pos |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
266 oldcl = self.commentlist |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 plist = self.getphraselist() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 self.gotonext() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 returnlist = [] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 if self.pos >= len(self.field): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
273 # Bad email address technically, no domain. |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
274 if plist: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 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
|
276 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 elif self.field[self.pos] in '.@': |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
278 # email address is just an addrspec |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
279 # 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
|
280 self.pos = oldpos |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
281 self.commentlist = oldcl |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
282 addrspec = self.getaddrspec() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
283 returnlist = [(SPACE.join(self.commentlist), addrspec)] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
284 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
285 elif self.field[self.pos] == ':': |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 # address is a group |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
287 returnlist = [] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
288 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
289 fieldlen = len(self.field) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 self.pos += 1 |
|
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 self.gotonext() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 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
|
294 self.pos += 1 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
295 break |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
296 returnlist = returnlist + self.getaddress() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 |
|
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 # 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
|
300 routeaddr = self.getrouteaddr() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 if self.commentlist: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 returnlist = [(SPACE.join(plist) + ' (' + |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
304 ' '.join(self.commentlist) + ')', routeaddr)] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
305 else: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 returnlist = [(SPACE.join(plist), routeaddr)] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
307 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
308 else: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 if plist: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
310 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
|
311 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
|
312 self.pos += 1 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 self.gotonext() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
315 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
|
316 self.pos += 1 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 return returnlist |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
318 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
319 def getrouteaddr(self): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
320 """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
|
321 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
322 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
|
323 """ |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
324 if self.field[self.pos] != '<': |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
325 return |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
326 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
327 expectroute = False |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
328 self.pos += 1 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
329 self.gotonext() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
330 adlist = '' |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
331 while self.pos < len(self.field): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
332 if expectroute: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
333 self.getdomain() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
334 expectroute = False |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
335 elif self.field[self.pos] == '>': |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
336 self.pos += 1 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
337 break |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
338 elif self.field[self.pos] == '@': |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
339 self.pos += 1 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
340 expectroute = True |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
341 elif self.field[self.pos] == ':': |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
342 self.pos += 1 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
343 else: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
344 adlist = self.getaddrspec() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
345 self.pos += 1 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
346 break |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
347 self.gotonext() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
348 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
349 return adlist |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
350 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
351 def getaddrspec(self): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
352 """Parse an RFC 2822 addr-spec.""" |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
353 aslist = [] |
|
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 self.gotonext() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
356 while self.pos < len(self.field): |
|
67031
9385c536f454
#9286: Fix the rfc822 parser to preserve whitespace in address local part.
R. David Murray <rdmurray@bitdance.com>
parents:
65062
diff
changeset
|
357 preserve_ws = True |
|
43019
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
358 if self.field[self.pos] == '.': |
|
67031
9385c536f454
#9286: Fix the rfc822 parser to preserve whitespace in address local part.
R. David Murray <rdmurray@bitdance.com>
parents:
65062
diff
changeset
|
359 if aslist and not aslist[-1].strip(): |
|
9385c536f454
#9286: Fix the rfc822 parser to preserve whitespace in address local part.
R. David Murray <rdmurray@bitdance.com>
parents:
65062
diff
changeset
|
360 aslist.pop() |
|
43019
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
361 aslist.append('.') |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
362 self.pos += 1 |
|
67031
9385c536f454
#9286: Fix the rfc822 parser to preserve whitespace in address local part.
R. David Murray <rdmurray@bitdance.com>
parents:
65062
diff
changeset
|
363 preserve_ws = False |
|
43019
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
364 elif self.field[self.pos] == '"': |
|
65062
ef5c57217b20
#1050268: make parseaddr 'quote' the contents of quoted strings in addresses.
R. David Murray <rdmurray@bitdance.com>
parents:
64274
diff
changeset
|
365 aslist.append('"%s"' % quote(self.getquote())) |
|
43019
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
366 elif self.field[self.pos] in self.atomends: |
|
67031
9385c536f454
#9286: Fix the rfc822 parser to preserve whitespace in address local part.
R. David Murray <rdmurray@bitdance.com>
parents:
65062
diff
changeset
|
367 if aslist and not aslist[-1].strip(): |
|
9385c536f454
#9286: Fix the rfc822 parser to preserve whitespace in address local part.
R. David Murray <rdmurray@bitdance.com>
parents:
65062
diff
changeset
|
368 aslist.pop() |
|
43019
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
369 break |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
370 else: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
371 aslist.append(self.getatom()) |
|
67031
9385c536f454
#9286: Fix the rfc822 parser to preserve whitespace in address local part.
R. David Murray <rdmurray@bitdance.com>
parents:
65062
diff
changeset
|
372 ws = self.gotonext() |
|
9385c536f454
#9286: Fix the rfc822 parser to preserve whitespace in address local part.
R. David Murray <rdmurray@bitdance.com>
parents:
65062
diff
changeset
|
373 if preserve_ws and ws: |
|
9385c536f454
#9286: Fix the rfc822 parser to preserve whitespace in address local part.
R. David Murray <rdmurray@bitdance.com>
parents:
65062
diff
changeset
|
374 aslist.append(ws) |
|
43019
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
375 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
376 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
|
377 return EMPTYSTRING.join(aslist) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
378 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
379 aslist.append('@') |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
380 self.pos += 1 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
381 self.gotonext() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
382 return EMPTYSTRING.join(aslist) + self.getdomain() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
383 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
384 def getdomain(self): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
385 """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
|
386 sdlist = [] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
387 while self.pos < len(self.field): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
388 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
|
389 self.pos += 1 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
390 elif self.field[self.pos] == '(': |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
391 self.commentlist.append(self.getcomment()) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
392 elif self.field[self.pos] == '[': |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
393 sdlist.append(self.getdomainliteral()) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
394 elif self.field[self.pos] == '.': |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
395 self.pos += 1 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
396 sdlist.append('.') |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
397 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
|
398 break |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
399 else: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
400 sdlist.append(self.getatom()) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
401 return EMPTYSTRING.join(sdlist) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
402 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
403 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
|
404 """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
|
405 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
406 `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
|
407 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
|
408 getdelimited returns the empty string. |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
409 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
410 `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
|
411 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
|
412 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
413 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
|
414 within the parsed fragment. |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
415 """ |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
416 if self.field[self.pos] != beginchar: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
417 return '' |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
418 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
419 slist = [''] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
420 quote = False |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
421 self.pos += 1 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
422 while self.pos < len(self.field): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
423 if quote: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
424 slist.append(self.field[self.pos]) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
425 quote = False |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
426 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
|
427 self.pos += 1 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
428 break |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
429 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
|
430 slist.append(self.getcomment()) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
431 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
|
432 elif self.field[self.pos] == '\\': |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
433 quote = True |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
434 else: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
435 slist.append(self.field[self.pos]) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
436 self.pos += 1 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
437 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
438 return EMPTYSTRING.join(slist) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
439 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
440 def getquote(self): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
441 """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
|
442 return self.getdelimited('"', '"\r', False) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
443 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
444 def getcomment(self): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
445 """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
|
446 return self.getdelimited('(', ')\r', True) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
447 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
448 def getdomainliteral(self): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
449 """Parse an RFC 2822 domain-literal.""" |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
450 return '[%s]' % self.getdelimited('[', ']\r', False) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
451 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
452 def getatom(self, atomends=None): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
453 """Parse an RFC 2822 atom. |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
454 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
455 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
|
456 (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
|
457 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
|
458 is legal in phrases).""" |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
459 atomlist = [''] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
460 if atomends is None: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
461 atomends = self.atomends |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
462 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
463 while self.pos < len(self.field): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
464 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
|
465 break |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
466 else: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
467 atomlist.append(self.field[self.pos]) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
468 self.pos += 1 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
469 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
470 return EMPTYSTRING.join(atomlist) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
471 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
472 def getphraselist(self): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
473 """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
|
474 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
475 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
|
476 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
|
477 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
|
478 """ |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
479 plist = [] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
480 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
481 while self.pos < len(self.field): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
482 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
|
483 self.pos += 1 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
484 elif self.field[self.pos] == '"': |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
485 plist.append(self.getquote()) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
486 elif self.field[self.pos] == '(': |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
487 self.commentlist.append(self.getcomment()) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
488 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
|
489 break |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
490 else: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
491 plist.append(self.getatom(self.phraseends)) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
492 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
493 return plist |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
494 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
495 class AddressList(AddrlistClass): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
496 """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
|
497 def __init__(self, field): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
498 AddrlistClass.__init__(self, field) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
499 if field: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
500 self.addresslist = self.getaddrlist() |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
501 else: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
502 self.addresslist = [] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
503 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
504 def __len__(self): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
505 return len(self.addresslist) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
506 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
507 def __add__(self, other): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
508 # Set union |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
509 newaddr = AddressList(None) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
510 newaddr.addresslist = self.addresslist[:] |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
511 for x in other.addresslist: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
512 if not x in self.addresslist: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
513 newaddr.addresslist.append(x) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
514 return newaddr |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
515 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
516 def __iadd__(self, other): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
517 # Set union, in-place |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
518 for x in other.addresslist: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
519 if not x in self.addresslist: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
520 self.addresslist.append(x) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
521 return self |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
522 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
523 def __sub__(self, other): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
524 # Set difference |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
525 newaddr = AddressList(None) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
526 for x in self.addresslist: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
527 if not x in other.addresslist: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
528 newaddr.addresslist.append(x) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
529 return newaddr |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
530 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
531 def __isub__(self, other): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
532 # Set difference, in-place |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
533 for x in other.addresslist: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
534 if x in self.addresslist: |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
535 self.addresslist.remove(x) |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
536 return self |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
537 |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
538 def __getitem__(self, index): |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
539 # Make indexing, slices, and 'in' work |
|
d5f3c2f416f2
Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
540 return self.addresslist[index] |
