Update ISO 3166 database

This commit is contained in:
Kovid Goyal
2019-11-02 09:31:05 +05:30
parent 1b93d540b7
commit 77af13c5f3
3 changed files with 1683 additions and 1716 deletions
-1710
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+6 -6
View File
@@ -791,7 +791,7 @@ class ISO3166(ISO639): # {{{
'iso3166.calibre_msgpack')
def run(self, opts):
src = self.j(self.d(self.SRC), 'setup', 'iso3166.xml')
src = self.j(self.d(self.SRC), 'setup', 'iso_3166-1.json')
if not os.path.exists(src):
raise Exception(src + ' does not exist')
dest = self.DEST
@@ -802,21 +802,21 @@ class ISO3166(ISO639): # {{{
self.info('Packed code is up to date')
return
self.info('Packing ISO-3166 codes to', dest)
from lxml import etree
root = etree.fromstring(open(src, 'rb').read())
with open(src, 'rb') as f:
db = json.load(f)
codes = set()
three_map = {}
name_map = {}
unicode_type = type(u'')
for x in root.xpath('//iso_3166_entry'):
two = x.get('alpha_2_code')
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_code')
three = x.get('alpha_3')
if three:
three_map[unicode_type(three)] = two
x = {u'names':name_map, u'codes':frozenset(codes), u'three_map':three_map}