-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.py
59 lines (44 loc) · 1.89 KB
/
deploy.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
import json
import pathlib
from ssnolib import CACHE_DIR
from ssnolib.utils import download_file
__this_dir__ = pathlib.Path(__file__).parent
def generate_namespace_file():
"""Generate namespace.py file from ssno_context.jsonld"""
namespace = 'ssno'
context_file = CACHE_DIR / 'ssno_context.jsonld'
context_file.unlink(missing_ok=True) # force download
if not context_file.exists():
context_url = "https://raw.githubusercontent.com/matthiasprobst/ssno/main/ssno_context.jsonld"
context_file = download_file(context_url, context_file)
# read context file:
with open(context_file) as f:
context = json.load(f)
url = context['@context'][namespace]
iris = {}
for k, v in context['@context'].items():
if '@id' in v:
if namespace in v['@id']:
name = v["@id"].rsplit(":", 1)[-1]
if name not in iris:
iris[name] = {'url': f'{url}{name}', 'keys': [k, ]}
else:
iris[name]['keys'].append(k)
with open(__this_dir__ / 'ssnolib' / 'namespace.py',
'w',
encoding='UTF8') as f:
f.write('from rdflib.namespace import DefinedNamespace, Namespace\n')
f.write('from rdflib.term import URIRef\n')
f.write(f'\n\nclass {namespace.upper()}(DefinedNamespace):')
f.write('\n # uri = "https://matthiasprobst.github.io/ssno#"')
f.write('\n # Generated with ssnolib')
for k, v in iris.items():
f.write(f'\n {k}: URIRef # {v["keys"]}')
f.write(f'\n\n _NS = Namespace("{url}")')
f.write('\n\n')
for k, v in iris.items():
for kk in v["keys"]:
key = kk.replace(' ', '_')
f.write(f'\nsetattr({namespace.upper()}, "{key}", {namespace.upper()}.{k})')
if __name__ == '__main__':
generate_namespace_file()