source: main/waeup.kofa/trunk/src/waeup/kofa/utils/country_helpers.py @ 7962

Last change on this file since 7962 was 7962, checked in by uli, 13 years ago

Add tools for automatic creation of country lists.

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