Metadata-Version: 2.5
Name: cimvocabcheck
Version: 1.3.0rc1
Summary: Validate SPARQL queries and SHACL shapes against CIM/CGMES schema profiles.
Project-URL: Homepage, https://opencgmes.soptim.de/cimvocabcheck/python
Project-URL: Documentation, https://opencgmes.soptim.de/cimvocabcheck/python
Project-URL: Source, https://github.com/SOPTIM/OpenCGMES
Project-URL: Issues, https://github.com/SOPTIM/OpenCGMES/issues
Author-email: SOPTIM AG <info@soptim.de>
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: cgmes,cim,iec61970,rdf,shacl,sparql,validation
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Software Development :: Quality Assurance
Classifier: Topic :: Text Processing :: Markup
Classifier: Typing :: Typed
Requires-Python: >=3.10
Provides-Extra: dev
Requires-Dist: mypy==2.3.1; extra == 'dev'
Requires-Dist: pytest==9.1.1; extra == 'dev'
Requires-Dist: ruff==0.16.8; extra == 'dev'
Provides-Extra: lint
Requires-Dist: mypy==2.3.1; extra == 'lint'
Requires-Dist: ruff==0.16.8; extra == 'lint'
Provides-Extra: test
Requires-Dist: pytest==9.1.1; extra == 'test'
Description-Content-Type: text/markdown

# cimvocabcheck (Python)

Validate SPARQL queries and SHACL shapes against CIM/CGMES schema profiles from Python.

This package is a **binding, not an engine**. The analysis — SPARQL parsing, the algebra walk,
the CGMES profile model, SHACL — is one Apache Jena implementation shared by every consumer, so a
finding you get here is the same finding your editor, your CI job and your colleague's .NET
service get. What this package adds is finding that engine, building its command line, and giving
you its published report as typed Python.

Full documentation: <https://opencgmes.soptim.de/cimvocabcheck/python>

## Install

```bash
pip install cimvocabcheck
```

Python 3.10+. The package has **no Python dependencies**. It does need an engine to drive — see
[Finding an engine](#finding-an-engine); the simplest is a JRE 21+ and the CLI fat JAR.

## Use it

```python
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".

### Validate everything in one call

Loading a CGMES profile set costs about a second; validating one more query after that costs
almost nothing. `validate()` therefore takes a **list**:

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

Spawning the engine once per file turns a one-second job into a one-second-per-file job.
`validate_file()` exists for the genuinely single-file case and simply calls `validate()`.

### Reading the report

```python
report.ok  # no input has an ERROR
report.summary.errors  # counts, matching exactly what this report contains
report.results  # one FileResult per input, in the order they were passed
report.errors  # flat findings, each carrying the file it came from
report.findings(code=Code.UNKNOWN_CLASS)
report.for_file("queries/line-segments.rq")
report.tool.version  # which 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` (the "you have the wrong profile
in scope" hint).

**Switch on `code`, never on `message`.** Messages change between releases; codes do not.

```python
from cimvocabcheck import Code

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

### Options

```python
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, unlike the CLI
    cwd="…",
    timeout=120,
)
```

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

Pass inputs as paths **relative to `cwd`**. They appear verbatim in the report, and CI annotations
only line up with your source tree when they are repository-relative.

`verbose` defaults to `True` here on purpose: a library hands back everything and lets you filter,
rather than discarding findings you cannot then recover. Pass `verbose=False` for the CLI's
errors-only behaviour.

## Finding an engine

`validate()` discovers one in this order, and the error names every place it looked:

| # | Source | |
| --- | --- | --- |
| 1 | the `engine=` argument | a path to a JAR or binary, or an `Engine` |
| 2 | `$CIMVOCABCHECK_JAR`, then `$CIMVOCABCHECK_BIN` | a fat JAR (run with `java`), or an executable |
| 3 | `cimvocabcheck` on `PATH` | |
| 4 | `docker 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.

Set `CIMVOCABCHECK_DOCKER_IMAGE` to pin a tag, or `CIMVOCABCHECK_NO_DOCKER=1` to take it out of the
chain. Note that the first run pulls the image, and that an `endpoint` on `localhost` is the
container's localhost, not yours.

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

## Errors

| Exception | When |
| --- | --- |
| `EngineNotFoundError` | nothing to run; the message lists everywhere that was searched |
| `ToolError` | the engine reported a usage/config failure (exit 2), or failed outright |
| `ReportParseError` | output was not a report of the contract major this binding targets |
| `EngineTimeoutError` | `timeout` elapsed |
| `UnreachablePathError` | a 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`, so a CI log can show what
actually happened.

## Command line

Installing the package puts a `cimvocabcheck` command on your `PATH` that forwards to whichever
engine it discovers — same flags, same output formats, same exit codes (`0` clean, `1` findings,
`2` usage). `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.

```bash
cimvocabcheck --schema profiles/ -f sarif queries/*.rq > cimvocabcheck.sarif
```

## pre-commit

```yaml
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.

## Versioning and compatibility

This 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 — new optional fields, new rule codes, new output formats — so the binding:

- keeps fields it does not know in `extra` rather than dropping them, and
- accepts codes it does not know (`annotation.is_known_code` tells you), because the contract
  requires an unrecognised code to be treated as a generic finding, never as an error.

The contract itself is documented at
<https://opencgmes.soptim.de/cimvocabcheck/report-contract>.

## Developing

The result model is **generated** from the published JSON Schema by a generator shared with the
.NET and Rust bindings — edit the generator, not the model. Convenience methods live in
`_ergonomics.py` and survive regeneration.

```bash
python3 ../codegen/generate_models.py --target python           # after a contract change
python3 ../codegen/generate_models.py --target python --check   # what CI runs
python -m pytest                            # engine-backed tests skip when no engine is found
# Against a locally built engine (mvn -pl cimvocabcheck/cli -am package, from the repo root).
# The glob must expand before the assignment, and an absolute path survives a cwd change.
export CIMVOCABCHECK_JAR="$(ls "$PWD"/../../cli/target/cimvocabcheck-cli-*.jar)"
CIMVOCABCHECK_TESTS_REQUIRE_ENGINE=1 python -m pytest   # fails, rather than skips, without the engine
```

## License

Apache-2.0 — see [LICENSE](LICENSE).
