imip-agent

Change of imiptools/data.py

792:fde5ece8dc9a
imiptools/data.py
     1.1 --- a/imiptools/data.py	Tue Sep 29 19:02:01 2015 +0200
     1.2 +++ b/imiptools/data.py	Tue Sep 29 19:03:24 2015 +0200
     1.3 @@ -705,6 +705,17 @@
     1.4  
     1.5  # Conversion functions.
     1.6  
     1.7 +def get_address_parts(values):
     1.8 +
     1.9 +    "Return name and address tuples for each of the given 'values'."
    1.10 +
    1.11 +    l = []
    1.12 +    for name, address in values and email.utils.getaddresses(values) or []:
    1.13 +        if is_mailto_uri(name):
    1.14 +            name = name[7:] # strip "mailto:"
    1.15 +        l.append((name, address))
    1.16 +    return l
    1.17 +
    1.18  def get_addresses(values):
    1.19  
    1.20      """
    1.21 @@ -713,24 +724,48 @@
    1.22      itself.
    1.23      """
    1.24  
    1.25 -    return [address for name, address in email.utils.getaddresses(values)]
    1.26 +    return [address for name, address in get_address_parts(values)]
    1.27  
    1.28  def get_address(value):
    1.29  
    1.30      "Return an e-mail address from the given 'value'."
    1.31  
    1.32      if not value: return None
    1.33 -    return get_addresses([value.startswith("mailto:") and value[7:] or value])[0]
    1.34 +    return get_addresses([value])[0]
    1.35 +
    1.36 +def get_verbose_address(value, attr=None):
    1.37 +
    1.38 +    """
    1.39 +    Return a verbose e-mail address featuring any name from the given 'value'
    1.40 +    and any accompanying 'attr' dictionary.
    1.41 +    """
    1.42 +
    1.43 +    name, address = get_address_parts([value])[0]
    1.44 +    if not name:
    1.45 +        name = attr and attr.get("CN")
    1.46 +    if name and address:
    1.47 +        return "%s <%s>" % (name, address)
    1.48 +    else:
    1.49 +        return address
    1.50 +
    1.51 +def is_mailto_uri(value):
    1.52 +    return value.lower().startswith("mailto:")
    1.53  
    1.54  def get_uri(value):
    1.55  
    1.56      "Return a URI for the given 'value'."
    1.57  
    1.58      if not value: return None
    1.59 -    return value.lower().startswith("mailto:") and ("mailto:%s" % value[7:]) or \
    1.60 +    return is_mailto_uri(value) and ("mailto:%s" % value[7:]) or \
    1.61             ":" in value and value or \
    1.62             "mailto:%s" % get_address(value)
    1.63  
    1.64 +def uri_parts(values):
    1.65 +
    1.66 +    "Return any common name plus the URI for each of the given 'values'."
    1.67 +
    1.68 +    return [(name, get_uri(address)) for name, address in get_address_parts(values)]
    1.69 +
    1.70  uri_value = get_uri
    1.71  
    1.72  def uri_values(values):