mustrd 0.2.6.1__py3-none-any.whl → 0.2.6.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mustrd/README.adoc +33 -0
- mustrd/__init__.py +1 -0
- mustrd/anzo_utils.py +123 -0
- mustrd/logger_setup.py +4 -0
- mustrd/model/triplestoreOntology.ttl +0 -8
- mustrd/model/triplestoreshapes.ttl +0 -3
- mustrd/mustrd.py +382 -208
- mustrd/mustrdAnzo.py +55 -130
- mustrd/mustrdGraphDb.py +3 -3
- mustrd/mustrdTestPlugin.py +278 -122
- mustrd/spec_component.py +16 -7
- mustrd/steprunner.py +16 -4
- {mustrd-0.2.6.1.dist-info → mustrd-0.2.6.3.dist-info}/METADATA +6 -7
- {mustrd-0.2.6.1.dist-info → mustrd-0.2.6.3.dist-info}/RECORD +17 -16
- {mustrd-0.2.6.1.dist-info → mustrd-0.2.6.3.dist-info}/WHEEL +1 -1
- mustrd/test/test_mustrd.py +0 -5
- {mustrd-0.2.6.1.dist-info → mustrd-0.2.6.3.dist-info}/LICENSE +0 -0
- {mustrd-0.2.6.1.dist-info → mustrd-0.2.6.3.dist-info}/entry_points.txt +0 -0
mustrd/mustrdAnzo.py
CHANGED
@@ -22,51 +22,24 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
22
|
SOFTWARE.
|
23
23
|
"""
|
24
24
|
|
25
|
-
import requests
|
26
|
-
from pyanzo import AnzoClient
|
27
25
|
from rdflib import Graph, ConjunctiveGraph, Literal, URIRef
|
28
|
-
from requests import ConnectTimeout,
|
29
|
-
from bs4 import BeautifulSoup
|
26
|
+
from requests import ConnectTimeout, HTTPError, ConnectionError
|
30
27
|
import logging
|
31
|
-
from .
|
32
|
-
|
33
|
-
|
34
|
-
# https://github.com/Semantic-partners/mustrd/issues/73
|
35
|
-
def manage_anzo_response(response: Response) -> str:
|
36
|
-
content_string = response.content.decode("utf-8")
|
37
|
-
if response.status_code == 200:
|
38
|
-
return content_string
|
39
|
-
elif response.status_code == 403:
|
40
|
-
html = BeautifulSoup(content_string, 'html.parser')
|
41
|
-
title_tag = html.title.string
|
42
|
-
raise HTTPError(f"Anzo authentication error, status code: {response.status_code}, content: {title_tag}")
|
43
|
-
else:
|
44
|
-
raise RequestException(f"Anzo error, status code: {response.status_code}, content: {content_string}")
|
45
|
-
|
46
|
-
|
47
|
-
def query_with_bindings(bindings: dict, when: str) -> str:
|
48
|
-
values = ""
|
49
|
-
for key, value in bindings.items():
|
50
|
-
values += f"VALUES ?{key} {{{value.n3()}}} "
|
51
|
-
split_query = when.lower().split("where {", 1)
|
52
|
-
return f"{split_query[0].strip()} WHERE {{ {values} {split_query[1].strip()}"
|
28
|
+
from mustrd.anzo_utils import query_azg, query_graphmart
|
29
|
+
from mustrd.anzo_utils import query_configuration, json_to_dictlist, ttl_to_graph
|
53
30
|
|
54
31
|
|
55
32
|
def execute_select(triple_store: dict, when: str, bindings: dict = None) -> str:
|
56
33
|
try:
|
57
34
|
if bindings:
|
58
35
|
when = query_with_bindings(bindings, when)
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
return manage_anzo_response(requests.post(url=url,
|
67
|
-
auth=(triple_store['username'], triple_store['password']),
|
68
|
-
data=data,
|
69
|
-
verify=False))
|
36
|
+
# FIXME: why do we have those tokens in a select query? in particular ${targetGraph}?
|
37
|
+
# FIXME: why do we also query the output graph?
|
38
|
+
when = when.replace("${fromSources}",
|
39
|
+
f"FROM <{triple_store['input_graph']}>\nFROM <{triple_store['output_graph']}>").replace(
|
40
|
+
"${targetGraph}", f"<{triple_store['output_graph']}>")
|
41
|
+
# TODO: manage results here
|
42
|
+
return query_azg(anzo_config=triple_store, query=when)
|
70
43
|
except (ConnectionError, TimeoutError, HTTPError, ConnectTimeout):
|
71
44
|
raise
|
72
45
|
|
@@ -76,32 +49,20 @@ def execute_update(triple_store: dict, when: str, bindings: dict = None) -> Grap
|
|
76
49
|
input_graph = triple_store['input_graph']
|
77
50
|
output_graph = triple_store['output_graph']
|
78
51
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
auth=(triple_store['username'],
|
90
|
-
triple_store['password']),
|
91
|
-
data=data,
|
92
|
-
verify=False))
|
52
|
+
# FIXME: that will only work with steps.
|
53
|
+
# We could replace USING clauses with using-graph-uri parameter
|
54
|
+
# But there is no parameter for default insert graphs.
|
55
|
+
substituted_query = when.replace("${usingSources}",
|
56
|
+
f"""USING <{input_graph}>
|
57
|
+
USING <{triple_store['output_graph']}>""").replace(
|
58
|
+
"${targetGraph}", f"<{output_graph}>")
|
59
|
+
|
60
|
+
response = query_azg(anzo_config=triple_store, query=substituted_query, is_update=True,
|
61
|
+
data_layers=[input_graph, output_graph], format="ttl")
|
93
62
|
logging.debug(f'response {response}')
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
'skipCache': 'true'}
|
98
|
-
everything_response = manage_anzo_response(requests.post(url=url,
|
99
|
-
auth=(triple_store['username'],
|
100
|
-
triple_store['password']),
|
101
|
-
data=check_data,
|
102
|
-
verify=False))
|
103
|
-
# todo deal with error responses
|
104
|
-
new_graph = Graph().parse(data=everything_response)
|
63
|
+
# TODO: deal with error responses
|
64
|
+
new_graph = ttl_to_graph(query_azg(anzo_config=triple_store, query="construct {?s ?p ?o} { ?s ?p ?o }",
|
65
|
+
format="ttl", data_layers=output_graph))
|
105
66
|
logging.debug(f"new_graph={new_graph.serialize(format='ttl')}")
|
106
67
|
return new_graph
|
107
68
|
|
@@ -110,35 +71,27 @@ def execute_construct(triple_store: dict, when: str, bindings: dict = None) -> G
|
|
110
71
|
try:
|
111
72
|
if bindings:
|
112
73
|
when = query_with_bindings(bindings, when)
|
113
|
-
|
114
|
-
|
115
|
-
'named-graph-uri': triple_store['input_graph'],
|
116
|
-
'skipCache': 'true'}
|
117
|
-
url = f"https://{triple_store['url']}:{triple_store['port']}/sparql?format=ttl"
|
118
|
-
response = requests.post(url=url,
|
119
|
-
auth=(triple_store['username'],
|
120
|
-
triple_store['password']),
|
121
|
-
data=data,
|
122
|
-
verify=False)
|
123
|
-
logging.debug(f'response {response}')
|
124
|
-
g = Graph().parse(data=manage_anzo_response(response))
|
125
|
-
logging.debug(f"Actual Result = {g.serialize(format='ttl')}")
|
126
|
-
return g
|
74
|
+
return ttl_to_graph(query_azg(anzo_config=triple_store, query=when, format="ttl",
|
75
|
+
data_layers=triple_store['input_graph']))
|
127
76
|
except (ConnectionError, TimeoutError, HTTPError, ConnectTimeout) as e:
|
128
77
|
logging.error(f'response {e}')
|
129
78
|
raise
|
130
79
|
|
131
80
|
|
81
|
+
def query_with_bindings(bindings: dict, when: str) -> str:
|
82
|
+
values = ""
|
83
|
+
for key, value in bindings.items():
|
84
|
+
values += f"VALUES ?{key} {{{value.n3()}}} "
|
85
|
+
split_query = when.lower().split("where {", 1)
|
86
|
+
return f"{split_query[0].strip()} WHERE {{ {values} {split_query[1].strip()}"
|
87
|
+
|
88
|
+
|
132
89
|
# Get Given or then from the content of a graphmart
|
133
90
|
def get_spec_component_from_graphmart(triple_store: dict, graphmart: URIRef, layer: URIRef = None) -> ConjunctiveGraph:
|
134
91
|
try:
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
return anzo_client.query_graphmart(graphmart=graphmart,
|
139
|
-
data_layers=layer,
|
140
|
-
query_string="CONSTRUCT {?s ?p ?o} WHERE {?s ?p ?o}",
|
141
|
-
skip_cache=True).as_quad_store().as_rdflib_graph()
|
92
|
+
return ttl_to_graph(query_graphmart(anzo_config=triple_store, graphmart=graphmart,
|
93
|
+
query="CONSTRUCT {?s ?p ?o} WHERE {?s ?p ?o}",
|
94
|
+
data_layers=layer, format="ttl"))
|
142
95
|
except RuntimeError as e:
|
143
96
|
raise ConnectionError(f"Anzo connection error, {e}")
|
144
97
|
|
@@ -153,46 +106,33 @@ def get_query_from_querybuilder(triple_store: dict, folder_name: Literal, query_
|
|
153
106
|
?queryFolder a <http://www.cambridgesemantics.com/ontologies/QueryPlayground#QueryFolder>;
|
154
107
|
<http://purl.org/dc/elements/1.1/title> "{folder_name}"
|
155
108
|
}}"""
|
156
|
-
|
157
|
-
|
158
|
-
password=triple_store['password'])
|
159
|
-
|
160
|
-
result = anzo_client.query_journal(query_string=query).as_table_results().as_record_dictionaries()
|
109
|
+
result = json_to_dictlist(query_configuration(
|
110
|
+
anzo_config=triple_store, query=query))
|
161
111
|
if len(result) == 0:
|
162
|
-
raise FileNotFoundError(
|
112
|
+
raise FileNotFoundError(
|
113
|
+
f"Query {query_name} not found in folder {folder_name}")
|
163
114
|
return result[0].get("query")
|
164
115
|
|
165
116
|
|
166
117
|
# https://github.com/Semantic-partners/mustrd/issues/102
|
167
118
|
def get_query_from_step(triple_store: dict, query_step_uri: URIRef) -> str:
|
168
|
-
query = f"""SELECT ?
|
119
|
+
query = f"""SELECT ?query WHERE {{
|
169
120
|
BIND(<{query_step_uri}> as ?stepUri)
|
170
121
|
?stepUri a <http://cambridgesemantics.com/ontologies/Graphmarts#Step>;
|
171
122
|
<http://cambridgesemantics.com/ontologies/Graphmarts#transformQuery> ?query
|
172
|
-
}}
|
173
|
-
|
174
|
-
anzo_client = AnzoClient(triple_store['url'], triple_store['port'],
|
175
|
-
username=triple_store['username'],
|
176
|
-
password=triple_store['password'])
|
177
|
-
record_dictionaries = anzo_client.query_journal(query_string=query).as_table_results().as_record_dictionaries()
|
123
|
+
}}"""
|
124
|
+
return json_to_dictlist(query_configuration(anzo_config=triple_store, query=query))[0]['query']
|
178
125
|
|
179
|
-
return record_dictionaries[0].get(
|
180
|
-
"query")
|
181
126
|
|
182
127
|
def get_queries_from_templated_step(triple_store: dict, query_step_uri: URIRef) -> dict:
|
183
|
-
|
184
|
-
query = f"""SELECT ?stepUri ?param_query ?query_template WHERE {{
|
128
|
+
query = f"""SELECT ?param_query ?query_template WHERE {{
|
185
129
|
BIND(<{query_step_uri}> as ?stepUri)
|
186
130
|
?stepUri a <http://cambridgesemantics.com/ontologies/Graphmarts#Step> ;
|
187
|
-
|
188
|
-
|
131
|
+
<http://cambridgesemantics.com/ontologies/Graphmarts#parametersTemplate> ?param_query ;
|
132
|
+
<http://cambridgesemantics.com/ontologies/Graphmarts#template> ?query_template .
|
189
133
|
}}
|
190
134
|
"""
|
191
|
-
|
192
|
-
username=triple_store['username'],
|
193
|
-
password=triple_store['password'])
|
194
|
-
record_dictionaries = anzo_client.query_journal(query_string=query).as_table_results().as_record_dictionaries()
|
195
|
-
return record_dictionaries[0]
|
135
|
+
return json_to_dictlist(query_configuration(anzo_config=triple_store, query=query))[0]
|
196
136
|
|
197
137
|
|
198
138
|
def get_queries_for_layer(triple_store: dict, graphmart_layer_uri: URIRef):
|
@@ -204,37 +144,26 @@ SELECT ?query ?param_query ?query_template
|
|
204
144
|
anzo:orderedValue ?query_step .
|
205
145
|
?query_step graphmarts:enabled true ;
|
206
146
|
OPTIONAL {{ ?query_step
|
207
|
-
|
208
|
-
|
147
|
+
graphmarts:parametersTemplate ?param_query ;
|
148
|
+
graphmarts:template ?query_template ;
|
209
149
|
. }}
|
210
150
|
OPTIONAL {{ ?query_step
|
211
|
-
|
151
|
+
graphmarts:transformQuery ?query ;
|
212
152
|
. }}
|
213
153
|
}}
|
214
154
|
ORDER BY ?index"""
|
215
|
-
|
216
|
-
username=triple_store['username'],
|
217
|
-
password=triple_store['password'])
|
218
|
-
return anzo_client.query_journal(query_string=query).as_table_results().as_record_dictionaries()
|
155
|
+
return json_to_dictlist(query_configuration(anzo_config=triple_store, query=query))
|
219
156
|
|
220
157
|
|
221
158
|
def upload_given(triple_store: dict, given: Graph):
|
222
159
|
logging.debug(f"upload_given {triple_store} {given}")
|
223
160
|
|
224
161
|
try:
|
225
|
-
|
226
|
-
|
227
|
-
clear_graph(triple_store, input_graph)
|
228
|
-
clear_graph(triple_store, output_graph)
|
162
|
+
clear_graph(triple_store, triple_store['input_graph'])
|
163
|
+
clear_graph(triple_store, triple_store['output_graph'])
|
229
164
|
serialized_given = given.serialize(format="nt")
|
230
165
|
insert_query = f"INSERT DATA {{graph <{triple_store['input_graph']}>{{{serialized_given}}}}}"
|
231
|
-
|
232
|
-
'update': insert_query,
|
233
|
-
'using-graph-uri': input_graph,
|
234
|
-
'using-named-graph-uri': input_graph}
|
235
|
-
response = requests.post(url=f"https://{triple_store['url']}:{triple_store['port']}/sparql",
|
236
|
-
auth=(triple_store['username'], triple_store['password']), data=data, verify=False)
|
237
|
-
manage_anzo_response(response)
|
166
|
+
query_azg(anzo_config=triple_store, query=insert_query, is_update=True)
|
238
167
|
except (ConnectionError, TimeoutError, HTTPError, ConnectTimeout):
|
239
168
|
raise
|
240
169
|
|
@@ -242,10 +171,6 @@ def upload_given(triple_store: dict, given: Graph):
|
|
242
171
|
def clear_graph(triple_store: dict, graph_uri: str):
|
243
172
|
try:
|
244
173
|
clear_query = f"CLEAR GRAPH <{graph_uri}>"
|
245
|
-
|
246
|
-
url = f"https://{triple_store['url']}:{triple_store['port']}/sparql"
|
247
|
-
response = requests.post(url=url,
|
248
|
-
auth=(triple_store['username'], triple_store['password']), data=data, verify=False)
|
249
|
-
manage_anzo_response(response)
|
174
|
+
query_azg(anzo_config=triple_store, query=clear_query, is_update=True)
|
250
175
|
except (ConnectionError, TimeoutError, HTTPError, ConnectTimeout):
|
251
176
|
raise
|
mustrd/mustrdGraphDb.py
CHANGED
@@ -49,7 +49,7 @@ def upload_given(triple_store: dict, given: Graph):
|
|
49
49
|
graph = "default"
|
50
50
|
if triple_store['input_graph']:
|
51
51
|
graph = urllib.parse.urlencode({'graph': triple_store['input_graph']})
|
52
|
-
url = f"{triple_store['url']}
|
52
|
+
url = f"{triple_store['url']}/repositories/{triple_store['repository']}" \
|
53
53
|
f"/rdf-graphs/service?{graph}"
|
54
54
|
# graph store PUT drop silently the graph or default and upload the payload
|
55
55
|
# https://www.w3.org/TR/sparql11-http-rdf-update/#http-put
|
@@ -82,7 +82,7 @@ def post_update_query(triple_store: dict, query: str, params: dict = None) -> st
|
|
82
82
|
params = add_graph_to_params(params, triple_store["input_graph"])
|
83
83
|
try:
|
84
84
|
return manage_graphdb_response(requests.post(
|
85
|
-
url=f"{triple_store['url']}
|
85
|
+
url=f"{triple_store['url']}/repositories/{triple_store['repository']}/statements",
|
86
86
|
data=query,
|
87
87
|
params=params,
|
88
88
|
auth=(triple_store['username'], triple_store['password']),
|
@@ -99,7 +99,7 @@ def post_query(triple_store: dict, query: str, accept: str, params: dict = None)
|
|
99
99
|
params = add_graph_to_params(params, triple_store["input_graph"])
|
100
100
|
try:
|
101
101
|
return manage_graphdb_response(
|
102
|
-
requests.post(url=f"{triple_store['url']}
|
102
|
+
requests.post(url=f"{triple_store['url']}/repositories/{triple_store['repository']}",
|
103
103
|
data=query,
|
104
104
|
params=params,
|
105
105
|
auth=(triple_store['username'], triple_store['password']),
|