-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
166 lines (135 loc) · 4.52 KB
/
helpers.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
import os
import csv
import json
import requests
import logging
from referencing import Registry, Resource, exceptions
from referencing.jsonschema import DRAFT202012
from rdflib.namespace import Namespace, RDF, RDFS, XSD, OWL, DCTERMS
from pylode.profiles.ontpub import OntPub
logger = logging.getLogger(__name__)
logging.getLogger("urllib3").setLevel(logging.WARNING)
REMOTE_SCHEMA_DIR = "https://raw.githubusercontent.com/openownership/data-standard/refs/heads/main/schema/"
SCHEMA_FILES = [
"components.json",
"entity-record.json",
"person-record.json",
"relationship-record.json",
"statement.json"
]
CODELIST_FILES = [
"addressType.csv",
"annotationMotivation.csv",
"directOrIndirect.csv",
"entitySubtype.csv",
"entityType.csv",
"interestType.csv",
"nameType.csv",
"personType.csv",
"recordStatus.csv",
"recordType.csv",
"securitiesIdentifierSchemes.csv",
"sourceType.csv",
"unspecifiedReason.csv"
]
def cap_first(s):
return s[:1].upper() + s[1:]
def get_remote_schemas():
schema_files = {}
for fn in SCHEMA_FILES:
r = requests.get(os.path.join(REMOTE_SCHEMA_DIR, fn))
j = r.json()
schema_files[fn] = j
return schema_files
def get_remote_codelists():
codelist_files = {}
for fn in CODELIST_FILES:
r = requests.get(os.path.join(REMOTE_SCHEMA_DIR, "codelists", fn))
t = r.text
codelist_files[fn] = t
return codelist_files
def get_schemas_and_codelists(schema_dir="schemas", overwrite=False):
"""
Fetch schema and codelist files from disc.
If they're not there, fetch from github and store them.
If overwrite = True, re-fetch them from github.
"""
schemas = {}
codelists = {}
for fn in SCHEMA_FILES:
fp = os.path.join(schema_dir, fn)
if not os.path.isfile(fp) or overwrite:
logger.info(f"Fetching {fn} from github")
r = requests.get(os.path.join(REMOTE_SCHEMA_DIR, fn))
schemas[fn] = r.json()
with open(fp, "w") as f:
f.write(r.json())
else:
logger.info(f"Using {fn} from cache")
with open(fp) as f:
schemas[fn] = json.load(f)
for fn in CODELIST_FILES:
fp = os.path.join(schema_dir, "codelists", fn)
if not os.path.isfile(fp) or overwrite:
logger.info(f"Fetching {fn} from github")
r = requests.get(os.path.join(REMOTE_SCHEMA_DIR, "codelists", fn))
codelists[fn] = r.text
with open(fp, "w") as f:
f.write(r.text)
else:
logger.info(f"Using {fn} from cache")
with open(fp) as f:
codelists[fn] = f.read()
return (schemas, codelists)
def get_codes(codelist_files, filename):
codes = []
cl = codelist_files.get(filename)
csvfile = cl.split("\n")
reader = csv.DictReader(csvfile)
for row in reader:
codes.append(row.get('code'))
return codes
def get_codes_and_info(codelist_files, filename):
codes = {}
cl = codelist_files.get(filename)
csvfile = cl.split("\n")
reader = csv.DictReader(csvfile)
for row in reader:
codes[row.get('code')] = (row.get('title'), row.get('description'))
return codes
def find_a_bit(registry, pointer):
# looks through all the schemas to resolve the pointer
# not smart enough to search subresources, so needs a full json pointer
if "urn:" in pointer:
# Get the root
schema = registry.get_or_retrieve(pointer)
return schema.value.pointer("", registry.resolver())
else:
for urn in registry:
schema = registry.get_or_retrieve(urn)
try:
return schema.value.pointer(pointer, registry.resolver())
except exceptions.PointerToNowhere:
pass
def get_properties(registry, pointer):
r = find_a_bit(registry, pointer)
try:
return [*r.contents.get("properties")]
except AttributeError:
return
def get_type(registry, pointer):
path = f"{pointer}/type"
bit = find_a_bit(registry, path)
return bit.contents
def schema_registry(schema_files):
schemas = []
for schema in schema_files.values():
schemas.append((schema.get("$id"),
Resource(contents=schema, specification=DRAFT202012)))
registry = Registry().with_resources(schemas)
return registry
def schema_resources(schema_files):
schemas = []
for schema in schema_files:
schemas.append(Resource(contents=schema, specification=DRAFT202012))
return schemas