File: encoding.py

package info (click to toggle)
python-scrapy 0.14.4-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,064 kB
  • sloc: python: 19,468; xml: 199; sh: 134; makefile: 67
file content (20 lines) | stat: -rw-r--r-- 646 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import codecs

from scrapy.conf import settings

_ENCODING_ALIASES = dict(settings['ENCODING_ALIASES_BASE'])
_ENCODING_ALIASES.update(settings['ENCODING_ALIASES'])

def encoding_exists(encoding, _aliases=_ENCODING_ALIASES):
    """Returns ``True`` if encoding is valid, otherwise returns ``False``"""
    try:
        codecs.lookup(resolve_encoding(encoding, _aliases))
    except LookupError:
        return False
    return True

def resolve_encoding(alias, _aliases=_ENCODING_ALIASES):
    """Return the encoding the given alias maps to, or the alias as passed if
    no mapping is found.
    """
    return _aliases.get(alias.lower(), alias)