# HG changeset patch # User Marti Raudsepp # Date 2009-11-26 08:47:39 # Node ID e5b44a7986d03adb905cf53563b35d6fb90e3ecf # Parent 4600e6222efb883b5539315b659ead023f5a277a 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(). diff --git a/mercurial/mail.py b/mercurial/mail.py --- a/mercurial/mail.py +++ b/mercurial/mail.py @@ -160,11 +160,7 @@ def headencode(ui, s, charsets=None, dis 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 @@ def addressencode(ui, address, charsets= 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.'''