|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# This software was developed at the National Institute of Standards |
| 4 | +# and Technology by employees of the Federal Government in the course |
| 5 | +# of their official duties. Pursuant to title 17 Section 105 of the |
| 6 | +# United States Code this software is not subject to copyright |
| 7 | +# protection and is in the public domain. NIST assumes no |
| 8 | +# responsibility whatsoever for its use by other parties, and makes |
| 9 | +# no guarantees, expressed or implied, about its quality, |
| 10 | +# reliability, or any other characteristic. |
| 11 | +# |
| 12 | +# We would appreciate acknowledgement if the software is used. |
| 13 | + |
| 14 | +__version__ = "0.1.0" |
| 15 | + |
| 16 | +import argparse |
| 17 | +import importlib.resources |
| 18 | +import logging |
| 19 | +import os |
| 20 | +import pathlib |
| 21 | +import sys |
| 22 | +import typing |
| 23 | + |
| 24 | +import rdflib # type: ignore |
| 25 | +import pyshacl # type: ignore |
| 26 | + |
| 27 | +import case_utils.ontology |
| 28 | + |
| 29 | +from case_utils.ontology.version_info import * |
| 30 | + |
| 31 | +_logger = logging.getLogger(os.path.basename(__file__)) |
| 32 | + |
| 33 | +def main() -> None: |
| 34 | + parser = argparse.ArgumentParser(description="CASE wrapper to PySHACL command line tool.") |
| 35 | + |
| 36 | + # Configure debug logging before running parse_args, because there could be an error raised before the construction of the argument parser. |
| 37 | + logging.basicConfig(level=logging.DEBUG if ("--debug" in sys.argv or "-d" in sys.argv) else logging.INFO) |
| 38 | + |
| 39 | + case_version_choices_list = ["none", "case-" + CURRENT_CASE_VERSION] |
| 40 | + |
| 41 | + # Add arguments specific to case_validate. |
| 42 | + parser.add_argument( |
| 43 | + '-d', |
| 44 | + '--debug', |
| 45 | + action='store_true', |
| 46 | + help='Output additional runtime messages.' |
| 47 | + ) |
| 48 | + parser.add_argument( |
| 49 | + "--built-version", |
| 50 | + choices=tuple(case_version_choices_list), |
| 51 | + default="case-"+CURRENT_CASE_VERSION, |
| 52 | + help="Monolithic aggregation of CASE ontology files at certain versions. Does not require networking to use. Default is most recent CASE release." |
| 53 | + ) |
| 54 | + parser.add_argument( |
| 55 | + "--ontology-graph", |
| 56 | + action="append", |
| 57 | + help="Combined ontology (i.e. subclass hierarchy) and shapes (SHACL) file, in any format accepted by rdflib recognized by file extension (e.g. .ttl). Will supplement ontology selected by --built-version. Can be given multiple times." |
| 58 | + ) |
| 59 | + |
| 60 | + # Inherit arguments from pyshacl. |
| 61 | + parser.add_argument( |
| 62 | + '--abort', |
| 63 | + action='store_true', |
| 64 | + help='(As with pyshacl CLI) Abort on first invalid data.' |
| 65 | + ) |
| 66 | + parser.add_argument( |
| 67 | + '-w', |
| 68 | + '--allow-warnings', |
| 69 | + action='store_true', |
| 70 | + help='(As with pyshacl CLI) Shapes marked with severity of Warning or Info will not cause result to be invalid.', |
| 71 | + ) |
| 72 | + parser.add_argument( |
| 73 | + "-f", |
| 74 | + "--format", |
| 75 | + choices=('human', 'turtle', 'xml', 'json-ld', 'nt', 'n3'), |
| 76 | + default='human', |
| 77 | + help="(ALMOST as with pyshacl CLI) Choose an output format. Default is \"human\". Difference: 'table' not provided." |
| 78 | + ) |
| 79 | + parser.add_argument( |
| 80 | + '-im', |
| 81 | + '--imports', |
| 82 | + action='store_true', |
| 83 | + help='(As with pyshacl CLI) Allow import of sub-graphs defined in statements with owl:imports.', |
| 84 | + ) |
| 85 | + parser.add_argument( |
| 86 | + '-i', |
| 87 | + '--inference', |
| 88 | + choices=('none', 'rdfs', 'owlrl', 'both'), |
| 89 | + default='none', |
| 90 | + help="(As with pyshacl CLI) Choose a type of inferencing to run against the Data Graph before validating. Default is \"none\".", |
| 91 | + ) |
| 92 | + |
| 93 | + parser.add_argument("in_graph") |
| 94 | + |
| 95 | + args = parser.parse_args() |
| 96 | + |
| 97 | + data_graph = rdflib.Graph() |
| 98 | + data_graph.parse(args.in_graph) |
| 99 | + |
| 100 | + ontology_graph = rdflib.Graph() |
| 101 | + if args.built_version != "none": |
| 102 | + ttl_filename = args.built_version + ".ttl" |
| 103 | + _logger.debug("ttl_filename = %r.", ttl_filename) |
| 104 | + ttl_data = importlib.resources.read_text(case_utils.ontology, ttl_filename) |
| 105 | + ontology_graph.parse(data=ttl_data, format="turtle") |
| 106 | + if args.ontology_graph: |
| 107 | + for arg_ontology_graph in args.ontology_graph: |
| 108 | + _logger.debug("arg_ontology_graph = %r.", arg_ontology_graph) |
| 109 | + ontology_graph.parse(arg_ontology_graph) |
| 110 | + |
| 111 | + validate_result : typing.Tuple[ |
| 112 | + bool, |
| 113 | + typing.Union[Exception, bytes, str, rdflib.Graph], |
| 114 | + str |
| 115 | + ] |
| 116 | + validate_result = pyshacl.validate( |
| 117 | + data_graph, |
| 118 | + shacl_graph=ontology_graph, |
| 119 | + ont_graph=ontology_graph, |
| 120 | + inference=args.inference, |
| 121 | + abort_on_first=args.abort, |
| 122 | + allow_warnings=True if args.allow_warnings else False, |
| 123 | + debug=True if args.debug else False, |
| 124 | + do_owl_imports=True if args.imports else False |
| 125 | + ) |
| 126 | + |
| 127 | + # Relieve RAM of the data graph after validation has run. |
| 128 | + del data_graph |
| 129 | + |
| 130 | + conforms = validate_result[0] |
| 131 | + validation_graph = validate_result[1] |
| 132 | + validation_text = validate_result[2] |
| 133 | + |
| 134 | + if args.format == "human": |
| 135 | + sys.stdout.write(validation_text) |
| 136 | + else: |
| 137 | + if isinstance(validation_graph, rdflib.Graph): |
| 138 | + validation_graph_str = validation_graph.serialize(format=args.format) |
| 139 | + sys.stdout.write(validation_graph_str) |
| 140 | + del validation_graph_str |
| 141 | + elif isinstance(validation_graph, bytes): |
| 142 | + sys.stdout.write(validation_graph.decode("utf-8")) |
| 143 | + elif isinstance(validation_graph, str): |
| 144 | + sys.stdout.write(validation_graph) |
| 145 | + else: |
| 146 | + raise NotImplementedError("Unexpected result type returned from validate: %r." % type(validation_graph)) |
| 147 | + |
| 148 | + sys.exit(0 if conforms else 1) |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + main() |
0 commit comments