This repository was archived by the owner on Apr 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate_jurisdictions.py
More file actions
79 lines (67 loc) · 2.82 KB
/
update_jurisdictions.py
File metadata and controls
79 lines (67 loc) · 2.82 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
import BeautifulSoup
import re
import os
import json
import sys
sys.path.insert(0, './license_xsl/licensexsl_tools')
import convert
def get_contents(soup, attr):
return str(soup(attr)[0].contents[0])
def license_versions_for_jurisdiction(license_type, soup, in_juri):
standard = soup('licenseclass', id=license_type)[0]
license2maxvers = {}
for lic in standard('license'):
lic_id = lic['id']
for juri in lic('jurisdiction'):
juri_id = juri['id']
if juri_id == in_juri: # right jurisdiction
for version in juri('version'):
version_id = version['id']
if license2maxvers.get(lic_id, '0') < version_id:
license2maxvers[lic_id] = version_id
return license2maxvers
def gen_jurisdiction_info():
soup = BeautifulSoup.BeautifulStoneSoup(open('license_xsl/licenses.xml'))
result = {}
for j_i in soup('jurisdiction-info'):
this_one = {}
this_ones_id = str(j_i['id'])
if this_ones_id:
if j_i['launched'] != 'true':
continue # next jurisdiction if this one isn't ready
#this_one['url'] = get_contents(j_i, 'uri')
available_versions = license_versions_for_jurisdiction(license_type='standard', soup=soup, in_juri=this_ones_id)
sampling_versions = license_versions_for_jurisdiction(license_type='sampling', soup=soup, in_juri=this_ones_id)
if available_versions:
this_one['version'] = str(max(available_versions.values()))
if sampling_versions:
this_one['sampling'] = str(max(sampling_versions.values()))
if this_ones_id == '-':
this_ones_id = 'generic'
this_one['generic'] = True
if this_ones_id == 'generic':
name = 'Unported'
else:
name = convert.country_id2name(country_id=this_ones_id, language='en_US').encode("ascii")
this_one['name'] = name
result[this_ones_id] = this_one
ret = json.write(result)
# Validate JSON generation
assert (json.read(ret) == result) # this includes a type check
return ret
def main():
modify_filename = 'js/cc-jurisdictions.js'
modify_me = open(modify_filename + '.in').read()
poss1 = '/* 8---< CUT HERE >----8 */'
poss2 = '/* --------------- FOLD HERE ---------------- */'
combined = '(' + re.escape(poss1) + '|' + re.escape(poss2) + ')'
parts = re.split(combined, modify_me)
assert(len(parts) == 5)
parts[2] = gen_jurisdiction_info()
result = '\n'.join(parts)
fd = open(modify_filename + '.tmp', 'w')
fd.write(result)
fd.close()
os.rename(modify_filename + '.tmp', modify_filename)
if __name__ == '__main__':
main()