A code cleansing pass
This commit is contained in:
parent
faf5e4d48f
commit
5c8fef903d
|
@ -6,6 +6,17 @@ Lifted directly from rfc822.py. This should eventually be rewritten.
|
|||
"""
|
||||
|
||||
import time
|
||||
from types import TupleType
|
||||
|
||||
try:
|
||||
True, False
|
||||
except NameError:
|
||||
True = 1
|
||||
False = 0
|
||||
|
||||
SPACE = ' '
|
||||
EMPTYSTRING = ''
|
||||
COMMASPACE = ', '
|
||||
|
||||
# Parse a date field
|
||||
_monthnames = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul',
|
||||
|
@ -55,12 +66,13 @@ def parsedate_tz(data):
|
|||
data = data[:5]
|
||||
[dd, mm, yy, tm, tz] = data
|
||||
mm = mm.lower()
|
||||
if not mm in _monthnames:
|
||||
if mm not in _monthnames:
|
||||
dd, mm = mm, dd.lower()
|
||||
if not mm in _monthnames:
|
||||
if mm not in _monthnames:
|
||||
return None
|
||||
mm = _monthnames.index(mm)+1
|
||||
if mm > 12: mm = mm - 12
|
||||
mm = _monthnames.index(mm) + 1
|
||||
if mm > 12:
|
||||
mm -= 12
|
||||
if dd[-1] == ',':
|
||||
dd = dd[:-1]
|
||||
i = yy.find(':')
|
||||
|
@ -112,9 +124,10 @@ def parsedate_tz(data):
|
|||
def parsedate(data):
|
||||
"""Convert a time string to a time tuple."""
|
||||
t = parsedate_tz(data)
|
||||
if type(t) == type( () ):
|
||||
if isinstance(t, TupleType):
|
||||
return t[:9]
|
||||
else: return t
|
||||
else:
|
||||
return t
|
||||
|
||||
|
||||
def mktime_tz(data):
|
||||
|
@ -164,10 +177,11 @@ class AddrlistClass:
|
|||
"""Parse up to the start of the next address."""
|
||||
while self.pos < len(self.field):
|
||||
if self.field[self.pos] in self.LWS + '\n\r':
|
||||
self.pos = self.pos + 1
|
||||
self.pos += 1
|
||||
elif self.field[self.pos] == '(':
|
||||
self.commentlist.append(self.getcomment())
|
||||
else: break
|
||||
else:
|
||||
break
|
||||
|
||||
def getaddrlist(self):
|
||||
"""Parse all addresses.
|
||||
|
@ -175,7 +189,7 @@ class AddrlistClass:
|
|||
Returns a list containing all of the addresses.
|
||||
"""
|
||||
result = []
|
||||
while 1:
|
||||
while True:
|
||||
ad = self.getaddress()
|
||||
if ad:
|
||||
result += ad
|
||||
|
@ -198,7 +212,7 @@ class AddrlistClass:
|
|||
if self.pos >= len(self.field):
|
||||
# Bad email address technically, no domain.
|
||||
if plist:
|
||||
returnlist = [(' '.join(self.commentlist), plist[0])]
|
||||
returnlist = [(SPACE.join(self.commentlist), plist[0])]
|
||||
|
||||
elif self.field[self.pos] in '.@':
|
||||
# email address is just an addrspec
|
||||
|
@ -206,18 +220,18 @@ class AddrlistClass:
|
|||
self.pos = oldpos
|
||||
self.commentlist = oldcl
|
||||
addrspec = self.getaddrspec()
|
||||
returnlist = [(' '.join(self.commentlist), addrspec)]
|
||||
returnlist = [(SPACE.join(self.commentlist), addrspec)]
|
||||
|
||||
elif self.field[self.pos] == ':':
|
||||
# address is a group
|
||||
returnlist = []
|
||||
|
||||
fieldlen = len(self.field)
|
||||
self.pos = self.pos + 1
|
||||
self.pos += 1
|
||||
while self.pos < len(self.field):
|
||||
self.gotonext()
|
||||
if self.pos < fieldlen and self.field[self.pos] == ';':
|
||||
self.pos = self.pos + 1
|
||||
self.pos += 1
|
||||
break
|
||||
returnlist = returnlist + self.getaddress()
|
||||
|
||||
|
@ -226,19 +240,20 @@ class AddrlistClass:
|
|||
routeaddr = self.getrouteaddr()
|
||||
|
||||
if self.commentlist:
|
||||
returnlist = [(' '.join(plist) + ' (' + \
|
||||
' '.join(self.commentlist) + ')', routeaddr)]
|
||||
else: returnlist = [(' '.join(plist), routeaddr)]
|
||||
returnlist = [(SPACE.join(plist) + ' (' +
|
||||
' '.join(self.commentlist) + ')', routeaddr)]
|
||||
else:
|
||||
returnlist = [(SPACE.join(plist), routeaddr)]
|
||||
|
||||
else:
|
||||
if plist:
|
||||
returnlist = [(' '.join(self.commentlist), plist[0])]
|
||||
returnlist = [(SPACE.join(self.commentlist), plist[0])]
|
||||
elif self.field[self.pos] in self.specials:
|
||||
self.pos = self.pos + 1
|
||||
self.pos += 1
|
||||
|
||||
self.gotonext()
|
||||
if self.pos < len(self.field) and self.field[self.pos] == ',':
|
||||
self.pos = self.pos + 1
|
||||
self.pos += 1
|
||||
return returnlist
|
||||
|
||||
def getrouteaddr(self):
|
||||
|
@ -249,25 +264,25 @@ class AddrlistClass:
|
|||
if self.field[self.pos] != '<':
|
||||
return
|
||||
|
||||
expectroute = 0
|
||||
self.pos = self.pos + 1
|
||||
expectroute = False
|
||||
self.pos += 1
|
||||
self.gotonext()
|
||||
adlist = ""
|
||||
adlist = ''
|
||||
while self.pos < len(self.field):
|
||||
if expectroute:
|
||||
self.getdomain()
|
||||
expectroute = 0
|
||||
expectroute = False
|
||||
elif self.field[self.pos] == '>':
|
||||
self.pos = self.pos + 1
|
||||
self.pos += 1
|
||||
break
|
||||
elif self.field[self.pos] == '@':
|
||||
self.pos = self.pos + 1
|
||||
expectroute = 1
|
||||
self.pos += 1
|
||||
expectroute = True
|
||||
elif self.field[self.pos] == ':':
|
||||
self.pos = self.pos + 1
|
||||
self.pos += 1
|
||||
else:
|
||||
adlist = self.getaddrspec()
|
||||
self.pos = self.pos + 1
|
||||
self.pos += 1
|
||||
break
|
||||
self.gotonext()
|
||||
|
||||
|
@ -281,41 +296,43 @@ class AddrlistClass:
|
|||
while self.pos < len(self.field):
|
||||
if self.field[self.pos] == '.':
|
||||
aslist.append('.')
|
||||
self.pos = self.pos + 1
|
||||
self.pos += 1
|
||||
elif self.field[self.pos] == '"':
|
||||
aslist.append('"%s"' % self.getquote())
|
||||
elif self.field[self.pos] in self.atomends:
|
||||
break
|
||||
else: aslist.append(self.getatom())
|
||||
else:
|
||||
aslist.append(self.getatom())
|
||||
self.gotonext()
|
||||
|
||||
if self.pos >= len(self.field) or self.field[self.pos] != '@':
|
||||
return ''.join(aslist)
|
||||
return EMPTYSTRING.join(aslist)
|
||||
|
||||
aslist.append('@')
|
||||
self.pos = self.pos + 1
|
||||
self.pos += 1
|
||||
self.gotonext()
|
||||
return ''.join(aslist) + self.getdomain()
|
||||
return EMPTYSTRING.join(aslist) + self.getdomain()
|
||||
|
||||
def getdomain(self):
|
||||
"""Get the complete domain name from an address."""
|
||||
sdlist = []
|
||||
while self.pos < len(self.field):
|
||||
if self.field[self.pos] in self.LWS:
|
||||
self.pos = self.pos + 1
|
||||
self.pos += 1
|
||||
elif self.field[self.pos] == '(':
|
||||
self.commentlist.append(self.getcomment())
|
||||
elif self.field[self.pos] == '[':
|
||||
sdlist.append(self.getdomainliteral())
|
||||
elif self.field[self.pos] == '.':
|
||||
self.pos = self.pos + 1
|
||||
self.pos += 1
|
||||
sdlist.append('.')
|
||||
elif self.field[self.pos] in self.atomends:
|
||||
break
|
||||
else: sdlist.append(self.getatom())
|
||||
return ''.join(sdlist)
|
||||
else:
|
||||
sdlist.append(self.getatom())
|
||||
return EMPTYSTRING.join(sdlist)
|
||||
|
||||
def getdelimited(self, beginchar, endchars, allowcomments = 1):
|
||||
def getdelimited(self, beginchar, endchars, allowcomments=True):
|
||||
"""Parse a header fragment delimited by special characters.
|
||||
|
||||
`beginchar' is the start character for the fragment.
|
||||
|
@ -332,36 +349,36 @@ class AddrlistClass:
|
|||
return ''
|
||||
|
||||
slist = ['']
|
||||
quote = 0
|
||||
self.pos = self.pos + 1
|
||||
quote = False
|
||||
self.pos += 1
|
||||
while self.pos < len(self.field):
|
||||
if quote == 1:
|
||||
if quote:
|
||||
slist.append(self.field[self.pos])
|
||||
quote = 0
|
||||
quote = False
|
||||
elif self.field[self.pos] in endchars:
|
||||
self.pos = self.pos + 1
|
||||
self.pos += 1
|
||||
break
|
||||
elif allowcomments and self.field[self.pos] == '(':
|
||||
slist.append(self.getcomment())
|
||||
elif self.field[self.pos] == '\\':
|
||||
quote = 1
|
||||
quote = True
|
||||
else:
|
||||
slist.append(self.field[self.pos])
|
||||
self.pos = self.pos + 1
|
||||
self.pos += 1
|
||||
|
||||
return ''.join(slist)
|
||||
return EMPTYSTRING.join(slist)
|
||||
|
||||
def getquote(self):
|
||||
"""Get a quote-delimited fragment from self's field."""
|
||||
return self.getdelimited('"', '"\r', 0)
|
||||
return self.getdelimited('"', '"\r', False)
|
||||
|
||||
def getcomment(self):
|
||||
"""Get a parenthesis-delimited fragment from self's field."""
|
||||
return self.getdelimited('(', ')\r', 1)
|
||||
return self.getdelimited('(', ')\r', True)
|
||||
|
||||
def getdomainliteral(self):
|
||||
"""Parse an RFC 2822 domain-literal."""
|
||||
return '[%s]' % self.getdelimited('[', ']\r', 0)
|
||||
return '[%s]' % self.getdelimited('[', ']\r', False)
|
||||
|
||||
def getatom(self, atomends=None):
|
||||
"""Parse an RFC 2822 atom.
|
||||
|
@ -377,10 +394,11 @@ class AddrlistClass:
|
|||
while self.pos < len(self.field):
|
||||
if self.field[self.pos] in atomends:
|
||||
break
|
||||
else: atomlist.append(self.field[self.pos])
|
||||
self.pos = self.pos + 1
|
||||
else:
|
||||
atomlist.append(self.field[self.pos])
|
||||
self.pos += 1
|
||||
|
||||
return ''.join(atomlist)
|
||||
return EMPTYSTRING.join(atomlist)
|
||||
|
||||
def getphraselist(self):
|
||||
"""Parse a sequence of RFC 2822 phrases.
|
||||
|
@ -393,14 +411,15 @@ class AddrlistClass:
|
|||
|
||||
while self.pos < len(self.field):
|
||||
if self.field[self.pos] in self.LWS:
|
||||
self.pos = self.pos + 1
|
||||
self.pos += 1
|
||||
elif self.field[self.pos] == '"':
|
||||
plist.append(self.getquote())
|
||||
elif self.field[self.pos] == '(':
|
||||
self.commentlist.append(self.getcomment())
|
||||
elif self.field[self.pos] in self.phraseends:
|
||||
break
|
||||
else: plist.append(self.getatom(self.phraseends))
|
||||
else:
|
||||
plist.append(self.getatom(self.phraseends))
|
||||
|
||||
return plist
|
||||
|
||||
|
@ -417,7 +436,7 @@ class AddressList(AddrlistClass):
|
|||
return len(self.addresslist)
|
||||
|
||||
def __str__(self):
|
||||
return ", ".join(map(dump_address_pair, self.addresslist))
|
||||
return COMMASPACE.join(map(dump_address_pair, self.addresslist))
|
||||
|
||||
def __add__(self, other):
|
||||
# Set union
|
||||
|
|
Loading…
Reference in New Issue