@click.command("check")
@click.argument(
"files",
nargs=-1,
required=True,
type=click.Path(exists=True, dir_okay=False, path_type=pathlib.Path),
)
@click.option(
"--ontology-dir",
type=click.Path(exists=True, file_okay=False, path_type=pathlib.Path),
default=None,
help="Directory of catalog ontology files (all RDF files in it).",
)
@click.option(
"--shapes-dir",
type=click.Path(exists=True, file_okay=False, path_type=pathlib.Path),
default=None,
help="Directory of SHACL shapes files.",
)
@click.option("--tenant", type=str, default=None, help="Tenant id.")
@click.option("--project", type=str, default=None, help="Project id.")
@click.option(
"--vocabulary",
type=click.Choice(["strict", "open"]),
default="strict",
show_default=True,
help="strict: a term outside the catalog is an error; open: a warning.",
)
@click.option("--json", "as_json", is_flag=True, help="Print the report as JSON.")
def check(
files: tuple[pathlib.Path, ...],
ontology_dir: pathlib.Path | None,
shapes_dir: pathlib.Path | None,
tenant: str | None,
project: str | None,
vocabulary: str,
as_json: bool,
) -> None:
"""Check FILES (merged into one facts graph) without a model call.
Exit status: 0 without error findings, 1 with any, 2 when the check cannot
run (a file cannot be read or holds no triples, or the catalog cannot be
loaded). Without --tenant/--project the catalog is read from
--ontology-dir and --shapes-dir; either may be omitted.
"""
from_store = tenant is not None or project is not None
if from_store and (ontology_dir is not None or shapes_dir is not None):
raise click.UsageError(
"--tenant/--project read the catalog from the store; "
"do not combine them with --ontology-dir or --shapes-dir."
)
graph = _load(files)
if not len(graph):
_fail("the files hold no triples")
if from_store:
catalog, shapes = _catalog_from_tenant(tenant, project)
else:
catalog = _load(_dir_files(ontology_dir))
shapes_graph = _load(_dir_files(shapes_dir))
shapes = shapes_graph if len(shapes_graph) else None
report = check_facts(
graph,
catalog,
shapes,
vocabulary="open" if vocabulary == "open" else "strict",
)
if as_json:
click.echo(report.model_dump_json(indent=2))
else:
_print(report)
errors = sum(by.get("error", 0) for by in report.counts.values())
raise SystemExit(1 if errors else 0)