Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
email._parseaddr.AddrlistClass Class Reference
Inheritance diagram for email._parseaddr.AddrlistClass:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def __init__
def gotonext
def getaddrlist
def getaddress
def getrouteaddr
def getaddrspec
def getdomain
def getdelimited
def getquote
def getcomment
def getdomainliteral
def getatom
def getphraselist

Public Attributes

 specials
 pos
 LWS
 CR
 FWS
 atomends
 phraseends
 field
 commentlist

Detailed Description

Address parser class by Ben Escoto.

To understand what this class does, it helps to have a copy of RFC 2822 in
front of you.

Note: this class interface is deprecated and may be removed in the future.
Use rfc822.AddressList instead.

Definition at line 174 of file _parseaddr.py.


Constructor & Destructor Documentation

def email._parseaddr.AddrlistClass.__init__ (   self,
  field 
)
Initialize a new instance.

`field' is an unparsed address header field, containing
one or more addresses.

Reimplemented in email._parseaddr.AddressList.

Definition at line 184 of file _parseaddr.py.

00184 
00185     def __init__(self, field):
00186         """Initialize a new instance.
00187 
00188         `field' is an unparsed address header field, containing
00189         one or more addresses.
00190         """
00191         self.specials = '()<>@,:;.\"[]'
00192         self.pos = 0
00193         self.LWS = ' \t'
00194         self.CR = '\r\n'
00195         self.FWS = self.LWS + self.CR
00196         self.atomends = self.specials + self.LWS + self.CR
00197         # Note that RFC 2822 now specifies `.' as obs-phrase, meaning that it
00198         # is obsolete syntax.  RFC 2822 requires that we recognize obsolete
00199         # syntax, so allow dots in phrases.
00200         self.phraseends = self.atomends.replace('.', '')
00201         self.field = field
00202         self.commentlist = []

Here is the caller graph for this function:


Member Function Documentation

Parse the next address.

Definition at line 231 of file _parseaddr.py.

00231 
00232     def getaddress(self):
00233         """Parse the next address."""
00234         self.commentlist = []
00235         self.gotonext()
00236 
00237         oldpos = self.pos
00238         oldcl = self.commentlist
00239         plist = self.getphraselist()
00240 
00241         self.gotonext()
00242         returnlist = []
00243 
00244         if self.pos >= len(self.field):
00245             # Bad email address technically, no domain.
00246             if plist:
00247                 returnlist = [(SPACE.join(self.commentlist), plist[0])]
00248 
00249         elif self.field[self.pos] in '.@':
00250             # email address is just an addrspec
00251             # this isn't very efficient since we start over
00252             self.pos = oldpos
00253             self.commentlist = oldcl
00254             addrspec = self.getaddrspec()
00255             returnlist = [(SPACE.join(self.commentlist), addrspec)]
00256 
00257         elif self.field[self.pos] == ':':
00258             # address is a group
00259             returnlist = []
00260 
00261             fieldlen = len(self.field)
00262             self.pos += 1
00263             while self.pos < len(self.field):
00264                 self.gotonext()
00265                 if self.pos < fieldlen and self.field[self.pos] == ';':
00266                     self.pos += 1
00267                     break
00268                 returnlist = returnlist + self.getaddress()
00269 
00270         elif self.field[self.pos] == '<':
00271             # Address is a phrase then a route addr
00272             routeaddr = self.getrouteaddr()
00273 
00274             if self.commentlist:
00275                 returnlist = [(SPACE.join(plist) + ' (' +
00276                                ' '.join(self.commentlist) + ')', routeaddr)]
00277             else:
00278                 returnlist = [(SPACE.join(plist), routeaddr)]
00279 
00280         else:
00281             if plist:
00282                 returnlist = [(SPACE.join(self.commentlist), plist[0])]
00283             elif self.field[self.pos] in self.specials:
00284                 self.pos += 1
00285 
00286         self.gotonext()
00287         if self.pos < len(self.field) and self.field[self.pos] == ',':
00288             self.pos += 1
00289         return returnlist

Here is the call graph for this function:

Here is the caller graph for this function:

Parse all addresses.

Returns a list containing all of the addresses.

Definition at line 217 of file _parseaddr.py.

00217 
00218     def getaddrlist(self):
00219         """Parse all addresses.
00220 
00221         Returns a list containing all of the addresses.
00222         """
00223         result = []
00224         while self.pos < len(self.field):
00225             ad = self.getaddress()
00226             if ad:
00227                 result += ad
00228             else:
00229                 result.append(('', ''))
00230         return result

Here is the call graph for this function:

Parse an RFC 2822 addr-spec.

Definition at line 322 of file _parseaddr.py.

00322 
00323     def getaddrspec(self):
00324         """Parse an RFC 2822 addr-spec."""
00325         aslist = []
00326 
00327         self.gotonext()
00328         while self.pos < len(self.field):
00329             preserve_ws = True
00330             if self.field[self.pos] == '.':
00331                 if aslist and not aslist[-1].strip():
00332                     aslist.pop()
00333                 aslist.append('.')
00334                 self.pos += 1
00335                 preserve_ws = False
00336             elif self.field[self.pos] == '"':
00337                 aslist.append('"%s"' % quote(self.getquote()))
00338             elif self.field[self.pos] in self.atomends:
00339                 if aslist and not aslist[-1].strip():
00340                     aslist.pop()
00341                 break
00342             else:
00343                 aslist.append(self.getatom())
00344             ws = self.gotonext()
00345             if preserve_ws and ws:
00346                 aslist.append(ws)
00347 
00348         if self.pos >= len(self.field) or self.field[self.pos] != '@':
00349             return EMPTYSTRING.join(aslist)
00350 
00351         aslist.append('@')
00352         self.pos += 1
00353         self.gotonext()
00354         return EMPTYSTRING.join(aslist) + self.getdomain()

Here is the call graph for this function:

Here is the caller graph for this function:

def email._parseaddr.AddrlistClass.getatom (   self,
  atomends = None 
)
Parse an RFC 2822 atom.

Optional atomends specifies a different set of end token delimiters
(the default is to use self.atomends).  This is used e.g. in
getphraselist() since phrase endings must not include the `.' (which
is legal in phrases).

Definition at line 423 of file _parseaddr.py.

00423 
00424     def getatom(self, atomends=None):
00425         """Parse an RFC 2822 atom.
00426 
00427         Optional atomends specifies a different set of end token delimiters
00428         (the default is to use self.atomends).  This is used e.g. in
00429         getphraselist() since phrase endings must not include the `.' (which
00430         is legal in phrases)."""
00431         atomlist = ['']
00432         if atomends is None:
00433             atomends = self.atomends
00434 
00435         while self.pos < len(self.field):
00436             if self.field[self.pos] in atomends:
00437                 break
00438             else:
00439                 atomlist.append(self.field[self.pos])
00440             self.pos += 1
00441 
00442         return EMPTYSTRING.join(atomlist)

Here is the caller graph for this function:

Get a parenthesis-delimited fragment from self's field.

Definition at line 415 of file _parseaddr.py.

00415 
00416     def getcomment(self):
00417         """Get a parenthesis-delimited fragment from self's field."""
00418         return self.getdelimited('(', ')\r', True)

Here is the call graph for this function:

Here is the caller graph for this function:

def email._parseaddr.AddrlistClass.getdelimited (   self,
  beginchar,
  endchars,
  allowcomments = True 
)
Parse a header fragment delimited by special characters.

`beginchar' is the start character for the fragment.
If self is not looking at an instance of `beginchar' then
getdelimited returns the empty string.

`endchars' is a sequence of allowable end-delimiting characters.
Parsing stops when one of these is encountered.

