mail: add parseaddrlist() function for parsing many addresses at once
authorMarti Raudsepp <marti@juffo.org>
Thu, 26 Nov 2009 10:47:39 +0200
changeset 9948 e5b44a7986d0
parent 9947 4600e6222efb
child 9949 bc3f762af82e
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().
mercurial/mail.py
--- 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.'''