-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmqa-scoring.py
163 lines (146 loc) · 5 KB
/
mqa-scoring.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
#!/usr/bin/env python3
'''
YODA (Your Open DAta)
EU CEF Action 2019-ES-IA-0121
University of Cantabria
Developer: Johnny Choque ([email protected])
'''
import requests
import json
from rdflib import Graph
import argparse
import mqaMetrics as mqa
import os
URL_EDP = 'https://data.europa.eu/api/mqa/shacl/validation/report'
HEADERS = {'content-type': 'application/rdf+xml'}
MACH_READ_FILE = os.path.join('edp-vocabularies', 'edp-machine-readable-format.rdf')
NON_PROP_FILE = os.path.join('edp-vocabularies', 'edp-non-proprietary-format.rdf')
def otherCases(pred, objs, g):
for obj in objs:
met = str_metric(obj, g)
if met == None:
print(' Result: WARN. Not included in MQA - '+ str_metric(pred, g))
else:
print(' Result: WARN. Not included in MQA - '+ str(met))
def str_metric(val, g):
valStr=str(val)
for prefix, ns in g.namespaces():
if val.find(ns) != -1:
metStr = valStr.replace(ns,prefix+":")
return metStr
def load_edp_vocabulary(file):
g = Graph()
g.parse(file, format="application/rdf+xml")
voc = []
for sub, pred, obj in g:
voc.append(str(sub))
return voc
def edp_validator(file, weight):
print('* SHACL validation')
try:
rdfFile = open(file, mode="r", encoding="utf-8")
except Exception as e:
raise SystemExit(e)
with rdfFile:
try:
payload = rdfFile.read().replace("\n", " ")
r_edp = requests.post(URL_EDP, data=payload.encode('utf-8'), headers=HEADERS)
r_edp.raise_for_status()
except requests.exceptions.HTTPError as err:
raise SystemExit(err)
report = json.loads(r_edp.text)
if valResult(report):
print(' Result: OK. The metadata has successfully passed the EDP validator. Weight assigned 30')
weight = weight + 30
else:
print(' Result: ERROR. DCAT-AP errors found in metadata')
return weight
def valResult(d):
if 'sh:conforms' in d:
return d['sh:conforms']
for k in d:
if isinstance(d[k], list):
for i in d[k]:
if 'sh:conforms' in i:
return i['sh:conforms']
def get_metrics(g):
metrics = {}
for sub, pred, obj in g:
if pred not in metrics.keys():
metrics[pred] = None
for pred in metrics.keys():
obj_list=[]
for obj in g.objects(predicate=pred):
obj_list.append(obj)
metrics[pred] = obj_list
return metrics
def main():
mach_read_voc = []
non_prop_voc = []
parser = argparse.ArgumentParser(description='Calculates the score obtained by a metadata according to the MQA methodology specified by data.europa.eu')
parser.add_argument('-f', '--file', type=str, required=True, help='RDF file to be validated')
args = parser.parse_args()
g = Graph()
g.parse(args.file, format="application/rdf+xml")
mach_read_voc = load_edp_vocabulary(MACH_READ_FILE)
non_prop_voc = load_edp_vocabulary(NON_PROP_FILE)
weight = 0
weight = edp_validator(args.file, weight)
print(' Current weight =',weight)
metrics = get_metrics(g)
f_res = {}
f_res = f_res.fromkeys(['result', 'url', 'weight'])
m_res = {}
m_res = m_res.fromkeys(['result', 'weight'])
for pred in metrics.keys():
met = str_metric(pred, g)
objs = metrics[pred]
print('*',met)
if met == "dcat:accessURL":
weight = mqa.accessURL(objs, weight)
elif met == "dcat:downloadURL":
weight = mqa.downloadURL(objs, weight)
elif met == "dcat:keyword":
weight = mqa.keyword(weight)
elif met == "dcat:theme":
weight = mqa.theme(weight)
elif met == "dct:spatial":
weight = mqa.spatial(weight)
elif met == "dct:temporal":
weight = mqa.temporal(weight)
elif met == "dct:format":
f_res = mqa.format(objs, mach_read_voc, non_prop_voc, weight)
weight = f_res['weight']
elif met == "dct:license":
weight = mqa.license(objs, weight)
elif met == "dcat:contactPoint":
weight = mqa.contactpoint(weight)
elif met == "dcat:mediaType":
m_res = mqa.mediatype(objs, weight)
weight = m_res['weight']
elif met == "dct:publisher":
weight = mqa.publisher(weight)
elif met == "dct:accessRights":
weight = mqa.accessrights(objs, weight)
elif met == "dct:issued":
weight = mqa.issued(weight)
elif met == "dct:modified":
weight = mqa.modified(weight)
elif met == "dct:rights":
weight = mqa.rights(weight)
elif met == "dcat:byteSize":
weight = mqa.byteSize(weight)
else:
otherCases(pred, objs, g)
print(' Current weight =',weight)
print('* dct:format & dcat:mediaType')
if f_res['result'] and m_res['result']:
weight = weight + 10
print(' Result: OK. The properties belong to a controlled vocabulary. Weight assigned 10')
print(' Current weight=',weight)
else:
print(' Result: WARN. The properties do not belong to a controlled vocabulary')
print('\n')
print('Overall MQA scoring:', str(weight))
if __name__ == "__main__":
main()