Python API reference¶
pip install ontoenv # Python 3.11+
The package exposes the Rust core through PyO3 bindings, with native rdflib interop. Pre-built wheels are published on PyPI; no Rust toolchain is required.
This page groups the API by purpose. Generated API has the generated signatures and docstrings.
Opening an environment¶
Call |
Behavior |
|---|---|
|
Create if missing, reopen if present. The normal entry point. |
|
Create a new environment; fails if one exists unless
|
|
Open an existing environment; fails if missing. Never synchronizes. |
|
Index an already-populated custom store for the first time. |
|
Rebuild the catalog after |
|
In-memory environment; nothing is persisted. |
sync accepts "auto" (default), "full", or "catalog" — see
Staying in sync.
**options accepts any key from Configuration. On reopen, an omitted
option preserves its saved value; an explicit value — including False,
"default", and [] — overrides it. Writable connections persist
overrides; read-only ones keep them session-local.
Direct constructor
OntoEnv(path=None, recreate=False, read_only=False, temporary=False,
search_directories=None, includes=None, excludes=None,
include_ontologies=None, exclude_ontologies=None,
strict=None, offline=None, require_ontology_names=None,
use_cached_ontologies=None, resolution_policy=None,
remote_cache_ttl_secs=None, graph_store=None, root=".")
Supported and used internally, but the named methods above express intent more
clearly. Note that recreate=True deletes and rebuilds the target
.ontoenv directory — it is not a reconnect.
Deprecated since version 0.6: create_or_use_cached=True emits DeprecationWarning; use
OntoEnv.connect(path). Removal planned for 0.7.
Deprecated since version 0.6: init_from_store=True; use OntoEnv.adopt(path, graph_store).
Closing¶
env.close()— release resources.env.flush()— write pending changes to storage.with OntoEnv.connect(...) as env:— callsclose()on exit.
Adding ontologies¶
Method |
Returns / notes |
|---|---|
|
The ontology’s IRI. location is a path, URL, or |
|
As |
|
The new IRI. Rewrites the stored graph and rebuilds the import graph. |
rename= rewrites every occurrence of the declared IRI in the stored graph,
except owl:versionIRI values. See Rename and alias ontologies.
Refreshing¶
Method |
Reconciles |
|---|---|
|
Ontology sources — files and URLs — following their imports. Without location, all configured sources. |
|
Graphs changed directly in a custom store. Returns a
|
graphs= is an exact set of backend graph IDs and is never expanded.
full=True cannot be combined with it.
Deprecated since version 0.6: update(all=True); use update(force=True).
Reading graphs¶
Every read comes in a read-only view and a mutable copy. See Views and copies.
Method |
Returns |
|---|---|
|
Read-only store-backed |
|
Mutable |
|
|
|
|
|
|
|
|
|
Read-only |
|
Mutable |
|
Re-snapshot the environment into an existing store-backed dataset. |
Deprecated since version 0.6: snapshot_as_dataset(...) and to_rdflib_dataset(...); use
get_dataset() / copy_dataset().
Streaming¶
iter_triples(uri)—(s, p, o)rdflib terms for one graph.iter_closure_triples(uri, recursion_depth=-1)— the same across a closure. Not de-duplicated across named graphs.
Merging into a caller’s graph¶
import_graph(destination_graph, uri, recursion_depth=-1)— merge the closure of uri into destination_graph in place.import_dependencies(graph, recursion_depth=-1, fetch_missing=False)— resolve graph’s ownowl:importsand merge them into it. Returns the merged IRIs.get_dependencies(graph, graph_name=None, recursion_depth=-1, fetch_missing=False)—(Graph, closure_iris); same resolution without modifying the caller’s graph. graph_name overrides the IRI used forsh:prefixesrewriting.
With fetch_missing=True, strict mode aborts on an unavailable import while
non-strict mode records and skips it. A completed best-effort call leaves no
recovery marker.
Inspecting¶
Method |
Returns |
|---|---|
|
Every ontology IRI in the environment. |
|
An |
|
IRIs that directly import uri. |
|
Closure IRIs. uri may be a string IRI or an |
|
Unresolvable |
|
Prefix → namespace mapping. |
|
Filesystem path of the graph store, if any. |
|
Print the environment state to stdout. |
Ontology exposes id, name, imports, location,
last_updated, version_properties, and namespace_map.
Aliases¶
add_alias(alias_iri, canonical_iri)— an alias may only point at a canonical IRI, never another alias.remove_alias(alias_iri)resolve_alias(alias_iri)→ canonical IRI orNoneget_aliases_for(canonical_iri)→ list of aliasesis_canonical_iri(iri)→bool
Aliases resolve transparently in get_graph, get_closure, uri in
env, and env[uri].
Runtime configuration¶
Each setting has a getter and a setter. These change an open, writable environment and persist the change.
Getter |
Setter |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Reconfiguration never triggers an implicit scan. Runtime modes apply
immediately; changed discovery paths and filters apply on the next
update().
Container protocols¶
len(env) # number of ontologies
uri in env # True if uri resolves — canonical name, alias, or source URL
env[uri] # shorthand for env.get_graph(uri)
for name in env: ... # iterate ontology IRIs
with OntoEnv.connect("./env") as env:
...
bool(env) is always True; use env is None to test for absence.
Exceptions¶
Exception |
Raised when |
|---|---|
|
A known unresolved |
|
Startup found an interrupted-mutation marker. See Recover an interrupted environment. |
|
A custom store changed in a way OntoEnv will not reconcile on its own —
it reports drift it cannot localize to specific graphs, its identity
does not match the saved catalog, or it changed during a scan. Retry
with |
|
An operation needs an optional store method the object does not implement. |
|
Mutating a read-only view, or looking up an IRI that was never declared or attempted. |
An unknown IRI stays a plain ValueError precisely so that catching
UnresolvedImportError for expected missing imports does not also swallow
genuine lookup mistakes.