forked from oeg-upm/foops_documentation_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttl_catalog.py
168 lines (133 loc) · 5.45 KB
/
ttl_catalog.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
from rdflib import Graph
import pystache
import os
import configparser
# query para extraer los tres datos que necesitamos de cada ttl para mostrar en el catálogo de test y de metrics
query = """
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ftr: <https://www.w3id.org/ftr#>
PREFIX dcat: <http://www.w3.org/ns/dcat#>
SELECT DISTINCT ?s ?title ?label ?version ?keywords ?license ?license_label
WHERE {
?s a ftr:Test .
?s dcterms:title ?title .
?s rdfs:label ?label .
?s dcat:version ?version .
?s dcat:keyword ?keywords .
?s dcterms:license ?license .
}
"""
query_metric = """
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dqv: <http://www.w3.org/ns/dqv#>
PREFIX dcat: <http://www.w3.org/ns/dcat#>
SELECT DISTINCT ?s ?title ?label ?version ?keywords ?license ?license_label
WHERE {
?s a dqv:Metric .
?s dcterms:title ?title .
?s rdfs:label ?label .
?s dcat:version ?version .
?s dcat:keyword ?keywords .
?s dcterms:license ?license .
}
"""
query_benchmark = """
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dqv: <http://www.w3.org/ns/dqv#>
PREFIX dcat: <http://www.w3.org/ns/dcat#>
PREFIX ftr: <https://www.w3id.org/ftr#>
SELECT DISTINCT ?s ?title ?label ?version ?keywords ?license ?license_label
WHERE {
?s a ftr:Benchmark .
?s dcterms:title ?title .
?s rdfs:label ?label .
?s dcat:version ?version .
?s dcat:keyword ?keywords .
?s dcterms:license ?license .
}
"""
def ttl_to_item_catalogue(path_ttl, query):
g = Graph()
g.parse(path_ttl, format="turtle")
# Ejecutar la consulta
results = g.query(query)
data = {}
keywords = []
for row in results:
data = {
'identifier': row.s,
'title': row.title,
'name': row.label,
'version': row.version,
'license': row.license
}
# transform uri license in label license more readable
label_license = ""
if row.license and row.license.strip() != "":
parts_uri = row.license.strip('/').split('/')
if "creativecommons" in row.license.lower():
label_license = ('CC-' + '-'.join(parts_uri[-2:])).upper()
else:
label_license = ('-'.join(parts_uri[-2:])).upper()
data['license_label'] = label_license
if str(row.keywords) not in keywords:
keywords.append(str(row.keywords))
all_keywords = ", ".join(keywords)
data['keywords'] = all_keywords
return data
def item_to_list(path, list, query):
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith(".ttl"):
# si encontramos el archivo ttl podemos llamar a las funciones de transformacion
path_ttl = os.path.join(root, file)
list.append(ttl_to_item_catalogue(path_ttl, query))
# recorrer las carpetas desde la raiz y obtener los que tengan un ttl
# github paths
# path_ttls = 'https://github.com/oeg-upm/fair_ontologies/tree/main/doc/test/'
# path_ttls_metric = 'https://github.com/oeg-upm/fair_ontologies/tree/main/doc/metric/'
# path_ttls_benchmark = 'https://github.com/oeg-upm/fair_ontologies/tree/main/doc/benchmark/'
# path_mustache = 'https://github.com/oeg-upm/fair_ontologies/tree/main/doc/template_catalog.html'
# path_catalogo = 'https://github.com/oeg-upm/fair_ontologies/tree/main/doc/catalog.html'
# path_ttls = '/Users/mbp_jjm/Documents/DOCUMENTACION UPM/Fair_Ontologies/doc/test/'
# path_ttls_metric = '/Users/mbp_jjm/Documents/DOCUMENTACION UPM/Fair_Ontologies/doc/metric/'
# path_ttls_benchmark = '/Users/mbp_jjm/Documents/DOCUMENTACION UPM/Fair_Ontologies/doc/benchmark/'
# path_mustache = '/Users/mbp_jjm/Documents/DOCUMENTACION UPM/Fair_Ontologies/doc/template_catalog.html'
# path_catalogo = '/Users/mbp_jjm/Documents/DOCUMENTACION UPM/Fair_Ontologies/doc/catalog.html'
# Cargar la configuración
config = configparser.ConfigParser()
config.read('config.ini')
# get paths
# template mustache
path_mustache_catalogo = config.get(
'Paths', 'path_mustache_catalogo').strip('"')
# ttls test, metrics and benchmark
path_ttls = config.get('Paths', 'path_ttls').strip('"')
path_ttls_benchmarks = config.get('Paths', 'path_ttls_benchmarks').strip('"')
path_ttls_metrics = config.get('Paths', 'path_ttls_metrics').strip('"')
# html catalog
path_catalogo = config.get('Paths', 'path_catalogo').strip('"')
tests = []
metrics = []
benchmarks = []
item_to_list(path_ttls, tests, query)
item_to_list(path_ttls_metrics, metrics, query_metric)
item_to_list(path_ttls_benchmarks, benchmarks, query_benchmark)
# sorted list of test and metrics by name
tests_sorted = sorted(tests, key=lambda x: x["name"])
metrics_sorted = sorted(metrics, key=lambda x: x["name"])
benchmarks_sorted = sorted(benchmarks, key=lambda x: x["name"])
# extraer su uri, name y descrpción. El identificador deberá tener como href el html creado en el proceso previo
with open(path_mustache_catalogo, 'r') as template_file:
template_content = template_file.read()
# sustituir la plantilla con los datos del diccionario
renderer = pystache.Renderer()
rendered_output = renderer.render(
template_content, {'tests': tests_sorted,
'metrics': metrics_sorted, 'benchmarks': benchmarks_sorted}
)
with open(path_catalogo, 'w') as output_file:
output_file.write(rendered_output)