1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2026-04-26 16:41:29 +02:00

Cleanup get_path usage in favor of pkg_resources.

This commit is contained in:
2020-04-12 18:33:23 +02:00
parent 0bf43ec6e8
commit 5aa0b1a0eb
17 changed files with 532 additions and 277 deletions
+27 -3
View File
@@ -6,6 +6,8 @@ __license__ = 'GPL v3'
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
from collections import namedtuple
import json
import pkg_resources
from ebook_converter.utils.localization import canonicalize_lang
@@ -17,9 +19,31 @@ 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']
src = pkg_resources.resource_filename('ebook_converter',
'data/iso_3166-1.json')
with open(src, 'rb') as f:
db = json.load(f)
codes = set()
three_map = {}
name_map = {}
unicode_type = type(u'')
for x in db['3166-1']:
two = x.get('alpha_2')
if two:
two = unicode_type(two)
codes.add(two)
name_map[two] = x.get('name')
if name_map[two]:
name_map[two] = unicode_type(name_map[two])
three = x.get('alpha_3')
if three:
three_map[unicode_type(three)] = two
data = {'names': name_map,
'codes': frozenset(codes),
'three_map': three_map}
ccodes, ccodemap, country_names = (data['codes'], data['three_map'],
data['names'])
return ccodes, ccodemap