Source code for uw.emailutils
"""Email-related utilities.
Contains a class that provides a simple extension of some Python standard
:mod:`email.utils` routines.
"""
from email.utils import formataddr, getaddresses, parseaddr
[docs]class emailaddr (tuple):
"""Simple class to represent an email address.
:param realname: the realname to be displayed with the email address, or
empty for no real name.
:param email: the email address itself.
Instances are always 2-tuples. The two elements are the realname and
address. This mostly just wraps procedures in :mod:`email.utils`.
"""
def __new__ (cls, realname, email):
return tuple.__new__ (cls, (realname or None, email))
@property
def realname (self):
"""The realname associated with the email address.
"""
return self[0]
@property
def email (self):
"""The email address itself.
"""
return self[1]
@property
def valid (self):
"""Perform a simple check for validity.
Runs this address through :func:`email.utils.getaddresses` and
verifies that the result is a single address whose address component
contains an '@'.
"""
result = getaddresses ([str (self)])
return len (result) == 1 and result[0][1].find ('@') >= 0
__str__ = formataddr
@staticmethod
[docs] def parse1 (address):
"""Parse a single email address using :func:`email.utils.parseaddr`.
"""
return emailaddr (*parseaddr (address))
@staticmethod
[docs] def parse (fieldvalues):
"""Parse email addresses using :func:`email.utils.getaddresses`.
"""
return [emailaddr (*addr) for addr in getaddresses (fieldvalues)]