annotate Lib/mailbox.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 7e188140b618
children d4f541467b6d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4573
73758db2f464 /usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents: 4316
diff changeset
1 #! /usr/bin/env python
4316
772828c1b1f6 Revamped test case and updated doc string.
Guido van Rossum <guido@python.org>
parents: 3360
diff changeset
2
772828c1b1f6 Revamped test case and updated doc string.
Guido van Rossum <guido@python.org>
parents: 3360
diff changeset
3 """Classes to handle Unix style, MMDF style, and MH style mailboxes."""
772828c1b1f6 Revamped test case and updated doc string.
Guido van Rossum <guido@python.org>
parents: 3360
diff changeset
4
772828c1b1f6 Revamped test case and updated doc string.
Guido van Rossum <guido@python.org>
parents: 3360
diff changeset
5
1596
2197dd241d8d Jack's module for parsing UNIX mailbox files
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 import rfc822
3230
118c063df453 Added class MHMailbox
Jack Jansen <jack.jansen@cwi.nl>
parents: 1618
diff changeset
7 import os
1596
2197dd241d8d Jack's module for parsing UNIX mailbox files
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
22136
7e188140b618 backport bwarsaw's checkin of
Michael W. Hudson <mwh@python.net>
parents: 19893
diff changeset
9 __all__ = ["UnixMailbox","MmdfMailbox","MHMailbox","Maildir","BabylMailbox",
7e188140b618 backport bwarsaw's checkin of
Michael W. Hudson <mwh@python.net>
parents: 19893
diff changeset
10 "PortableUnixMailbox"]
16198
32755b456369 a few more modules get __all__
Skip Montanaro <skip@pobox.com>
parents: 15543
diff changeset
11
1596
2197dd241d8d Jack's module for parsing UNIX mailbox files
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 class _Mailbox:
16372
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
13 def __init__(self, fp, factory=rfc822.Message):
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
14 self.fp = fp
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
15 self.seekp = 0
16372
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
16 self.factory = factory
1596
2197dd241d8d Jack's module for parsing UNIX mailbox files
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
17660
30d7518e17e5 Make the Mailbox objects support iteration -- they already had the
Fred Drake <fdrake@acm.org>
parents: 17530
diff changeset
18 def __iter__(self):
19893
3af006503678 SF bug #461073: mailbox __iter__ bug, by Andrew Dalke.
Guido van Rossum <guido@python.org>
parents: 19228
diff changeset
19 return iter(self.next, None)
17660
30d7518e17e5 Make the Mailbox objects support iteration -- they already had the
Fred Drake <fdrake@acm.org>
parents: 17530
diff changeset
20
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
21 def next(self):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
22 while 1:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
23 self.fp.seek(self.seekp)
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
24 try:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
25 self._search_start()
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
26 except EOFError:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
27 self.seekp = self.fp.tell()
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
28 return None
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
29 start = self.fp.tell()
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
30 self._search_end()
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
31 self.seekp = stop = self.fp.tell()
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 15297
diff changeset
32 if start != stop:
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
33 break
16372
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
34 return self.factory(_Subfile(self.fp, start, stop))
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
35
1596
2197dd241d8d Jack's module for parsing UNIX mailbox files
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
2197dd241d8d Jack's module for parsing UNIX mailbox files
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 class _Subfile:
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
38 def __init__(self, fp, start, stop):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
39 self.fp = fp
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
40 self.start = start
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
41 self.stop = stop
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
42 self.pos = self.start
1596
2197dd241d8d Jack's module for parsing UNIX mailbox files
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
44 def read(self, length = None):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
45 if self.pos >= self.stop:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
46 return ''
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
47 remaining = self.stop - self.pos
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
48 if length is None or length < 0:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
49 length = remaining
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
50 elif length > remaining:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
51 length = remaining
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
52 self.fp.seek(self.pos)
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
53 data = self.fp.read(length)
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
54 self.pos = self.fp.tell()
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
55 return data
1596
2197dd241d8d Jack's module for parsing UNIX mailbox files
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
57 def readline(self, length = None):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
58 if self.pos >= self.stop:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
59 return ''
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
60 if length is None:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
61 length = self.stop - self.pos
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
62 self.fp.seek(self.pos)
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
63 data = self.fp.readline(length)
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
64 self.pos = self.fp.tell()
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
65 return data
1596
2197dd241d8d Jack's module for parsing UNIX mailbox files
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
67 def readlines(self, sizehint = -1):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
68 lines = []
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
69 while 1:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
70 line = self.readline()
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
71 if not line:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
72 break
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
73 lines.append(line)
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
74 if sizehint >= 0:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
75 sizehint = sizehint - len(line)
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
76 if sizehint <= 0:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
77 break
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
78 return lines
10452
c9e10ed26d9a Add readlines() to _Subfile class. Not clear who would need it, but
Guido van Rossum <guido@python.org>
parents: 9725
diff changeset
79
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
80 def tell(self):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
81 return self.pos - self.start
1596
2197dd241d8d Jack's module for parsing UNIX mailbox files
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
83 def seek(self, pos, whence=0):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
84 if whence == 0:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
85 self.pos = self.start + pos
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
86 elif whence == 1:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
87 self.pos = self.pos + pos
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
88 elif whence == 2:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
89 self.pos = self.stop + pos
1596
2197dd241d8d Jack's module for parsing UNIX mailbox files
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
91 def close(self):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
92 del self.fp
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
93
1596
2197dd241d8d Jack's module for parsing UNIX mailbox files
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
22136
7e188140b618 backport bwarsaw's checkin of
Michael W. Hudson <mwh@python.net>
parents: 19893
diff changeset
95 # Recommended to use PortableUnixMailbox instead!
1596
2197dd241d8d Jack's module for parsing UNIX mailbox files
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 class UnixMailbox(_Mailbox):
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
97 def _search_start(self):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
98 while 1:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
99 pos = self.fp.tell()
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
100 line = self.fp.readline()
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
101 if not line:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
102 raise EOFError
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
103 if line[:5] == 'From ' and self._isrealfromline(line):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
104 self.fp.seek(pos)
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
105 return
1596
2197dd241d8d Jack's module for parsing UNIX mailbox files
Guido van Rossum <guido@python.org>
parents:
diff changeset
106
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
107 def _search_end(self):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
108 self.fp.readline() # Throw away header line
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
109 while 1:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
110 pos = self.fp.tell()
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
111 line = self.fp.readline()
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
112 if not line:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
113 return
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
114 if line[:5] == 'From ' and self._isrealfromline(line):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
115 self.fp.seek(pos)
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
116 return
1596
2197dd241d8d Jack's module for parsing UNIX mailbox files
Guido van Rossum <guido@python.org>
parents:
diff changeset
117
16372
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
118 # An overridable mechanism to test for From-line-ness. You can either
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
119 # specify a different regular expression or define a whole new
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
120 # _isrealfromline() method. Note that this only gets called for lines
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
121 # starting with the 5 characters "From ".
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
122 #
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
123 # BAW: According to
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
124 #http://home.netscape.com/eng/mozilla/2.0/relnotes/demo/content-length.html
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
125 # the only portable, reliable way to find message delimiters in a BSD (i.e
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
126 # Unix mailbox) style folder is to search for "\n\nFrom .*\n", or at the
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
127 # beginning of the file, "^From .*\n". While _fromlinepattern below seems
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
128 # like a good idea, in practice, there are too many variations for more
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
129 # strict parsing of the line to be completely accurate.
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
130 #
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
131 # _strict_isrealfromline() is the old version which tries to do stricter
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
132 # parsing of the From_ line. _portable_isrealfromline() simply returns
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
133 # true, since it's never called if the line doesn't already start with
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
134 # "From ".
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
135 #
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
136 # This algorithm, and the way it interacts with _search_start() and
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
137 # _search_end() may not be completely correct, because it doesn't check
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
138 # that the two characters preceding "From " are \n\n or the beginning of
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
139 # the file. Fixing this would require a more extensive rewrite than is
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
140 # necessary. For convenience, we've added a StrictUnixMailbox class which
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
141 # uses the older, more strict _fromlinepattern regular expression.
7838
fc950433d158 UnixMailbox: don't be fooled by lines that begin with "From " but
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
142
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
143 _fromlinepattern = r"From \s*[^\s]+\s+\w\w\w\s+\w\w\w\s+\d?\d\s+" \
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
144 r"\d?\d:\d\d(:\d\d)?(\s+[^\s]+)?\s+\d\d\d\d\s*$"
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
145 _regexp = None
7838
fc950433d158 UnixMailbox: don't be fooled by lines that begin with "From " but
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
146
16372
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
147 def _strict_isrealfromline(self, line):
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
148 if not self._regexp:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
149 import re
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
150 self._regexp = re.compile(self._fromlinepattern)
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
151 return self._regexp.match(line)
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
152
16372
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
153 def _portable_isrealfromline(self, line):
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
154 return 1
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
155
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
156 _isrealfromline = _strict_isrealfromline
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
157
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
158
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
159 class PortableUnixMailbox(UnixMailbox):
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
160 _isrealfromline = UnixMailbox._portable_isrealfromline
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
161
7838
fc950433d158 UnixMailbox: don't be fooled by lines that begin with "From " but
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
162
1596
2197dd241d8d Jack's module for parsing UNIX mailbox files
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 class MmdfMailbox(_Mailbox):
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
164 def _search_start(self):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
165 while 1:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
166 line = self.fp.readline()
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
167 if not line:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
168 raise EOFError
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
169 if line[:5] == '\001\001\001\001\n':
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
170 return
1596
2197dd241d8d Jack's module for parsing UNIX mailbox files
Guido van Rossum <guido@python.org>
parents:
diff changeset
171
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
172 def _search_end(self):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
173 while 1:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
174 pos = self.fp.tell()
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
175 line = self.fp.readline()
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
176 if not line:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
177 return
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
178 if line == '\001\001\001\001\n':
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
179 self.fp.seek(pos)
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
180 return
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
181
1596
2197dd241d8d Jack's module for parsing UNIX mailbox files
Guido van Rossum <guido@python.org>
parents:
diff changeset
182
3230
118c063df453 Added class MHMailbox
Jack Jansen <jack.jansen@cwi.nl>
parents: 1618
diff changeset
183 class MHMailbox:
16372
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
184 def __init__(self, dirname, factory=rfc822.Message):
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
185 import re
13911
6bf1281ec763 Improve MHMailbox: messages are now sorted in numerical order.
Guido van Rossum <guido@python.org>
parents: 13475
diff changeset
186 pat = re.compile('^[1-9][0-9]*$')
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
187 self.dirname = dirname
13916
bc0153a739ae Use built in function filter instead of doing it laboriously by hand.
Sjoerd Mullender <sjoerd@acm.org>
parents: 13911
diff changeset
188 # the three following lines could be combined into:
bc0153a739ae Use built in function filter instead of doing it laboriously by hand.
Sjoerd Mullender <sjoerd@acm.org>
parents: 13911
diff changeset
189 # list = map(long, filter(pat.match, os.listdir(self.dirname)))
bc0153a739ae Use built in function filter instead of doing it laboriously by hand.
Sjoerd Mullender <sjoerd@acm.org>
parents: 13911
diff changeset
190 list = os.listdir(self.dirname)
bc0153a739ae Use built in function filter instead of doing it laboriously by hand.
Sjoerd Mullender <sjoerd@acm.org>
parents: 13911
diff changeset
191 list = filter(pat.match, list)
13911
6bf1281ec763 Improve MHMailbox: messages are now sorted in numerical order.
Guido van Rossum <guido@python.org>
parents: 13475
diff changeset
192 list = map(long, list)
6bf1281ec763 Improve MHMailbox: messages are now sorted in numerical order.
Guido van Rossum <guido@python.org>
parents: 13475
diff changeset
193 list.sort()
6bf1281ec763 Improve MHMailbox: messages are now sorted in numerical order.
Guido van Rossum <guido@python.org>
parents: 13475
diff changeset
194 # This only works in Python 1.6 or later;
6bf1281ec763 Improve MHMailbox: messages are now sorted in numerical order.
Guido van Rossum <guido@python.org>
parents: 13475
diff changeset
195 # before that str() added 'L':
6bf1281ec763 Improve MHMailbox: messages are now sorted in numerical order.
Guido van Rossum <guido@python.org>
parents: 13475
diff changeset
196 self.boxes = map(str, list)
16372
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
197 self.factory = factory
3230
118c063df453 Added class MHMailbox
Jack Jansen <jack.jansen@cwi.nl>
parents: 1618
diff changeset
198
17660
30d7518e17e5 Make the Mailbox objects support iteration -- they already had the
Fred Drake <fdrake@acm.org>
parents: 17530
diff changeset
199 def __iter__(self):
19893
3af006503678 SF bug #461073: mailbox __iter__ bug, by Andrew Dalke.
Guido van Rossum <guido@python.org>
parents: 19228
diff changeset
200 return iter(self.next, None)
17660
30d7518e17e5 Make the Mailbox objects support iteration -- they already had the
Fred Drake <fdrake@acm.org>
parents: 17530
diff changeset
201
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
202 def next(self):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
203 if not self.boxes:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
204 return None
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
205 fn = self.boxes[0]
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
206 del self.boxes[0]
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
207 fp = open(os.path.join(self.dirname, fn))
16372
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
208 return self.factory(fp)
7752
b4b0e4e759c5 A few lines were indented using spaces instead of tabs -- fix them.
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
209
9725
4b1b9432b26b Patch by Mike Meyer:
Guido van Rossum <guido@python.org>
parents: 8685
diff changeset
210
4b1b9432b26b Patch by Mike Meyer:
Guido van Rossum <guido@python.org>
parents: 8685
diff changeset
211 class Maildir:
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
212 # Qmail directory mailbox
9725
4b1b9432b26b Patch by Mike Meyer:
Guido van Rossum <guido@python.org>
parents: 8685
diff changeset
213
16372
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
214 def __init__(self, dirname, factory=rfc822.Message):
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
215 self.dirname = dirname
16372
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
216 self.factory = factory
9725
4b1b9432b26b Patch by Mike Meyer:
Guido van Rossum <guido@python.org>
parents: 8685
diff changeset
217
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
218 # check for new mail
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
219 newdir = os.path.join(self.dirname, 'new')
14771
133007646fb4 Maildir.__init__(): Use the correct filter for filenames, so that this
Fred Drake <fdrake@acm.org>
parents: 14548
diff changeset
220 boxes = [os.path.join(newdir, f)
133007646fb4 Maildir.__init__(): Use the correct filter for filenames, so that this
Fred Drake <fdrake@acm.org>
parents: 14548
diff changeset
221 for f in os.listdir(newdir) if f[0] != '.']
9725
4b1b9432b26b Patch by Mike Meyer:
Guido van Rossum <guido@python.org>
parents: 8685
diff changeset
222
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
223 # Now check for current mail in this maildir
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
224 curdir = os.path.join(self.dirname, 'cur')
14771
133007646fb4 Maildir.__init__(): Use the correct filter for filenames, so that this
Fred Drake <fdrake@acm.org>
parents: 14548
diff changeset
225 boxes += [os.path.join(curdir, f)
133007646fb4 Maildir.__init__(): Use the correct filter for filenames, so that this
Fred Drake <fdrake@acm.org>
parents: 14548
diff changeset
226 for f in os.listdir(curdir) if f[0] != '.']
9725
4b1b9432b26b Patch by Mike Meyer:
Guido van Rossum <guido@python.org>
parents: 8685
diff changeset
227
15297
f20deaeaf8b9 Maildir.__init__(): Make sure self.boxes is set.
Fred Drake <fdrake@acm.org>
parents: 14954
diff changeset
228 self.boxes = boxes
f20deaeaf8b9 Maildir.__init__(): Make sure self.boxes is set.
Fred Drake <fdrake@acm.org>
parents: 14954
diff changeset
229
17660
30d7518e17e5 Make the Mailbox objects support iteration -- they already had the
Fred Drake <fdrake@acm.org>
parents: 17530
diff changeset
230 def __iter__(self):
19893
3af006503678 SF bug #461073: mailbox __iter__ bug, by Andrew Dalke.
Guido van Rossum <guido@python.org>
parents: 19228
diff changeset
231 return iter(self.next, None)
17660
30d7518e17e5 Make the Mailbox objects support iteration -- they already had the
Fred Drake <fdrake@acm.org>
parents: 17530
diff changeset
232
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
233 def next(self):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
234 if not self.boxes:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
235 return None
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
236 fn = self.boxes[0]
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
237 del self.boxes[0]
14548
e9c313745302 Detlef Lannert <lannert@uni-duesseldorf.de>:
Fred Drake <fdrake@acm.org>
parents: 13916
diff changeset
238 fp = open(fn)
16372
670a0194a8da Two changes:
Barry Warsaw <barry@python.org>
parents: 16198
diff changeset
239 return self.factory(fp)
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
240
9725
4b1b9432b26b Patch by Mike Meyer:
Guido van Rossum <guido@python.org>
parents: 8685
diff changeset
241
5531
5454fe4925d3 Added BabylMailbox class by Fred Lundh (untested).
Guido van Rossum <guido@python.org>
parents: 5443
diff changeset
242 class BabylMailbox(_Mailbox):
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
243 def _search_start(self):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
244 while 1:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
245 line = self.fp.readline()
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
246 if not line:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
247 raise EOFError
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
248 if line == '*** EOOH ***\n':
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
249 return
5531
5454fe4925d3 Added BabylMailbox class by Fred Lundh (untested).
Guido van Rossum <guido@python.org>
parents: 5443
diff changeset
250
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
251 def _search_end(self):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
252 while 1:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
253 pos = self.fp.tell()
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
254 line = self.fp.readline()
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
255 if not line:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
256 return
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
257 if line == '\037\014\n':
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
258 self.fp.seek(pos)
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
259 return
5531
5454fe4925d3 Added BabylMailbox class by Fred Lundh (untested).
Guido van Rossum <guido@python.org>
parents: 5443
diff changeset
260
5454fe4925d3 Added BabylMailbox class by Fred Lundh (untested).
Guido van Rossum <guido@python.org>
parents: 5443
diff changeset
261
4316
772828c1b1f6 Revamped test case and updated doc string.
Guido van Rossum <guido@python.org>
parents: 3360
diff changeset
262 def _test():
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
263 import sys
3230
118c063df453 Added class MHMailbox
Jack Jansen <jack.jansen@cwi.nl>
parents: 1618
diff changeset
264
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
265 args = sys.argv[1:]
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
266 if not args:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
267 for key in 'MAILDIR', 'MAIL', 'LOGNAME', 'USER':
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
268 if os.environ.has_key(key):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
269 mbox = os.environ[key]
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
270 break
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
271 else:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
272 print "$MAIL, $LOGNAME nor $USER set -- who are you?"
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
273 return
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
274 else:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
275 mbox = args[0]
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
276 if mbox[:1] == '+':
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
277 mbox = os.environ['HOME'] + '/Mail/' + mbox[1:]
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
278 elif not '/' in mbox:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
279 mbox = '/usr/mail/' + mbox
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
280 if os.path.isdir(mbox):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
281 if os.path.isdir(os.path.join(mbox, 'cur')):
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
282 mb = Maildir(mbox)
11742
840e18eec07f Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents: 11571
diff changeset
283 else:
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
284 mb = MHMailbox(mbox)
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
285 else:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
286 fp = open(mbox, 'r')
22136
7e188140b618 backport bwarsaw's checkin of
Michael W. Hudson <mwh@python.net>
parents: 19893
diff changeset
287 mb = PortableUnixMailbox(fp)
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
288
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
289 msgs = []
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
290 while 1:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
291 msg = mb.next()
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
292 if msg is None:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
293 break
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
294 msgs.append(msg)
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
295 if len(args) <= 1:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
296 msg.fp = None
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
297 if len(args) > 1:
15297
f20deaeaf8b9 Maildir.__init__(): Make sure self.boxes is set.
Fred Drake <fdrake@acm.org>
parents: 14954
diff changeset
298 num = int(args[1])
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
299 print 'Message %d body:'%num
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
300 msg = msgs[num-1]
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
301 msg.rewindbody()
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
302 sys.stdout.write(msg.fp.read())
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
303 else:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
304 print 'Mailbox',mbox,'has',len(msgs),'messages:'
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
305 for msg in msgs:
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
306 f = msg.getheader('from') or ""
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
307 s = msg.getheader('subject') or ""
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
308 d = msg.getheader('date') or ""
14954
342497ad8fec Add missing "s" from format string.
Fred Drake <fdrake@acm.org>
parents: 14771
diff changeset
309 print '-%20.20s %20.20s %-30.30s'%(f, d[5:], s)
4316
772828c1b1f6 Revamped test case and updated doc string.
Guido van Rossum <guido@python.org>
parents: 3360
diff changeset
310
772828c1b1f6 Revamped test case and updated doc string.
Guido van Rossum <guido@python.org>
parents: 3360
diff changeset
311
772828c1b1f6 Revamped test case and updated doc string.
Guido van Rossum <guido@python.org>
parents: 3360
diff changeset
312 if __name__ == '__main__':
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 12227
diff changeset
313 _test()