37
37
import os
38
38
import sys
39
39
import warnings
40
- from typing import Dict , List , Optional , Set , Tuple , Union
40
+ from typing import Any , Dict , List , Optional , Set , Tuple , Union
41
41
42
42
import pyshacl # type: ignore
43
43
import rdflib
@@ -65,6 +65,14 @@ class NonExistentCDOConceptWarning(UserWarning):
65
65
pass
66
66
67
67
68
+ class NonExistentCASEVersionError (Exception ):
69
+ """
70
+ This class is used when an invalid CASE version is requested that is not supported by the library.
71
+ """
72
+
73
+ pass
74
+
75
+
68
76
class ValidationResult :
69
77
def __init__ (
70
78
self ,
@@ -204,10 +212,21 @@ def get_ontology_graph(
204
212
205
213
if case_version != "none" :
206
214
# Load bundled CASE ontology at requested version.
207
- if case_version is None :
215
+ if case_version is None or case_version == "" :
208
216
case_version = CURRENT_CASE_VERSION
217
+ # If the first character case_version is numeric, prepend case- to it. This allows for the version to be passed
218
+ # by the library as both case-1.2.0 and 1.2.0
219
+ if case_version [0 ].isdigit ():
220
+ case_version = "case-" + case_version
209
221
ttl_filename = case_version + ".ttl"
210
222
_logger .debug ("ttl_filename = %r." , ttl_filename )
223
+ # Ensure the requested version of the CASE ontology is available and if not, throw an appropriate exception
224
+ # that can be returned in a user-friendly message.
225
+ if not importlib .resources .is_resource (case_utils .ontology , ttl_filename ):
226
+ raise NonExistentCASEVersionError (
227
+ f"The requested version ({ case_version } ) of the CASE ontology is not available. Please choose a "
228
+ f"different version. The latest supported version is: { CURRENT_CASE_VERSION } "
229
+ )
211
230
ttl_data = importlib .resources .read_text (case_utils .ontology , ttl_filename )
212
231
ontology_graph .parse (data = ttl_data , format = "turtle" )
213
232
@@ -221,19 +240,23 @@ def get_ontology_graph(
221
240
222
241
def validate (
223
242
input_file : str ,
243
+ * args : Any ,
224
244
case_version : Optional [str ] = None ,
225
245
supplemental_graphs : Optional [List [str ]] = None ,
226
246
abort_on_first : bool = False ,
227
247
inference : Optional [str ] = "none" ,
248
+ ** kwargs : Any ,
228
249
) -> ValidationResult :
229
250
"""
230
251
Validate the given data graph against the given CASE ontology version and supplemental graphs.
231
-
252
+ :param *args: The positional arguments to pass to the underlying pyshacl.validate function.
232
253
:param input_file: The path to the file containing the data graph to validate.
233
- :param case_version: The version of the CASE ontology to use. If None, the most recent version will be used.
254
+ :param case_version: The version of the CASE ontology to use (e.g. 1.2.0). If None, the most recent version will
255
+ be used.
234
256
:param supplemental_graphs: The supplemental graphs to use. If None, no supplemental graphs will be used.
235
257
:param abort_on_first: Whether to abort on the first validation error.
236
258
:param inference: The type of inference to use. If "none", no inference will be used.
259
+ :param **kwargs: The keyword arguments to pass to the underlying pyshacl.validate function.
237
260
:return: The validation result object containing the defined properties.
238
261
"""
239
262
# Convert the data graph string to a rdflib.Graph object.
@@ -260,6 +283,8 @@ def validate(
260
283
allow_warnings = False ,
261
284
debug = False ,
262
285
do_owl_imports = False ,
286
+ args = args ,
287
+ kwargs = kwargs ,
263
288
)
264
289
265
290
# Relieve RAM of the data graph after validation has run.
@@ -409,7 +434,7 @@ def main() -> None:
409
434
allow_warnings = True if args .allow_warnings else False ,
410
435
debug = True if args .debug else False ,
411
436
do_owl_imports = True if args .imports else False ,
412
- ** validator_kwargs
437
+ ** validator_kwargs ,
413
438
)
414
439
415
440
# Relieve RAM of the data graph after validation has run.
0 commit comments