Cleanup
[rox-menu.git] / parseaddrlist.py
blobb9f707f7a26850d9bbcd926f81e289c17df00328
1 import email.Utils
3 def tokensplit(s, splitchars=None, quotes='"\''):
4 """Split a string into tokens at every occcurence of splitchars.
6 splitchars within quotes are disregarded.
7 """
9 if splitchars is None:
10 import string
11 splitchars = string.whitespace
12 inquote = None
13 l = []; token = []
14 for c in s:
15 if c in quotes:
16 if c == inquote:
17 inquote = None
18 elif inquote is None:
19 inquote = c
20 if c in splitchars and inquote is None:
21 l.append("".join(token))
22 token = []
23 inquote = None
24 else:
25 token.append(c)
26 else:
27 l.append("".join(token))
28 return l
30 def parseaddrlist(alist):
31 """Parse a comma-separated list of email address into (name, mail) tuples.
32 """
34 l = []
35 for addr in tokensplit(alist, ',', quotes='"'):
36 addr = addr.strip()
37 if addr:
38 l.append(email.Utils.parseaddr(addr))
39 return l
42 if __name__ == '__main__':
43 al = '"Hacker, J. Random" <random@hacker.net>, Joe O\'Donnel <joe@foo.com>, luser@dau.org, alice (Alice Bobton), <zaphod@frogstar.net>, '
44 print parseaddrlist(al)