-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make most of headers logic python 3 compatible
- Loading branch information
Showing
12 changed files
with
249 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,38 @@ | ||
import codecs | ||
|
||
import six | ||
|
||
from flanker.mime.message.utils import to_unicode | ||
|
||
_ALIASES = { | ||
'sjis': 'shift_jis', | ||
'windows-874': 'cp874', | ||
'koi8-r': 'koi8_r' | ||
} | ||
|
||
|
||
def convert_to_unicode(charset, value): | ||
#in case of unicode we have nothing to do | ||
if isinstance(value, six.text_type): | ||
return value | ||
|
||
charset = _translate_charset(charset) | ||
return to_unicode(value, charset=charset) | ||
|
||
if six.PY2: | ||
return value | ||
|
||
def _translate_charset(charset): | ||
"""Translates crappy charset into Python analogue (if supported). | ||
value = value.encode('ascii') | ||
|
||
Otherwise returns unmodified. | ||
""" | ||
# ev: (ticket #2819) | ||
if "sjis" in charset.lower(): | ||
return 'shift_jis' | ||
charset = _ensure_charset(charset) | ||
value = to_unicode(value, charset) | ||
return value | ||
|
||
# cp874 looks to be an alias for windows-874 | ||
if "windows-874" == charset.lower(): | ||
return "cp874" | ||
|
||
if 'koi8-r' in charset.lower(): | ||
return 'koi8_r' | ||
def _ensure_charset(charset): | ||
charset = charset.lower() | ||
try: | ||
codecs.lookup(charset) | ||
return charset | ||
except LookupError: | ||
pass | ||
|
||
if 'utf-8' in charset.lower() or charset.lower() == 'x-unknown': | ||
return 'utf-8' | ||
charset = _ALIASES.get(charset) | ||
if charset: | ||
return charset | ||
|
||
return charset | ||
return 'utf-8' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.