| 8 | from email import _header_value_parser as parser |
| 9 | |
| 10 | class Address: |
| 11 | |
| 12 | def __init__(self, display_name='', username='', domain='', addr_spec=None): |
| 13 | """Create an object representing a full email address. |
| 14 | |
| 15 | An address can have a 'display_name', a 'username', and a 'domain'. In |
| 16 | addition to specifying the username and domain separately, they may be |
| 17 | specified together by using the addr_spec keyword *instead of* the |
| 18 | username and domain keywords. If an addr_spec string is specified it |
| 19 | must be properly quoted according to RFC 5322 rules; an error will be |
| 20 | raised if it is not. |
| 21 | |
| 22 | An Address object has display_name, username, domain, and addr_spec |
| 23 | attributes, all of which are read-only. The addr_spec and the string |
| 24 | value of the object are both quoted according to RFC5322 rules, but |
| 25 | without any Content Transfer Encoding. |
| 26 | |
| 27 | """ |
| 28 | |
| 29 | inputs = ''.join(filter(None, (display_name, username, domain, addr_spec))) |
| 30 | if '\r' in inputs or '\n' in inputs: |
| 31 | raise ValueError("invalid arguments; address parts cannot contain CR or LF") |
| 32 | |
| 33 | # This clause with its potential 'raise' may only happen when an |
| 34 | # application program creates an Address object using an addr_spec |
| 35 | # keyword. The email library code itself must always supply username |
| 36 | # and domain. |
| 37 | if addr_spec is not None: |
| 38 | if username or domain: |
| 39 | raise TypeError("addrspec specified when username and/or " |
| 40 | "domain also specified") |
| 41 | a_s, rest = parser.get_addr_spec(addr_spec) |
| 42 | if rest: |
| 43 | raise ValueError("Invalid addr_spec; only '{}' " |
| 44 | "could be parsed from '{}'".format( |
| 45 | a_s, addr_spec)) |
| 46 | if a_s.all_defects: |
| 47 | raise a_s.all_defects[0] |
| 48 | username = a_s.local_part |
| 49 | domain = a_s.domain |
| 50 | self._display_name = display_name |
| 51 | self._username = username |
| 52 | self._domain = domain |
| 53 | |
| 54 | @property |
| 55 | def display_name(self): |
| 56 | return self._display_name |
| 57 | |
| 58 | @property |
| 59 | def username(self): |
| 60 | return self._username |
| 61 | |
| 62 | @property |
| 63 | def domain(self): |
| 64 | return self._domain |
| 65 | |
| 66 | @property |
| 67 | def addr_spec(self): |
no outgoing calls
searching dependent graphs…