This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
578 lines (463 loc) · 16.7 KB
/
utils.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
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
import base64
import logging
import os
import pickle
import time
import markdown
import requests
from SPARQLWrapper import SPARQLWrapper, JSON, BASIC
from flask import g
from rdflib import Graph, SKOS, URIRef
from xml.dom.minidom import Document as xml_Document
import urllib
import re
from bs4 import BeautifulSoup
import vocprez._config as config
from . import source
__all__ = [
"cache_write",
"url_encode",
"sparql_query",
"draw_concept_hierarchy",
"get_graph",
"url_decode"
]
def cache_write(cache_object):
"""
Function to write object to cache if cache file is older than cache_hours.
"""
logging.debug("cache_write()")
# create dir if not there
if not os.path.isdir(os.path.dirname(config.CACHE_FILE)):
os.makedirs(os.path.dirname(config.CACHE_FILE))
with open(config.CACHE_FILE, "wb") as cache_file:
pickle.dump(cache_object, cache_file)
def cache_clear():
logging.debug("cache_clear()")
# clear the Flask cache
if hasattr(g, "VOCABS"):
g.VOCABS = None
# remove the pickle cache
if os.path.isfile(config.CACHE_FILE):
os.unlink(config.CACHE_FILE)
def cache_load():
logging.debug("cache_load()")
if config.DEBUG:
logging.debug("DEBUG so purge cache")
cache_clear()
if hasattr(g, "VOCABS"):
logging.debug("have g, doing nothing")
pass
elif os.path.isfile(config.CACHE_FILE):
logging.debug("rebuild g from CACHE_FILE")
g.VOCABS = {}
cache_file_age = time.time() - os.stat(config.CACHE_FILE).st_mtime
if cache_file_age < config.CACHE_HOURS * 3600:
with open(config.CACHE_FILE, "rb") as f:
g.VOCABS = pickle.load(f)
else: # old cache file
logging.debug("rebuild g & CACHE_FILE from collect() methods")
cache_clear()
for source_details in config.DATA_SOURCES.values():
getattr(source, source_details["source"]).collect(source_details)
cache_write(g.VOCABS)
else:
logging.debug("build g & CACHE_FILE from collect() methods")
g.VOCABS = {}
for source_details in config.DATA_SOURCES.values():
getattr(source, source_details["source"]).collect(source_details)
cache_write(g.VOCABS)
def cache_reload():
"""Cache purging function used in multiple places"""
logging.debug("cache_reload()")
cache_clear()
cache_load()
def draw_concept_hierarchy(hierarchy):
tab = "\t"
previous_length = 1
text = ""
tracked_items = []
for item in hierarchy:
mult = None
if item[0] > previous_length + 2: # SPARQL query error on length value
for tracked_item in tracked_items:
if tracked_item["name"] == item[3]:
mult = tracked_item["indent"] + 1
if mult is None:
found = False
for tracked_item in tracked_items:
if tracked_item["name"] == item[3]:
found = True
if not found:
mult = 0
if mult is None: # else: # everything is normal
mult = item[0] - 1
t = tab * mult + "* [" + item[2] + "](" + get_content_uri(item[1]) + ")\n"
text += t
previous_length = mult
tracked_items.append({"name": item[1], "indent": mult})
return markdown.markdown(text)
def render_concept_tree(html_doc):
soup = BeautifulSoup(html_doc, "html.parser")
# concept_hierarchy = soup.find(id='concept-hierarchy')
uls = soup.find_all("ul")
for i, ul in enumerate(uls):
# Don't add HTML class nested to the first 'ul' found.
if not i == 0:
ul["class"] = "nested"
if ul.parent.name == "li":
temp = BeautifulSoup(str(ul.parent.a.extract()), "html.parser")
ul.parent.insert(
0, BeautifulSoup('<span class="caret">', "html.parser")
)
ul.parent.span.insert(0, temp)
return soup
def get_graph(endpoint, q, sparql_username=None, sparql_password=None):
"""
Function to return an rdflib Graph object containing the results of a query
"""
result_graph = Graph()
response = submit_sparql_query(
endpoint,
q,
sparql_username=sparql_username,
sparql_password=sparql_password,
accept_format="xml",
)
result_graph.parse(data=response)
return result_graph
def sparql_query(
q,
sparql_endpoint=config.SPARQL_ENDPOINT,
sparql_username=config.SPARQL_USERNAME,
sparql_password=config.SPARQL_PASSWORD):
sparql = SPARQLWrapper(sparql_endpoint)
sparql.setQuery(q)
sparql.setReturnFormat(JSON)
if sparql_username and sparql_password:
sparql.setHTTPAuth(BASIC)
sparql.setCredentials(sparql_username, sparql_password)
try:
r = sparql.queryAndConvert()
if isinstance(r, xml_Document):
def getText(node):
nodelist = node.childNodes
result = []
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
result.append(node.data)
return ''.join(result)
results = []
for result in r.getElementsByTagName('result'):
bindings = {}
for binding in result.getElementsByTagName('binding'):
for val in binding.childNodes:
bindings[binding.getAttribute("name")] = {
"type": "uri" if val.tagName == "uri" else "literal",
"value": getText(val)
}
results.append(bindings)
return results
elif isinstance(r, dict):
# JSON
return r["results"]["bindings"]
else:
raise Exception("Could not convert results from SPARQL endpoint")
except Exception as e:
logging.debug("SPARQL query failed: {}".format(e))
logging.debug(
"endpoint={}\nsparql_username={}\nsparql_password={}\n{}".format(
q, sparql_endpoint, sparql_username, sparql_password
)
)
return None
def submit_sparql_query(
endpoint, q, sparql_username=None, sparql_password=None, accept_format="json"
):
"""
Function to submit a sparql query and return the textual response
"""
accept_format = {
"json": "application/json",
"xml": "application/rdf+xml",
"turtle": "application/turtle",
}.get(accept_format) or "application/json"
headers = {
"Accept": accept_format,
"Content-Type": "application/sparql-query",
"Accept-Encoding": "UTF-8",
}
if sparql_username and sparql_password:
# logging.debug('Authenticating with username {} and password {}'.format(sparql_username, sparql_password))
headers["Authorization"] = "Basic " + base64.encodebytes(
"{}:{}".format(sparql_username, sparql_password).encode("utf-8")
).strip().decode("utf-8")
params = None
retries = 0
while True:
try:
response = requests.post(
endpoint,
headers=headers,
params=params,
data=q,
timeout=config.SPARQL_TIMEOUT,
)
# logging.debug('Response content: {}'.format(str(response.content)))
assert (
response.status_code == 200
), "Response status code {} != 200".format(response.status_code)
return response.text
except Exception as e:
logging.warning("SPARQL query failed: {}".format(e))
retries += 1
if retries <= config.MAX_RETRIES:
time.sleep(config.RETRY_SLEEP_SECONDS)
continue # Go around again
else:
break
raise (BaseException("SPARQL query failed"))
def get_prefLabel_from_uri(uri):
return " ".join(str(uri).split("#")[-1].split("/")[-1].split("_"))
def get_narrowers(uri, depth):
"""
Recursively get all skos:narrower properties as a list.
:param uri: URI node
:param depth: The current depth
:param g: The graph
:return: list of tuples(tree_depth, uri, prefLabel)
:rtype: list
"""
depth += 1
# Some RVA sources won't load on first try, so ..
# if failed, try load again.
g = None
max_attempts = 10
for i in range(max_attempts):
try:
g = Graph().parse(uri + ".ttl", format="turtle")
break
except:
logging.warning(
"Failed to load resource at URI {}. Attempt: {}.".format(uri, i + 1)
)
if not g:
raise Exception(
"Failed to load Graph from {}. Maximum attempts exceeded {}.".format(
uri, max_attempts
)
)
items = []
for s, p, o in g.triples((None, SKOS.broader, URIRef(uri))):
items.append((depth, str(s), get_prefLabel_from_uri(s)))
items.sort(key=lambda x: x[2])
count = 0
for item in items:
count += 1
new_items = get_narrowers(item[1], item[0])
items = items[:count] + new_items + items[count:]
count += len(new_items)
return items
def url_decode(s):
try:
return urllib.parse.unquote(s)
except:
pass
def url_encode(s):
try:
return urllib.parse.quote(s)
except:
pass
def make_title(s):
# make title from URI
title = " ".join(s.split("#")[-1].split("/")[-1].split("_")).title()
# replace dashes and periods with whitespace
title = re.sub("[-.]+", " ", title).title()
return title
def version():
from vocprez import __version__ as v
return v
def is_email(email):
"""
Check if the email is a valid email.
:param email: The email to be tested.
:return: True if the email matches the static regular expression, else false.
:rtype: bool
"""
pattern = r"[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
return True if re.search(pattern, email) is not None else False
def strip_mailto(email):
return email[7:]
def contains_mailto(email):
if email[:7] == "mailto:":
return True
return False
def is_url(url):
"""
Check if the url is a valid url.
:param url: The url to be tested.
:type url: str
:return: True if the url passes the validation, else false.
:rtype: bool
"""
if isinstance(url, URIRef):
return True
pattern = re.compile(
r"^(?:http|ftp)s?://" # http:// or https://
r"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|" # domain...
r"localhost|" # localhost...
r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})" # ...or ip
r"(?::\d+)?" # optional port
r"(?:/?|[/?]\S+)$",
re.IGNORECASE,
)
return True if re.search(pattern, url) is not None else False
def get_a_vocab_key():
"""
Get the first key from the g.VOCABS dictionary.
:return: Key name
:rtype: str
"""
try:
return next(iter(g.VOCABS))
except:
return None
def get_a_vocab_source_key():
"""
Get the first key from the config.VOCABS dictionary.
:return: Key name
:rtype: str
"""
try:
return next(iter(g.VOCABS))
except:
return None
def match(vocabs, query):
"""
Generate a generator of vocabulary items that match the search query
:param vocabs: The vocabulary list of items.
:param query: The search query string.
:return: A generator of words that match the search query.
:rtype: generator
"""
for word in vocabs:
if query.lower() in word.title.lower():
yield word
def parse_markdown(s):
return markdown.markdown(s)
def make_query_string(qsas: dict):
if not qsas:
return ""
pairs = []
for k, v in qsas.items():
pairs.append("{}={}".format(k, url_encode(v)))
return "&".join(sorted(pairs))
def get_system_uri(uri: str, qsas: dict = {}, system_uri_override=None):
s = make_query_string(qsas)
new_uri = uri.replace(config.ABSOLUTE_URI_BASE, config.SYSTEM_BASE_URI)
if s is not None and s != "":
return new_uri + "?" + s
else:
return new_uri
def get_absolute_uri(uri, qsas:dict = {}):
if "?" not in uri:
s = make_query_string(qsas)
return url_decode(uri) if s == "" else url_decode(uri) + "?" + make_query_string(qsas)
else:
# system_uri = uri.split("?")[0]
for qsa in sorted(uri.split("?")[1].split("&")):
kv = qsa.split("=")
# ensure that any given qsas vars override same vars in uri
if qsas.get(kv[0]) is None:
qsas[kv[0]] = kv[1]
if qsas.get("uri") is not None:
uri = url_decode(qsas["uri"])
del qsas["uri"]
else:
uri = uri.split("?")[0]
s = make_query_string(qsas)
return uri if s == "" else uri + "?" + make_query_string(qsas)
def get_content_uri(uri, qsas: dict = {}, system_uri_override=None):
if config.USE_SYSTEM_URIS:
return get_system_uri(uri, qsas=qsas, system_uri_override=system_uri_override)
else:
return get_absolute_uri(uri, qsas=qsas)
def get_alt_prof_uri(uri):
if hasattr(config, "USE_ABS_ALT_URI") and config.USE_ABS_ALT_URI:
return get_absolute_uri(uri, qsas={"_profile": "alt"})
else:
return get_content_uri(uri, qsas={"_profile": "alt"})
def get_vocab_uri_from_concept_uri(concept_uri):
new_uri = get_content_uri(concept_uri)
if config.USE_SYSTEM_URIS:
base = config.SYSTEM_BASE_URI
else:
base = config.ABSOLUTE_URI_BASE
if "/standard_name/" in concept_uri:
return base + "/standard_name/"
elif "/collection/" in concept_uri:
m = re.search(base + "/collection/([A-Z][0-9]{2})/current/", new_uri)
if m:
return m[0]
else:
return concept_uri
else:
return concept_uri
def get_vocab_id(vocab_or_concept_uri):
if "/standard_name/" in vocab_or_concept_uri:
return "standard_name"
m = re.search("collection\/([A-Z0-9]{3})\/current", vocab_or_concept_uri)
if m is not None:
return m[1]
m = re.search("scheme\/([A-Z\_]+)\/current", vocab_or_concept_uri)
if m is not None:
return m[1]
return "Ext"
def get_concept_id(concept_uri):
if "/standard_name/" in concept_uri:
return concept_uri.split("/standard_name/")[1].rstrip("/")
m = re.search("\/current\/([A-Z0-9\_]+)\/", concept_uri)
if m is not None:
return m[1]
return concept_uri.split("/current/")[-1].replace("/", "")
def get_pretty_mediatype(mediatype):
MEDIATYPE_NAMES = {
"text/html": "HTML",
"application/json": "JSON",
"text/turtle": "Turtle",
"application/rdf+xml": "RDF/XML",
"application/ld+json": "JSON-LD",
"text/n3": "N3",
"application/n-triples": "N-Triples",
}
return MEDIATYPE_NAMES.get(mediatype, mediatype)
def get_status_label(mediatype):
STATUSES = {
"http://www.opengis.net/def/status/accepted": "accepted",
"http://www.opengis.net/def/status/deprecated": "deprecated",
"http://www.opengis.net/def/status/experimental": "experimental",
"http://www.opengis.net/def/status/invalid": "invalid",
"http://www.opengis.net/def/status/notAccepted": "notAccepted",
"http://www.opengis.net/def/status/reserved": "reserved",
"http://www.opengis.net/def/status/retired": "retired",
"http://www.opengis.net/def/status/stable": "stable",
"http://www.opengis.net/def/status/submitted": "submitted",
"http://www.opengis.net/def/status/superseded": "superseded",
"http://www.opengis.net/def/status/valid": "valid",
}
return STATUSES.get(mediatype, mediatype)
def serialize_by_mediatype(g: Graph, mediatype: str, prefixes: dict = None) -> str:
if prefixes is not None:
for k, v in prefixes.items():
g.bind(k, v, replace=True)
if mediatype in ["application/rdf+json", "application/json"]:
return g.serialize(format="json-ld")
elif mediatype in ["application/rdf+xml", "application/xml", "text/xml"]:
return g.serialize(format="pretty-xml")
else:
return g.serialize(format=mediatype)
def suppressed_properties():
return [
"http://www.w3.org/2000/01/rdf-schema#label",
"http://www.w3.org/2004/02/skos/core#inScheme",
]