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