source: main/waeup.kofa/branches/0.2/src/waeup/kofa/utils/country_helpers.py @ 17497

Last change on this file since 17497 was 8057, checked in by Henrik Bettermann, 13 years ago

Adjust copyright header and propset svn:keywords.

  • Property svn:keywords set to Id
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 = 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
55def 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
71def 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
94def 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
Note: See TracBrowser for help on using the repository browser.