Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9efdef1

Browse files
committed
Added parsing of ISO 3166 files
1 parent eee08cd commit 9efdef1

1 file changed

Lines changed: 104 additions & 7 deletions

File tree

Tools/world/world

Lines changed: 104 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/env python
1+
#! /usr/bin/env python1.5
22

33
"""Print the long name of an Internet domain.
44

@@ -17,31 +17,53 @@ in coordination with the ISO 3166 Maintenance Agency at DIN Berlin.
1717

1818
The latest known change to this information was:
1919

20-
Thu Feb 10 10:20:28 MET 1994
20+
Thu Aug 7 17:59:51 MET DST 1997
2121

2222
This script also knows about non-geographic top-level domains.
2323

24-
Usage: %s [-d] [-h] addr [addr ...]
24+
Usage: %s [-d] [-p|-P file] [-h] addr [addr ...]
2525

26-
-d (--dump) -- print mapping of all known top-level domains
27-
-h (--help) -- print this help message
26+
--dump
27+
-d
28+
Print mapping of all top-level domains.
29+
30+
--parse file
31+
--p file
32+
--P file
33+
--Parse file
34+
Parse an iso3166-countrycodes file (given as the argument).
35+
This first the two letter country code (it ignores the three
36+
letter code), followed by the country name. With -P option,
37+
output is in the form of a Python dictionary, and country
38+
names are normalized w.r.t. capitalization. This makes it
39+
appropriate for cutting and pasting back into this file.
40+
41+
-h
42+
--help
43+
Print this message.
2844

2945
"""
30-
__version__ = '1.0'
46+
__version__ = '2.0'
3147
__author__ = 'Barry Warsaw <[email protected]>'
3248
__source__ = '<url:http://www.python.org/~bwarsaw/pyware/>'
3349

3450

3551
import sys
3652
import string
3753
import getopt
54+
try:
55+
import re
56+
except ImportError:
57+
print 'Python 1.5 is required!'
58+
sys.exit(1)
3859

3960

4061

4162
def usage(status=0):
4263
print __doc__ % sys.argv[0]
4364
sys.exit(status)
4465

66+
4567
def resolve(rawaddr):
4668
parts = string.splitfields(rawaddr, '.')
4769
if not len(parts):
@@ -56,18 +78,90 @@ def resolve(rawaddr):
5678
print 'Where in the world is %s?' % rawaddr
5779

5880

81+
82+
def parse(file, normalize):
83+
try:
84+
fp = open(file)
85+
except IOError, (err, msg):
86+
print msg, ':', file
87+
88+
cre = re.compile('(.*?)[ \t]+([A-Z]{2})[ \t]+[A-Z]{3}[ \t]+[0-9]{3}')
89+
scanning = 0
90+
91+
if normalize:
92+
print 'country = {'
93+
94+
while 1:
95+
line = fp.readline()
96+
if line == '':
97+
break # EOF
98+
if scanning:
99+
mo = cre.match(line)
100+
if not mo:
101+
line = string.strip(line)
102+
if not line:
103+
continue
104+
elif line[0] == '-':
105+
break
106+
else:
107+
print 'Could not parse line:', line
108+
continue
109+
country, code = mo.group(1, 2)
110+
if normalize:
111+
words = string.split(country)
112+
for i in range(len(words)):
113+
w = words[i]
114+
# XXX special cases
115+
if w in ('AND', 'OF', 'OF)', 'name:', 'METROPOLITAN'):
116+
words[i] = string.lower(w)
117+
elif w == 'THE' and i <> 1:
118+
words[i] = string.lower(w)
119+
elif len(w) > 3 and w[1] == "'":
120+
words[i] = string.upper(w[0:3]) + \
121+
string.lower(w[3:])
122+
elif w == '(U.S.)':
123+
pass
124+
elif w[0] == '(' and w <> '(local':
125+
words[i] = '(' + string.capitalize(w[1:])
126+
elif string.find(w, '-'):
127+
words[i] = string.join(
128+
map(string.capitalize, string.split(w, '-')),
129+
'-')
130+
else:
131+
words[i] = string.capitalize(w)
132+
code = string.lower(code)
133+
country = string.join(words)
134+
print ' "%s": "%s",' % (code, country)
135+
else:
136+
print code, country
137+
138+
elif line[0] == '-':
139+
scanning = 1
140+
141+
if normalize:
142+
print ' }'
143+
59144

60145
def main():
61146
help = 0
62147
status = 0
63148
dump = 0
149+
parsefile = None
150+
normalize = 0
64151

65-
opts, args = getopt.getopt(sys.argv[1:], 'hd', ['help', 'dump'])
152+
opts, args = getopt.getopt(sys.argv[1:],
153+
'p:P:hd',
154+
['parse', 'Parse', 'PARSE', 'help', 'dump'])
66155
for arg, val in opts:
67156
if arg in ('-h', '--help'):
68157
help = 1
69158
elif arg in ('-d', '--dump'):
70159
dump = 1
160+
elif arg in ('-p', '--parse'):
161+
parsefile = val
162+
elif arg in ('-P', '--Parse', '--PARSE'):
163+
parsefile = val
164+
normalize = 1
71165

72166
if help:
73167
usage(status)
@@ -84,9 +178,12 @@ def main():
84178
codes.sort()
85179
for code in codes:
86180
print ' %2s:' % code, country[code]
181+
elif parsefile:
182+
parse(parsefile, normalize)
87183
else:
88184
map(resolve, args)
89185

186+
90187

91188
# The mappings
92189
nameorg = {

0 commit comments

Comments
 (0)