-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshapiro_util.py
66 lines (55 loc) · 2.06 KB
/
shapiro_util.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
from urllib.parse import urlparse
import colorlog
import logging
handler = colorlog.StreamHandler()
handler.setFormatter(
colorlog.ColoredFormatter(
"%(asctime)s %(log_color)s%(levelname)-8s %(name)-14s %(reset)s%(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
)
def get_logger(name: str):
logger = colorlog.getLogger(name)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
return logger
class BadSchemaException(Exception):
def __init__(self):
super().__init__()
class NotFoundException(Exception):
def __init__(self, content: str):
self.content = content
class ConflictingPropertyException(Exception):
def __init__(self, content: str):
self.content = content
def prefix(iri: str, name: str) -> str:
known = {
"http://www.w3.org/2000/01/rdf-schema#": "rdfs:",
"http://www.w3.org/2004/02/skos/core#": "skos:",
"http://purl.org/dc/terms/": "dct:",
"http://www.w3.org/2002/07/owl#": "owl:",
"http://www.w3.org/ns/shacl#": "shacl:",
"http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf:",
"http://schema.org": "schema:",
"http://www.w3.org/ns/adms#": "adms:",
"http://www.w3.org/2001/XMLSchema#": "xsd:",
"http://http://xmlns.com/foaf/0.1/": "foaf:",
"http://dbpedia.org/resource/": "dbpedia:",
"http://www.w3.org/ns/odrl1/2/": "odrl:",
"http://www.w3.org/ns/org#": "org:",
"http://www.w3.org/2006/time#": "time:",
"http://www.w3.org/TR/vocab-dcat-2/#": "dcat:",
"https://www.w3.org/ns/dcat#": "dcat:",
"http://purl.org/adms/status/": "adms:",
"http://xmlns.com/foaf/0.1/": "foaf:",
}
for k in known.keys():
if iri.lower().startswith(k.lower()):
return known[k] + name
return name
def prune_iri(iri: str, name_only: bool = False) -> str:
url = urlparse(iri)
result = url.path.strip("/")
result = result.split("/")[-1] if not url.fragment else url.fragment
if not name_only: result = prefix(iri, result)
return result