1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2025-12-28 20:42:27 +01:00
Files
ebook-converter/ebook_converter/spell/__init__.py
gryf 0f9792df36 Convert calibre modules to ebook_converter.
Here is the first batch of modules, which are needed for converting
several formats to LRF. Some of the logic has been change, more cleanups
will follow.
2020-04-19 15:16:48 +02:00

41 lines
1.2 KiB
Python

#!/usr/bin/env python2
# vim:fileencoding=utf-8
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
from collections import namedtuple
from ebook_converter.utils.localization import canonicalize_lang
DictionaryLocale = namedtuple('DictionaryLocale', 'langcode countrycode')
ccodes, ccodemap, country_names = None, None, None
def get_codes():
global ccodes, ccodemap, country_names
if ccodes is None:
from ebook_converter.utils.serialize import msgpack_loads
data = msgpack_loads(P('localization/iso3166.calibre_msgpack', allow_user_override=False, data=True))
ccodes, ccodemap, country_names = data['codes'], data['three_map'], data['names']
return ccodes, ccodemap
def parse_lang_code(raw):
raw = raw or ''
parts = raw.replace('_', '-').split('-')
lc = canonicalize_lang(parts[0])
if lc is None:
raise ValueError('Invalid language code: %r' % raw)
cc = None
if len(parts) > 1:
ccodes, ccodemap = get_codes()[:2]
q = parts[1].upper()
if q in ccodes:
cc = q
else:
cc = ccodemap.get(q, None)
return DictionaryLocale(lc, cc)