annotate tools/flag_data.py @ 943:cf9918328c64

Haystack tweaks for Django 1.7.7. I had to upgrade to Haystack 2.3.1 to get it to work with Django 1.7.7. I also had to update the Xapian backend. But I ran into problems. On my laptop anyway (Ubuntu 14.0.4), xapian gets mad when search terms are greater than 245 chars (or something) when indexing. So I created a custom field that would simply omit terms greater than 64 chars and used this field everywhere I previously used a CharField. Secondly, the custom search form was broken now. Something changed in the Xapian backend and exact searches stopped working. Fortunately the auto_query (which I was using originally and broke during an upgrade) started working again. So I cut the search form back over to doing an auto_query. I kept the form the same (3 fields) because I didn't want to change the form and I think it's better that way.
author Brian Neal <bgneal@gmail.com>
date Wed, 13 May 2015 20:25:07 -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()