Skip to content

rdfcrate

AttachedCrate

Bases: RoCrate

An RO-Crate stored in a directory, with all contents local to that directory.

See https://www.researchobject.org/ro-crate/specification/1.2-DRAFT/structure#attached-ro-crate

metadata_entity: schemaorg.CreativeWork property

The metadata entity of the RO-Crate

root: Path = Path(path).resolve() instance-attribute

The RO-Crate directory

get_issues() -> list[CheckIssue]

Returns a list of issues found in the RO-Crate using the rocrate-validator package.

register_dir(path: Path | str, *props: EntityArgs, recursive: Recursive = False, **kwargs: Any) -> schemaorg.Dataset

See [RoCrate.register_dir][rdfcrate.wrapper.RoCrate.register_dir].

register_file(path: Path | str, *props: EntityArgs, add_size: bool = False, **kwargs: Any)

See [RoCrate.register_file][rdfcrate.wrapper.RoCrate.register_file].

Parameters:

Name Type Description Default
add_size bool

If true, automatically add the size of the file to the metadata

False

validate(threshold: Severity | None = None) -> None

Raises an exception if the RO-Crate is not valid.

write()

Writes the RO-Crate to ro-crate-metadata.json

ContextGraph

A graph and associated JSON-LD context

Typical usage involves creating new subgraphs via RdfType.add and then merging them into this context graph.

context_source: list[dict[str, Any] | str | None] | dict[str, Any] | str | None property

Returns the components that make up the context for this graph.

full_context: Context property

Returns a Context that can be used for resolving terms. It should not be used for serialization and context_source should be used instead.

__init__(**kwargs: Unpack[ContextGraphKwargs])

Initializes a new ContextGraph with an optional initial graph. If no graph is provided, an empty Graph is created.

add(subgraph: ContextGraph | Graph | Triple) -> None

Adds a subgraph or triple to this context graph. If the subgraph is a ContextGraph, it will also merge the context.

add_entity(entity: EntityClass, *args: EntityArgs) -> EntityClass

Adds any type of entity to the crate

Returns:

Type Description
EntityClass

The ID of the new entity

add_metadata(entity: RdfClass, *args: EntityArgs) -> None

Add metadata for an existing entity.

Returns:

Type Description
None

The ID of the updated entity

Parameters:

Name Type Description Default
uri

ID of the entity being described

required

compile(**kwargs) -> str

Compiles the RO-Crate to a JSON-LD string

Parameters:

Name Type Description Default
strip_datatypes

If True, any datatypes or languages will be stripped from literals. This is useful for compatibility with systems that do not support datatypes.

required
**kwargs

Additional keyword arguments to pass to the RDFLib serializer

{}

navigate() -> GraphNavigator

Returns a navigator that can be used to traverse and mutate the graph.

navigate_to(iri: IdentifiedNode | RdfClass) -> UriNode

Returns a navigator for the given IRI. This lets you mutate and query the graph using the rdfnav package.

register_term(term: RdfTerm | Term) -> None

Adds a custom term to the context

strip_datatypes(strip_datatypes: bool = True, strip_languages: bool = True) -> Graph

Returns a copy of the graph with datatypes and/or languages stripped from literals.

Parameters:

Name Type Description Default
strip_datatypes bool

If True, any datatypes will be stripped from literals.

True
strip_languages bool

If True, any languages will be stripped from literals.

True

DetatchedCrate dataclass

RdfClass

Bases: RdfType

An RDF entity that has a URI (or blank node).

Typically these are instance of rdfs:Class or owl:Class.

add(*args: EntityArgs, graph: ContextGraph | None = None) -> ContextGraph

Adds triples to a graph with this entity as the subject.

If the graph is not provided, an empty one will be created and returned.

Example

from rdfcrate import ContextGraph, rdfs, sdo

graph = ContextGraph() sdo.Thing("http://example.org/thing").add( sdo.name("My Thing"), sdo.description("An example thing"), graph=graph )

adhoc(term: RdfTerm) -> type[Self] classmethod

Makes an ad-hoc type class from a term.

to_type_property() -> rdf.type classmethod

Converts this class wrapper to an instance of RdfProperty that can be used to tag entities with this type.

update(*args: EntityArgs, graph: ContextGraph | None = None) -> ContextGraph

Updates this entity with the given properties, in a graph.

If the graph is not provided, an empty one will be created and returned.

with_term_label(label: str) -> type[RdfClass] classmethod

Creates a new instance of this class with the given label. This is useful in cases where the term label is already defined by another vocabulary

RdfLiteral

Bases: RdfType

An RDF entity that is a literal value.

Typically these are instances of rdfs:Literal

RdfProperty dataclass

Bases: PropertyProtocol, Generic[T]

Represents the double of (predicate, object), with the subject being the class this is attached to.

This is the normal way properties will be defined As a user, you will typically use this class by instantiating subclasses from the vocabs module and attaching them to entities. If you want to define custom vocabulary, you can first subclass this and then create instances of that subclass.

adhoc(term: RdfTerm, object: T) -> RdfProperty staticmethod

Makes an ad-hoc property class from a term.

If you want to use the property multiple times, you should define a subclass instead.

reverse(subject: RdfType) -> ReverseProperty classmethod

Utility for when you want to make the current entity the object of a property, rather than the subject.

with_term_label(label: str) -> type[RdfProperty[T]] classmethod

Creates a new instance of this property with the given label. This is useful in cases where the term label is already defined by another vocabulary

RdfTerm

Wrapper for a term with a label and URI. This is needed because RO-Crate bans the use of full URIs in JSON-LD.

ReverseProperty dataclass

Bases: PropertyProtocol

Represents the double of (subject, predicate), with the object being the class this is attached to.

SpecVersion

get_context() -> dict

Loads the context from the context URL.