Mercurial > cpython-lru_cache-weakref
annotate Lib/rfc822.py @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | e05480db71ab |
| children | 466664ad38e4 |
| rev | line source |
|---|---|
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 # RFC-822 message manipulation class. |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 # |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 # XXX This is only a very rough sketch of a full RFC-822 parser; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
4 # in particular the tokenizing of addresses does not adhere to all the |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
5 # quoting rules. |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 # |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 # Directions for use: |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 # |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 # To create a Message object: first open a file, e.g.: |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 # fp = open(file, 'r') |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 # (or use any other legal way of getting an open file object, e.g. use |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 # sys.stdin or call os.popen()). |
|
1435
560ec7030817
* Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents:
914
diff
changeset
|
13 # Then pass the open file object to the Message() constructor: |
|
560ec7030817
* Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents:
914
diff
changeset
|
14 # m = Message(fp) |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 # |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 # To get the text of a particular header there are several methods: |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 # str = m.getheader(name) |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 # str = m.getrawheader(name) |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 # where name is the name of the header, e.g. 'Subject'. |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 # The difference is that getheader() strips the leading and trailing |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 # whitespace, while getrawheader() doesn't. Both functions retain |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 # embedded whitespace (including newlines) exactly as they are |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 # specified in the header, and leave the case of the text unchanged. |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 # |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
25 # For addresses and address lists there are functions |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
26 # realname, mailaddress = m.getaddr(name) and |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
27 # list = m.getaddrlist(name) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
28 # where the latter returns a list of (realname, mailaddr) tuples. |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
29 # |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
30 # There is also a method |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
31 # time = m.getdate(name) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
32 # which parses a Date-like field and returns a time-compatible tuple, |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
33 # i.e. a tuple such as returned by time.localtime() or accepted by |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
34 # time.mktime(). |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
35 # |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 # See the class definition for lower level access methods. |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 # |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 # There are also some utility functions here. |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 import regex |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 import string |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
43 import time |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 |
|
2780
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
46 _blanklines = ('\r\n', '\n') # Optimization for islast() |
|
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
47 |
|
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
48 |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 class Message: |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 # Initialize the class instance and read the headers. |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 |
|
2780
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
53 def __init__(self, fp, seekable = 1): |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 self.fp = fp |
|
2780
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
55 self.seekable = seekable |
|
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
56 self.startofheaders = None |
|
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
57 self.startofbody = None |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 # |
|
2780
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
59 if self.seekable: |
|
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
60 try: |
|
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
61 self.startofheaders = self.fp.tell() |
|
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
62 except IOError: |
|
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
63 self.seekable = 0 |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 # |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 self.readheaders() |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 # |
|
2780
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
67 if self.seekable: |
|
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
68 try: |
|
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
69 self.startofbody = self.fp.tell() |
|
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
70 except IOError: |
|
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
71 self.seekable = 0 |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 # Rewind the file to the start of the body (if seekable). |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 def rewindbody(self): |
|
2780
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
77 if not self.seekable: |
|
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
78 raise IOError, "unseekable file" |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 self.fp.seek(self.startofbody) |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 # Read header lines up to the entirely blank line that |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 # terminates them. The (normally blank) line that ends the |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 # headers is skipped, but not included in the returned list. |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 # If a non-header line ends the headers, (which is an error), |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 # an attempt is made to backspace over it; it is never |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 # included in the returned list. |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 # |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 # The variable self.status is set to the empty string if all |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 # went well, otherwise it is an error message. |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 # The variable self.headers is a completely uninterpreted list |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 # of lines contained in the header (so printing them will |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 # reproduce the header exactly as it appears in the file). |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 def readheaders(self): |
|
3638
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
96 self.dict = {} |
|
2780
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
97 self.unixfrom = '' |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 self.headers = list = [] |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 self.status = '' |
|
3638
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
100 headerseen = "" |
|
2725
8222041f5a91
Skip old-style 'From name time' lines at beginning of message.
Jack Jansen <jack.jansen@cwi.nl>
parents:
1872
diff
changeset
|
101 firstline = 1 |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 while 1: |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 line = self.fp.readline() |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 if not line: |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 self.status = 'EOF in headers' |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 break |
|
2725
8222041f5a91
Skip old-style 'From name time' lines at beginning of message.
Jack Jansen <jack.jansen@cwi.nl>
parents:
1872
diff
changeset
|
107 # Skip unix From name time lines |
| 2745 | 108 if firstline and line[:5] == 'From ': |
|
2780
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
109 self.unixfrom = self.unixfrom + line |
|
2725
8222041f5a91
Skip old-style 'From name time' lines at beginning of message.
Jack Jansen <jack.jansen@cwi.nl>
parents:
1872
diff
changeset
|
110 continue |
|
8222041f5a91
Skip old-style 'From name time' lines at beginning of message.
Jack Jansen <jack.jansen@cwi.nl>
parents:
1872
diff
changeset
|
111 firstline = 0 |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 if self.islast(line): |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 break |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 elif headerseen and line[0] in ' \t': |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 # It's a continuation line. |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 list.append(line) |
|
3638
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
117 x = (self.dict[headerseen] + "\n " + |
|
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
118 string.strip(line)) |
|
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
119 self.dict[headerseen] = string.strip(x) |
|
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
120 elif ':' in line: |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 # It's a header line. |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 list.append(line) |
|
3638
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
123 i = string.find(line, ':') |
|
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
124 headerseen = string.lower(line[:i]) |
|
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
125 self.dict[headerseen] = string.strip( |
|
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
126 line[i+1:]) |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 else: |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 # It's not a header line; stop here. |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 if not headerseen: |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 self.status = 'No headers' |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 else: |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 self.status = 'Bad header' |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 # Try to undo the read. |
|
2780
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
134 if self.seekable: |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 self.fp.seek(-len(line), 1) |
|
2780
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
136 else: |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 self.status = \ |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 self.status + '; bad seek' |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 break |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 # Method to determine whether a line is a legal end of |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 # RFC-822 headers. You may override this method if your |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
144 # application wants to bend the rules, e.g. to strip trailing |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
145 # whitespace, or to recognise MH template separators |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
146 # ('--------'). For convenience (e.g. for code reading from |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
147 # sockets) a line consisting of \r\n also matches. |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 def islast(self, line): |
|
2780
4113f4fbaae5
added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents:
2745
diff
changeset
|
150 return line in _blanklines |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 # Look through the list of headers and find all lines matching |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 # a given header name (and their continuation lines). |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 # A list of the lines is returned, without interpretation. |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 # If the header does not occur, an empty list is returned. |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 # If the header occurs multiple times, all occurrences are |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 # returned. Case is not important in the header name. |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 def getallmatchingheaders(self, name): |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 name = string.lower(name) + ':' |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 n = len(name) |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 list = [] |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 hit = 0 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 for line in self.headers: |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 if string.lower(line[:n]) == name: |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 hit = 1 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 elif line[:1] not in string.whitespace: |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 hit = 0 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 if hit: |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 list.append(line) |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 return list |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 # Similar, but return only the first matching header (and its |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 # continuation lines). |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 def getfirstmatchingheader(self, name): |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 name = string.lower(name) + ':' |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 n = len(name) |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 list = [] |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 hit = 0 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 for line in self.headers: |
|
1778
ffdfaf1b24d5
* Lib/rfc822.py: fix two bugs: error in readheaders interpreting
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
184 if hit: |
|
ffdfaf1b24d5
* Lib/rfc822.py: fix two bugs: error in readheaders interpreting
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
185 if line[:1] not in string.whitespace: |
|
ffdfaf1b24d5
* Lib/rfc822.py: fix two bugs: error in readheaders interpreting
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
186 break |
|
ffdfaf1b24d5
* Lib/rfc822.py: fix two bugs: error in readheaders interpreting
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
187 elif string.lower(line[:n]) == name: |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 hit = 1 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 if hit: |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 list.append(line) |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 return list |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 # A higher-level interface to getfirstmatchingheader(). |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 # Return a string containing the literal text of the header |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 # but with the keyword stripped. All leading, trailing and |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 # embedded whitespace is kept in the string, however. |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 # Return None if the header does not occur. |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 def getrawheader(self, name): |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 list = self.getfirstmatchingheader(name) |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 if not list: |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 return None |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 list[0] = list[0][len(name) + 1:] |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 return string.joinfields(list, '') |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 |
|
3638
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
208 # The normal interface: return a stripped version of the |
|
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
209 # header value with a name, or None if it doesn't exist. This |
|
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
210 # uses the dictionary version which finds the *last* such |
|
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
211 # header. |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 def getheader(self, name): |
|
3638
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
214 try: |
|
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
215 return self.dict[string.lower(name)] |
|
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
216 except KeyError: |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 return None |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
220 # Retrieve a single address from a header as a tuple, e.g. |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
221 # ('Guido van Rossum', 'guido@cwi.nl'). |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
222 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
223 def getaddr(self, name): |
|
3638
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
224 try: |
|
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
225 data = self[name] |
|
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
226 except KeyError: |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
227 return None, None |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
228 return parseaddr(data) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
229 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
230 # Retrieve a list of addresses from a header, where each |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
231 # address is a tuple as returned by getaddr(). |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
232 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
233 def getaddrlist(self, name): |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
234 # XXX This function is not really correct. The split |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
235 # on ',' might fail in the case of commas within |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
236 # quoted strings. |
|
3638
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
237 try: |
|
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
238 data = self[name] |
|
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
239 except KeyError: |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
240 return [] |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
241 data = string.splitfields(data, ',') |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
242 for i in range(len(data)): |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
243 data[i] = parseaddr(data[i]) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
244 return data |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
245 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
246 # Retrieve a date field from a header as a tuple compatible |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
247 # with time.mktime(). |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
248 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
249 def getdate(self, name): |
|
3638
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
250 try: |
|
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
251 data = self[name] |
|
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
252 except KeyError: |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
253 return None |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
254 return parsedate(data) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
255 |
|
4559
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
256 # Retrieve a date field from a header as a 10-tuple. |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
257 # The first 9 elements make up a tuple compatible |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
258 # with time.mktime(), and the 10th is the offset |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
259 # of the poster's time zone from GMT/UTC. |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
260 |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
261 def getdate_tz(self, name): |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
262 try: |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
263 data = self[name] |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
264 except KeyError: |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
265 return None |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
266 return parsedate_tz(data) |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
267 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
268 |
|
3638
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
269 # Access as a dictionary (only finds *last* header of each type): |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
271 def __len__(self): |
|
3638
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
272 return len(self.dict) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
273 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
274 def __getitem__(self, name): |
|
3638
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
275 return self.dict[string.lower(name)] |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
276 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
277 def has_key(self, name): |
|
3638
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
278 return self.dict.has_key(string.lower(name)) |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
279 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
280 def keys(self): |
|
3638
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
281 return self.dict.keys() |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
282 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
283 def values(self): |
|
3638
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
284 return self.dict.values() |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
285 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
286 def items(self): |
|
3638
c283b7bbda09
optimization of getheader() using a dictionary
Guido van Rossum <guido@python.org>
parents:
2780
diff
changeset
|
287 return self.dict.items() |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
288 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
289 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
291 # Utility functions |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
292 # ----------------- |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
294 # XXX Should fix these to be really conformant. |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
295 # XXX The inverses of the parse functions may also be useful. |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
296 |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 # Remove quotes from a string. |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 def unquote(str): |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 if len(str) > 1: |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 if str[0] == '"' and str[-1:] == '"': |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 return str[1:-1] |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
304 if str[0] == '<' and str[-1:] == '>': |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
305 return str[1:-1] |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 return str |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
307 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
308 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
309 # Parse an address into (name, address) tuple |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
310 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
311 def parseaddr(address): |
|
3824
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
312 import string |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
313 str = '' |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
314 email = '' |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
315 comment = '' |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
316 backslash = 0 |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
317 dquote = 0 |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
318 space = 0 |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
319 paren = 0 |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
320 bracket = 0 |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
321 seen_bracket = 0 |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
322 for c in address: |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
323 if backslash: |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
324 str = str + c |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
325 backslash = 0 |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
326 continue |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
327 if c == '\\': |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
328 backslash = 1 |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
329 continue |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
330 if dquote: |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
331 if c == '"': |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
332 dquote = 0 |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
333 else: |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
334 str = str + c |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
335 continue |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
336 if c == '"': |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
337 dquote = 1 |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
338 continue |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
339 if c in string.whitespace: |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
340 space = 1 |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
341 continue |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
342 if space: |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
343 str = str + ' ' |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
344 space = 0 |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
345 if paren: |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
346 if c == '(': |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
347 paren = paren + 1 |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
348 str = str + c |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
349 continue |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
350 if c == ')': |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
351 paren = paren - 1 |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
352 if paren == 0: |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
353 comment = comment + str |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
354 str = '' |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
355 continue |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
356 if c == '(': |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
357 paren = paren + 1 |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
358 if bracket: |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
359 email = email + str |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
360 str = '' |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
361 elif not seen_bracket: |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
362 email = email + str |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
363 str = '' |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
364 continue |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
365 if bracket: |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
366 if c == '>': |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
367 bracket = 0 |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
368 email = email + str |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
369 str = '' |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
370 continue |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
371 if c == '<': |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
372 bracket = 1 |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
373 seen_bracket = 1 |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
374 comment = comment + str |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
375 str = '' |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
376 email = '' |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
377 continue |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
378 if c == '#' and not bracket and not paren: |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
379 # rest is comment |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
380 break |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
381 str = str + c |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
382 if str: |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
383 if seen_bracket: |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
384 if bracket: |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
385 email = str |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
386 else: |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
387 comment = comment + str |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
388 else: |
|
3824
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
389 if paren: |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
390 comment = comment + str |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
391 else: |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
392 email = email + str |
|
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
393 return string.strip(comment), string.strip(email) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
394 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
395 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
396 # Parse a date field |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
397 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
398 _monthnames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
399 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
400 |
|
4559
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
401 # The timezone table does not include the military time zones defined |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
402 # in RFC822, other than Z. According to RFC1123, the description in |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
403 # RFC822 gets the signs wrong, so we can't rely on any such time |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
404 # zones. RFC1123 recommends that numeric timezone indicators be used |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
405 # instead of timezone names. |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
406 |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
407 _timezones = {'UT':0, 'UTC':0, 'GMT':0, 'Z':0, |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
408 'AST': -400, 'ADT': -300, # Atlantic standard |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
409 'EST': -500, 'EDT': -400, # Eastern |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
410 'CST': -600, 'CDT':-500, # Centreal |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
411 'MST':-700, 'MDT':-600, # Mountain |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
412 'PST':-800, 'PDT':-700 # Pacific |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
413 } |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
414 |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
415 def parsedate_tz(data): |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
416 data = string.split(data) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
417 if data[0][-1] == ',': |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
418 # There's a dayname here. Skip it |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
419 del data[0] |
|
4835
e05480db71ab
Added support for RFC 850 style dates, as used by some HTTP servers
Guido van Rossum <guido@python.org>
parents:
4696
diff
changeset
|
420 if len(data) == 3: # RFC 850 date, deprecated |
|
e05480db71ab
Added support for RFC 850 style dates, as used by some HTTP servers
Guido van Rossum <guido@python.org>
parents:
4696
diff
changeset
|
421 stuff = string.split(data[0], '-') |
|
e05480db71ab
Added support for RFC 850 style dates, as used by some HTTP servers
Guido van Rossum <guido@python.org>
parents:
4696
diff
changeset
|
422 if len(stuff) == 3: |
|
e05480db71ab
Added support for RFC 850 style dates, as used by some HTTP servers
Guido van Rossum <guido@python.org>
parents:
4696
diff
changeset
|
423 data = stuff + data[1:] |
|
1872
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
1778
diff
changeset
|
424 if len(data) == 4: |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
1778
diff
changeset
|
425 s = data[3] |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
1778
diff
changeset
|
426 i = string.find(s, '+') |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
1778
diff
changeset
|
427 if i > 0: |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
1778
diff
changeset
|
428 data[3:] = [s[:i], s[i+1:]] |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
1778
diff
changeset
|
429 else: |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
1778
diff
changeset
|
430 data.append('') # Dummy tz |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
431 if len(data) < 5: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
432 return None |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
433 data = data[:5] |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
434 [dd, mm, yy, tm, tz] = data |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
435 if not mm in _monthnames: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
436 return None |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
437 mm = _monthnames.index(mm)+1 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
438 tm = string.splitfields(tm, ':') |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
439 if len(tm) == 2: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
440 [thh, tmm] = tm |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
441 tss = '0' |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
442 else: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
443 [thh, tmm, tss] = tm |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
444 try: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
445 yy = string.atoi(yy) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
446 dd = string.atoi(dd) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
447 thh = string.atoi(thh) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
448 tmm = string.atoi(tmm) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
449 tss = string.atoi(tss) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
450 except string.atoi_error: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
451 return None |
|
4559
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
452 tzoffset=0 |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
453 tz=string.upper(tz) |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
454 if _timezones.has_key(tz): |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
455 tzoffset=_timezones[tz] |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
456 else: |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
457 try: |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
458 tzoffset=string.atoi(tz) |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
459 except string.atoi_error: |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
460 pass |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
461 # Convert a timezone offset into seconds ; -0500 -> -18000 |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
462 if tzoffset<0: tzsign=-1 |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
463 else: tzsign=1 |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
464 tzoffset=tzoffset*tzsign |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
465 tzoffset = tzsign * ( (tzoffset/100)*3600 + (tzoffset % 100)*60) |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
466 tuple = (yy, mm, dd, thh, tmm, tss, 0, 0, 0, tzoffset) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
467 return tuple |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
468 |
|
4559
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
469 def parsedate(data): |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
470 t=parsedate_tz(data) |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
471 if type(t)==type( () ): |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
472 return t[:9] |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
473 else: return t |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
474 |
|
4696
de27da5f22f1
Add mktime_tz() which turns a date_tz 10-tuple into a standard Unix timestamp.
Guido van Rossum <guido@python.org>
parents:
4559
diff
changeset
|
475 def mktime_tz(data): |
|
de27da5f22f1
Add mktime_tz() which turns a date_tz 10-tuple into a standard Unix timestamp.
Guido van Rossum <guido@python.org>
parents:
4559
diff
changeset
|
476 """Turn a 10-tuple as returned by parsedate_tz() into a UTC timestamp. |
|
de27da5f22f1
Add mktime_tz() which turns a date_tz 10-tuple into a standard Unix timestamp.
Guido van Rossum <guido@python.org>
parents:
4559
diff
changeset
|
477 |
|
de27da5f22f1
Add mktime_tz() which turns a date_tz 10-tuple into a standard Unix timestamp.
Guido van Rossum <guido@python.org>
parents:
4559
diff
changeset
|
478 Minor glitch: this first interprets the first 8 elements as a |
|
de27da5f22f1
Add mktime_tz() which turns a date_tz 10-tuple into a standard Unix timestamp.
Guido van Rossum <guido@python.org>
parents:
4559
diff
changeset
|
479 local time and then compensates for the timezone difference; |
|
de27da5f22f1
Add mktime_tz() which turns a date_tz 10-tuple into a standard Unix timestamp.
Guido van Rossum <guido@python.org>
parents:
4559
diff
changeset
|
480 this may yield a slight error around daylight savings time |
|
de27da5f22f1
Add mktime_tz() which turns a date_tz 10-tuple into a standard Unix timestamp.
Guido van Rossum <guido@python.org>
parents:
4559
diff
changeset
|
481 switch dates. Not enough to worry about for common use. |
|
de27da5f22f1
Add mktime_tz() which turns a date_tz 10-tuple into a standard Unix timestamp.
Guido van Rossum <guido@python.org>
parents:
4559
diff
changeset
|
482 |
|
de27da5f22f1
Add mktime_tz() which turns a date_tz 10-tuple into a standard Unix timestamp.
Guido van Rossum <guido@python.org>
parents:
4559
diff
changeset
|
483 """ |
|
de27da5f22f1
Add mktime_tz() which turns a date_tz 10-tuple into a standard Unix timestamp.
Guido van Rossum <guido@python.org>
parents:
4559
diff
changeset
|
484 t = time.mktime(data[:8] + (0,)) |
|
de27da5f22f1
Add mktime_tz() which turns a date_tz 10-tuple into a standard Unix timestamp.
Guido van Rossum <guido@python.org>
parents:
4559
diff
changeset
|
485 return t + data[9] - time.timezone |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
486 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
487 # When used as script, run a small test program. |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
488 # The first command line argument must be a filename containing one |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
489 # message in RFC-822 format. |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
490 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
491 if __name__ == '__main__': |
|
3824
820aba1046c3
New, improved parseaddr() by Sjoerd.
Guido van Rossum <guido@python.org>
parents:
3638
diff
changeset
|
492 import sys, os |
|
4559
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
493 file = os.path.join(os.environ['HOME'], 'Mail/inbox/1') |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
494 if sys.argv[1:]: file = sys.argv[1] |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
495 f = open(file, 'r') |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
496 m = Message(f) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
497 print 'From:', m.getaddr('from') |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
498 print 'To:', m.getaddrlist('to') |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
499 print 'Subject:', m.getheader('subject') |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
500 print 'Date:', m.getheader('date') |
|
4559
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
501 date = m.getdate_tz('date') |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
502 if date: |
|
4559
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
503 print 'ParsedDate:', time.asctime(date[:-1]), |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
504 hhmmss = date[-1] |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
505 hhmm, ss = divmod(hhmmss, 60) |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
506 hh, mm = divmod(hhmm, 60) |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
507 print "%+03d%02d" % (hh, mm), |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
508 if ss: print ".%02d" % ss, |
|
adbcc0ab9eb6
Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents:
3824
diff
changeset
|
509 print |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
510 else: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
511 print 'ParsedDate:', None |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
512 m.rewindbody() |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
513 n = 0 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
514 while f.readline(): |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
515 n = n + 1 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
516 print 'Lines:', n |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
517 print '-'*70 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
518 print 'len =', len(m) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
519 if m.has_key('Date'): print 'Date =', m['Date'] |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
520 if m.has_key('X-Nonsense'): pass |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
521 print 'keys =', m.keys() |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
522 print 'values =', m.values() |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
523 print 'items =', m.items() |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
524 |
