def various_encodings_test(): v = '"=?utf-8?b?6ICD5Y+W5YiG5Lqr?=" <*****@*****.**>' eq_(u'"考取分享" <*****@*****.**>', encodedword.mime_to_unicode(v)) v = """=?UTF-8?B?0JbQtdC60LA=?= <*****@*****.**>, =?UTF-8?B?0JrQvtC90YbQtdCy0L7QuQ==?= <*****@*****.**>""" eq_(u"Жека <*****@*****.**>, Концевой <*****@*****.**>", encodedword.mime_to_unicode(v)) v = encodedword.mime_to_unicode("=?utf-8?b?0JrQvtC90YbQtdCy0L7QuQ==?= <*****@*****.**>, Bob <*****@*****.**>, =?utf-8?b?0JLQuNC90YE=?= <*****@*****.**>") eq_(u"Концевой <*****@*****.**>, Bob <*****@*****.**>, Винс <*****@*****.**>", v) v = '=?UTF-8?B?0J/RgNC+0LLQtdGA0Y/QtdC8INGA0YPRgdGB0LrQuNC1INGB0LDQsdC2?=\n =?UTF-8?B?0LXQutGC0Ysg0Lgg0Y7QvdC40LrQvtC0IOKYoA==?=' eq_(u'Проверяем русские сабжекты и юникод ☠', encodedword.mime_to_unicode(v)) v = '=?UTF-8?B?0J/RgNC+0LLQtdGA0Y/QtdC8INGA0YPRgdGB0LrQuNC1INGB0LDQsdC2?=\r\n =?UTF-8?B?0LXQutGC0Ysg0Lgg0Y7QvdC40LrQvtC0IOKYoA==?=' eq_(u'Проверяем русские сабжекты и юникод ☠', encodedword.mime_to_unicode(v)) v = '=?utf-8?Q?Evaneos-Concepci=C3=B3n.pdf?=' eq_(u'Evaneos-Concepción.pdf', encodedword.mime_to_unicode(v)) v = u'=?gb2312?Q?Hey_There=D7=B2=D8=B0?=' eq_(u'Hey There撞匕', encodedword.mime_to_unicode(v)) v = u'=?gb18030?Q?Hey_There=D7=B2=D8=B0?=' eq_(u'Hey There撞匕', encodedword.mime_to_unicode(v)) v = parse(u'Тест длинного дисплей нейма <*****@*****.**>') eq_(v.display_name, encodedword.mime_to_unicode(v.ace_display_name))
def neutral_headings_test(): v = """from mail-iy0-f179.google.com (mail-iy0-f179.google.com \t[209.85.210.179]) \tby mxa.mailgun.org (Postfix) with ESMTP id 2D0D3F01116 \tfor <*****@*****.**>; Fri, 17 Dec 2010 12:50:07 +0000 (UTC)""" eq_( u"from mail-iy0-f179.google.com (mail-iy0-f179.google.com\t[209.85.210.179])\tby mxa.mailgun.org (Postfix) with ESMTP id 2D0D3F01116\tfor <*****@*****.**>; Fri, 17 Dec 2010 12:50:07 +0000 (UTC)", encodedword.mime_to_unicode(v), ) v = '''multipart/mixed; boundary="===============7553021138737466228=="''' eq_(v, encodedword.mime_to_unicode(v))
def happy_mime_to_unicode_test(): v = """ =?utf-8?B?U2ltcGxlIHRleHQuIEhvdyBhcmUgeW91PyDQmtCw0Log0YLRiyDQv9C+0LY=?=\n =?utf-8?B?0LjQstCw0LXRiNGMPw==?=""" eq_(u'Simple text. How are you? Как ты поживаешь?', encodedword.mime_to_unicode(v)) v = ' =?US-ASCII?Q?Foo?= <*****@*****.**>' eq_(u'Foo <*****@*****.**>', encodedword.mime_to_unicode(v)) v = '''=?UTF-8?Q?=D1=80=D1=83=D1=81=D1=81=D0=BA=D0=B8=D0=B9?=\n =?UTF-8?Q?_=D0=B8?= english112 =?UTF-8?Q?=D1=81=D0=B0=D0=B1=D0=B6?= subject''' eq_(u'русский и english112 сабж subject', encodedword.mime_to_unicode(v)) v = '=?iso-8859-1?B?SOlhdnkgTel05WwgVW7uY/hk?=\n\t=?iso-8859-1?Q?=E9?=' eq_(u'Héavy Métål Unîcødé', encodedword.mime_to_unicode(v))
def aol_encodings_test(): v = ''' =?utf-8?Q?=D0=AD=D1=82=D0=BE_=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD?= =?utf-8?Q?=D0=B8=D0=B5_=D1=81_=D0=B4=D0=BB=D0=B8=D0=BD=D0=BD=D1=8B=D0=BC?= =?utf-8?Q?_=D1=81=D0=B0=D0=B1=D0=B6=D0=B5=D0=BA=D1=82=D0=BE=D0=BC_=D1=81?= =?utf-8?Q?=D0=BF=D0=B5=D1=86=D0=B8=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE_=D1=87?= =?utf-8?Q?=D1=82=D0=BE=D0=B1=D1=8B_=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80?= =?utf-8?Q?=D0=B8=D1=82=D1=8C_=D0=BA=D0=BE=D0=B4=D0=B8=D1=80=D0=BE=D0=B2?= =?utf-8?Q?=D0=BA=D0=B8?=''' eq_(u"Это сообщение с длинным сабжектом специально чтобы проверить кодировки", encodedword.mime_to_unicode(v))
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 concatenate(parts): """ Concatenates splitted parts of a parameter in a single parameter, e.g. URL*0="ftp://"; URL*1="cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar" becomes: URL="ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar" """ part = parts[0] if is_old_style(part): # old-style parameters do not support any continuations return encodedword.mime_to_unicode(get_value(part)) else: return "".join( decode_new_style(p) for p in partition(parts))
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 various_encodings_test(): v = '"=?utf-8?b?6ICD5Y+W5YiG5Lqr?=" <*****@*****.**>' eq_(u'"考取分享" <*****@*****.**>', encodedword.mime_to_unicode(v)) v = """=?UTF-8?B?0JbQtdC60LA=?= <*****@*****.**>, =?UTF-8?B?0JrQvtC90YbQtdCy0L7QuQ==?= <*****@*****.**>""" eq_(u"Жека <*****@*****.**>, Концевой <*****@*****.**>", encodedword.mime_to_unicode(v)) v = encodedword.mime_to_unicode( "=?utf-8?b?0JrQvtC90YbQtdCy0L7QuQ==?= <*****@*****.**>, Bob <*****@*****.**>, =?utf-8?b?0JLQuNC90YE=?= <*****@*****.**>" ) eq_(u"Концевой <*****@*****.**>, Bob <*****@*****.**>, Винс <*****@*****.**>", v) v = "=?UTF-8?B?0J/RgNC+0LLQtdGA0Y/QtdC8INGA0YPRgdGB0LrQuNC1INGB0LDQsdC2?=\n =?UTF-8?B?0LXQutGC0Ysg0Lgg0Y7QvdC40LrQvtC0IOKYoA==?=" eq_(u"Проверяем русские сабжекты и юникод ☠", encodedword.mime_to_unicode(v)) v = "=?UTF-8?B?0J/RgNC+0LLQtdGA0Y/QtdC8INGA0YPRgdGB0LrQuNC1INGB0LDQsdC2?=\r\n =?UTF-8?B?0LXQutGC0Ysg0Lgg0Y7QvdC40LrQvtC0IOKYoA==?=" eq_(u"Проверяем русские сабжекты и юникод ☠", encodedword.mime_to_unicode(v)) v = u"=?utf-8?Q?Evaneos-Concepci=C3=B3n.pdf?=" eq_(u"Evaneos-Concepción.pdf", encodedword.mime_to_unicode(v))
def missing_padding_mime_to_unicode_test(): v = """ =?utf-8?B?U2ltcGxlIHRleHQuIEhvdyBhcmUgeW91PyDQmtCw0Log0YLRiyDQv9C+0LY?=\n =?utf-8?B?0LjQstCw0LXRiNGMPw?=""" eq_(u'Simple text. How are you? Как ты поживаешь?', encodedword.mime_to_unicode(v))
def gmail_encodings_test(): v = ''' =?KOI8-R?B?/NTPINPPz8Ldxc7JxSDTIMTMyc7O2c0g08HC1g==?= =?KOI8-R?B?xcvUz80g09DFw8nBzNjOzyDe1M/C2SDQ0s/XxdLJ1A==?= =?KOI8-R?B?2CDLz8TJ0s/Xy8k=?=''' eq_(u"Это сообщение с длинным сабжектом специально чтобы проверить кодировки", encodedword.mime_to_unicode(v))
def lying_encodings_mime_to_unicode_test(): v = '''=?US-ASCII?Q?=D1=80=D1=83=D1=81=D1=81=D0=BA=D0=B8=D0=B9?=\n english112 =?UTF-8?Q?=D1=81=D0=B0=D0=B1=D0=B6?= subject''' eq_(u'русский english112 сабж subject', encodedword.mime_to_unicode(v))
def hotmail_encodings_test(): v = ''' =?koi8-r?B?/NTPINPPz8LdxQ==?= =?koi8-r?B?zsnFINMgxMzJzg==?= =?koi8-r?B?ztnNINPBwtbFyw==?= =?koi8-r?B?1M/NINPQxcPJwQ==?= =?koi8-r?B?zNjOzyDe1M/C2Q==?= =?koi8-r?B?INDSz9fF0snU2A==?= =?koi8-r?B?IMvPxMnSz9fLyQ==?=''' eq_(u"Это сообщение с длинным сабжектом специально чтобы проверить кодировки", encodedword.mime_to_unicode(v))
def test_error_reporting(): eq_("Sasha", encodedword.mime_to_unicode("Sasha"))
def yahoo_encodings_test(): v = ''' =?utf-8?B?0K3RgtC+INGB0L7QvtCx0YnQtdC90LjQtSDRgSDQtNC70LjQvdC90YvQvCA=?= =?utf-8?B?0YHQsNCx0LbQtdC60YLQvtC8INGB0L/QtdGG0LjQsNC70YzQvdC+INGH0YI=?= =?utf-8?B?0L7QsdGLINC/0YDQvtCy0LXRgNC40YLRjCDQutC+0LTQuNGA0L7QstC60Lg=?=''' eq_(u"Это сообщение с длинным сабжектом специально чтобы проверить кодировки", encodedword.mime_to_unicode(v))
def outlook_encodings_test(): v = '''=?koi8-r?B?/NTPINPPz8Ldxc7JxSDTIMTMyc7O2c0g08HC1sXL1M/NINPQxcPJwQ==?= =?koi8-r?B?zNjOzyDe1M/C2SDQ0s/XxdLJ1Nggy8/EydLP18vJ?=''' eq_(u"Это сообщение с длинным сабжектом специально чтобы проверить кодировки", encodedword.mime_to_unicode(v))
def outlook_encodings_test(): v = '''=?koi8-r?B?/NTPINPPz8Ldxc7JxSDTIMTMyc7O2c0g08HC1sXL1M/NINPQxcPJwQ==?= =?koi8-r?B?zNjOzyDe1M/C2SDQ0s/XxdLJ1Nggy8/EydLP18vJ?=''' eq_( u"Это сообщение с длинным сабжектом специально чтобы проверить кодировки", encodedword.mime_to_unicode(v))
def test_header_to_unicode(): assert_equal(u'Eugueny ώ Kontsevoy', mime_to_unicode("=?UTF-8?Q?Eugueny_=CF=8E_Kontsevoy?=") ) assert_equal(u'hello', mime_to_unicode("hello")) assert_equal(None, mime_to_unicode(None))
def test_header_to_unicode(): assert_equal(u'Eugueny ώ Kontsevoy', mime_to_unicode("=?UTF-8?Q?Eugueny_=CF=8E_Kontsevoy?=")) assert_equal(u'hello', mime_to_unicode("hello")) assert_equal(None, mime_to_unicode(None))
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)
def lying_encodings_mime_to_unicode_test(): v = """=?US-ASCII?Q?=D1=80=D1=83=D1=81=D1=81=D0=BA=D0=B8=D0=B9?=\n =?unknonw?Q?_=D0=B8?= english112 =?UTF-8?Q?=D1=81=D0=B0=D0=B1=D0=B6?= subject""" eq_(u"русский и english112 сабж subject", encodedword.mime_to_unicode(v))
def display_name(self): if self._display_name is None: return u'' return mime_to_unicode(self._display_name)