Mercurial > public > sg101
comparison tools/flag_data.py @ 607:34b1dd3f84fa
Created a tool to generate ISO-3166 country data as a JSON file.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 25 Jul 2012 19:40:36 -0500 |
parents | |
children | 678a1a2ef55a |
comparison
equal
deleted
inserted
replaced
606:c8b4dfb2d1e5 | 607:34b1dd3f84fa |
---|---|
1 """ | |
2 Application to read the ISO-3166 country data and output a JSON datastructure | |
3 for use in the SG101 code. We also print a report of any missing country | |
4 icons. If we don't have an icon for a country, we don't include it in our JSON | |
5 data. | |
6 | |
7 """ | |
8 import argparse | |
9 import json | |
10 import os.path | |
11 from xml.etree.ElementTree import ElementTree | |
12 | |
13 | |
14 def main(): | |
15 parser = argparse.ArgumentParser(description=("Transform Debian's ISO-3166 " | |
16 "data into JSON for use on SG101")) | |
17 parser.add_argument('--xml', '-x', required=True, help='path to XML file') | |
18 parser.add_argument('--icon-dir', '-i', required=True, | |
19 help='path to icon directory') | |
20 | |
21 args = parser.parse_args() | |
22 | |
23 xml_file = os.path.expanduser(args.xml) | |
24 icon_dir = os.path.expanduser(args.icon_dir) | |
25 | |
26 with open(xml_file, 'r') as fp: | |
27 et = ElementTree(file=fp) | |
28 | |
29 country_data = {} | |
30 for node in et.iterfind('iso_3166_entry'): | |
31 code = node.get('alpha_2_code').lower() | |
32 name = node.get('common_name', node.get('name')) | |
33 | |
34 # see if we have an icon for this country | |
35 | |
36 if not os.path.exists(os.path.join(icon_dir, '%s.png' % code)): | |
37 print "Could not find icon for %s (%s)" % (name, code) | |
38 else: | |
39 country_data[code] = name | |
40 | |
41 s = json.dumps(country_data, indent=4, sort_keys=True, ensure_ascii=False) | |
42 print s.encode('utf-8') | |
43 | |
44 | |
45 if __name__ == '__main__': | |
46 main() |