mail: add parseaddrlist() function for parsing many addresses at once
Adds a new function addrlistencode() to mercurial.mail, like addressencode(),
but accepts a list of addresses as input, and returns a list of formatted
addresses. This will be used by patchbomb.
The difference is that each element in the input list can contain multiple
addresses (comma separated or otherwise), and are split using Python's
email.Utils.getaddresses().
--- a/mercurial/mail.py Thu Nov 26 12:23:28 2009 +0200
+++ b/mercurial/mail.py Thu Nov 26 10:47:39 2009 +0200
@@ -160,11 +160,7 @@
return str(email.Header.Header(s, cs))
return s
-def addressencode(ui, address, charsets=None, display=False):
- '''Turns address into RFC-2047 compliant header.'''
- if display or not address:
- return address or ''
- name, addr = email.Utils.parseaddr(address)
+def _addressencode(ui, name, addr, charsets=None):
name = headencode(ui, name, charsets)
try:
acc, dom = addr.split('@')
@@ -181,6 +177,26 @@
raise util.Abort(_('invalid local address: %s') % addr)
return email.Utils.formataddr((name, addr))
+def addressencode(ui, address, charsets=None, display=False):
+ '''Turns address into RFC-2047 compliant header.'''
+ if display or not address:
+ return address or ''
+ name, addr = email.Utils.parseaddr(address)
+ return _addressencode(ui, name, addr, charsets)
+
+def addrlistencode(ui, addrs, charsets=None, display=False):
+ '''Turns a list of addresses into a list of RFC-2047 compliant headers.
+ A single element of input list may contain multiple addresses, but output
+ always has one address per item'''
+ if display:
+ return [a.strip() for a in addrs if a.strip()]
+
+ result = []
+ for name, addr in email.Utils.getaddresses(addrs):
+ if name or addr:
+ result.append(_addressencode(ui, name, addr, charsets))
+ return result
+
def mimeencode(ui, s, charsets=None, display=False):
'''creates mime text object, encodes it if needed, and sets
charset and transfer-encoding accordingly.'''