Skip to main content

Python

cimvocabcheck on PyPI validates SPARQL queries and SHACL shapes from Python. It is a binding, not a second implementation: it drives the CLI and parses its report, so a finding it returns is the same finding the editors, the CI job and any other language binding produce.

pip install cimvocabcheck

Python 3.10 or newer, with no Python dependencies. It does need an engine to drive — see Finding an engine.

Validating​

from cimvocabcheck import validate

report = validate(["queries/line-segments.rq", "shapes/equipment.ttl"], schema="profiles/")

if not report.ok:
for finding in report.errors:
print(finding) # queries/line-segments.rq:3:12: ERROR[UNKNOWN_CLASS] Class <…> …

Findings are data, not exceptions. A file with errors comes back as a report whose ok is False; exceptions are reserved for "the engine could not run, or could not be trusted".

One call, every input​

Loading a CGMES profile set costs about a second, and validating one more query after that costs almost nothing — so validate() takes a list and returns one report:

from pathlib import Path

report = validate(sorted(Path("queries").glob("*.rq")), config="opencgmes.jsonc")

Spawning the engine per file turns a one-second job into a one-second-per-file job. validate_file() exists for the genuinely single-file case and calls validate() underneath. validate_text() validates a query held in memory by passing it on stdin; because the engine tells SHACL from SPARQL by file suffix, shapes have to come from a file.

Options​

validate(
paths,
schema="profiles/", # RDFS file(s) or directories; or …
config="opencgmes.jsonc", # … a config file; or …
endpoint="http://localhost:3030/ds/query", # … a SPARQL endpoint
profiles=["http://iec.ch/TC57/ns/CIM/CoreEquipment-EU/3.0"],
strictness="strict", # permissive | default | strict | pedantic
verbose=True, # include WARN/INFO — the default here
cwd="…", timeout=120, extra_args=[],
)

With no schema, config or endpoint the engine discovers the nearest opencgmes.jsonc above cwd; without one it checks syntax only.

Pass inputs relative to cwd: they appear verbatim in the report, and CI annotations only line up with a source tree when they are repository-relative.

verbose defaults to True, unlike the CLI. A library hands everything back and lets the caller filter, rather than discarding findings the caller cannot then recover; pass verbose=False for the CLI's errors-only behaviour.

The result​

report.okno input has an ERROR
report.summarycounts, matching exactly what this report contains
report.resultsone FileResult per input, in the order they were passed
report.errors / warnings / infosflat findings, each carrying the file it came from
report.findings(severity=…, code=…)the same, narrowed
report.for_file(path)one input's result
report.tool.versionwhich engine produced this

Each finding carries severity, code, message and — when the engine could resolve them — line, column, term, graph and found_in_other_profiles.

Switch on code, never on message: messages change between releases, codes do not.

from cimvocabcheck import Code

typos = report.findings(code=Code.UNKNOWN_CLASS) + report.findings(code=Code.UNKNOWN_PROPERTY)

Finding an engine​

The binding ships no engine. It discovers one in this order, and the error names every place it looked:

#Source
1the engine= argumenta path to a JAR or binary, or an Engine object
2$CIMVOCABCHECK_JAR, then $CIMVOCABCHECK_BINa fat JAR (run with java), or an executable
3cimvocabcheck on PATH
4docker run ghcr.io/soptim/cimvocabcheck-cli:<tag>last resort, with the engine image of the binding's own version: :1.2.0 for 1.2.0, :1.2.0-rc.1 for a release candidate, :edge (built from main) for a development build. Never :latest, which would change the engine under an unchanged binding

A JAR engine needs a JRE 21 or newer, found via JAVA_HOME or PATH. A local engine is never started under an ASCII-only locale: when the effective one (LC_ALL, else LC_CTYPE, else LANG) is C, POSIX or unset, as in many containers and CI images, the engine gets C.UTF-8 so it can open non-ASCII file names. It does too when a locale the environment names is not installed (LANG=en_US.UTF-8 without the locale package, going by locale -a): the C library then falls back to C for every category, so the engine gets LC_ALL=C.UTF-8.

The Docker fallback mounts the working directory at the image's /work and passes inputs inside it relative to it. Everything else it has to read is mounted read-only at /host followed by its host path, and results still come back under the names you passed:

  • an input, schema or config file outside the working directory, so a batch behaves as it does with a local engine. An input that does not exist is passed on and reported as UNREADABLE_INPUT;
  • an opencgmes.jsonc above the working directory, which is then passed as --config unless the call names its own schema source, so a run from a subdirectory sees the same config a local engine would;
  • the schema paths a config names, wherever they lead, so its relative paths work as they do on the host. One that would land on a directory of the image itself (../usr, from a config in the working directory) is refused.

CIMVOCABCHECK_DOCKER_IMAGE pins a tag and CIMVOCABCHECK_NO_DOCKER=1 takes it out of the chain. The first run pulls the image, and an endpoint on localhost is then the container's localhost.

python -m cimvocabcheck --print-engine # which engine would be used here

Errors​

ExceptionWhen
EngineNotFoundErrornothing to run; the message lists everywhere that was searched
ToolErrorthe engine reported a usage/config failure (exit 2), or failed outright
ReportParseErroroutput was not a report of the contract major this binding targets
EngineTimeoutErrortimeout elapsed
UnreachablePathErrora path the Docker engine cannot reach: a schema or config outside the working directory that does not exist, a Windows path a config names, one that would land on a directory of the image itself, or (command line only) init --dir outside the working directory

ToolError carries argv, returncode, stdout and stderr.

Command line and pre-commit​

Installing the package puts a cimvocabcheck command on PATH that forwards to whichever engine it discovers — same flags, same formats, same exit codes. python -m cimvocabcheck does the same.

With the Docker engine the command rewrites the paths it is given the way the library does: a config, schema or input outside the working directory is mounted. init --dir outside it is refused, because the engine writes there and the mounts are read-only.

repos:
- repo: local
hooks:
- id: cimvocabcheck
name: CIMVocabCheck
entry: cimvocabcheck
language: python
additional_dependencies: ["cimvocabcheck"]
files: \.(rq|sparql|ttl|shacl)$
require_serial: true

require_serial matters: without it pre-commit splits the files across processes and each one reloads the profile set.

Versions and compatibility​

The package tracks the engine it was generated against (1.2.x binding ↔ 1.2.x engine) and accepts any engine declaring the same report contract major. Within a major the contract only ever adds, so the binding keeps fields it does not know in extra and accepts codes it does not know — annotation.is_known_code tells you which — because an unrecognised code is a generic finding, never an error.

The result model is generated from the published JSON Schema the package vendors; a contract change is picked up by regenerating, and CI fails when the two have drifted apart.