If `allowcomments' is non-zero, embedded RFC 2822 comments are allowed
within the parsed fragment.

Definition at line 374 of file _parseaddr.py.

00374 
00375     def getdelimited(self, beginchar, endchars, allowcomments=True):
00376         """Parse a header fragment delimited by special characters.
00377 
00378         `beginchar' is the start character for the fragment.
00379         If self is not looking at an instance of `beginchar' then
00380         getdelimited returns the empty string.
00381 
00382         `endchars' is a sequence of allowable end-delimiting characters.
00383         Parsing stops when one of these is encountered.
00384 
00385         If `allowcomments' is non-zero, embedded RFC 2822 comments are allowed
00386         within the parsed fragment.
00387         """
00388         if self.field[self.pos] != beginchar:
00389             return ''
00390 
00391         slist = ['']
00392         quote = False
00393         self.pos += 1
00394         while self.pos < len(self.field):
00395             if quote:
00396                 slist.append(self.field[self.pos])
00397                 quote = False
00398             elif self.field[self.pos] in endchars:
00399                 self.pos += 1
00400                 break
00401             elif allowcomments and self.field[self.pos] == '(':
00402                 slist.append(self.getcomment())
00403                 continue        # have already advanced pos from getcomment
00404             elif self.field[self.pos] == '\\':
00405                 quote = True
00406             else:
00407                 slist.append(self.field[self.pos])
00408             self.pos += 1
00409 
00410         return EMPTYSTRING.join(slist)

Here is the call graph for this function:

Here is the caller graph for this function:

Get the complete domain name from an address.

Definition at line 355 of file _parseaddr.py.

00355 
00356     def getdomain(self):
00357         """Get the complete domain name from an address."""
00358         sdlist = []
00359         while self.pos < len(self.field):
00360             if self.field[self.pos] in self.LWS:
00361                 self.pos += 1
00362             elif self.field[self.pos] == '(':
00363                 self.commentlist.append(self.getcomment())
00364             elif self.field[self.pos] == '[':
00365                 sdlist.append(self.getdomainliteral())
00366             elif self.field[self.pos] == '.':
00367                 self.pos += 1
00368                 sdlist.append('.')
00369             elif self.field[self.pos] in self.atomends:
00370                 break
00371             else:
00372                 sdlist.append(self.getatom())
00373         return EMPTYSTRING.join(sdlist)

Here is the call graph for this function:

Here is the caller graph for this function:

Parse an RFC 2822 domain-literal.

Definition at line 419 of file _parseaddr.py.

00419 
00420     def getdomainliteral(self):
00421         """Parse an RFC 2822 domain-literal."""
00422         return '[%s]' % self.getdelimited('[', ']\r', False)

Here is the call graph for this function:

Here is the caller graph for this function:

Parse a sequence of RFC 2822 phrases.

A phrase is a sequence of words, which are in turn either RFC 2822
atoms or quoted-strings.  Phrases are canonicalized by squeezing all
runs of continuous whitespace into one space.

Definition at line 443 of file _parseaddr.py.

00443 
00444     def getphraselist(self):
00445         """Parse a sequence of RFC 2822 phrases.
00446 
00447         A phrase is a sequence of words, which are in turn either RFC 2822
00448         atoms or quoted-strings.  Phrases are canonicalized by squeezing all
00449         runs of continuous whitespace into one space.
00450         """
00451         plist = []
00452 
00453         while self.pos < len(self.field):
00454             if self.field[self.pos] in self.FWS:
00455                 self.pos += 1
00456             elif self.field[self.pos] == '"':
00457                 plist.append(self.getquote())
00458             elif self.field[self.pos] == '(':
00459                 self.commentlist.append(self.getcomment())
00460             elif self.field[self.pos] in self.phraseends:
00461                 break
00462             else:
00463                 plist.append(self.getatom(self.phraseends))
00464 
00465         return plist

Here is the call graph for this function:

Here is the caller graph for this function:

Get a quote-delimited fragment from self's field.

Definition at line 411 of file _parseaddr.py.

00411 
00412     def getquote(self):
00413         """Get a quote-delimited fragment from self's field."""
00414         return self.getdelimited('"', '"\r', False)

Here is the call graph for this function:

Here is the caller graph for this function:

Parse a route address (Return-path value).

This method just skips all the route stuff and returns the addrspec.

Definition at line 290 of file _parseaddr.py.

00290 
00291     def getrouteaddr(self):
00292         """Parse a route address (Return-path value).
00293 
00294         This method just skips all the route stuff and returns the addrspec.
00295         """
00296         if self.field[self.pos] != '<':
00297             return
00298 
00299         expectroute = False
00300         self.pos += 1
00301         self.gotonext()
00302         adlist = ''
00303         while self.pos < len(self.field):
00304             if expectroute:
00305                 self.getdomain()
00306                 expectroute = False
00307             elif self.field[self.pos] == '>':
00308                 self.pos += 1
00309                 break
00310             elif self.field[self.pos] == '@':
00311                 self.pos += 1
00312                 expectroute = True
00313             elif self.field[self.pos] == ':':
00314                 self.pos += 1
00315             else:
00316                 adlist = self.getaddrspec()
00317                 self.pos += 1
00318                 break
00319             self.gotonext()
00320 
00321         return adlist

Here is the call graph for this function:

Here is the caller graph for this function:

Skip white space and extract comments.

Definition at line 203 of file _parseaddr.py.

00203 
00204     def gotonext(self):
00205         """Skip white space and extract comments."""
00206         wslist = []
00207         while self.pos < len(self.field):
00208             if self.field[self.pos] in self.LWS + '\n\r':
00209                 if self.field[self.pos] not in '\n\r':
00210                     wslist.append(self.field[self.pos])
00211                 self.pos += 1
00212             elif self.field[self.pos] == '(':
00213                 self.commentlist.append(self.getcomment())
00214             else:
00215                 break
00216         return EMPTYSTRING.join(wslist)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 195 of file _parseaddr.py.

Definition at line 201 of file _parseaddr.py.

Definition at line 193 of file _parseaddr.py.

Definition at line 200 of file _parseaddr.py.

Definition at line 194 of file _parseaddr.py.

Definition at line 192 of file _parseaddr.py.

Definition at line 199 of file _parseaddr.py.

Definition at line 191 of file _parseaddr.py.

Definition at line 190 of file _parseaddr.py.


The documentation for this class was generated from the following file: