From 0753e908ab456d8be6cc3815a55ada3cc323ed1c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 1 Mar 2014 11:48:40 +0530 Subject: [PATCH] Add ISO 3166 country codes --- setup/commands.py | 5 +- setup/install.py | 2 +- setup/iso3166.xml | 1710 +++++++++++++++++++++++++++++++++++++++++ setup/publish.py | 1 + setup/translations.py | 34 + 5 files changed, 1749 insertions(+), 3 deletions(-) create mode 100644 setup/iso3166.xml diff --git a/setup/commands.py b/setup/commands.py index 410b73d0263d..eb61b11260ae 100644 --- a/setup/commands.py +++ b/setup/commands.py @@ -7,7 +7,7 @@ __docformat__ = 'restructuredtext en' __all__ = [ - 'pot', 'translations', 'get_translations', 'iso639', + 'pot', 'translations', 'get_translations', 'iso639', 'iso3166', 'build', 'server', 'mathjax', 'gui', 'develop', 'install', @@ -25,11 +25,12 @@ ] -from setup.translations import POT, GetTranslations, Translations, ISO639 +from setup.translations import POT, GetTranslations, Translations, ISO639, ISO3166 pot = POT() translations = Translations() get_translations = GetTranslations() iso639 = ISO639() +iso3166 = ISO3166() from setup.extensions import Build build = Build() diff --git a/setup/install.py b/setup/install.py index df691ae0f7fe..a132f449e709 100644 --- a/setup/install.py +++ b/setup/install.py @@ -56,7 +56,7 @@ class Develop(Command): short_description = 'Setup a development environment for calibre' MODE = 0o755 - sub_commands = ['build', 'resources', 'iso639', 'gui',] + sub_commands = ['build', 'resources', 'iso639', 'iso3166', 'gui',] def add_postinstall_options(self, parser): parser.add_option('--make-errors-fatal', action='store_true', default=False, diff --git a/setup/iso3166.xml b/setup/iso3166.xml new file mode 100644 index 000000000000..229d1f8adcfa --- /dev/null +++ b/setup/iso3166.xml @@ -0,0 +1,1710 @@ + + + + + + + + + +]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/setup/publish.py b/setup/publish.py index ec4b7b31eb1b..e11dbaf8eb1f 100644 --- a/setup/publish.py +++ b/setup/publish.py @@ -22,6 +22,7 @@ class Stage1(Command): 'resources', 'translations', 'iso639', + 'iso3166', 'gui', ] diff --git a/setup/translations.py b/setup/translations.py index a7bf0b09715b..8708b8e2b48a 100644 --- a/setup/translations.py +++ b/setup/translations.py @@ -399,3 +399,37 @@ def clean(self): # }}} +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) +# }}}