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