jsonschema_extras.registries package¶
- jsonschema_extras.registries.LOADS_FN_JSON_DEFAULT(text, *, loads_kwargs=None)¶
Deserialize a schema from JSON text
Satisfies
LoadTextFn.
- exception jsonschema_extras.registries.NoSuchResourceFromValueError[source]¶
Bases:
ValueErrorValueErrorin a schema’s URI that should be interpreted as absence of the resource.
- jsonschema_extras.registries.RESOURCE_FROM_CONTENTS_FN_DEFAULT(contents, default_specification=<class 'referencing._core.Specification'>)¶
Create a resource guessing which specification applies to the contents.
- Raises:
CannotDetermineSpecification – if the given contents don’t have any discernible information which could be used to guess which specification they identify as
- Return type:
- Parameters:
contents (D)
default_specification (type[Specification[D]] | Specification[D])
- class jsonschema_extras.registries.RetrievalURITranslator(retrieve, uri_base_old, uri_base_new, *, allow_old=False)[source]¶
Bases:
Generic[D]Decorator for a
Retrievecallable translating URIs that match with a given old base URI to a new base URI.When retrieving a resource, this decorator attempts to resolve the portion of the URI relative to the old base URI. If successful, it rewrites and appends this portion to the new base URI. If translation fails, it either falls back to the original URI (if allow_old is True) or immediately raises
NoSuchResource.Both base URIs and specific schema URIs are restricted: each must have a scheme and a path, and no other components are allowed (i.e. no credentials, netloc, query, fragment). “Parent” segments
..in the path of a schema URI are prohibited. All of the above properties are validated.
- class jsonschema_extras.registries.RetrieversChain(retrievers=(), postpone_excs=(), pass_excs=(), should_postpone_exc_fn=<function RetrieversChain.<lambda>>)[source]¶
Bases:
list[Retrieve[D]],Generic[D]Chain of
Retrievecallables that attempts each retriever in order until one succeeds.Warning
The chain is based on
list, so it is mutable (and has all methods oflist). If you mutate it after passing to aRegistry, make sure you know what you are doing.Allows a user to customize which exceptions are postponed vs raised immediately.
On retrieval failure, provides detailed cause info.
- Parameters:
retrievers (
Iterable[Retrieve[TypeVar(D)]]) – Retriever objects that implementreferencing.typing.Retrieve.postpone_excs (
Collection[type[Exception]]) – Exceptions that should trigger a retry with the next retriever.NoSuchResourceis always added implicitly to the collection. Default:().pass_excs (
Collection[type[Exception]]) – Exceptions that should be re‑raised immediately, bypassing postponement logic. Default:().should_postpone_exc_fn (
Callable[[Exception],bool]) – Predicate to dynamically decide whether to postpone a caught exception (default:lambda e: False). (Technically can be used to immediately raiseNoSuchResource, whatever you might want it for.)
- property pass_excs: Collection[type[Exception]]¶
Exposed read‑only view of pass_excs passed into the constructor.
- property postpone_excs: Collection[type[Exception]]¶
Exposed read‑only view of postpone_excs passed into the constructor.
- jsonschema_extras.registries.build_schemas_from_filesystem_retriever(uri_base, path, *, open_kwargs=None, cache=None, loads=<function schema_data_from_json_text>, from_contents=<bound method Resource.from_contents of <class 'referencing._core.Resource'>>)[source]¶
Returns a new retrieval callable for schemas on a filesystem under a specified root.
The returned retriever maps URIs under uri_base to files under path. See the rules of this mapping and restrictions on URIs in description of
jsonschema_extras.registries.filesystem.- Parameters:
uri_base (
str) – Base URI corresponding to the filesystem root. Must have the schemefile:and a path, no other components are allowed (i.e. no credentials, netloc, query, fragment).path (
str|PathLike[str]) – Root filesystem path containing the schemas.open_kwargs (
Mapping[str,Any] |None) – Keyword arguments to pass to Python built-in functionopen(). Allowed arguments:buffering,encoding(default:'utf-8'),errors,newline.cache (
GenericAlias[TypeVar(D)] |Literal['default'] |None) – Caching decorator forRetrieve, or'default'to use the default caching implementation (see description ofjsonschema_extras.registries.retrievalfor details). Defaults toNone, meaning no caching.loads (
GenericAlias[TypeVar(D)]) – Function to deserialize resource contents (for example, JSON data structure from JSON string). Default:LOADS_FN_JSON_DEFAULT(for JSON).from_contents (
GenericAlias[TypeVar(D)]) – Function to produce aResourcefrom deserialized resource contents. Default:from_contents().
- Return type:
- Returns:
A
Retrievecallable that resolves schema URIs under uri_base from files under path (with caching, if instructed so).- Raises:
ValueError – On invalid uri_base.
TypeError – If there are arguments in open_kwargs other than allowed arguments.
- jsonschema_extras.registries.file_path_from_uri_by_base(uri, uri_base, path)[source]¶
Computes the file path of a locally stored schema based on its URI.
Maps a URI under uri_base to a file under path. See the rules of this mapping and restrictions on URIs in description of
jsonschema_extras.registries.filesystem.- Parameters:
uri (
str) – URI of the schema to retrieve. Should be relative to uri_base, otherwiseNoSuchResourceis raised. Must have the schemefile:and a path (with no “parent” segments..), no other components are allowed (i.e. no credentials, netloc, query, fragment).uri_base (
str) – Base URI corresponding to the filesystem root. Must have the schemefile:and a path, no other components are allowed (i.e. no credentials, netloc, query, fragment).path (
str|PathLike[str]) – Root filesystem path containing the schemas.
- Return type:
- Returns:
Path under path to the schema on the filesystem.
- Raises:
NoSuchResourceFromValueError – URI resolution relative to the base URI failed.
ValueError – On invalid uri or uri_base.
Examples
>>> file_path_from_uri_by_base( ... 'file:/schemas/person.json', ... 'file:/schemas/', ... '/var/data/schemas', ... ).as_posix() '/var/data/schemas/person.json'
>>> file_path_from_uri_by_base( ... 'file:/schemas/definitions/address.json', ... 'file:/schemas/', ... '/var/data/schemas', ... ).as_posix() '/var/data/schemas/definitions/address.json'
- jsonschema_extras.registries.retrieve_text_from_filesystem(uri, uri_base, path, *, open_kwargs=None)[source]¶
Retrieves text of a schema on a filesystem under a specified root.
Maps URIs under uri_base to files under path. See the rules of this mapping and restrictions on URIs in description of
jsonschema_extras.registries.filesystem.- Parameters:
uri (
str) – URI of the schema to retrieve. Should be relative to uri_base, otherwiseNoSuchResourceis raised. Must have the schemefile:and a path (with no “parent” segments..), no other components are allowed (i.e. no credentials, netloc, query, fragment).uri_base (
str) – Base URI corresponding to the filesystem root. Must have the schemefile:and a path, no other components are allowed (i.e. no credentials, netloc, query, fragment).path (
str|PathLike[str]) – Root filesystem path containing the schemas.open_kwargs (
Mapping[str,Any] |None) – Keyword arguments to pass to Python built-in functionopen(). Allowed arguments:buffering,encoding(default:'utf-8'),errors,newline.
- Return type:
- Returns:
Text (serialized representation) of the schema loaded from the filesystem.
- Raises:
referencing.exceptions.NoSuchResource – If a schema by the given URI does not exist (URI resolution relative to the base URI failed (has a
NoSuchResourceFromValueErroras cause), or the file was not found on the filesystem (hasFileNotFoundErroras cause).OSError – If the file cannot be opened for a reason other than it was not found. Passed through from the Python built-in function
open().ValueError – On invalid uri or uri_base. OR If there was an encoding error when reading the file (passed through from the Python built-in function
open()).TypeError – If there are arguments in open_kwargs other than allowed arguments.
- jsonschema_extras.registries.schema_data_from_json_text(text, *, loads_kwargs=None)[source]¶
Deserialize a schema from JSON text
Satisfies
LoadTextFn.
- jsonschema_extras.registries.to_cached_resource(cache=None, loads=<function schema_data_from_json_text>, from_contents=<bound method Resource.from_contents of <class 'referencing._core.Resource'>>)[source]¶
Build a decorator to make a resource retrieval callable out of a
RetrieveTextFnwith caching.RetrieveTextFnretrieves serialized representation by URI. A decorator produced by this function adds deserialization (loads), creating aResourceand caching.- Parameters:
cache (
GenericAlias[TypeVar(D)] |None) – Caching decorator forRetrieve, orNoneto use the default caching implementation (seereferencing.retrieval.to_cached_resource()). Defaults toNone.loads (
GenericAlias[TypeVar(D)]) – Function to deserialize resource contents (for example, JSON data structure from JSON string). Default:LOADS_FN_JSON_DEFAULT(for JSON).from_contents (
GenericAlias[TypeVar(D)]) – Function to produce aResourcefrom deserialized resource contents. Default:from_contents().
- Return type:
- Returns:
Decorator taking a text retriever
RetrieveTextFnand returning a caching resource retriever.
- jsonschema_extras.registries.to_maybe_cached_resource(cache=None, loads=<function schema_data_from_json_text>, from_contents=<bound method Resource.from_contents of <class 'referencing._core.Resource'>>)[source]¶
Build a decorator to make a resource retrieval callable out of a
RetrieveTextFnwith optional caching.RetrieveTextFnretrieves serialized representation by URI. A decorator produced by this function adds deserialization (loads), creating aResourceand optional caching.- Parameters:
cache (
GenericAlias[TypeVar(D)] |Literal['default'] |None) – Caching decorator forRetrieve, or'default'to use the default caching implementation (seereferencing.retrieval.to_cached_resource()). Defaults toNone, meaning no caching.loads (
GenericAlias[TypeVar(D)]) – Function to deserialize resource contents (for example, JSON data structure from JSON string). Default:LOADS_FN_JSON_DEFAULT(for JSON).from_contents (
GenericAlias[TypeVar(D)]) – Function to produce aResourcefrom deserialized resource contents. Default:from_contents().
- Return type:
- Returns:
Decorator taking a text retriever
RetrieveTextFnand returning a resource retriever.
- jsonschema_extras.registries.to_resource(loads=<function schema_data_from_json_text>, from_contents=<bound method Resource.from_contents of <class 'referencing._core.Resource'>>)[source]¶
Build a decorator to make a resource retrieval callable out of a
RetrieveTextFn.RetrieveTextFnretrieves serialized representation by URI. A decorator produced by this function adds deserialization (loads), creating aResource(with no caching).- Parameters:
loads (
GenericAlias[TypeVar(D)]) – Function to deserialize resource contents (for example, JSON data structure from JSON string). Default:LOADS_FN_JSON_DEFAULT(for JSON).from_contents (
GenericAlias[TypeVar(D)]) – Function to produce aResourcefrom deserialized resource contents. Default:from_contents().
- Return type:
- Returns:
Decorator taking a text retriever
RetrieveTextFnand returning a caching resource retriever.