File: charsets.py

package info (click to toggle)
python-flanker 0.9.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,976 kB
  • sloc: python: 9,308; makefile: 4
file content (35 lines) | stat: -rw-r--r-- 627 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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):
    if isinstance(value, six.text_type):
        return value

    charset = _ensure_charset(charset)
    value = to_unicode(value, charset)
    return value


def _ensure_charset(charset):
    charset = charset.lower()
    try:
        codecs.lookup(charset)
        return charset
    except LookupError:
        pass

    charset = _ALIASES.get(charset)
    if charset:
        return charset

    return 'utf-8'