annotate tools/flag_data.py @ 973:6f55c086db1e
Guess file extension based on content-type.
When downloading a file, and no path is supplied to store it, guess the file
extension using mimetypes and the content-type header.
Also supply a unit test for the HotLinkImageForm.
author |
Brian Neal <bgneal@gmail.com> |
date |
Thu, 01 Oct 2015 19:44:45 -0500 |
parents |
678a1a2ef55a |
children |
|
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@609
|
11 import sys
|
bgneal@607
|
12 from xml.etree.ElementTree import ElementTree
|
bgneal@607
|
13
|
bgneal@607
|
14
|
bgneal@607
|
15 def main():
|
bgneal@607
|
16 parser = argparse.ArgumentParser(description=("Transform Debian's ISO-3166 "
|
bgneal@607
|
17 "data into JSON for use on SG101"))
|
bgneal@607
|
18 parser.add_argument('--xml', '-x', required=True, help='path to XML file')
|
bgneal@607
|
19 parser.add_argument('--icon-dir', '-i', required=True,
|
bgneal@607
|
20 help='path to icon directory')
|
bgneal@607
|
21
|
bgneal@607
|
22 args = parser.parse_args()
|
bgneal@607
|
23
|
bgneal@607
|
24 xml_file = os.path.expanduser(args.xml)
|
bgneal@607
|
25 icon_dir = os.path.expanduser(args.icon_dir)
|
bgneal@607
|
26
|
bgneal@607
|
27 with open(xml_file, 'r') as fp:
|
bgneal@607
|
28 et = ElementTree(file=fp)
|
bgneal@607
|
29
|
bgneal@607
|
30 country_data = {}
|
bgneal@607
|
31 for node in et.iterfind('iso_3166_entry'):
|
bgneal@607
|
32 code = node.get('alpha_2_code').lower()
|
bgneal@607
|
33 name = node.get('common_name', node.get('name'))
|
bgneal@607
|
34
|
bgneal@607
|
35 # see if we have an icon for this country
|
bgneal@607
|
36
|
bgneal@607
|
37 if not os.path.exists(os.path.join(icon_dir, '%s.png' % code)):
|
bgneal@609
|
38 sys.stderr.write("Could not find icon for %s (%s)\n" % (name, code))
|
bgneal@607
|
39 else:
|
bgneal@607
|
40 country_data[code] = name
|
bgneal@607
|
41
|
bgneal@607
|
42 s = json.dumps(country_data, indent=4, sort_keys=True, ensure_ascii=False)
|
bgneal@607
|
43 print s.encode('utf-8')
|
bgneal@607
|
44
|
bgneal@607
|
45
|
bgneal@607
|
46 if __name__ == '__main__':
|
bgneal@607
|
47 main()
|