def __init__(self, raw_display_name=None, raw_addr_spec=None, _display_name=None, _mailbox=None, _hostname=None): raw_display_name = _to_parser_input(raw_display_name) raw_addr_spec = _to_parser_input(raw_addr_spec) if raw_display_name and raw_addr_spec: mailbox = addr_spec_parser.parse(raw_addr_spec, lexer.clone()) self._display_name = _to_text(raw_display_name) self._mailbox = _to_text(mailbox.local_part) self._hostname = _to_text(mailbox.domain) elif raw_display_name: mailbox = mailbox_parser.parse(raw_display_name, lexer.clone()) self._display_name = _to_text(mailbox.display_name) self._mailbox = _to_text(mailbox.local_part) self._hostname = _to_text(mailbox.domain) elif raw_addr_spec: mailbox = addr_spec_parser.parse(raw_addr_spec, lexer.clone()) self._display_name = u'' self._mailbox = _to_text(mailbox.local_part) self._hostname = _to_text(mailbox.domain) elif _mailbox and _hostname: self._display_name = _display_name or u'' self._mailbox = _mailbox self._hostname = _hostname else: raise SyntaxError('failed to create EmailAddress: bad parameters') # Convert display name to decoded unicode string. if (self._display_name.startswith('=?') and self._display_name.endswith('?=')): self._display_name = mime_to_unicode(self._display_name) if (self._display_name.startswith('"') and self._display_name.endswith('"') and len(self._display_name) > 2): self._display_name = smart_unquote(self._display_name) # Convert hostname to lowercase unicode string. self._hostname = self._hostname.lower() if self._hostname.startswith('xn--') or '.xn--' in self._hostname: self._hostname = idna.decode(self._hostname) if not is_pure_ascii(self._hostname): idna.encode(self._hostname) assert isinstance(self._display_name, six.text_type) assert isinstance(self._mailbox, six.text_type) assert isinstance(self._hostname, six.text_type)
def __init__(self, raw=None, _address=None): if raw: raw = _to_parser_input(raw) url = url_parser.parse(raw, lexer.clone()) self._address = urlparse(url.address) elif _address: self._address = urlparse(_address) else: raise SyntaxError('failed to create UrlAddress: bad parameters')
def __init__(self, raw=None, address=None): if raw: if isinstance(raw, unicode): raw = raw.encode('utf-8') parser = url_parser url = parser.parse(raw.strip(), lexer=lexer.clone()) self._address = urlparse(url.address) elif address: self._address = urlparse(address) else: raise SyntaxError('failed to create UrlAddress: bad parameters')
def parse_discrete_list(address_list, as_tuple=False, metrics=False): """ Given an string, returns an AddressList object (an iterable list representing parsed email addresses and urls). Returns an AddressList object and optionally metrics on processing time if requested. Examples: >>> address.parse_list('A <a@b>') [A <a@b>] >>> address.parse_list('A <a@b>, C <d@e>') [A <a@b>, C <d@e>] >>> address.parse_list('A <a@b>, C, D <d@e>') None >>> address.parse_list('A <a@b>, D <d@e>, http://localhost') [A <a@b>, D <d@e>, http://localhost] """ mtimes = {'parsing': 0} parser = mailbox_or_url_list_parser # normalize inputs to bytestring address_list_s = address_list if isinstance(address_list, unicode): address_list_s = address_list.encode('utf-8') # sanity checks if not address_list_s: return _parse_list_result(as_tuple, AddressList(), [], mtimes) if len(address_list_s) > MAX_ADDRESS_LIST_LENGTH: _log.warning('address list exceeds maximum length of %s', MAX_ADDRESS_LIST_LENGTH) return _parse_list_result(as_tuple, AddressList(), [address_list], mtimes) bstart = time() try: parse_list_rs = parser.parse(address_list_s.strip(), lexer=lexer.clone()) addr_list_obj, bad_addr_list = _lift_parse_list_result(parse_list_rs) if len(addr_list_obj) == 0: bad_addr_list.append(address_list_s) mtimes['parsing'] = time() - bstart except (LexError, YaccError, SyntaxError): return _parse_list_result(as_tuple, AddressList(), [address_list], mtimes) return _parse_list_result(as_tuple, addr_list_obj, bad_addr_list, mtimes)
def parse_discrete_list(address_list, as_tuple=False, metrics=False): """ Given an string, returns an AddressList object (an iterable list representing parsed email addresses and urls). Returns an AddressList object and optionally metrics on processing time if requested. Examples: >>> address.parse_list('A <a@b>') [A <a@b>] >>> address.parse_list('A <a@b>, C <d@e>') [A <a@b>, C <d@e>] >>> address.parse_list('A <a@b>, C, D <d@e>') None >>> address.parse_list('A <a@b>, D <d@e>, http://localhost') [A <a@b>, D <d@e>, http://localhost] """ mtimes = {'parsing': 0} # Normalize the input to binary for Python 2, and text for Python 3. address_list_s = _to_parser_input(address_list) # sanity checks if not address_list_s: return _parse_list_result(as_tuple, AddressList(), [], mtimes) if len(address_list_s) > MAX_ADDRESS_LIST_LENGTH: _log.warning('address list exceeds maximum length of %s', MAX_ADDRESS_LIST_LENGTH) return _parse_list_result(as_tuple, AddressList(), [address_list], mtimes) bstart = time() try: parse_list_rs = mailbox_or_url_list_parser.parse(address_list_s.strip(), lexer.clone()) addr_list_obj, bad_addr_list = _lift_parse_list_result(parse_list_rs) if len(addr_list_obj) == 0: bad_addr_list.append(address_list_s) mtimes['parsing'] = time() - bstart except (LexError, YaccError, SyntaxError): return _parse_list_result(as_tuple, AddressList(), [address_list], mtimes) return _parse_list_result(as_tuple, addr_list_obj, bad_addr_list, mtimes)
def parse(address, addr_spec_only=False, strict=False, metrics=False): """ Given a string, returns a scalar object representing a single full mailbox (display name and addr-spec), addr-spec, or a url. If parsing the entire string fails and strict is not set to True, fall back to trying to parse the last word only and assume everything else is the display name. Returns an Address object and optionally metrics on processing time if requested. Examples: >>> address.parse('John Smith <*****@*****.**') John Smith <*****@*****.**> >>> print address.parse('John <*****@*****.**>', addr_spec_only=True) None >>> print address.parse('*****@*****.**', addr_spec_only=True) '*****@*****.**' >>> address.parse('http://host.com/post?q') http://host.com/post?q >>> print address.parse('foo') None """ mtimes = {'parsing': 0} if addr_spec_only: parser = addr_spec_parser else: parser = mailbox_or_url_parser address = _to_parser_input(address) # sanity checks if not address: return None, mtimes if len(address) > MAX_ADDRESS_LENGTH: _log.warning('address exceeds maximum length of %s', MAX_ADDRESS_LENGTH) return None, mtimes bstart = time() try: parse_rs = parser.parse(address.strip(), lexer=lexer.clone()) addr_obj = _lift_parse_result(parse_rs) except (LexError, YaccError, SyntaxError): addr_obj = None if addr_obj is None and not strict: addr_parts = address.split(' ') addr_spec = addr_parts[-1] if len(addr_spec) < len(address): try: parse_rs = parser.parse(addr_spec, lexer=lexer.clone()) addr_obj = _lift_parse_result(parse_rs) if addr_obj: display_name = ' '.join(addr_parts[:-1]) if isinstance(display_name, six.binary_type): display_name = display_name.decode('utf-8') addr_obj._display_name = display_name except (LexError, YaccError, SyntaxError): addr_obj = None mtimes['parsing'] = time() - bstart return addr_obj, mtimes
def __init__(self, raw_display_name=None, raw_addr_spec=None, display_name=None, mailbox=None, hostname=None): if isinstance(raw_display_name, unicode): raw_display_name = raw_display_name.encode('utf-8') if isinstance(raw_addr_spec, unicode): raw_addr_spec = raw_addr_spec.encode('utf-8') if raw_display_name and raw_addr_spec: parser = addr_spec_parser mailbox = parser.parse(raw_addr_spec.strip(), lexer=lexer.clone()) self._display_name = raw_display_name self._mailbox = mailbox.local_part self._hostname = mailbox.domain elif raw_display_name: parser = mailbox_parser mailbox = parser.parse(raw_display_name.strip(), lexer=lexer.clone()) self._display_name = mailbox.display_name self._mailbox = mailbox.local_part self._hostname = mailbox.domain elif raw_addr_spec: parser = addr_spec_parser mailbox = parser.parse(raw_addr_spec.strip(), lexer=lexer.clone()) self._display_name = '' self._mailbox = mailbox.local_part self._hostname = mailbox.domain elif mailbox and hostname: self._display_name = display_name or '' self._mailbox = mailbox self._hostname = hostname else: raise SyntaxError('failed to create EmailAddress: bad parameters') # Convert display name to decoded unicode string. if (self._display_name.startswith('=?') and self._display_name.endswith('?=')): self._display_name = mime_to_unicode(self._display_name) if (self._display_name.startswith('"') and self._display_name.endswith('"') and len(self._display_name) > 2): self._display_name = smart_unquote(self._display_name) if isinstance(self._display_name, str): self._display_name = self._display_name.decode('utf-8') # Convert localpart to unicode string. if isinstance(self._mailbox, str): self._mailbox = self._mailbox.decode('utf-8') # Convert hostname to lowercase unicode string. self._hostname = self._hostname.lower() if self._hostname.startswith('xn--') or '.xn--' in self._hostname: self._hostname = idna.decode(self._hostname) if isinstance(self._hostname, str): self._hostname = self._hostname.decode('utf-8') if not is_pure_ascii(self._hostname): idna.encode(self._hostname)