Convert SHACL model file to code bindings
Project description
Convert SHACL Model to code bindings
This tool can be used to convert a SHACL model into various code bindings
Installation
shacl2code can be installed using pip:
python3 -m pip install shacl2code
Usage
shacl2code can generate bindings from either a local file:
shacl2code generate -i model.jsonld python -o out.py
Or from a URL:
shacl2code generate -i https://example.com/rdf/model.jsonld python -o out.py
Or from stdin:
cat model.jsonld | shacl2code generate -i - python -o - > out.py
For more information, run:
shacl2code --help
The available language bindings can be viewed by running:
shacl2code list
Developing
Developing on shacl2code is best done using a virtual environment. You can
configure one and install shacl2code in editable mode with all necessary
development dependencies by running:
python3 -m venv .venv
. .venv/bin/activate
pip install -e ".[dev]"
Testing
shacl2code has a test suite written in pytest. To run it, setup a
virtual environment as shown above, then run:
pytest
In addition to the test results, a test coverage report will also be generated using pytest-cov
The test suite is quite extensive and can catch a lot of errors. When adding new features to the code generation, please add tests to ensure that these features continue to behave as expected.
All changes to the code are expected to pass the test suite before they merge.
Custom Annotations
shacl2code supports a number of custom annotations that can be specified in a
SHACL model to give hints about the generated code. All of these annotations
live in the https://jpewdev.github.io/shacl2code/schema# namespace, and
commonly are given the sh-to-code prefix to make it easier to reference them.
For example, in Turtle one would add the prefix mapping:
@prefix sh-to-code: <https://jpewdev.github.io/shacl2code/schema#> .
ID Property Name
The idPropertyName annotation allows a class to specify what the name of the
"property" that specifies the RDF subject of an object is for serializations
that support it. For example, in JSON-LD, the @id property indicates the
subject in RDF. If you wanted to alias the @id property to another name, the
idPropertyName annotation will let you do this. For example, the following
turtle will use MyId instead of @id when writing JSON-LD bindings:
<MyClass> a owl:Class, sh:NodeShape ;
sh-to-code:idPropertyName "MyId"
.
When doing this, the class would then look like this in JSON-LD:
{
"@type": "MyClass",
"MyId": "http://example.com/id"
}
The idPropertyName annotation is inherited by derived classes, so for example
any class that derived from MyClass would also use MyId as the subject
property.
Note: This only specifies what the name of the field should be in generated
bindings and has no bearing on how an RDF parser would interpret the property.
In order to still be parsed by RDF, you would also need context file that maps
MyId to @id, for example:
{
"@context": {
"MyId": "@id"
}
}
shacl2code doesn't do this for you, nor does it validate that you have done
it.
Extensible Classes
Most bindings generated from shacl2code are "closed" in that they do not
allow extra properties to be added to object outside of what is specified in
model. This ensures that field name typos and other unintended properties are
not added to an object. However, in some cases a class may be specifically
intended to be extended such that arbitrary fields can be added to it, which
can be done using the isExtensible property. This is a boolean property that
indicates if a class can be extended, and defaults to false. For example, the
following turtle will declare a class as extensible:
<MyClass> a owl:Class, sh:NodeShape ;
sh-to-code:isExtensible true
.
The isExtensible property is not inherited by derived classes, meaning it
is possible to have a class derived from MyClass which is itself not
extensible.
The mechanism for dealing with extensible classes will vary between the different bindings, but in general it means that they will not be very picky about object types and properties in any location where an extensible class is allowed.
Note: You may want to be careful about where and how many extensible
classes are allowed in your model. If there are too many and they are allowed
anywhere, it may mean that typos in object types (e.g. @type in JSON-LD) are
not caught by validation as they will have to be assumed to be a derived class
from an extensible type.
Abstract Classes
By default, classes generated by shacl2code are all instantiable (i.e. they
can be created). In some instances, it may be desirable to declare a class as
abstract (meaning that it cannot be instantiated, but non-abstract derived
classes can). There are several ways of marking a class as abstract listed
below, in order of preference (with the most preferred being first).
SHACL Validated Abstract Classes
A class can be prevented from being directly instantiated using SHACL by adding a constraint on the shape that it cannot be of its own type. This can be done with the following turtle:
<MyClass> a owl:Class, sh:NodeShape ;
# SHACL to prevent a class from being instantiated as this exact type
sh:property [
sh:path rdf:type ;
sh:not [ sh:hasValue <MyClass> ]
] .
shacl2code will detect this pattern and generate abstract bindings for
MyClass.
This method is most preferred, since it is enforced by SHACL and not just
shacl2code bindings
shacl2code Annotation
shacl2code has a custom annotation that can be used to mark a class as
abstract. This can be done with the boolean isAbstract property. For
example, the following turtle will declare a class as abstract:
<MyClass> a owl:Class, sh:NodeShape ;
sh-to-code:isAbstract true
.
The isAbstract property is not inherited by derived classes, so any derived
classes are automatically concrete unless they indicate otherwise.
SPDX Abstract Class Parent
It is also possible to define a class as abstract by declaring it to be of
type: http://spdx.invalid./AbstractClass, but this is not preferred.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file shacl2code-0.0.25.tar.gz.
File metadata
- Download URL: shacl2code-0.0.25.tar.gz
- Upload date:
- Size: 291.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2985a618b9b8cdf0f3d09c1c72d060bc41ac2a239cede7e9d46cc2b3e3bc3c47
|
|
| MD5 |
8fdce168598bb18405b2d8807ab60462
|
|
| BLAKE2b-256 |
02d3e4cc85c7644f7c410055ec3d06e5d922e5d1833c70bc031cf123629e7899
|
Provenance
The following attestation bundles were made for shacl2code-0.0.25.tar.gz:
Publisher:
publish.yaml on JPEWdev/shacl2code
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
shacl2code-0.0.25.tar.gz -
Subject digest:
2985a618b9b8cdf0f3d09c1c72d060bc41ac2a239cede7e9d46cc2b3e3bc3c47 - Sigstore transparency entry: 864345038
- Sigstore integration time:
-
Permalink:
JPEWdev/shacl2code@ab0bd6f9b78839c552c1b8c61553adeecce1e64d -
Branch / Tag:
refs/tags/v0.0.25 - Owner: https://github.com/JPEWdev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@ab0bd6f9b78839c552c1b8c61553adeecce1e64d -
Trigger Event:
release
-
Statement type:
File details
Details for the file shacl2code-0.0.25-py3-none-any.whl.
File metadata
- Download URL: shacl2code-0.0.25-py3-none-any.whl
- Upload date:
- Size: 296.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cea83299d912d22b112641cfa953f8efca543843e339089d9462d0124c3e74c8
|
|
| MD5 |
e7cd37a327e847862f717fc9b9065fb5
|
|
| BLAKE2b-256 |
dcffd7fda9db9e072df28e676d2cb30576c316721dadcbfd2836d8f755d6fb07
|
Provenance
The following attestation bundles were made for shacl2code-0.0.25-py3-none-any.whl:
Publisher:
publish.yaml on JPEWdev/shacl2code
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
shacl2code-0.0.25-py3-none-any.whl -
Subject digest:
cea83299d912d22b112641cfa953f8efca543843e339089d9462d0124c3e74c8 - Sigstore transparency entry: 864345044
- Sigstore integration time:
-
Permalink:
JPEWdev/shacl2code@ab0bd6f9b78839c552c1b8c61553adeecce1e64d -
Branch / Tag:
refs/tags/v0.0.25 - Owner: https://github.com/JPEWdev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@ab0bd6f9b78839c552c1b8c61553adeecce1e64d -
Trigger Event:
release
-
Statement type: