1 | """Tools to create country lists. |
---|
2 | |
---|
3 | Helpers that can retrieve files from ISO servers with current ISO |
---|
4 | codes and country names. |
---|
5 | |
---|
6 | Also helpers for turning this lists into a runnable Python module |
---|
7 | (countries.py) are provided. |
---|
8 | |
---|
9 | These helpers are meant to be used from the debug schell. You can use |
---|
10 | them like this:: |
---|
11 | |
---|
12 | $ ./bin/kofactl debug |
---|
13 | >>> from waeup.kofa.utils.country_helpers import update_list |
---|
14 | >>> from waeup.kofa.utils.country_helpers import update_module |
---|
15 | >>> update_list() |
---|
16 | >>> update_module() |
---|
17 | |
---|
18 | This will retrieve a current list of countries and their ISO codes |
---|
19 | from iso-committee servers, store the file locally (countries.txt) and |
---|
20 | afterwards compile a valid Python module (countries.py) out of the |
---|
21 | list. |
---|
22 | |
---|
23 | """ |
---|
24 | import datetime |
---|
25 | import os |
---|
26 | import urllib |
---|
27 | |
---|
28 | SRC_URL = 'http://www.iso.org/iso/list-en1-semic-3.txt' |
---|
29 | COUNTRY_LIST = os.path.join(os.path.dirname(__file__), 'countries.txt') |
---|
30 | COUNTRY_MODULE = os.path.join(os.path.dirname(__file__), 'countries.py') |
---|
31 | |
---|
32 | TEMPLATE = """# -*- coding: utf-8 -*- |
---|
33 | # |
---|
34 | # This file was automatically generated. Please do not edit |
---|
35 | # |
---|
36 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
37 | |
---|
38 | COUNTRIES = ${COUNTRY_LIST} |
---|
39 | """ |
---|
40 | |
---|
41 | def mangle_country_name(name): |
---|
42 | """Turn uppercase country names into capitalized. |
---|
43 | |
---|
44 | Some special cases are handled as well ('of', 'the', etc.) |
---|
45 | """ |
---|
46 | name = name.replace('(', '( ') # handle letters following brackets |
---|
47 | name = ' '.join([x.capitalize() for x in name.split(' ')]) |
---|
48 | name = name.replace('( ', '(') |
---|
49 | for term in ['Of', ' And', 'The']: |
---|
50 | name = name.replace(term, term.lower()) |
---|
51 | name = name.replace('U.s.', 'U.S.') |
---|
52 | name = name.replace("D'i", "d'I") # Cote d'Ivoire |
---|
53 | return name |
---|
54 | |
---|
55 | def update_list(): |
---|
56 | """Update the local country.txt list. |
---|
57 | |
---|
58 | File is received from iso-servers. |
---|
59 | """ |
---|
60 | print "Receiving list from %s" % SRC_URL |
---|
61 | contents = urllib.urlopen(SRC_URL).read() |
---|
62 | print "Storing list at %s" % COUNTRY_LIST |
---|
63 | fp = open(COUNTRY_LIST, 'wb') |
---|
64 | fp.write('Received on %s \r\nfrom %s\r\n-----------------\r\n' % ( |
---|
65 | datetime.datetime.now(), SRC_URL)) |
---|
66 | fp.write(contents) |
---|
67 | fp.close() |
---|
68 | print "Done." |
---|
69 | return |
---|
70 | |
---|
71 | def mangle_list(): |
---|
72 | """Read country list and extract countries and their ISO-codes. |
---|
73 | |
---|
74 | Returns an iterable of tuples (<COUNTRY>, <ISO-CODE>). |
---|
75 | """ |
---|
76 | lines = open(COUNTRY_LIST, 'rb').readlines() |
---|
77 | read_header = False |
---|
78 | for num, line in enumerate(lines): |
---|
79 | if read_header is False and line != '\r\n': |
---|
80 | continue |
---|
81 | if read_header is False: |
---|
82 | read_header = True |
---|
83 | continue |
---|
84 | if line == '\r\n': |
---|
85 | continue |
---|
86 | line = line.decode('iso-8859-1').strip() |
---|
87 | try: |
---|
88 | country, code = line.split(';', 1) |
---|
89 | except: |
---|
90 | print "Could not process: line %s:", num, line |
---|
91 | continue |
---|
92 | yield mangle_country_name(country), code |
---|
93 | |
---|
94 | def update_module(template=TEMPLATE, module_path=COUNTRY_MODULE): |
---|
95 | """Update local countries.py with data from countries.txt |
---|
96 | """ |
---|
97 | print "Parsing countries and their ISO codes..." |
---|
98 | c_list = "(\n" |
---|
99 | for country, code in mangle_list(): |
---|
100 | c_list = c_list + u' (_(u"%s"), u"%s"),\n' % (country, code) |
---|
101 | c_list += ')\n' |
---|
102 | contents = template.replace('${COUNTRY_LIST}', c_list) |
---|
103 | contents = contents.encode('utf-8') |
---|
104 | print "Write new module:", module_path |
---|
105 | open(module_path, 'wb').write(contents) |
---|
106 | print "Done." |
---|
107 | return |
---|