-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_code.py
More file actions
executable file
·540 lines (449 loc) · 18.2 KB
/
make_code.py
File metadata and controls
executable file
·540 lines (449 loc) · 18.2 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Copyright 2021-2025 Rumma & Ko Ltd
# License: GNU Affero General Public License v3 (see file COPYING for details)
"""
Started as a copy of lino_xl.lib.countries.wikidata
"""
import os
import json
import datetime
import importlib.metadata
import requests
import click
from pprint import pprint, pformat
from pathlib import Path
from bs4 import BeautifulSoup
from commondata.utils import Country, FIELDS, LANGUAGES, ENTITY_BASE
from commondata import __version__
dt = datetime.datetime
FILE_DIR = Path(os.path.abspath(__file__)).parent
PREAMBLE = f"""\
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# code generated by commondata {__version__} make_code.py
# fmt: off
"""
TRAILER = """
# end of generated code
"""
class WD:
countries = None
cities = None
not_found_threshold = 15
max_depth = 100
class Properties:
instance_of = "P31"
subclass_of = "P279"
country = "P17"
series_ordinal = "P1545"
iso_3166_1_alpha_2_code = "P297"
iso_3166_1_alpha_3_code = "P298"
postal_code = "P281"
contains_administrative_territorial_entity = "P150"
located_in_the_administrative_territorial_entity = "P131"
p = Properties
country = "Q6256"
sovereign_state = "Q3624078"
state = "Q7275"
city = "Q515"
human_settlement = "wd:Q486972"
administrative_subdivisions = [
'Q10864048', 'Q13220204', 'Q13221722', 'Q14757767', 'Q15640612',
'Q22927291'
]
first_level_administrative_country_subdivision = "Q10864048"
second_level_administrative_country_subdivision = "Q13220204"
third_level_administrative_country_subdivision = "Q13221722"
fourth_level_administrative_country_subdivision = "Q14757767"
fifth_level_administrative_country_subdivision = "Q15640612"
sixth_level_administrative_country_subdivision = "Q22927291"
administrative_territorial_entity_of_a_specific_level = "Q1799794"
iso2wikidata = {'ee': 'Q191', 'bd': 'Q902', 'be': 'Q31', 'ca': 'Q16'}
def write_wdqs_json(obj, file):
with open(file, 'w') as f:
json.dump(obj, f)
def read_wdqs_json(file):
with open(file, 'r') as f:
obj = json.load(f)
return obj
def query_wdqs(query):
url = "https://query.wikidata.org/sparql"
resp = requests.get(url, params={'format': 'json', 'query': query})
if resp.status_code == 429:
return []
return resp.json()['results']['bindings']
def parse_object(obj):
# if 'en' in languages:
# languages.remove('en')
# name = obj['entityLabel']['value'] # .lower()
# c = dict(name=name,
# entity=obj['entity']['value'].split('entity')[1].split('/')[1])
entity = obj['entity']['value']
if entity.startswith(ENTITY_BASE):
entity = entity[len(ENTITY_BASE):]
else:
raise Exception("Unexpected entity base {}".format(entity))
c = dict(entity=entity)
# for k in ("isoCode2", "isoCode3", "zipCode", "entityDescription"):
for k in ("isoCode2", "isoCode3", "zipCode", "population"):
if (v := obj.get(k, None)) is not None:
c[k] = v['value']
# isoCode3 = obj.get('isoCode3', None)
# if isoCode3 is not None:
# c['isoCode3'] = isoCode3['value']
#
# isoCode2 = obj.get('isoCode2', None)
# if isoCode2 is not None:
# c['isoCode2'] = isoCode2['value']
# desc = obj.get('entityDescription', None)
# if desc is not None:
# c['description'] = desc['value']
#
# zipCode = obj.get('zipCode', None)
# if zipCode is not None:
# c['zipCode'] = zipCode['value']
name = dict(en=obj['entityLabel']['value'])
desc = dict()
for lang in LANGUAGES:
v = obj.get('entity_' + lang, None)
if v is not None:
name[lang] = v['value']
# c['name_' + lang] = obj['entity_' + lang]['value'] # .lower()
v = obj.get('entityDesc_' + lang, None)
if v is not None:
desc[lang] = v['value']
# c['description_' + lang] = desc['value']
c.update(name=name, desc=desc)
return c
def build_query(entity='country', of=None, **kw):
# if 'en' in languages:
# languages.remove('en')
select = "SELECT ?entity ?entityLabel ?entityDescription ?population"
if entity == 'country':
select += " ?isoCode2 ?isoCode3"
if entity in ['human_settlement', 'admin_entity_contains']:
select += " ?zipCode"
other_langs = [lang for lang in LANGUAGES if lang != "en"]
for lang in other_langs:
select += " ?entity_" + lang + " ?entityDesc_" + lang
select += "\n"
where = "WHERE {\n"
if entity != "admin_entity_contains":
where += "\t?entity (wdt:" + WD.p.instance_of + "|wdt:" + WD.p.subclass_of + ") wd:"
if entity == 'country':
# where += WD.state + ".\n\t?entity wdt:P297 ?isoCode2.\n"
# where += WD.country + ".\n\t?entity wdt:P297 ?isoCode2.\n"
where += WD.sovereign_state + ".\n\t?entity wdt:P297 ?isoCode2.\n"
where += "\t?entity wdt:P298 ?isoCode3.\n"
where += "\t?entity wdt:P1082 ?population.\n"
elif of is None:
raise ValueError("Parameter of cannot be None.")
elif entity == 'city':
where += WD.city + ".\n\t?entity wdt:P17 wd:" + of + ".\n"
elif entity == 'country_subdivision':
series_ordinal = kw.get('series_ordinal', None)
if series_ordinal is None:
raise KeyError("series_ordinal must be provided is an argument.")
where += (WD.administrative_subdivisions[series_ordinal] + ".\n" +
"\t?entity wdt:P17 wd:" + of + ".\n")
elif entity == 'human_settlement':
subdivision = kw.get('subdivision', None)
if subdivision is None:
raise KeyError("subdivision must be provided is an argument.")
where += (subdivision + ".\n" + "\t?entity wdt:P17 wd:" + of + ".\n" +
"\tOPTIONAL {?entity wdt:P281 ?zipCode.}\n")
elif entity in ['admin_entity_contains']:
where += ("\t?entity wdt:P131 wd:" + of + ".\n" +
"\tOPTIONAL {?entity wdt:P281 ?zipCode.}\n")
else:
raise ValueError(
'Value of entity must be one of the following:\n' +
'"country", "city", "country_subdivision" or "human_settlement".')
where += "\tSERVICE wikibase:label { bd:serviceParam wikibase:language 'en'. }\n"
if len(other_langs) > 1:
where += "\tOPTIONAL {\n"
for lang in other_langs:
where += ("\t\tSERVICE wikibase:label {\n" +
"\t\t\tbd:serviceParam wikibase:language '" + lang +
"'.\n" + "\t\t\t?entity rdfs:label ?entity_" + lang +
".\n" + "\t\t\t?entity schema:description ?entityDesc_" +
lang + ".\n" + "\t\t} hint:Prior hint:runLast false.\n")
where += "\t}\n"
where += "}"
return select + where
def get_countries():
# languages = [lang.split('-')[0] for lang in languages]
# print("Getting countries data from WDQS server.")
q = build_query(entity='country')
# print("="*20)
# print(q)
# print("="*20)
resp = query_wdqs(q)
# pprint(resp)
# print("="*20)
# countries = dict()
# isoMap = dict()
#
iso2 = dict()
iso3 = dict()
for country in resp:
# isoCode2 = country['isoCode2']['value'].lower()
p = parse_object(country)
country = Country(*[p.get(k, None) for k in FIELDS])
if country.isoCode2 in iso2:
# raise Exception(
print("Duplicate isoCode2 {} for both {} and {}".format(
country.isoCode2, iso2[country.isoCode2], country))
continue
if country.isoCode3 in iso3:
# raise Exception(
print("Duplicate isoCode3 {} for both {} and {}".format(
country.isoCode3, iso3[country.isoCode3], country))
continue
iso2[country.isoCode2] = country
iso3[country.isoCode3] = country
yield country
# countries[isoCode2] = c
# isoMap[name.lower().replace(' ', '_')] = isoCode2
# from collections import namedtuple
# Place = namedtuple("Place", FIELDS)
def _query_cities(entity):
q = build_query(entity='city', of=entity)
resp = query_wdqs(q)
cities = dict()
for city in resp:
name, c = parse_object(city)
cities[name.lower().replace(" ", '_')] = c
return cities
def get_cities(of=[], filename='cities.json', force_update=False):
filepath = FILE_DIR / filename
results = dict()
if not force_update and os.path.isfile(filepath):
obj = read_wdqs_json(filepath)
if not of:
return obj
new_langs = [
lang for lang in languages if lang not in obj['languages']
]
languages = new_langs + obj['languages']
for iso in of:
entity = country_lookup(iso, languages)
if iso not in obj['countries']:
print("Getting cities data from WDQS server for ",
entity + ":" + iso, " (iso code)")
cities = _query_cities(entity, languages)
obj['countries'].append(iso)
obj['updated'] = dt.timestamp(dt.utcnow())
elif new_langs:
print("Getting cities data from WDQS server for ",
entity + ":" + iso, " (iso code)")
cities = _query_cities(entity, languages)
else:
print("Read cities data from " + str(filepath) + " for ",
entity + ":" + iso, " (iso code)")
cities = obj['data'][iso]
results[iso] = cities
obj['languages'] = languages
obj['data'].update(results)
write_wdqs_json(obj, 'cities.json')
return results
else:
if not of:
raise ValueError("Parameter of cannot be empty.")
country_codes = []
for iso in of:
entity = country_lookup(iso, languages)
country_codes.append(iso)
print("Getting cities data from WDQS server for",
entity + ":" + iso, "(iso code)")
cities = _query_cities(entity, languages)
results[iso] = cities
if 'en' not in languages:
languages += ['en']
obj = dict(
title='cities',
languages=languages,
countries=country_codes,
updated=dt.timestamp(dt.utcnow()),
timeZone='UTC',
data=results,
)
write_wdqs_json(obj, 'cities.json')
return obj
def _query_subdivisions(country, query_by='bulk'):
subdivisions = dict()
subdivisionLevelMap = dict()
if query_by in ['bulk', 'subdivision']:
for i in range(1, 7):
q = build_query(entity='country_subdivision',
of=country,
series_ordinal=i - 1)
resp = query_wdqs(q)
if not len(resp):
continue
else:
if len(resp) == 1:
_, subdivision = parse_object(resp[0])
else:
subdivision = dict(name='Unknown', entity='Unknown')
arias = dict()
print(("Getting places by 'country subdivision' " +
"within subdivision: {name: " + subdivision['name'] +
", level: " + str(i) + "} from WDQS server."))
for sd in resp:
name, sda = parse_object(sd)
if len(resp) > 1:
arias[name.lower().replace(' ', '_')] = sda
q = build_query(entity='human_settlement',
of=country,
subdivision=sda['entity'])
rsp = query_wdqs(q)
for aria in rsp:
name, a = parse_object(aria)
arias[name.lower().replace(' ', '_')] = a
subdivision['data'] = arias
subdivisions[str(i)] = subdivision
if len(resp) == 1:
subdivisionLevelMap[subdivision['name']] = i
if query_by in ['bulk', 'admin_entity']: # This is expensive
def query_ad_ety(ety, name, controller):
q = build_query(entity='admin_entity_contains', of=ety)
print(("Getting places by 'administrative entity containing' " +
"within administrative entity: " + ety + ":" + name +
" of subdivision level: " + str(controller['level']) +
" from WDQS server."))
resp = query_wdqs(q)
controller_child = dict(level=controller['level'] + 1,
not_found_threshold=WD.not_found_threshold)
subdivision = subdivisions.get(str(controller['level']), None)
if subdivision is None:
subdivision = dict(data=dict(),
name='Unknown',
entity='Unknown')
for adety in resp:
name, ety = parse_object(adety)
subdivision['data'][name.lower().replace(' ', '_')] = ety
if controller_child[
'not_found_threshold'] != 0 and WD.max_depth > controller[
'level']:
query_ad_ety(ety['entity'], name, controller_child)
elif WD.max_depth == 100:
WD.max_depth = controller['level']
if len(resp):
subdivisions[str(controller['level'])] = subdivision
elif controller['not_found_threshold'] > 0:
controller['not_found_threshold'] -= 1
controller = dict(level=1, not_found_threshold=WD.not_found_threshold)
query_ad_ety(country, 'country', controller)
WD.max_depth = 100
obj = dict(title='human_settlements',
updated=dt.timestamp(dt.utcnow()),
timeZone='UTC',
data=subdivisions,
subdivisionLevelMap=subdivisionLevelMap)
return obj
def get_human_settlements(of,
series_ordinal=-1,
filename=None,
force_update=False):
country = country_lookup(of)
if filename is None:
filepath = FILE_DIR / ("human_settlements_in_" + of + ".json")
else:
filepath = FILE_DIR / filename
def ready_resp(langs):
print("Getting places within ", country, ":", of,
" (iso code) from WDQS server.")
obj = _query_subdivisions(country, langs)
obj['country'] = of
write_wdqs_json(obj, filepath)
return obj
if not force_update and os.path.isfile(filepath):
obj = read_wdqs_json(filepath)
new_langs = [
lang for lang in LANGUAGES if lang not in obj['languages']
]
if new_langs:
languages = obj['languages'] + new_langs
return ready_resp(languages)
elif series_ordinal == -1:
print("Read places within " + of + " (iso code) from file: " +
str(filepath))
return obj
else:
print(("Read places within " + of +
" (iso code) of subdivision level: " + series_ordinal +
" from file: " + str(filepath)))
return obj[series_ordinal - 1]
else:
return ready_resp(languages)
def write_code_file(filename, content_func, batch):
if filename.exists() and not batch:
msg = f"OK to overwrite {filename} ?"
click.echo(msg + " [Y/n]", nl=False)
c = click.getchar()
click.echo(c)
if c in "nN":
return
click.echo(f"Write {filename}...")
with open(filename, encoding='utf-8', mode='w') as f:
f.write(PREAMBLE + content_func() + TRAILER)
def countries():
__version__ = importlib.metadata.version("commondata")
# countries = list(get_countries())
countries = sorted(get_countries(), key=lambda c: c.isoCode2)
iso2entity = {c.isoCode2: c.entity for c in countries}
countries = pformat(countries)
iso2entity = pformat(iso2entity)
return f"""
from commondata.utils import Country, FIELDS
LANGUAGES = {LANGUAGES}
COUNTRIES = {countries}
ISO2ENTITY = {iso2entity}
"""
def peppolcodes():
DELIVERY_UNITS = dict()
url = "https://github.com/OpenPEPPOL/peppol-bis-invoice-3/raw/refs/heads/master/structure/codelist/UNECERec20-11e.xml"
res = requests.get(url)
assert res.status_code == 200
soup = BeautifulSoup(res.text, "xml")
# print(soup.contents)
codelist = soup.find("CodeList")
for ci in codelist.find_all("Code"):
# print(ci)
# print(ci.id.string, ci.find("name").string, ci.description)
u = (ci.Name.string, ci.Description.string if ci.Description else None)
DELIVERY_UNITS[ci.Id.string] = u
# EAS schemes:
# url = "https://docs.peppol.eu/edelivery/codelists/v9.0/Peppol%20Code%20Lists%20-%20Participant%20identifier%20schemes%20v9.0.json"
url = "https://docs.peppol.eu/edelivery/codelists/v9.2/Peppol%20Code%20Lists%20-%20Participant%20identifier%20schemes%20v9.2.json"
res = requests.get(url).json()
# pprint(res)
# for scheme in res['values']:
# if scheme['state'] == "active":
# print("{iso6523} {schemeid} {scheme-name}".format(**scheme))
COUNTRY2SCHEME = dict()
for scheme in res['values']:
if scheme['schemeid'].endswith(":VAT"):
COUNTRY2SCHEME[scheme['country']] = scheme['iso6523']
return f"""
COUNTRY2SCHEME = {pformat(COUNTRY2SCHEME)}
DELIVERY_UNITS = {pformat(DELIVERY_UNITS)}
"""
@click.command()
@click.option("--batch",
"-b",
default=False,
help="Whether to suppress any interactive questions.")
def main(batch):
write_code_file(FILE_DIR / "commondata/countries.py", countries, batch)
# get_cities(['bd', 'de'])
# for iso_code in ['bd', 'de', 'ee', 'be']:
# get_human_settlements(iso_code)
# get_human_settlements('be')
# _query_subdivisions(country_lookup('ee'), query_by='admin_entity')
write_code_file(FILE_DIR / "commondata/peppolcodes.py", peppolcodes, batch)
# write_code_file(FILE_DIR / "commondata/peppolunits.py", peppolunitcodes, batch)
if __name__ == '__main__':
main()