Make tabnanny happy.

mailbox.py: Convert to 4-space indents.
This commit is contained in:
Fred Drake 2000-07-09 16:44:26 +00:00
parent c2844af87b
commit dbbf76bd5a
2 changed files with 235 additions and 235 deletions

View File

@ -7,273 +7,272 @@ import rfc822
import os import os
class _Mailbox: class _Mailbox:
def __init__(self, fp):
self.fp = fp
self.seekp = 0
def __init__(self, fp): def seek(self, pos, whence=0):
self.fp = fp if whence==1: # Relative to current position
self.seekp = 0 self.pos = self.pos + pos
if whence==2: # Relative to file's end
self.pos = self.stop + pos
else: # Default - absolute position
self.pos = self.start + pos
def seek(self, pos, whence=0): def next(self):
if whence==1: # Relative to current position while 1:
self.pos = self.pos + pos self.fp.seek(self.seekp)
if whence==2: # Relative to file's end try:
self.pos = self.stop + pos self._search_start()
else: # Default - absolute position except EOFError:
self.pos = self.start + pos self.seekp = self.fp.tell()
return None
start = self.fp.tell()
self._search_end()
self.seekp = stop = self.fp.tell()
if start <> stop:
break
return rfc822.Message(_Subfile(self.fp, start, stop))
def next(self):
while 1:
self.fp.seek(self.seekp)
try:
self._search_start()
except EOFError:
self.seekp = self.fp.tell()
return None
start = self.fp.tell()
self._search_end()
self.seekp = stop = self.fp.tell()
if start <> stop:
break
return rfc822.Message(_Subfile(self.fp, start, stop))
class _Subfile: class _Subfile:
def __init__(self, fp, start, stop):
self.fp = fp
self.start = start
self.stop = stop
self.pos = self.start
def __init__(self, fp, start, stop): def read(self, length = None):
self.fp = fp if self.pos >= self.stop:
self.start = start return ''
self.stop = stop remaining = self.stop - self.pos
self.pos = self.start if length is None or length < 0:
length = remaining
elif length > remaining:
length = remaining
self.fp.seek(self.pos)
data = self.fp.read(length)
self.pos = self.fp.tell()
return data
def read(self, length = None): def readline(self, length = None):
if self.pos >= self.stop: if self.pos >= self.stop:
return '' return ''
remaining = self.stop - self.pos if length is None:
if length is None or length < 0: length = self.stop - self.pos
length = remaining self.fp.seek(self.pos)
elif length > remaining: data = self.fp.readline(length)
length = remaining self.pos = self.fp.tell()
self.fp.seek(self.pos) return data
data = self.fp.read(length)
self.pos = self.fp.tell()
return data
def readline(self, length = None): def readlines(self, sizehint = -1):
if self.pos >= self.stop: lines = []
return '' while 1:
if length is None: line = self.readline()
length = self.stop - self.pos if not line:
self.fp.seek(self.pos) break
data = self.fp.readline(length) lines.append(line)
self.pos = self.fp.tell() if sizehint >= 0:
return data sizehint = sizehint - len(line)
if sizehint <= 0:
break
return lines
def readlines(self, sizehint = -1): def tell(self):
lines = [] return self.pos - self.start
while 1:
line = self.readline()
if not line:
break
lines.append(line)
if sizehint >= 0:
sizehint = sizehint - len(line)
if sizehint <= 0:
break
return lines
def tell(self): def seek(self, pos, whence=0):
return self.pos - self.start if whence == 0:
self.pos = self.start + pos
elif whence == 1:
self.pos = self.pos + pos
elif whence == 2:
self.pos = self.stop + pos
def seek(self, pos, whence=0): def close(self):
if whence == 0: del self.fp
self.pos = self.start + pos
elif whence == 1:
self.pos = self.pos + pos
elif whence == 2:
self.pos = self.stop + pos
def close(self):
del self.fp
class UnixMailbox(_Mailbox): class UnixMailbox(_Mailbox):
def _search_start(self):
while 1:
pos = self.fp.tell()
line = self.fp.readline()
if not line:
raise EOFError
if line[:5] == 'From ' and self._isrealfromline(line):
self.fp.seek(pos)
return
def _search_start(self): def _search_end(self):
while 1: self.fp.readline() # Throw away header line
pos = self.fp.tell() while 1:
line = self.fp.readline() pos = self.fp.tell()
if not line: line = self.fp.readline()
raise EOFError if not line:
if line[:5] == 'From ' and self._isrealfromline(line): return
self.fp.seek(pos) if line[:5] == 'From ' and self._isrealfromline(line):
return self.fp.seek(pos)
return
def _search_end(self): # An overridable mechanism to test for From-line-ness.
self.fp.readline() # Throw away header line # You can either specify a different regular expression
while 1: # or define a whole new _isrealfromline() method.
pos = self.fp.tell() # Note that this only gets called for lines starting with
line = self.fp.readline() # the 5 characters "From ".
if not line:
return
if line[:5] == 'From ' and self._isrealfromline(line):
self.fp.seek(pos)
return
# An overridable mechanism to test for From-line-ness. _fromlinepattern = r"From \s*[^\s]+\s+\w\w\w\s+\w\w\w\s+\d?\d\s+" \
# You can either specify a different regular expression r"\d?\d:\d\d(:\d\d)?(\s+[^\s]+)?\s+\d\d\d\d\s*$"
# or define a whole new _isrealfromline() method. _regexp = None
# Note that this only gets called for lines starting with
# the 5 characters "From ".
_fromlinepattern = r"From \s*[^\s]+\s+\w\w\w\s+\w\w\w\s+\d?\d\s+" \ def _isrealfromline(self, line):
r"\d?\d:\d\d(:\d\d)?(\s+[^\s]+)?\s+\d\d\d\d\s*$" if not self._regexp:
_regexp = None import re
self._regexp = re.compile(self._fromlinepattern)
return self._regexp.match(line)
def _isrealfromline(self, line):
if not self._regexp:
import re
self._regexp = re.compile(self._fromlinepattern)
return self._regexp.match(line)
class MmdfMailbox(_Mailbox): class MmdfMailbox(_Mailbox):
def _search_start(self):
while 1:
line = self.fp.readline()
if not line:
raise EOFError
if line[:5] == '\001\001\001\001\n':
return
def _search_start(self): def _search_end(self):
while 1: while 1:
line = self.fp.readline() pos = self.fp.tell()
if not line: line = self.fp.readline()
raise EOFError if not line:
if line[:5] == '\001\001\001\001\n': return
return if line == '\001\001\001\001\n':
self.fp.seek(pos)
return
def _search_end(self):
while 1:
pos = self.fp.tell()
line = self.fp.readline()
if not line:
return
if line == '\001\001\001\001\n':
self.fp.seek(pos)
return
class MHMailbox: class MHMailbox:
def __init__(self, dirname):
import re
pat = re.compile('^[0-9][0-9]*$')
self.dirname = dirname
files = os.listdir(self.dirname)
self.boxes = []
for f in files:
if pat.match(f):
self.boxes.append(f)
def __init__(self, dirname): def next(self):
import re if not self.boxes:
pat = re.compile('^[0-9][0-9]*$') return None
self.dirname = dirname fn = self.boxes[0]
files = os.listdir(self.dirname) del self.boxes[0]
self.boxes = [] fp = open(os.path.join(self.dirname, fn))
for f in files: return rfc822.Message(fp)
if pat.match(f):
self.boxes.append(f)
def next(self):
if not self.boxes:
return None
fn = self.boxes[0]
del self.boxes[0]
fp = open(os.path.join(self.dirname, fn))
return rfc822.Message(fp)
class Maildir: class Maildir:
# Qmail directory mailbox
# Qmail directory mailbox def __init__(self, dirname):
import string
self.dirname = dirname
self.boxes = []
def __init__(self, dirname): # check for new mail
import string newdir = os.path.join(self.dirname, 'new')
self.dirname = dirname for file in os.listdir(newdir):
self.boxes = [] if len(string.split(file, '.')) > 2:
self.boxes.append(os.path.join(newdir, file))
# check for new mail # Now check for current mail in this maildir
newdir = os.path.join(self.dirname, 'new') curdir = os.path.join(self.dirname, 'cur')
for file in os.listdir(newdir): for file in os.listdir(curdir):
if len(string.split(file, '.')) > 2: if len(string.split(file, '.')) > 2:
self.boxes.append(os.path.join(newdir, file)) self.boxes.append(os.path.join(curdir, file))
# Now check for current mail in this maildir def next(self):
curdir = os.path.join(self.dirname, 'cur') if not self.boxes:
for file in os.listdir(curdir): return None
if len(string.split(file, '.')) > 2: fn = self.boxes[0]
self.boxes.append(os.path.join(curdir, file)) del self.boxes[0]
fp = open(os.path.join(self.dirname, fn))
return rfc822.Message(fp)
def next(self):
if not self.boxes:
return None
fn = self.boxes[0]
del self.boxes[0]
fp = open(os.path.join(self.dirname, fn))
return rfc822.Message(fp)
class BabylMailbox(_Mailbox): class BabylMailbox(_Mailbox):
def _search_start(self):
while 1:
line = self.fp.readline()
if not line:
raise EOFError
if line == '*** EOOH ***\n':
return
def _search_start(self): def _search_end(self):
while 1: while 1:
line = self.fp.readline() pos = self.fp.tell()
if not line: line = self.fp.readline()
raise EOFError if not line:
if line == '*** EOOH ***\n': return
return if line == '\037\014\n':
self.fp.seek(pos)
def _search_end(self): return
while 1:
pos = self.fp.tell()
line = self.fp.readline()
if not line:
return
if line == '\037\014\n':
self.fp.seek(pos)
return
def _test(): def _test():
import time import time
import sys import sys
import string import string
import os import os
args = sys.argv[1:] args = sys.argv[1:]
if not args: if not args:
for key in 'MAILDIR', 'MAIL', 'LOGNAME', 'USER': for key in 'MAILDIR', 'MAIL', 'LOGNAME', 'USER':
if os.environ.has_key(key): if os.environ.has_key(key):
mbox = os.environ[key] mbox = os.environ[key]
break break
else:
print "$MAIL, $LOGNAME nor $USER set -- who are you?"
return
else: else:
mbox = args[0] print "$MAIL, $LOGNAME nor $USER set -- who are you?"
if mbox[:1] == '+': return
mbox = os.environ['HOME'] + '/Mail/' + mbox[1:] else:
elif not '/' in mbox: mbox = args[0]
mbox = '/usr/mail/' + mbox if mbox[:1] == '+':
if os.path.isdir(mbox): mbox = os.environ['HOME'] + '/Mail/' + mbox[1:]
if os.path.isdir(os.path.join(mbox, 'cur')): elif not '/' in mbox:
mb = Maildir(mbox) mbox = '/usr/mail/' + mbox
else: if os.path.isdir(mbox):
mb = MHMailbox(mbox) if os.path.isdir(os.path.join(mbox, 'cur')):
mb = Maildir(mbox)
else: else:
fp = open(mbox, 'r') mb = MHMailbox(mbox)
mb = UnixMailbox(fp) else:
fp = open(mbox, 'r')
msgs = [] mb = UnixMailbox(fp)
while 1:
msg = mb.next() msgs = []
if msg is None: while 1:
break msg = mb.next()
msgs.append(msg) if msg is None:
if len(args) <= 1: break
msg.fp = None msgs.append(msg)
if len(args) > 1: if len(args) <= 1:
num = string.atoi(args[1]) msg.fp = None
print 'Message %d body:'%num if len(args) > 1:
msg = msgs[num-1] num = string.atoi(args[1])
msg.rewindbody() print 'Message %d body:'%num
sys.stdout.write(msg.fp.read()) msg = msgs[num-1]
else: msg.rewindbody()
print 'Mailbox',mbox,'has',len(msgs),'messages:' sys.stdout.write(msg.fp.read())
for msg in msgs: else:
f = msg.getheader('from') or "" print 'Mailbox',mbox,'has',len(msgs),'messages:'
s = msg.getheader('subject') or "" for msg in msgs:
d = msg.getheader('date') or "" f = msg.getheader('from') or ""
print '%20.20s %18.18s %-30.30s'%(f, d[5:], s) s = msg.getheader('subject') or ""
d = msg.getheader('date') or ""
print '%20.20s %18.18s %-30.30s'%(f, d[5:], s)
if __name__ == '__main__': if __name__ == '__main__':
_test() _test()

