mustrd 0.1.0__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.
src/steprunner.py ADDED
@@ -0,0 +1,166 @@
1
+ """
2
+ MIT License
3
+
4
+ Copyright (c) 2023 Semantic Partners Ltd
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ """
24
+
25
+ import json
26
+
27
+ import logger_setup
28
+ from multimethods import MultiMethod, Default
29
+ from namespace import MUST
30
+ from rdflib import Graph, URIRef
31
+ from mustrdRdfLib import execute_select as execute_select_rdflib
32
+ from mustrdRdfLib import execute_construct as execute_construct_rdflib
33
+ from mustrdRdfLib import execute_update as execute_update_rdflib
34
+ from mustrdAnzo import upload_given as upload_given_anzo
35
+ from mustrdAnzo import execute_update as execute_update_anzo
36
+ from mustrdAnzo import execute_construct as execute_construct_anzo
37
+ from mustrdAnzo import execute_select as execute_select_anzo
38
+ from mustrdGraphDb import upload_given as upload_given_graphdb
39
+ from mustrdGraphDb import execute_update as execute_update_graphdb
40
+ from mustrdGraphDb import execute_construct as execute_construct_graphdb
41
+ from mustrdGraphDb import execute_select as execute_select_graphdb
42
+ from spec_component import AnzoWhenSpec, WhenSpec
43
+
44
+ log = logger_setup.setup_logger(__name__)
45
+
46
+
47
+ def dispatch_upload_given(triple_store: dict, given: Graph):
48
+ ts = triple_store['type']
49
+ log.info(f"dispatch_upload_given to {ts}")
50
+ return ts
51
+
52
+
53
+ upload_given = MultiMethod('upload_given', dispatch_upload_given)
54
+
55
+
56
+ @upload_given.method(MUST.RdfLib)
57
+ def _upload_given_rdflib(triple_store: dict, given: Graph):
58
+ triple_store["given"] = given
59
+
60
+
61
+ @upload_given.method(MUST.GraphDb)
62
+ def _upload_given_graphdb(triple_store: dict, given: Graph):
63
+ upload_given_graphdb(triple_store, given)
64
+
65
+
66
+ @upload_given.method(MUST.Anzo)
67
+ def _upload_given_anzo(triple_store: dict, given: Graph):
68
+ upload_given_anzo(triple_store, given)
69
+
70
+
71
+ def dispatch_run_when(spec_uri: URIRef, triple_store: dict, when: WhenSpec):
72
+ ts = triple_store['type']
73
+ query_type = when.queryType
74
+ log.info(f"dispatch_run_when to SPARQL type {query_type} to {ts}")
75
+ return ts, query_type
76
+
77
+
78
+ run_when = MultiMethod('run_when', dispatch_run_when)
79
+
80
+
81
+ @run_when.method((MUST.Anzo, MUST.UpdateSparql))
82
+ def _anzo_run_when_update(spec_uri: URIRef, triple_store: dict, when: AnzoWhenSpec):
83
+ return execute_update_anzo(triple_store, when.value, when.bindings)
84
+
85
+
86
+ @run_when.method((MUST.Anzo, MUST.ConstructSparql))
87
+ def _anzo_run_when_construct(spec_uri: URIRef, triple_store: dict, when: AnzoWhenSpec):
88
+ return execute_construct_anzo(triple_store, when.value, when.bindings)
89
+
90
+
91
+ @run_when.method((MUST.Anzo, MUST.SelectSparql))
92
+ def _anzo_run_when_select(spec_uri: URIRef, triple_store: dict, when: AnzoWhenSpec):
93
+ return execute_select_anzo(triple_store, when.value, when.bindings)
94
+
95
+
96
+ @run_when.method((MUST.GraphDb, MUST.UpdateSparql))
97
+ def _graphdb_run_when_update(spec_uri: URIRef, triple_store: dict, when: WhenSpec):
98
+ return execute_update_graphdb(triple_store, when.value, when.bindings)
99
+
100
+
101
+ @run_when.method((MUST.GraphDb, MUST.ConstructSparql))
102
+ def _graphdb_run_when_construct(spec_uri: URIRef, triple_store: dict, when: WhenSpec):
103
+ return execute_construct_graphdb(triple_store, when.value, when.bindings)
104
+
105
+
106
+ @run_when.method((MUST.GraphDb, MUST.SelectSparql))
107
+ def _graphdb_run_when_select(spec_uri: URIRef, triple_store: dict, when: WhenSpec):
108
+ return execute_select_graphdb(triple_store, when.value, when.bindings)
109
+
110
+
111
+ @run_when.method((MUST.RdfLib, MUST.UpdateSparql))
112
+ def _rdflib_run_when_update(spec_uri: URIRef, triple_store: dict, when: WhenSpec):
113
+ return execute_update_rdflib(triple_store, triple_store["given"], when.value, when.bindings)
114
+
115
+
116
+ @run_when.method((MUST.RdfLib, MUST.ConstructSparql))
117
+ def _rdflib_run_when_construct(spec_uri: URIRef, triple_store: dict, when: WhenSpec):
118
+ return execute_construct_rdflib(triple_store, triple_store["given"], when.value, when.bindings)
119
+
120
+
121
+ @run_when.method((MUST.RdfLib, MUST.SelectSparql))
122
+ def _rdflib_run_when_select(spec_uri: URIRef, triple_store: dict, when: WhenSpec):
123
+ return execute_select_rdflib(triple_store, triple_store["given"], when.value, when.bindings)
124
+
125
+
126
+ @run_when.method((MUST.Anzo, MUST.AnzoQueryDrivenUpdateSparql))
127
+ def _multi_run_when_anzo_query_driven_update(spec_uri: URIRef, triple_store: dict, when: AnzoWhenSpec):
128
+ # run the parameters query to obtain the values for the template step and put them into a dictionary
129
+ query_parameters = json.loads(execute_select_anzo(triple_store, when.paramQuery, None))
130
+ if len(query_parameters['results']['bindings']) > 0:
131
+ # replace the anzo query placeholders with the input and output graphs
132
+ when_template = when.queryTemplate.replace(
133
+ "${usingSources}",
134
+ f"USING <{triple_store['input_graph']}> \nUSING <{triple_store['output_graph']}>").replace(
135
+ "${targetGraph}", f"<{triple_store['output_graph']}>")
136
+
137
+ # for each set of parameters insert their values into the template an run it
138
+ for params in query_parameters['results']['bindings']:
139
+ when_query = when_template
140
+ for param in params:
141
+ if params[param].get('datatype'):
142
+ value = params[param]['value']
143
+ else:
144
+ if params[param]['type'] == 'uri':
145
+ value = '<' + params[param]['value'] + '>'
146
+ else:
147
+ value = '"' + params[param]['value'] + '"'
148
+ when_query = when_query.replace("${" + param + "}", value)
149
+ result = execute_update_anzo(triple_store, when_query, None)
150
+ return result
151
+
152
+
153
+ @run_when.method(Default)
154
+ def _multi_run_when_default(spec_uri: URIRef, triple_store: dict, when: WhenSpec):
155
+ if when.queryType == MUST.AskSparql:
156
+ log.warning(f"Skipping {spec_uri}, SPARQL ASK not implemented.")
157
+ msg = "SPARQL ASK not implemented."
158
+ elif when.queryType == MUST.DescribeSparql:
159
+ log.warning(f"Skipping {spec_uri}, SPARQL DESCRIBE not implemented.")
160
+ msg = "SPARQL DESCRIBE not implemented."
161
+ elif triple_store['type'] not in [MUST.Anzo, MUST.GraphDb, MUST.RdfLib]:
162
+ msg = f"{when.queryType} not implemented for {triple_store['type']}"
163
+ else:
164
+ log.warning(f"Skipping {spec_uri}, {when.queryType} is not a valid SPARQL query type.")
165
+ msg = f"{when.queryType} is not a valid SPARQL query type."
166
+ raise NotImplementedError(msg)
@@ -0,0 +1,19 @@
1
+ <table class="table">
2
+ <thead>
3
+ <tr>
4
+ <th scope="col">module</th>
5
+ <th scope="col">class</th>
6
+ <th scope="col">test</th>
7
+ <th scope="col">status</th>
8
+ <tr>
9
+ </thead>
10
+ <tbody>
11
+ {% for result in result_list -%}
12
+ <tr>
13
+ <td>{{result.module_name}}</td>
14
+ <td>{{result.class_name}}</td>
15
+ <td>{{result.test_name}}</td>
16
+ <td>{{result.status}}</td>
17
+ </tr>
18
+ {% endfor %}</tbody>
19
+ </table>
@@ -0,0 +1,9 @@
1
+
2
+ {% for result in result_list %}
3
+ <details>
4
+ {{environment.get_template("md_stats_template.jinja").render(name = result.name, stats = result.stats)}}
5
+ <ul>
6
+ {{result.render()}}
7
+ </ul>
8
+ </details>
9
+ {% endfor %}
@@ -0,0 +1,3 @@
1
+ <summary>
2
+ {{name}}: <br/> total: {{stats.count}}, success: {{stats.success_count}}, fail: {{stats.fail_count}}, skipped: {{stats.skipped_count}}
3
+ </summary>
src/utils.py ADDED
@@ -0,0 +1,38 @@
1
+ """
2
+ MIT License
3
+
4
+ Copyright (c) 2023 Semantic Partners Ltd
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ """
24
+
25
+ import json
26
+ from pathlib import Path
27
+
28
+
29
+ # Keep this function in a file directly under project root / src
30
+ def get_project_root() -> Path:
31
+ return Path(__file__).parent.parent
32
+
33
+ def is_json(myjson: str) -> bool:
34
+ try:
35
+ json.loads(myjson)
36
+ except ValueError:
37
+ return False
38
+ return True