forked from fonttools/region-flags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regions.py
executable file
·191 lines (155 loc) · 5.21 KB
/
regions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python3
import re
import unicodedata
def load_aliases(filename):
return dict([
[x.strip() for x in line.split('\t')]
for line in open(filename, encoding='utf-8')
])
def load_region_entries(filename):
entries = []
entry = {}
fields = []
region_file_obj = open(filename, encoding='utf-8')
region_file_obj.readline()
region_file_obj.readline()
for line in region_file_obj:
if line.startswith('%%'):
entries.append(entry)
entry = {}
continue
if line.startswith(' '):
# Continuation
entry[fields[0]] += ' ' + line.strip()
continue
fields = [x.strip() for x in line.split(':')]
entry[fields[0]] = fields[1]
entries.append(entry)
return entries
def load_regions():
entries = []
entries.extend(load_region_entries('data/language-subtag-registry'))
entries.extend(load_region_entries('data/language-subtag-private'))
regions = {
e['Subtag']: e
for e in entries
if e['Type'] == 'region'
and len(e['Subtag']) == 2
and e['Description'] != 'Private use'
and 'Deprecated' not in e
}
for r_val_key in regions.values():
del r_val_key['Type']
del r_val_key['Subtag']
return regions
def strip_accents(s):
return ''.join(c
for c in unicodedata.normalize('NFD', s)
if unicodedata.category(c) != 'Mn'
)
def full_title(s):
parts = s.split(", ")
return " ".join(parts[::-1])
def strip_brackets(s):
return re.sub(r' \[.*\]', '', s)
def load_subregion_entries(filename):
entries = []
subregions_file_obj = open(filename, encoding='utf-8')
schema = [
'Subdivision category',
'3166-2 code',
'Subdivision name',
'Language code',
'Romanization system',
'Parent subdivision',
]
for line in subregions_file_obj:
if line.startswith(';') == False:
fields = [x for x in line.strip('\n').split('\t')]
entries.append({k: v for k, v in zip(schema, fields)})
return entries
def load_subregions():
subregions = {}
# US: States (50) and DC
subregions.update({
e['3166-2 code']: {
'Subdivision name': e['Subdivision name'],
}
for e in load_subregion_entries('data/iso-3166-2-us.tsv')
if e['Language code'] == 'en'
and e['Subdivision category'] in ['state', 'district']
})
# GB: Countries (3) and provinces (1)
subregions.update({
e['3166-2 code']: {
'Subdivision name': strip_brackets(e['Subdivision name']),
}
for e in load_subregion_entries('data/iso-3166-2-gb.tsv')
if e['Language code'] == 'en'
and e['Subdivision category'] in ['country', 'province']
})
# CA: Provinces (10) and territories (3)
subregions.update({
e['3166-2 code']: {
'Subdivision name': e['Subdivision name'],
}
for e in load_subregion_entries('data/iso-3166-2-ca.tsv')
if e['Language code'] == 'en'
and e['Subdivision category'] in ['province', 'territory']
})
# CO: departments (32) and capital districts (1)
subregions.update({
e['3166-2 code']: {
'Subdivision name': e['Subdivision name'],
}
for e in load_subregion_entries('data/iso-3166-2-co.tsv')
if e['Subdivision category'] in ['department', 'capital district']
})
# MX: States (31) and CDMX
subregions.update({
e['3166-2 code']: {
'Subdivision name': strip_accents(e['Subdivision name']),
}
for e in load_subregion_entries('data/iso-3166-2-mx.tsv')
if e['Language code'] == 'es'
and e['Subdivision category'] in ['state', 'federal district']
})
# ES: Autonomous communities(17) and autonomous cities in North Africa (2)
subregions.update({
e['3166-2 code'].rstrip("*"): {
'Subdivision name': full_title(strip_brackets(e['Subdivision name'])),
}
for e in load_subregion_entries('data/iso-3166-2-es.tsv')
if e['Subdivision category'] in ['autonomous community',
'autonomous city in North Africa']
and not e['Subdivision name'].endswith('*')
})
# AU: States (6) and territories (2)
subregions.update({
e['3166-2 code']: {
'Subdivision name': e['Subdivision name'],
}
for e in load_subregion_entries('data/iso-3166-2-au.tsv')
if e['Language code'] == 'en'
and e['Subdivision category'] in ['state', 'territory']
})
# DE: Lands (16)
subregions.update({
e['3166-2 code']: {
'Subdivision name': e['Subdivision name'],
}
for e in load_subregion_entries('data/iso-3166-2-de.tsv')
if e['Subdivision category'] in ['land']
})
return subregions
def load_all():
regions = load_regions()
keys = sorted(regions.keys())
for k in keys:
print('%s %s' % (k, regions[k]))
subregions = load_subregions()
keys = sorted(subregions.keys())
for k in keys:
print('%s %s' % (k, subregions[k]))
if __name__ == '__main__':
load_all()