View File

@ -17,7 +17,8 @@ class shlex:
self.instream = sys.stdin self.instream = sys.stdin
self.infile = None self.infile = None
self.commenters = '#' self.commenters = '#'
self.wordchars = 'abcdfeghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_' self.wordchars = ('abcdfeghijklmnopqrstuvwxyz'
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_')
self.whitespace = ' \t\r\n' self.whitespace = ' \t\r\n'
self.quotes = '\'"' self.quotes = '\'"'
self.state = ' ' self.state = ' '
@ -25,7 +26,7 @@ class shlex:
self.lineno = 1 self.lineno = 1
self.debug = 0 self.debug = 0
self.token = '' self.token = ''
self.filestack = [] self.filestack = []
self.source = None self.source = None
if self.debug: if self.debug:
print 'shlex: reading from %s, line %d' \ print 'shlex: reading from %s, line %d' \
@ -45,7 +46,7 @@ class shlex:
if self.debug >= 1: if self.debug >= 1:
print "shlex: popping token " + `tok` print "shlex: popping token " + `tok`
return tok return tok
# No pushback. Get a token. # No pushback. Get a token.
raw = self.read_token() raw = self.read_token()
# Handle inclusions # Handle inclusions
while raw == self.source: while raw == self.source:
@ -88,18 +89,18 @@ class shlex:
if self.debug >= 3: if self.debug >= 3:
print "shlex: in state", repr(self.state), \ print "shlex: in state", repr(self.state), \
"I see character:", repr(nextchar) "I see character:", repr(nextchar)
if self.state == None: if self.state is None:
self.token = ''; # past end of file self.token = ''; # past end of file
break break
elif self.state == ' ': elif self.state == ' ':
if not nextchar: if not nextchar:
self.state = None; # end of file self.state = None; # end of file
break break
elif nextchar in self.whitespace: elif nextchar in self.whitespace:
if self.debug >= 2: if self.debug >= 2:
print "shlex: I see whitespace in whitespace state" print "shlex: I see whitespace in whitespace state"
if self.token: if self.token:
break # emit current token break # emit current token
else: else:
continue continue
elif nextchar in self.commenters: elif nextchar in self.commenters:
@ -114,7 +115,7 @@ class shlex:
else: else:
self.token = nextchar self.token = nextchar
if self.token: if self.token:
break # emit current token break # emit current token
else: else:
continue continue
elif self.state in self.quotes: elif self.state in self.quotes:
@ -124,14 +125,14 @@ class shlex:
break break
elif self.state == 'a': elif self.state == 'a':
if not nextchar: if not nextchar:
self.state = None; # end of file self.state = None; # end of file
break break
elif nextchar in self.whitespace: elif nextchar in self.whitespace:
if self.debug >= 2: if self.debug >= 2:
print "shlex: I see whitespace in word state" print "shlex: I see whitespace in word state"
self.state = ' ' self.state = ' '
if self.token: if self.token:
break # emit current token break # emit current token
else: else:
continue continue
elif nextchar in self.commenters: elif nextchar in self.commenters:
@ -145,7 +146,7 @@ class shlex:
print "shlex: I see punctuation in word state" print "shlex: I see punctuation in word state"
self.state = ' ' self.state = ' '
if self.token: if self.token:
break # emit current token break # emit current token
else: else:
continue continue
result = self.token result = self.token