def __init__(self, raw_display_name=None, raw_addr_spec=None, display_name=None, mailbox=None, hostname=None): if isinstance(raw_display_name, str): raw_display_name = raw_display_name.encode('utf-8') if isinstance(raw_addr_spec, str): 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') 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') if isinstance(self._mailbox, str): self._mailbox = self._mailbox.decode('utf-8') if isinstance(self._hostname, str): self._hostname = self._hostname.decode('utf-8')
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 _lift_parser_result(retval): if isinstance(retval, Mailbox): return EmailAddress(display_name=smart_unquote( retval.display_name.decode('utf-8')), mailbox=retval.local_part.decode('utf-8'), hostname=retval.domain.decode('utf-8')) if isinstance(retval, Url): return UrlAddress(address=retval.address.decode('utf-8')) if isinstance(retval, list): return AddressList(map(_lift_parser_result, retval)) return None
def _lift_parse_result(parse_rs): if isinstance(parse_rs, Mailbox): try: return EmailAddress(display_name=smart_unquote( parse_rs.display_name.decode('utf-8')), mailbox=parse_rs.local_part.decode('utf-8'), hostname=parse_rs.domain.decode('utf-8')) except (UnicodeError, IDNAError): return None if isinstance(parse_rs, Url): return UrlAddress(address=parse_rs.address.decode('utf-8')) return None
def _lift_parse_result(parse_rs): if isinstance(parse_rs, Mailbox): try: return EmailAddress(_display_name=smart_unquote( _to_text(parse_rs.display_name)), _mailbox=_to_text(parse_rs.local_part), _hostname=_to_text(parse_rs.domain)) except (UnicodeError, IDNAError): return None if isinstance(parse_rs, Url): return UrlAddress(_address=_to_text(parse_rs.address)) return None
def _lift_parse_result(parse_rs): if isinstance(parse_rs, Mailbox): try: return EmailAddress( _display_name=smart_unquote(_to_text(parse_rs.display_name)), _mailbox=_to_text(parse_rs.local_part), _hostname=_to_text(parse_rs.domain)) except (UnicodeError, IDNAError): return None if isinstance(parse_rs, Url): return UrlAddress(_address=_to_text(parse_rs.address)) return None
def _lift_parse_result(parse_rs): if isinstance(parse_rs, Mailbox): try: return EmailAddress( display_name=smart_unquote(parse_rs.display_name.decode('utf-8')), mailbox=parse_rs.local_part.decode('utf-8'), hostname=parse_rs.domain.decode('utf-8')) except (UnicodeError, IDNAError): return None if isinstance(parse_rs, Url): return UrlAddress(address=parse_rs.address.decode('utf-8')) return None
def __init__(self, display_name, spec=None, parsed_name=None): if spec is None: spec = display_name display_name = None assert(spec) if parsed_name: self.display_name = smart_unquote(mime_to_unicode(parsed_name)) elif display_name: self.display_name = display_name else: self.display_name = '' parts = spec.rsplit('@', 1) self.mailbox = parts[0] self.hostname = parts[1].lower() self.address = self.mailbox + "@" + self.hostname self.addr_type = self.Type.Email
def __init__(self, display_name, spec=None, parsed_name=None): if spec is None: spec = display_name display_name = None assert(spec) if parsed_name: self.display_name = smart_unquote(mime_to_unicode(parsed_name)) elif display_name: self.display_name = display_name else: self.display_name = u'' parts = spec.rsplit('@', 1) self.mailbox = parts[0] self.hostname = parts[1].lower() self.address = self.mailbox + "@" + self.hostname self.addr_type = self.Type.Email
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('"') and len(self._display_name) > 2): self._display_name = smart_unquote(self._display_name) if (self._display_name.startswith('=?') and self._display_name.endswith('?=')): self._display_name = mime_to_unicode(self._display_name) if isinstance(self._display_name, str): self._display_name = self._display_name.decode('utf-8') # Convert localpart to unicode string. # TODO consider lowercasing it here. if isinstance(self._mailbox, str): self._mailbox = self._mailbox.decode('utf-8') # Convert hostname to lowercase unicode string. if self._hostname.startswith('xn--'): self._hostname = idna.decode(self._hostname) if isinstance(self._hostname, str): self._hostname = self._hostname.decode('utf-8') self._hostname = self._hostname.lower()
def test_unquote__unicode(): eq_(u'Превед Медвед', smart_unquote(u'Превед Медвед'))
def test_unquote__nothing_to_unquote(): eq_('foo\\.;\tbar', smart_unquote('foo\\.;\tbar'))
def test_unquote(): eq_('foo bar "(bazz)" blah oops', smart_unquote('foo "bar \\"(bazz)\\" blah" oops')) eq_('foo; bar. \\bazz\\', smart_unquote('"foo;" "bar." "\\\\bazz\\\\"')) eq_('"foo"bar"', smart_unquote('"\\"foo\\"bar\\"'))
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)