annotate Lib/rfc822.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 7a125913a375
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
1 """RFC 2822 message manipulation.
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
2
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
3 Note: This is only a very rough sketch of a full RFC-822 parser; in particular
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
4 the tokenizing of addresses does not adhere to all the quoting rules.
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
5
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
6 Note: RFC 2822 is a long awaited update to RFC 822. This module should
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
7 conform to RFC 2822, and is thus mis-named (it's not worth renaming it). Some
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
8 effort at RFC 2822 updates have been made, but a thorough audit has not been
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
9 performed. Consider any RFC 2822 non-conformance to be a bug.
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
10
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
11 RFC 2822: http://www.faqs.org/rfcs/rfc2822.html
18639
d3c25b06b7e5 Stoopid change, just to mention that the last checkin resolves SF bug
Barry Warsaw <barry@python.org>
parents: 18638
diff changeset
12 RFC 822 : http://www.faqs.org/rfcs/rfc822.html (obsolete)
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
13
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
14 Directions for use:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
15
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
16 To create a Message object: first open a file, e.g.:
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
17
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
18 fp = open(file, 'r')
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
19
8481
614a7ffee113 Some changes suggested/provided by Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 8365
diff changeset
20 You can use any other legal way of getting an open file object, e.g. use
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
21 sys.stdin or call os.popen(). Then pass the open file object to the Message()
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
22 constructor:
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
23
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
24 m = Message(fp)
914
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
26 This class can work with any input object that supports a readline method. If
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
27 the input object has seek and tell capability, the rewindbody method will
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
28 work; also illegal lines will be pushed back onto the input stream. If the
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
29 input object lacks seek but has an `unread' method that can push back a line
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
30 of input, Message will use that to push back illegal lines. Thus this class
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
31 can be used to parse messages coming from a buffered stream.
8481
614a7ffee113 Some changes suggested/provided by Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 8365
diff changeset
32
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
33 The optional `seekable' argument is provided as a workaround for certain stdio
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
34 libraries in which tell() discards buffered data before discovering that the
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
35 lseek() system call doesn't work. For maximum portability, you should set the
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
36 seekable argument to zero to prevent that initial \code{tell} when passing in
72984
86e3943d0d5b Remove duplication.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 47958
diff changeset
37 an unseekable object such as a file object created from a socket object. If
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
38 it is 1 on entry -- which it is by default -- the tell() method of the open
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
39 file object is called once; if this raises an exception, seekable is reset to
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
40 0. For other nonzero values of seekable, this test is not made.
8481
614a7ffee113 Some changes suggested/provided by Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 8365
diff changeset
41
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
42 To get the text of a particular header there are several methods:
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
43
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
44 str = m.getheader(name)
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
45 str = m.getrawheader(name)
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
46
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
47 where name is the name of the header, e.g. 'Subject'. The difference is that
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
48 getheader() strips the leading and trailing whitespace, while getrawheader()
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
49 doesn't. Both functions retain embedded whitespace (including newlines)
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
50 exactly as they are specified in the header, and leave the case of the text
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
51 unchanged.
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
52
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
53 For addresses and address lists there are functions
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
54
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
55 realname, mailaddress = m.getaddr(name)
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
56 list = m.getaddrlist(name)
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
57
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
58 where the latter returns a list of (realname, mailaddr) tuples.
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
59
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
60 There is also a method
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
61
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
62 time = m.getdate(name)
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
63
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
64 which parses a Date-like field and returns a time-compatible tuple,
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
65 i.e. a tuple such as returned by time.localtime() or accepted by
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
66 time.mktime().
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
67
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
68 See the class definition for lower level access methods.
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
69
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
70 There are also some utility functions here.
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
71 """
8512
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
72 # Cleanup and extensions by Eric S. Raymond <esr@thyrsus.com>
914
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
74 import time
914
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
47659
6585fd1d9ad7 add py3k warnings to rfc822
Benjamin Peterson <benjamin@python.org>
parents: 40694
diff changeset
76 from warnings import warnpy3k
47958
e7a6a4515ab0 fix test_py3kwarns
Benjamin Peterson <benjamin@python.org>
parents: 47659
diff changeset
77 warnpy3k("in 3.x, rfc822 has been removed in favor of the email package",
e7a6a4515ab0 fix test_py3kwarns
Benjamin Peterson <benjamin@python.org>
parents: 47659
diff changeset
78 stacklevel=2)
47659
6585fd1d9ad7 add py3k warnings to rfc822
Benjamin Peterson <benjamin@python.org>
parents: 40694
diff changeset
79
16613
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15871
diff changeset
80 __all__ = ["Message","AddressList","parsedate","parsedate_tz","mktime_tz"]
914
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
82 _blanklines = ('\r\n', '\n') # Optimization for islast()
2780
4113f4fbaae5 added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents: 2745
diff changeset
83
4113f4fbaae5 added seekable option; save unix from lines; speed up islast()
Guido van Rossum <guido@python.org>
parents: 2745
diff changeset
84
914
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 class Message:
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
86 """Represents a single RFC 2822-compliant message."""
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
87
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
88 def __init__(self, fp, seekable = 1):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
89 """Initialize the class instance and read the headers."""
8481
614a7ffee113 Some changes suggested/provided by Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 8365
diff changeset
90 if seekable == 1:
614a7ffee113 Some changes suggested/provided by Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 8365
diff changeset
91 # Exercise tell() to make sure it works
614a7ffee113 Some changes suggested/provided by Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 8365
diff changeset
92 # (and then assume seek() works, too)
614a7ffee113 Some changes suggested/provided by Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 8365
diff changeset
93 try:
614a7ffee113 Some changes suggested/provided by Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 8365
diff changeset
94 fp.tell()
18381
dc74823b49ea Clean up a bare except: clause.
unknown <tools@python.org>
parents: 17914
diff changeset
95 except (AttributeError, IOError):
8481
614a7ffee113 Some changes suggested/provided by Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 8365
diff changeset
96 seekable = 0
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
97 self.fp = fp
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
98 self.seekable = seekable
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
99 self.startofheaders = None
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
100 self.startofbody = None
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
101 #
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
102 if self.seekable:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
103 try:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
104 self.startofheaders = self.fp.tell()
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
105 except IOError:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
106 self.seekable = 0
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
107 #
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
108 self.readheaders()
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
109 #
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
110 if self.seekable:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
111 try:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
112 self.startofbody = self.fp.tell()
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
113 except IOError:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
114 self.seekable = 0
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
115
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
116 def rewindbody(self):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
117 """Rewind the file to the start of the body (if seekable)."""
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
118 if not self.seekable:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
119 raise IOError, "unseekable file"
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
120 self.fp.seek(self.startofbody)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
121
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
122 def readheaders(self):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
123 """Read header lines.
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
124
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
125 Read header lines up to the entirely blank line that terminates them.
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
126 The (normally blank) line that ends the headers is skipped, but not
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
127 included in the returned list. If a non-header line ends the headers,
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
128 (which is an error), an attempt is made to backspace over it; it is
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
129 never included in the returned list.
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
130
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
131 The variable self.status is set to the empty string if all went well,
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
132 otherwise it is an error message. The variable self.headers is a
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
133 completely uninterpreted list of lines contained in the header (so
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
134 printing them will reproduce the header exactly as it appears in the
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
135 file).
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
136 """
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
137 self.dict = {}
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
138 self.unixfrom = ''
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
139 self.headers = lst = []
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
140 self.status = ''
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
141 headerseen = ""
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
142 firstline = 1
8694
f41580b4eea1 Don't use calculations on values gotten from tell(). Also use a
Guido van Rossum <guido@python.org>
parents: 8537
diff changeset
143 startofline = unread = tell = None
f41580b4eea1 Don't use calculations on values gotten from tell(). Also use a
Guido van Rossum <guido@python.org>
parents: 8537
diff changeset
144 if hasattr(self.fp, 'unread'):
f41580b4eea1 Don't use calculations on values gotten from tell(). Also use a
Guido van Rossum <guido@python.org>
parents: 8537
diff changeset
145 unread = self.fp.unread
f41580b4eea1 Don't use calculations on values gotten from tell(). Also use a
Guido van Rossum <guido@python.org>
parents: 8537
diff changeset
146 elif self.seekable:
f41580b4eea1 Don't use calculations on values gotten from tell(). Also use a
Guido van Rossum <guido@python.org>
parents: 8537
diff changeset
147 tell = self.fp.tell
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
148 while 1:
8694
f41580b4eea1 Don't use calculations on values gotten from tell(). Also use a
Guido van Rossum <guido@python.org>
parents: 8537
diff changeset
149 if tell:
15394
96c394a43b5b Implement the suggestion of bug_id=122070: surround tell() call with
Guido van Rossum <guido@python.org>
parents: 14842
diff changeset
150 try:
96c394a43b5b Implement the suggestion of bug_id=122070: surround tell() call with
Guido van Rossum <guido@python.org>
parents: 14842
diff changeset
151 startofline = tell()
96c394a43b5b Implement the suggestion of bug_id=122070: surround tell() call with
Guido van Rossum <guido@python.org>
parents: 14842
diff changeset
152 except IOError:
96c394a43b5b Implement the suggestion of bug_id=122070: surround tell() call with
Guido van Rossum <guido@python.org>
parents: 14842
diff changeset
153 startofline = tell = None
96c394a43b5b Implement the suggestion of bug_id=122070: surround tell() call with
Guido van Rossum <guido@python.org>
parents: 14842
diff changeset
154 self.seekable = 0
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
155 line = self.fp.readline()
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
156 if not line:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
157 self.status = 'EOF in headers'
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
158 break
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
159 # Skip unix From name time lines
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
160 if firstline and line.startswith('From '):
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
161 self.unixfrom = self.unixfrom + line
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
162 continue
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
163 firstline = 0
8484
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
164 if headerseen and line[0] in ' \t':
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
165 # It's a continuation line.
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
166 lst.append(line)
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
167 x = (self.dict[headerseen] + "\n " + line.strip())
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
168 self.dict[headerseen] = x.strip()
8484
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
169 continue
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
170 elif self.iscomment(line):
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
171 # It's a comment. Ignore it.
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
172 continue
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
173 elif self.islast(line):
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
174 # Note! No pushback here! The delimiter line gets eaten.
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
175 break
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
176 headerseen = self.isheader(line)
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
177 if headerseen:
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
178 # It's a legal header line, save it.
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
179 lst.append(line)
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
180 self.dict[headerseen] = line[len(headerseen)+1:].strip()
8484
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
181 continue
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
182 else:
8484
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
183 # It's not a header line; throw it back and stop here.
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
184 if not self.dict:
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
185 self.status = 'No headers'
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
186 else:
8484
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
187 self.status = 'Non-header line where header expected'
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
188 # Try to undo the read.
8694
f41580b4eea1 Don't use calculations on values gotten from tell(). Also use a
Guido van Rossum <guido@python.org>
parents: 8537
diff changeset
189 if unread:
f41580b4eea1 Don't use calculations on values gotten from tell(). Also use a
Guido van Rossum <guido@python.org>
parents: 8537
diff changeset
190 unread(line)
f41580b4eea1 Don't use calculations on values gotten from tell(). Also use a
Guido van Rossum <guido@python.org>
parents: 8537
diff changeset
191 elif tell:
f41580b4eea1 Don't use calculations on values gotten from tell(). Also use a
Guido van Rossum <guido@python.org>
parents: 8537
diff changeset
192 self.fp.seek(startofline)
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
193 else:
8481
614a7ffee113 Some changes suggested/provided by Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 8365
diff changeset
194 self.status = self.status + '; bad seek'
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
195 break
8484
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
196
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
197 def isheader(self, line):
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
198 """Determine whether a given line is a legal header.
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
199
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
200 This method should return the header name, suitably canonicalized.
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
201 You may override this method in order to use Message parsing on tagged
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
202 data in RFC 2822-like formats with special header formats.
8484
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
203 """
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
204 i = line.find(':')
8484
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
205 if i > 0:
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
206 return line[:i].lower()
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
207 return None
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
208
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
209 def islast(self, line):
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
210 """Determine whether a line is a legal end of RFC 2822 headers.
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
211
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
212 You may override this method if your application wants to bend the
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
213 rules, e.g. to strip trailing whitespace, or to recognize MH template
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
214 separators ('--------'). For convenience (e.g. for code reading from
79083
7a125913a375 #15980: properly escape newlines in docstrings. Patch by Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72984
diff changeset
215 sockets) a line consisting of \\r\\n also matches.
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
216 """
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
217 return line in _blanklines
8481
614a7ffee113 Some changes suggested/provided by Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 8365
diff changeset
218
614a7ffee113 Some changes suggested/provided by Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 8365
diff changeset
219 def iscomment(self, line):
614a7ffee113 Some changes suggested/provided by Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 8365
diff changeset
220 """Determine whether a line should be skipped entirely.
614a7ffee113 Some changes suggested/provided by Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 8365
diff changeset
221
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
222 You may override this method in order to use Message parsing on tagged
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
223 data in RFC 2822-like formats that support embedded comments or
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
224 free-text data.
8481
614a7ffee113 Some changes suggested/provided by Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 8365
diff changeset
225 """
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 21499
diff changeset
226 return False
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
227
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
228 def getallmatchingheaders(self, name):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
229 """Find all header lines matching a given header name.
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
230
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
231 Look through the list of headers and find all lines matching a given
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
232 header name (and their continuation lines). A list of the lines is
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
233 returned, without interpretation. If the header does not occur, an
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
234 empty list is returned. If the header occurs multiple times, all
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
235 occurrences are returned. Case is not important in the header name.
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
236 """
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
237 name = name.lower() + ':'
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
238 n = len(name)
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
239 lst = []
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
240 hit = 0
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
241 for line in self.headers:
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
242 if line[:n].lower() == name:
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
243 hit = 1
15693
280a78ed0035 Duh. Instead of string.whitespace and string.digits, use isspace()
Guido van Rossum <guido@python.org>
parents: 15583
diff changeset
244 elif not line[:1].isspace():
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
245 hit = 0
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
246 if hit:
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
247 lst.append(line)
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
248 return lst
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
249
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
250 def getfirstmatchingheader(self, name):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
251 """Get the first header line matching name.
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
252
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
253 This is similar to getallmatchingheaders, but it returns only the
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
254 first matching header (and its continuation lines).
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
255 """
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
256 name = name.lower() + ':'
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
257 n = len(name)
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
258 lst = []
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
259 hit = 0
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
260 for line in self.headers:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
261 if hit:
15693
280a78ed0035 Duh. Instead of string.whitespace and string.digits, use isspace()
Guido van Rossum <guido@python.org>
parents: 15583
diff changeset
262 if not line[:1].isspace():
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
263 break
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
264 elif line[:n].lower() == name:
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
265 hit = 1
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
266 if hit:
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
267 lst.append(line)
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
268 return lst
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
269
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
270 def getrawheader(self, name):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
271 """A higher-level interface to getfirstmatchingheader().
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
272
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
273 Return a string containing the literal text of the header but with the
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
274 keyword stripped. All leading, trailing and embedded whitespace is
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
275 kept in the string, however. Return None if the header does not
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
276 occur.
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
277 """
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
278
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
279 lst = self.getfirstmatchingheader(name)
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
280 if not lst:
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
281 return None
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
282 lst[0] = lst[0][len(name) + 1:]
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
283 return ''.join(lst)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
284
8481
614a7ffee113 Some changes suggested/provided by Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 8365
diff changeset
285 def getheader(self, name, default=None):
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
286 """Get the header value for a name.
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
287
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
288 This is the normal interface: it returns a stripped version of the
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
289 header value for a given header name, or None if it doesn't exist.
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
290 This uses the dictionary version which finds the *last* such header.
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
291 """
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
292 return self.dict.get(name.lower(), default)
8481
614a7ffee113 Some changes suggested/provided by Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 8365
diff changeset
293 get = getheader
10782
54a30d6b01f1 Message.getheader(): Fixed grammatical error in docstring.
Fred Drake <fdrake@acm.org>
parents: 10624
diff changeset
294
54a30d6b01f1 Message.getheader(): Fixed grammatical error in docstring.
Fred Drake <fdrake@acm.org>
parents: 10624
diff changeset
295 def getheaders(self, name):
54a30d6b01f1 Message.getheader(): Fixed grammatical error in docstring.
Fred Drake <fdrake@acm.org>
parents: 10624
diff changeset
296 """Get all values for a header.
54a30d6b01f1 Message.getheader(): Fixed grammatical error in docstring.
Fred Drake <fdrake@acm.org>
parents: 10624
diff changeset
297
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
298 This returns a list of values for headers given more than once; each
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
299 value in the result list is stripped in the same way as the result of
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
300 getheader(). If the header is not given, return an empty list.
10782
54a30d6b01f1 Message.getheader(): Fixed grammatical error in docstring.
Fred Drake <fdrake@acm.org>
parents: 10624
diff changeset
301 """
54a30d6b01f1 Message.getheader(): Fixed grammatical error in docstring.
Fred Drake <fdrake@acm.org>
parents: 10624
diff changeset
302 result = []
54a30d6b01f1 Message.getheader(): Fixed grammatical error in docstring.
Fred Drake <fdrake@acm.org>
parents: 10624
diff changeset
303 current = ''
54a30d6b01f1 Message.getheader(): Fixed grammatical error in docstring.
Fred Drake <fdrake@acm.org>
parents: 10624
diff changeset
304 have_header = 0
54a30d6b01f1 Message.getheader(): Fixed grammatical error in docstring.
Fred Drake <fdrake@acm.org>
parents: 10624
diff changeset
305 for s in self.getallmatchingheaders(name):
15693
280a78ed0035 Duh. Instead of string.whitespace and string.digits, use isspace()
Guido van Rossum <guido@python.org>
parents: 15583
diff changeset
306 if s[0].isspace():
10782
54a30d6b01f1 Message.getheader(): Fixed grammatical error in docstring.
Fred Drake <fdrake@acm.org>
parents: 10624
diff changeset
307 if current:
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
308 current = "%s\n %s" % (current, s.strip())
10782
54a30d6b01f1 Message.getheader(): Fixed grammatical error in docstring.
Fred Drake <fdrake@acm.org>
parents: 10624
diff changeset
309 else:
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
310 current = s.strip()
10782
54a30d6b01f1 Message.getheader(): Fixed grammatical error in docstring.
Fred Drake <fdrake@acm.org>
parents: 10624
diff changeset
311 else:
54a30d6b01f1 Message.getheader(): Fixed grammatical error in docstring.
Fred Drake <fdrake@acm.org>
parents: 10624
diff changeset
312 if have_header:
54a30d6b01f1 Message.getheader(): Fixed grammatical error in docstring.
Fred Drake <fdrake@acm.org>
parents: 10624
diff changeset
313 result.append(current)
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
314 current = s[s.find(":") + 1:].strip()
10782
54a30d6b01f1 Message.getheader(): Fixed grammatical error in docstring.
Fred Drake <fdrake@acm.org>
parents: 10624
diff changeset
315 have_header = 1
54a30d6b01f1 Message.getheader(): Fixed grammatical error in docstring.
Fred Drake <fdrake@acm.org>
parents: 10624
diff changeset
316 if have_header:
54a30d6b01f1 Message.getheader(): Fixed grammatical error in docstring.
Fred Drake <fdrake@acm.org>
parents: 10624
diff changeset
317 result.append(current)
10980
ae96b1a128f8 Message.getheaders(): If there are no matching headers, return an
Fred Drake <fdrake@acm.org>
parents: 10788
diff changeset
318 return result
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
319
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
320 def getaddr(self, name):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
321 """Get a single address from a header, as a tuple.
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
322
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
323 An example return value:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
324 ('Guido van Rossum', 'guido@cwi.nl')
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
325 """
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
326 # New, by Ben Escoto
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
327 alist = self.getaddrlist(name)
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
328 if alist:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
329 return alist[0]
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
330 else:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
331 return (None, None)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
332
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
333 def getaddrlist(self, name):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
334 """Get a list of addresses from a header.
9889
9f13d7380bf8 Message.getaddrlist(): This now handles multiple occurances of the
Barry Warsaw <barry@python.org>
parents: 9726
diff changeset
335
9f13d7380bf8 Message.getaddrlist(): This now handles multiple occurances of the
Barry Warsaw <barry@python.org>
parents: 9726
diff changeset
336 Retrieves a list of addresses from a header, where each address is a
9f13d7380bf8 Message.getaddrlist(): This now handles multiple occurances of the
Barry Warsaw <barry@python.org>
parents: 9726
diff changeset
337 tuple as returned by getaddr(). Scans all named headers, so it works
9f13d7380bf8 Message.getaddrlist(): This now handles multiple occurances of the
Barry Warsaw <barry@python.org>
parents: 9726
diff changeset
338 properly with multiple To: or Cc: headers for example.
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
339 """
9889
9f13d7380bf8 Message.getaddrlist(): This now handles multiple occurances of the
Barry Warsaw <barry@python.org>
parents: 9726
diff changeset
340 raw = []
9f13d7380bf8 Message.getaddrlist(): This now handles multiple occurances of the
Barry Warsaw <barry@python.org>
parents: 9726
diff changeset
341 for h in self.getallmatchingheaders(name):
11742
840e18eec07f Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents: 11657
diff changeset
342 if h[0] in ' \t':
840e18eec07f Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents: 11657
diff changeset
343 raw.append(h)
840e18eec07f Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents: 11657
diff changeset
344 else:
840e18eec07f Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents: 11657
diff changeset
345 if raw:
840e18eec07f Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents: 11657
diff changeset
346 raw.append(', ')
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
347 i = h.find(':')
9889
9f13d7380bf8 Message.getaddrlist(): This now handles multiple occurances of the
Barry Warsaw <barry@python.org>
parents: 9726
diff changeset
348 if i > 0:
9f13d7380bf8 Message.getaddrlist(): This now handles multiple occurances of the
Barry Warsaw <barry@python.org>
parents: 9726
diff changeset
349 addr = h[i+1:]
9f13d7380bf8 Message.getaddrlist(): This now handles multiple occurances of the
Barry Warsaw <barry@python.org>
parents: 9726
diff changeset
350 raw.append(addr)
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
351 alladdrs = ''.join(raw)
23011
989377cdf053 AddrlistClass -> AddressList
Barry Warsaw <barry@python.org>
parents: 22867
diff changeset
352 a = AddressList(alladdrs)
23500
ad904db7344a Message.getaddrlist(): Use the AddressList.addresslist attribute
Barry Warsaw <barry@python.org>
parents: 23011
diff changeset
353 return a.addresslist
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
354
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
355 def getdate(self, name):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
356 """Retrieve a date field from a header.
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
357
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
358 Retrieves a date field from the named header, returning a tuple
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
359 compatible with time.mktime().
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
360 """
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
361 try:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
362 data = self[name]
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
363 except KeyError:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
364 return None
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
365 return parsedate(data)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
366
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
367 def getdate_tz(self, name):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
368 """Retrieve a date field from a header as a 10-tuple.
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
369
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
370 The first 9 elements make up a tuple compatible with time.mktime(),
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
371 and the 10th is the offset of the poster's time zone from GMT/UTC.
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
372 """
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
373 try:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
374 data = self[name]
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
375 except KeyError:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
376 return None
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
377 return parsedate_tz(data)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
378
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
379
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
380 # Access as a dictionary (only finds *last* header of each type):
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
381
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
382 def __len__(self):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
383 """Get the number of headers in a message."""
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
384 return len(self.dict)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
385
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
386 def __getitem__(self, name):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
387 """Get a specific header, as from a dictionary."""
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
388 return self.dict[name.lower()]
8484
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
389
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
390 def __setitem__(self, name, value):
8512
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
391 """Set the value of a header.
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
392
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
393 Note: This is not a perfect inversion of __getitem__, because any
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
394 changed headers get stuck at the end of the raw-headers list rather
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
395 than where the altered header was.
8512
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
396 """
8484
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
397 del self[name] # Won't fail if it doesn't exist
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
398 self.dict[name.lower()] = value
8484
2e4f178d7e17 Support new overridable method, isheader() (ESR).
Guido van Rossum <guido@python.org>
parents: 8481
diff changeset
399 text = name + ": " + value
34569
89b8ccf14c0c Convert splitlines to for-loop (handles case where input does not have a trailing newline).
Raymond Hettinger <python@rcn.com>
parents: 34561
diff changeset
400 for line in text.split("\n"):
89b8ccf14c0c Convert splitlines to for-loop (handles case where input does not have a trailing newline).
Raymond Hettinger <python@rcn.com>
parents: 34561
diff changeset
401 self.headers.append(line + "\n")
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
402
7799
cf723991d75f Added a __delitem__ to the Message class.
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
403 def __delitem__(self, name):
cf723991d75f Added a __delitem__ to the Message class.
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
404 """Delete all occurrences of a specific header, if it is present."""
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
405 name = name.lower()
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23519
diff changeset
406 if not name in self.dict:
11289
f2c920d03f44 After much hemming and hawing, we decided to roll back Fred's change.
Guido van Rossum <guido@python.org>
parents: 11272
diff changeset
407 return
f2c920d03f44 After much hemming and hawing, we decided to roll back Fred's change.
Guido van Rossum <guido@python.org>
parents: 11272
diff changeset
408 del self.dict[name]
f2c920d03f44 After much hemming and hawing, we decided to roll back Fred's change.
Guido van Rossum <guido@python.org>
parents: 11272
diff changeset
409 name = name + ':'
7799
cf723991d75f Added a __delitem__ to the Message class.
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
410 n = len(name)
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
411 lst = []
7799
cf723991d75f Added a __delitem__ to the Message class.
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
412 hit = 0
cf723991d75f Added a __delitem__ to the Message class.
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
413 for i in range(len(self.headers)):
cf723991d75f Added a __delitem__ to the Message class.
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
414 line = self.headers[i]
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
415 if line[:n].lower() == name:
7799
cf723991d75f Added a __delitem__ to the Message class.
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
416 hit = 1
15693
280a78ed0035 Duh. Instead of string.whitespace and string.digits, use isspace()
Guido van Rossum <guido@python.org>
parents: 15583
diff changeset
417 elif not line[:1].isspace():
7799
cf723991d75f Added a __delitem__ to the Message class.
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
418 hit = 0
cf723991d75f Added a __delitem__ to the Message class.
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
419 if hit:
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
420 lst.append(i)
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
421 for i in reversed(lst):
7799
cf723991d75f Added a __delitem__ to the Message class.
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
422 del self.headers[i]
cf723991d75f Added a __delitem__ to the Message class.
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
423
17914
ff6a6d347195 Per discussion with Barry, make the default value for both get() and
Fred Drake <fdrake@acm.org>
parents: 17901
diff changeset
424 def setdefault(self, name, default=""):
17901
ae8c4f470a0e Added .get() and .setdefault() support to rfc822.Message.
Fred Drake <fdrake@acm.org>
parents: 16613
diff changeset
425 lowername = name.lower()
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23519
diff changeset
426 if lowername in self.dict:
17901
ae8c4f470a0e Added .get() and .setdefault() support to rfc822.Message.
Fred Drake <fdrake@acm.org>
parents: 16613
diff changeset
427 return self.dict[lowername]
ae8c4f470a0e Added .get() and .setdefault() support to rfc822.Message.
Fred Drake <fdrake@acm.org>
parents: 16613
diff changeset
428 else:
17914
ff6a6d347195 Per discussion with Barry, make the default value for both get() and
Fred Drake <fdrake@acm.org>
parents: 17901
diff changeset
429 text = name + ": " + default
34569
89b8ccf14c0c Convert splitlines to for-loop (handles case where input does not have a trailing newline).
Raymond Hettinger <python@rcn.com>
parents: 34561
diff changeset
430 for line in text.split("\n"):
89b8ccf14c0c Convert splitlines to for-loop (handles case where input does not have a trailing newline).
Raymond Hettinger <python@rcn.com>
parents: 34561
diff changeset
431 self.headers.append(line + "\n")
17914
ff6a6d347195 Per discussion with Barry, make the default value for both get() and
Fred Drake <fdrake@acm.org>
parents: 17901
diff changeset
432 self.dict[lowername] = default
17901
ae8c4f470a0e Added .get() and .setdefault() support to rfc822.Message.
Fred Drake <fdrake@acm.org>
parents: 16613
diff changeset
433 return default
ae8c4f470a0e Added .get() and .setdefault() support to rfc822.Message.
Fred Drake <fdrake@acm.org>
parents: 16613
diff changeset
434
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
435 def has_key(self, name):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
436 """Determine whether a message contains the named header."""
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23519
diff changeset
437 return name.lower() in self.dict
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23519
diff changeset
438
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23519
diff changeset
439 def __contains__(self, name):
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23519
diff changeset
440 """Determine whether a message contains the named header."""
24263
e6033254df21 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23691
diff changeset
441 return name.lower() in self.dict
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
442
33537
526427e883e8 Bug #1030125: rfc822 __iter__ problem
Raymond Hettinger <python@rcn.com>
parents: 33002
diff changeset
443 def __iter__(self):
526427e883e8 Bug #1030125: rfc822 __iter__ problem
Raymond Hettinger <python@rcn.com>
parents: 33002
diff changeset
444 return iter(self.dict)
526427e883e8 Bug #1030125: rfc822 __iter__ problem
Raymond Hettinger <python@rcn.com>
parents: 33002
diff changeset
445
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
446 def keys(self):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
447 """Get all of a message's header field names."""
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
448 return self.dict.keys()
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
449
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
450 def values(self):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
451 """Get all of a message's header field values."""
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
452 return self.dict.values()
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
453
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
454 def items(self):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
455 """Get all of a message's headers.
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
456
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
457 Returns a list of name, value tuples.
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
458 """
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
459 return self.dict.items()
914
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
460
8481
614a7ffee113 Some changes suggested/provided by Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 8365
diff changeset
461 def __str__(self):
30899
96170f75e3e9 Make Message.__str__ more efficient.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30865
diff changeset
462 return ''.join(self.headers)
914
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
463
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
464
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
465 # Utility functions
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
466 # -----------------
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
467
6624
be07921d9713 New address parser by Ben Escoto replaces
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
468 # XXX Should fix unquote() and quote() to be really conformant.
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
469 # 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
470
914
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
471
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
472 def unquote(s):
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
473 """Remove quotes from a string."""
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
474 if len(s) > 1:
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
475 if s.startswith('"') and s.endswith('"'):
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
476 return s[1:-1].replace('\\\\', '\\').replace('\\"', '"')
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
477 if s.startswith('<') and s.endswith('>'):
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
478 return s[1:-1]
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
479 return s
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
480
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
481
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
482 def quote(s):
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
483 """Add quotes around a string."""
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
484 return s.replace('\\', '\\\\').replace('"', '\\"')
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
485
6624
be07921d9713 New address parser by Ben Escoto replaces
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
486
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
487 def parseaddr(address):
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
488 """Parse an address into a (realname, mailaddr) tuple."""
23011
989377cdf053 AddrlistClass -> AddressList
Barry Warsaw <barry@python.org>
parents: 22867
diff changeset
489 a = AddressList(address)
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
490 lst = a.addresslist
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
491 if not lst:
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
492 return (None, None)
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
493 return lst[0]
6624
be07921d9713 New address parser by Ben Escoto replaces
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
494
be07921d9713 New address parser by Ben Escoto replaces
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
495
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
496 class AddrlistClass:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
497 """Address parser class by Ben Escoto.
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
498
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
499 To understand what this class does, it helps to have a copy of
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
500 RFC 2822 in front of you.
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
501
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
502 http://www.faqs.org/rfcs/rfc2822.html
8512
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
503
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
504 Note: this class interface is deprecated and may be removed in the future.
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
505 Use rfc822.AddressList instead.
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
506 """
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
507
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
508 def __init__(self, field):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
509 """Initialize a new instance.
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
510
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
511 `field' is an unparsed address header field, containing one or more
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
512 addresses.
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
513 """
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
514 self.specials = '()<>@,:;.\"[]'
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
515 self.pos = 0
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
516 self.LWS = ' \t'
9889
9f13d7380bf8 Message.getaddrlist(): This now handles multiple occurances of the
Barry Warsaw <barry@python.org>
parents: 9726
diff changeset
517 self.CR = '\r\n'
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
518 self.atomends = self.specials + self.LWS + self.CR
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
519 # Note that RFC 2822 now specifies `.' as obs-phrase, meaning that it
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
520 # is obsolete syntax. RFC 2822 requires that we recognize obsolete
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
521 # syntax, so allow dots in phrases.
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
522 self.phraseends = self.atomends.replace('.', '')
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
523 self.field = field
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
524 self.commentlist = []
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
525
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
526 def gotonext(self):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
527 """Parse up to the start of the next address."""
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
528 while self.pos < len(self.field):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
529 if self.field[self.pos] in self.LWS + '\n\r':
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
530 self.pos = self.pos + 1
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
531 elif self.field[self.pos] == '(':
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
532 self.commentlist.append(self.getcomment())
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
533 else: break
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
534
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
535 def getaddrlist(self):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
536 """Parse all addresses.
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
537
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
538 Returns a list containing all of the addresses.
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
539 """
20965
5545818f581a Fix for bug #481221, getaddrlist() failing on long addresses.
Barry Warsaw <barry@python.org>
parents: 20961
diff changeset
540 result = []
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
541 ad = self.getaddress()
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
542 while ad:
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
543 result += ad
20965
5545818f581a Fix for bug #481221, getaddrlist() failing on long addresses.
Barry Warsaw <barry@python.org>
parents: 20961
diff changeset
544 ad = self.getaddress()
5545818f581a Fix for bug #481221, getaddrlist() failing on long addresses.
Barry Warsaw <barry@python.org>
parents: 20961
diff changeset
545 return result
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
546
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
547 def getaddress(self):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
548 """Parse the next address."""
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
549 self.commentlist = []
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
550 self.gotonext()
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
551
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
552 oldpos = self.pos
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
553 oldcl = self.commentlist
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
554 plist = self.getphraselist()
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
555
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
556 self.gotonext()
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
557 returnlist = []
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
558
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
559 if self.pos >= len(self.field):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
560 # Bad email address technically, no domain.
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
561 if plist:
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
562 returnlist = [(' '.join(self.commentlist), plist[0])]
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
563
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
564 elif self.field[self.pos] in '.@':
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
565 # email address is just an addrspec
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
566 # this isn't very efficient since we start over
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
567 self.pos = oldpos
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
568 self.commentlist = oldcl
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
569 addrspec = self.getaddrspec()
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
570 returnlist = [(' '.join(self.commentlist), addrspec)]
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
571
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
572 elif self.field[self.pos] == ':':
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
573 # address is a group
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
574 returnlist = []
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
575
11125
442ffe9a58c1 AddrlistClass.getaddress(): when parsing `:'s, in the loop, watch out
Barry Warsaw <barry@python.org>
parents: 10989
diff changeset
576 fieldlen = len(self.field)
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
577 self.pos += 1
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
578 while self.pos < len(self.field):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
579 self.gotonext()
11125
442ffe9a58c1 AddrlistClass.getaddress(): when parsing `:'s, in the loop, watch out
Barry Warsaw <barry@python.org>
parents: 10989
diff changeset
580 if self.pos < fieldlen and self.field[self.pos] == ';':
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
581 self.pos += 1
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
582 break
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
583 returnlist = returnlist + self.getaddress()
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
584
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
585 elif self.field[self.pos] == '<':
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
586 # Address is a phrase then a route addr
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
587 routeaddr = self.getrouteaddr()
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
588
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
589 if self.commentlist:
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
590 returnlist = [(' '.join(plist) + ' (' + \
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
591 ' '.join(self.commentlist) + ')', routeaddr)]
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
592 else: returnlist = [(' '.join(plist), routeaddr)]
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
593
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
594 else:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
595 if plist:
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
596 returnlist = [(' '.join(self.commentlist), plist[0])]
9889
9f13d7380bf8 Message.getaddrlist(): This now handles multiple occurances of the
Barry Warsaw <barry@python.org>
parents: 9726
diff changeset
597 elif self.field[self.pos] in self.specials:
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
598 self.pos += 1
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
599
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
600 self.gotonext()
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
601 if self.pos < len(self.field) and self.field[self.pos] == ',':
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
602 self.pos += 1
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
603 return returnlist
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
604
6624
be07921d9713 New address parser by Ben Escoto replaces
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
605 def getrouteaddr(self):
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
606 """Parse a route address (Return-path value).
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
607
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
608 This method just skips all the route stuff and returns the addrspec.
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
609 """
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
610 if self.field[self.pos] != '<':
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
611 return
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
612
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
613 expectroute = 0
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
614 self.pos += 1
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
615 self.gotonext()
21499
88baa4c4eee1 SF patch #495358 (Artur Zaprzala): rfc822.AddressList and "<>" address
Guido van Rossum <guido@python.org>
parents: 20965
diff changeset
616 adlist = ""
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
617 while self.pos < len(self.field):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
618 if expectroute:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
619 self.getdomain()
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
620 expectroute = 0
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
621 elif self.field[self.pos] == '>':
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
622 self.pos += 1
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
623 break
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
624 elif self.field[self.pos] == '@':
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
625 self.pos += 1
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
626 expectroute = 1
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
627 elif self.field[self.pos] == ':':
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
628 self.pos += 1
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
629 else:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
630 adlist = self.getaddrspec()
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
631 self.pos += 1
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
632 break
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
633 self.gotonext()
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
634
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
635 return adlist
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
636
6624
be07921d9713 New address parser by Ben Escoto replaces
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
637 def getaddrspec(self):
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
638 """Parse an RFC 2822 addr-spec."""
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
639 aslist = []
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
640
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
641 self.gotonext()
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
642 while self.pos < len(self.field):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
643 if self.field[self.pos] == '.':
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
644 aslist.append('.')
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
645 self.pos += 1
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
646 elif self.field[self.pos] == '"':
10989
6cafa3d4e229 Barry Scott writes:
Guido van Rossum <guido@python.org>
parents: 10980
diff changeset
647 aslist.append('"%s"' % self.getquote())
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
648 elif self.field[self.pos] in self.atomends:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
649 break
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
650 else: aslist.append(self.getatom())
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
651 self.gotonext()
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
652
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
653 if self.pos >= len(self.field) or self.field[self.pos] != '@':
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
654 return ''.join(aslist)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
655
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
656 aslist.append('@')
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
657 self.pos += 1
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
658 self.gotonext()
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
659 return ''.join(aslist) + self.getdomain()
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
660
6624
be07921d9713 New address parser by Ben Escoto replaces
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
661 def getdomain(self):
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
662 """Get the complete domain name from an address."""
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
663 sdlist = []
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
664 while self.pos < len(self.field):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
665 if self.field[self.pos] in self.LWS:
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
666 self.pos += 1
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
667 elif self.field[self.pos] == '(':
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
668 self.commentlist.append(self.getcomment())
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
669 elif self.field[self.pos] == '[':
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
670 sdlist.append(self.getdomainliteral())
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
671 elif self.field[self.pos] == '.':
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
672 self.pos += 1
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
673 sdlist.append('.')
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
674 elif self.field[self.pos] in self.atomends:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
675 break
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
676 else: sdlist.append(self.getatom())
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
677 return ''.join(sdlist)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
678
6624
be07921d9713 New address parser by Ben Escoto replaces
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
679 def getdelimited(self, beginchar, endchars, allowcomments = 1):
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
680 """Parse a header fragment delimited by special characters.
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
681
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
682 `beginchar' is the start character for the fragment. If self is not
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
683 looking at an instance of `beginchar' then getdelimited returns the
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
684 empty string.
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
685
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
686 `endchars' is a sequence of allowable end-delimiting characters.
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
687 Parsing stops when one of these is encountered.
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
688
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
689 If `allowcomments' is non-zero, embedded RFC 2822 comments are allowed
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
690 within the parsed fragment.
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
691 """
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
692 if self.field[self.pos] != beginchar:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
693 return ''
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
694
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
695 slist = ['']
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
696 quote = 0
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
697 self.pos += 1
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
698 while self.pos < len(self.field):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
699 if quote == 1:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
700 slist.append(self.field[self.pos])
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
701 quote = 0
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
702 elif self.field[self.pos] in endchars:
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
703 self.pos += 1
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
704 break
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
705 elif allowcomments and self.field[self.pos] == '(':
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
706 slist.append(self.getcomment())
38063
f62bd5118c78 Port forward from 2.4 branch:
Barry Warsaw <barry@python.org>
parents: 34569
diff changeset
707 continue # have already advanced pos from getcomment
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
708 elif self.field[self.pos] == '\\':
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
709 quote = 1
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
710 else:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
711 slist.append(self.field[self.pos])
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
712 self.pos += 1
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
713
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
714 return ''.join(slist)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
715
6624
be07921d9713 New address parser by Ben Escoto replaces
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
716 def getquote(self):
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
717 """Get a quote-delimited fragment from self's field."""
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
718 return self.getdelimited('"', '"\r', 0)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
719
6624
be07921d9713 New address parser by Ben Escoto replaces
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
720 def getcomment(self):
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
721 """Get a parenthesis-delimited fragment from self's field."""
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
722 return self.getdelimited('(', ')\r', 1)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
723
6624
be07921d9713 New address parser by Ben Escoto replaces
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
724 def getdomainliteral(self):
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
725 """Parse an RFC 2822 domain-literal."""
14842
460ab19a3289 AddrlistClass.getdomainliteral(): rfc822 requires that the domain
Barry Warsaw <barry@python.org>
parents: 14164
diff changeset
726 return '[%s]' % self.getdelimited('[', ']\r', 0)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
727
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
728 def getatom(self, atomends=None):
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
729 """Parse an RFC 2822 atom.
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
730
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
731 Optional atomends specifies a different set of end token delimiters
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
732 (the default is to use self.atomends). This is used e.g. in
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
733 getphraselist() since phrase endings must not include the `.' (which
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
734 is legal in phrases)."""
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
735 atomlist = ['']
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
736 if atomends is None:
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
737 atomends = self.atomends
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
738
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
739 while self.pos < len(self.field):
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
740 if self.field[self.pos] in atomends:
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
741 break
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
742 else: atomlist.append(self.field[self.pos])
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
743 self.pos += 1
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
744
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
745 return ''.join(atomlist)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
746
6624
be07921d9713 New address parser by Ben Escoto replaces
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
747 def getphraselist(self):
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
748 """Parse a sequence of RFC 2822 phrases.
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
749
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
750 A phrase is a sequence of words, which are in turn either RFC 2822
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
751 atoms or quoted-strings. Phrases are canonicalized by squeezing all
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
752 runs of continuous whitespace into one space.
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
753 """
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
754 plist = []
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
755
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
756 while self.pos < len(self.field):
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
757 if self.field[self.pos] in self.LWS:
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
758 self.pos += 1
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
759 elif self.field[self.pos] == '"':
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
760 plist.append(self.getquote())
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
761 elif self.field[self.pos] == '(':
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
762 self.commentlist.append(self.getcomment())
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
763 elif self.field[self.pos] in self.phraseends:
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
764 break
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
765 else:
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
766 plist.append(self.getatom(self.phraseends))
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
767
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
768 return plist
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
769
8512
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
770 class AddressList(AddrlistClass):
18638
e53bcbc3165e Fix address parsing to be RFC 2822 conformant. Specifically, dots are
Barry Warsaw <barry@python.org>
parents: 18381
diff changeset
771 """An AddressList encapsulates a list of parsed RFC 2822 addresses."""
8512
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
772 def __init__(self, field):
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
773 AddrlistClass.__init__(self, field)
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
774 if field:
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
775 self.addresslist = self.getaddrlist()
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
776 else:
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
777 self.addresslist = []
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
778
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
779 def __len__(self):
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
780 return len(self.addresslist)
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
781
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
782 def __str__(self):
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
783 return ", ".join(map(dump_address_pair, self.addresslist))
8512
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
784
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
785 def __add__(self, other):
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
786 # Set union
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
787 newaddr = AddressList(None)
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
788 newaddr.addresslist = self.addresslist[:]
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
789 for x in other.addresslist:
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
790 if not x in self.addresslist:
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
791 newaddr.addresslist.append(x)
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
792 return newaddr
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
793
14164
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
794 def __iadd__(self, other):
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
795 # Set union, in-place
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
796 for x in other.addresslist:
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
797 if not x in self.addresslist:
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
798 self.addresslist.append(x)
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
799 return self
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
800
8512
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
801 def __sub__(self, other):
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
802 # Set difference
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
803 newaddr = AddressList(None)
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
804 for x in self.addresslist:
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
805 if not x in other.addresslist:
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
806 newaddr.addresslist.append(x)
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
807 return newaddr
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
808
14164
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
809 def __isub__(self, other):
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
810 # Set difference, in-place
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
811 for x in other.addresslist:
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
812 if x in self.addresslist:
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
813 self.addresslist.remove(x)
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
814 return self
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
815
8514
1ef40d5acce2 Add __getitem__ to AddressList object, to make it a sequence.
Guido van Rossum <guido@python.org>
parents: 8512
diff changeset
816 def __getitem__(self, index):
1ef40d5acce2 Add __getitem__ to AddressList object, to make it a sequence.
Guido van Rossum <guido@python.org>
parents: 8512
diff changeset
817 # Make indexing, slices, and 'in' work
11254
0d73ce6d6226 Correct typo in AddressList.__getitem__. By Moshe Zadka.
Guido van Rossum <guido@python.org>
parents: 11125
diff changeset
818 return self.addresslist[index]
8514
1ef40d5acce2 Add __getitem__ to AddressList object, to make it a sequence.
Guido van Rossum <guido@python.org>
parents: 8512
diff changeset
819
8512
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
820 def dump_address_pair(pair):
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
821 """Dump a (name, address) pair in a canonicalized form."""
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
822 if pair[0]:
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
823 return '"' + pair[0] + '" <' + pair[1] + '>'
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
824 else:
4ded4018b0eb Some extra comments and docstrings, and a new class (AddressList), all by ESR.
Guido van Rossum <guido@python.org>
parents: 8484
diff changeset
825 return pair[1]
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
826
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
827 # Parse a date field
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
828
9726
4336a190505b Patch by Mike Meyer:
Guido van Rossum <guido@python.org>
parents: 9724
diff changeset
829 _monthnames = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul',
4336a190505b Patch by Mike Meyer:
Guido van Rossum <guido@python.org>
parents: 9724
diff changeset
830 'aug', 'sep', 'oct', 'nov', 'dec',
11742
840e18eec07f Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents: 11657
diff changeset
831 'january', 'february', 'march', 'april', 'may', 'june', 'july',
9726
4336a190505b Patch by Mike Meyer:
Guido van Rossum <guido@python.org>
parents: 9724
diff changeset
832 'august', 'september', 'october', 'november', 'december']
4336a190505b Patch by Mike Meyer:
Guido van Rossum <guido@python.org>
parents: 9724
diff changeset
833 _daynames = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
834
4559
adbcc0ab9eb6 Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents: 3824
diff changeset
835 # 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
836 # 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
837 # 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
838 # 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
839 # 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
840
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
841 _timezones = {'UT':0, 'UTC':0, 'GMT':0, 'Z':0,
8365
5f83f97aaeb8 Neatify the _timezones table and remove a misleading comment about
Guido van Rossum <guido@python.org>
parents: 7799
diff changeset
842 'AST': -400, 'ADT': -300, # Atlantic (used in Canada)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7515
diff changeset
843 'EST': -500, 'EDT': -400, # Eastern
8365
5f83f97aaeb8 Neatify the _timezones table and remove a misleading comment about
Guido van Rossum <guido@python.org>
parents: 7799
diff changeset
844 'CST': -600, 'CDT': -500, # Central
5f83f97aaeb8 Neatify the _timezones table and remove a misleading comment about
Guido van Rossum <guido@python.org>
parents: 7799
diff changeset
845 'MST': -700, 'MDT': -600, # Mountain
5f83f97aaeb8 Neatify the _timezones table and remove a misleading comment about
Guido van Rossum <guido@python.org>
parents: 7799
diff changeset
846 'PST': -800, 'PDT': -700 # Pacific
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
847 }
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
848
4559
adbcc0ab9eb6 Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents: 3824
diff changeset
849
adbcc0ab9eb6 Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents: 3824
diff changeset
850 def parsedate_tz(data):
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
851 """Convert a date string to a time tuple.
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
852
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
853 Accounts for military timezones.
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
854 """
20961
300b68d63b9f parsedate_tz(): If data is false, return None. Fixes bug #478115,
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
855 if not data:
300b68d63b9f parsedate_tz(): If data is false, return None. Fixes bug #478115,
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
856 return None
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
857 data = data.split()
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
858 if data[0][-1] in (',', '.') or data[0].lower() in _daynames:
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
859 # There's a dayname here. Skip it
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
860 del data[0]
40694
d7be03ebf466 Bug #1249573: fix rfc822.parsedate not accepting a certain date format
Georg Brandl <georg@python.org>
parents: 38063
diff changeset
861 else:
d7be03ebf466 Bug #1249573: fix rfc822.parsedate not accepting a certain date format
Georg Brandl <georg@python.org>
parents: 38063
diff changeset
862 # no space after the "weekday,"?
d7be03ebf466 Bug #1249573: fix rfc822.parsedate not accepting a certain date format
Georg Brandl <georg@python.org>
parents: 38063
diff changeset
863 i = data[0].rfind(',')
d7be03ebf466 Bug #1249573: fix rfc822.parsedate not accepting a certain date format
Georg Brandl <georg@python.org>
parents: 38063
diff changeset
864 if i >= 0:
d7be03ebf466 Bug #1249573: fix rfc822.parsedate not accepting a certain date format
Georg Brandl <georg@python.org>
parents: 38063
diff changeset
865 data[0] = data[0][i+1:]
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
866 if len(data) == 3: # RFC 850 date, deprecated
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
867 stuff = data[0].split('-')
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
868 if len(stuff) == 3:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
869 data = stuff + data[1:]
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
870 if len(data) == 4:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
871 s = data[3]
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
872 i = s.find('+')
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
873 if i > 0:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
874 data[3:] = [s[:i], s[i+1:]]
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
875 else:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
876 data.append('') # Dummy tz
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
877 if len(data) < 5:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
878 return None
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
879 data = data[:5]
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
880 [dd, mm, yy, tm, tz] = data
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
881 mm = mm.lower()
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
882 if not mm in _monthnames:
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
883 dd, mm = mm, dd.lower()
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
884 if not mm in _monthnames:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
885 return None
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
886 mm = _monthnames.index(mm)+1
10788
335b4c02f10d Mike Meyer reports a bug in his patch (several months ago) that
Guido van Rossum <guido@python.org>
parents: 10782
diff changeset
887 if mm > 12: mm = mm - 12
9726
4336a190505b Patch by Mike Meyer:
Guido van Rossum <guido@python.org>
parents: 9724
diff changeset
888 if dd[-1] == ',':
11742
840e18eec07f Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents: 11657
diff changeset
889 dd = dd[:-1]
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
890 i = yy.find(':')
9726
4336a190505b Patch by Mike Meyer:
Guido van Rossum <guido@python.org>
parents: 9724
diff changeset
891 if i > 0:
11742
840e18eec07f Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents: 11657
diff changeset
892 yy, tm = tm, yy
9726
4336a190505b Patch by Mike Meyer:
Guido van Rossum <guido@python.org>
parents: 9724
diff changeset
893 if yy[-1] == ',':
11742
840e18eec07f Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents: 11657
diff changeset
894 yy = yy[:-1]
15693
280a78ed0035 Duh. Instead of string.whitespace and string.digits, use isspace()
Guido van Rossum <guido@python.org>
parents: 15583
diff changeset
895 if not yy[0].isdigit():
11742
840e18eec07f Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents: 11657
diff changeset
896 yy, tz = tz, yy
9726
4336a190505b Patch by Mike Meyer:
Guido van Rossum <guido@python.org>
parents: 9724
diff changeset
897 if tm[-1] == ',':
11742
840e18eec07f Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents: 11657
diff changeset
898 tm = tm[:-1]
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
899 tm = tm.split(':')
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
900 if len(tm) == 2:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
901 [thh, tmm] = tm
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
902 tss = '0'
9724
56f38bf3af9f Avoid crash in parsedate_tz() on certain invalid dates -- when the
Guido van Rossum <guido@python.org>
parents: 8694
diff changeset
903 elif len(tm) == 3:
56f38bf3af9f Avoid crash in parsedate_tz() on certain invalid dates -- when the
Guido van Rossum <guido@python.org>
parents: 8694
diff changeset
904 [thh, tmm, tss] = tm
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
905 else:
9724
56f38bf3af9f Avoid crash in parsedate_tz() on certain invalid dates -- when the
Guido van Rossum <guido@python.org>
parents: 8694
diff changeset
906 return None
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
907 try:
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
908 yy = int(yy)
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
909 dd = int(dd)
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
910 thh = int(thh)
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
911 tmm = int(tmm)
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
912 tss = int(tss)
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
913 except ValueError:
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
914 return None
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
915 tzoffset = None
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
916 tz = tz.upper()
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23519
diff changeset
917 if tz in _timezones:
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
918 tzoffset = _timezones[tz]
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
919 else:
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
920 try:
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
921 tzoffset = int(tz)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15693
diff changeset
922 except ValueError:
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
923 pass
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
924 # Convert a timezone offset into seconds ; -0500 -> -18000
7377
f78af5bc78ed Feature added by Bill van Melle: when no timezone is present, assume
Guido van Rossum <guido@python.org>
parents: 7349
diff changeset
925 if tzoffset:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7515
diff changeset
926 if tzoffset < 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7515
diff changeset
927 tzsign = -1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7515
diff changeset
928 tzoffset = -tzoffset
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7515
diff changeset
929 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7515
diff changeset
930 tzsign = 1
19694
3716b116fb31 The first batch of changes recommended by the fixdiv tool. These are
Guido van Rossum <guido@python.org>
parents: 19679
diff changeset
931 tzoffset = tzsign * ( (tzoffset//100)*3600 + (tzoffset % 100)*60)
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
932 return (yy, mm, dd, thh, tmm, tss, 0, 1, 0, tzoffset)
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
933
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
934
4559
adbcc0ab9eb6 Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents: 3824
diff changeset
935 def parsedate(data):
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
936 """Convert a time string to a time tuple."""
15583
b8f5b7267f14 Get rid of string functions. References to string.whitespace,
Guido van Rossum <guido@python.org>
parents: 15394
diff changeset
937 t = parsedate_tz(data)
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
938 if t is None:
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
939 return t
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
940 return t[:9]
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
941
4559
adbcc0ab9eb6 Added support for timezone in date field. getdate_tz() and
Guido van Rossum <guido@python.org>
parents: 3824
diff changeset
942
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
943 def mktime_tz(data):
8365
5f83f97aaeb8 Neatify the _timezones table and remove a misleading comment about
Guido van Rossum <guido@python.org>
parents: 7799
diff changeset
944 """Turn a 10-tuple as returned by parsedate_tz() into a UTC timestamp."""
7377
f78af5bc78ed Feature added by Bill van Melle: when no timezone is present, assume
Guido van Rossum <guido@python.org>
parents: 7349
diff changeset
945 if data[9] is None:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7515
diff changeset
946 # No zone info, so localtime is better assumption than GMT
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7515
diff changeset
947 return time.mktime(data[:8] + (-1,))
7377
f78af5bc78ed Feature added by Bill van Melle: when no timezone is present, assume
Guido van Rossum <guido@python.org>
parents: 7349
diff changeset
948 else:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7515
diff changeset
949 t = time.mktime(data[:8] + (0,))
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7515
diff changeset
950 return t - data[9] - time.timezone
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
951
10624
34fd25242731 Utility function that yields a properly formatted time string.
Guido van Rossum <guido@python.org>
parents: 9889
diff changeset
952 def formatdate(timeval=None):
34fd25242731 Utility function that yields a properly formatted time string.
Guido van Rossum <guido@python.org>
parents: 9889
diff changeset
953 """Returns time format preferred for Internet standards.
34fd25242731 Utility function that yields a properly formatted time string.
Guido van Rossum <guido@python.org>
parents: 9889
diff changeset
954
34fd25242731 Utility function that yields a properly formatted time string.
Guido van Rossum <guido@python.org>
parents: 9889
diff changeset
955 Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
19504
59931db572b2 Add content-type header to ftp URLs (SF patch #454553)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19216
diff changeset
956
59931db572b2 Add content-type header to ftp URLs (SF patch #454553)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19216
diff changeset
957 According to RFC 1123, day and month names must always be in
59931db572b2 Add content-type header to ftp URLs (SF patch #454553)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19216
diff changeset
958 English. If not for that, this code could use strftime(). It
59931db572b2 Add content-type header to ftp URLs (SF patch #454553)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19216
diff changeset
959 can't because strftime() honors the locale and could generated
59931db572b2 Add content-type header to ftp URLs (SF patch #454553)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19216
diff changeset
960 non-English names.
10624
34fd25242731 Utility function that yields a properly formatted time string.
Guido van Rossum <guido@python.org>
parents: 9889
diff changeset
961 """
34fd25242731 Utility function that yields a properly formatted time string.
Guido van Rossum <guido@python.org>
parents: 9889
diff changeset
962 if timeval is None:
34fd25242731 Utility function that yields a properly formatted time string.
Guido van Rossum <guido@python.org>
parents: 9889
diff changeset
963 timeval = time.time()
19504
59931db572b2 Add content-type header to ftp URLs (SF patch #454553)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19216
diff changeset
964 timeval = time.gmtime(timeval)
59931db572b2 Add content-type header to ftp URLs (SF patch #454553)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19216
diff changeset
965 return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
966 ("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")[timeval[6]],
19504
59931db572b2 Add content-type header to ftp URLs (SF patch #454553)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19216
diff changeset
967 timeval[2],
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
968 ("Jan", "Feb", "Mar", "Apr", "May", "Jun",
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
969 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")[timeval[1]-1],
19679
6958784b1cef Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 19504
diff changeset
970 timeval[0], timeval[3], timeval[4], timeval[5])
10624
34fd25242731 Utility function that yields a properly formatted time string.
Guido van Rossum <guido@python.org>
parents: 9889
diff changeset
971
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
972
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
973 # 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
974 # 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
975 # message in RFC-822 format.
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
976
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
977 if __name__ == '__main__':
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
978 import sys, os
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
979 file = os.path.join(os.environ['HOME'], 'Mail/inbox/1')
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
980 if sys.argv[1:]: file = sys.argv[1]
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
981 f = open(file, 'r')
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
982 m = Message(f)
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
983 print 'From:', m.getaddr('from')
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
984 print 'To:', m.getaddrlist('to')
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
985 print 'Subject:', m.getheader('subject')
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
986 print 'Date:', m.getheader('date')
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
987 date = m.getdate_tz('date')
11657
023ed2663074 Fix by Nick Russo in processing of timezone in test program; the
Guido van Rossum <guido@python.org>
parents: 11375
diff changeset
988 tz = date[-1]
023ed2663074 Fix by Nick Russo in processing of timezone in test program; the
Guido van Rossum <guido@python.org>
parents: 11375
diff changeset
989 date = time.localtime(mktime_tz(date))
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
990 if date:
11657
023ed2663074 Fix by Nick Russo in processing of timezone in test program; the
Guido van Rossum <guido@python.org>
parents: 11375
diff changeset
991 print 'ParsedDate:', time.asctime(date),
023ed2663074 Fix by Nick Russo in processing of timezone in test program; the
Guido van Rossum <guido@python.org>
parents: 11375
diff changeset
992 hhmmss = tz
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
993 hhmm, ss = divmod(hhmmss, 60)
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
994 hh, mm = divmod(hhmm, 60)
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
995 print "%+03d%02d" % (hh, mm),
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
996 if ss: print ".%02d" % ss,
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
997 print
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
998 else:
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
999 print 'ParsedDate:', None
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
1000 m.rewindbody()
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
1001 n = 0
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
1002 while f.readline():
34561
4040aa216b8a Wholistic code cleanup / modernization:
Raymond Hettinger <python@rcn.com>
parents: 33537
diff changeset
1003 n += 1
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
1004 print 'Lines:', n
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
1005 print '-'*70
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
1006 print 'len =', len(m)
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23519
diff changeset
1007 if 'Date' in m: print 'Date =', m['Date']
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23519
diff changeset
1008 if 'X-Nonsense' in m: pass
6815
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
1009 print 'keys =', m.keys()
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
1010 print 'values =', m.values()
6131d3f9af2d Doc strings and reformatting with 4 spaces bty Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6624
diff changeset
1011 print 'items =', m.items()