Add ISO 3166 country codes

This commit is contained in:
Kovid Goyal 2014-03-01 11:48:40 +05:30
parent b4f677c99d
commit 0753e908ab
5 changed files with 1749 additions and 3 deletions

View File

@ -7,7 +7,7 @@ __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
__all__ = [ __all__ = [
'pot', 'translations', 'get_translations', 'iso639', 'pot', 'translations', 'get_translations', 'iso639', 'iso3166',
'build', 'server', 'mathjax', 'build', 'server', 'mathjax',
'gui', 'gui',
'develop', 'install', 'develop', 'install',
@ -25,11 +25,12 @@ __all__ = [
] ]
from setup.translations import POT, GetTranslations, Translations, ISO639 from setup.translations import POT, GetTranslations, Translations, ISO639, ISO3166
pot = POT() pot = POT()
translations = Translations() translations = Translations()
get_translations = GetTranslations() get_translations = GetTranslations()
iso639 = ISO639() iso639 = ISO639()
iso3166 = ISO3166()
from setup.extensions import Build from setup.extensions import Build
build = Build() build = Build()

View File

@ -56,7 +56,7 @@ class Develop(Command):
short_description = 'Setup a development environment for calibre' short_description = 'Setup a development environment for calibre'
MODE = 0o755 MODE = 0o755
sub_commands = ['build', 'resources', 'iso639', 'gui',] sub_commands = ['build', 'resources', 'iso639', 'iso3166', 'gui',]
def add_postinstall_options(self, parser): def add_postinstall_options(self, parser):
parser.add_option('--make-errors-fatal', action='store_true', default=False, parser.add_option('--make-errors-fatal', action='store_true', default=False,

1710
setup/iso3166.xml Normal file

File diff suppressed because it is too large Load Diff

View File

@ -22,6 +22,7 @@ class Stage1(Command):
'resources', 'resources',
'translations', 'translations',
'iso639', 'iso639',
'iso3166',
'gui', 'gui',
] ]

View File

@ -399,3 +399,37 @@ class ISO639(Command): # {{{
# }}} # }}}
class ISO3166(ISO639): # {{{
description = 'Compile country code maps for performance'
DEST = os.path.join(os.path.dirname(POT.SRC), 'resources', 'localization',
'iso3166.pickle')
def run(self, opts):
src = self.j(self.d(self.SRC), 'setup', 'iso_639_3.xml')
if not os.path.exists(src):
raise Exception(src + ' does not exist')
dest = self.DEST
base = self.d(dest)
if not os.path.exists(base):
os.makedirs(base)
if not self.newer(dest, [src, __file__]):
self.info('Pickled code is up to date')
return
self.info('Pickling ISO-3166 codes to', dest)
from lxml import etree
root = etree.fromstring(open(src, 'rb').read())
codes = set()
three_map = {}
name_map = {}
for x in root.xpath('//iso_3166_entry'):
two = x.get('alpha_2_code', None)
three = x.get('alpha_3_code')
codes.add(two)
name_map[two] = x.get('name')
if three:
three_map[three] = two
from cPickle import dump
x = {'names':name_map, 'codes':frozenset(codes), 'three_map':three_map}
dump(x, open(dest, 'wb'), -1)
# }}}