owmeta_core.json_schema module¶
- exception owmeta_core.json_schema.AssignmentValidationException[source]¶
Bases:
ValidationException
Raised when an attempt is made to assign an inappropriate value with
Creator
- exception owmeta_core.json_schema.SchemaException[source]¶
Bases:
Exception
Raised for an invalid input given to
TypeCreator
- exception owmeta_core.json_schema.ValidationException[source]¶
Bases:
Exception
Raised for an invalid input given to
Creator
- class owmeta_core.json_schema.Creator(schema)[source]¶
Bases:
object
Creates objects based on a JSON schema augmented with type annotations as would be produced by
TypeCreator
Currently, only annotations for JSON objects are supported. In the future, conversions for all types (arrays, numbers, ints, strings) may be supported.
Takes a schema annotated with ‘_owm_type’ entries indicating which types are expected at each position in the object and produces an instance of the root type described in the schema
- Parameters
- schema
dict
The annotated schema
- schema
- assign(obj, name, value)[source]¶
Assign the given value to a property with the given name on the object
- create(instance, ident=None)[source]¶
Creates an instance of the root OWM type given a deserialized instance of the type described in our JSON schema.
A context can be passed in and it will be used to contextualize the OWM types
- Parameters
- instance
dict
The JSON object to create from
- context
owmeta_core.context.Context
The context in which the object should be created
- instance
- Raises
ValidationException
Raised when there’s an error with the given instance compared to the schema
- class owmeta_core.json_schema.DataObjectTypeCreator(*args, module, context=None, **kwargs)[source]¶
Bases:
TypeCreator
Creates DataObject types from a JSON Schema
- Attributes
- Parameters
- module
str
The module in which classes will be defined
- context
owmeta_core.context.Context
orstr
The class context in which the various types will be declared
- module
- class owmeta_core.json_schema.DataSourceTypeCreator(*args, module, context=None, **kwargs)[source]¶
Bases:
DataObjectTypeCreator
Creates DataSource types from a JSON Schema
- Parameters
- module
str
The module in which classes will be defined
- context
owmeta_core.context.Context
orstr
The class context in which the various types will be declared
- module
- class owmeta_core.json_schema.TypeCreator(name, schema, definition_base_name='')[source]¶
Bases:
object
Creates OWM types from a JSON schema and produces a copy of the schema annotated with the created types.
- Parameters
- name
str
The name of the root class and the base-name for all classes derived from a schema’s properties
- schema
dict
A JSON schema as would be returned by
json.load()
- definition_base_name
str
The base-name for types defined in the schema’s definitions. optional. By default, definitions just take the capitalized form of their key in the “definitions” block
- name
- create_type(path, schema)[source]¶
Create the OWM type.
At this point, the properties for the schema will already be created.
- extract_name(path)[source]¶
Generates a class name from the path to the sub-schema
- Parameters
- path
tuple
Path to the sub-schema
- path
- proc_prop(path, key, value)[source]¶
Process property named
key
with the givenvalue
.The
path
will not include the key but will be the path of the definition that contains the property. For example, in:{"$schema": "http://json-schema.org/schema", "title": "Example Schema", "type": "object", "properties": {"data": {"type": "object", "properties": { "data_data": {"type": "string"} }}}}
proc_prop
would be called as.proc_prop((), 'data', {'type': 'object', ...})
fordata
, but fordata_data
, it would be called like.proc_prop(('properties', 'data'), 'data_data', {'type': 'string'})
- classmethod retrieve_type(annotated_schema, pointer='')[source]¶
Look up the type created for the object at the given JSON pointer location
- Parameters
- Returns
type
The type at the given JSON pointer location
- Raises
LookupError
Raised when the pointer has no referent in the given document or there’s type associated with the referent
- owmeta_core.json_schema.resolve_fragment(document, fragment)[source]¶
Resolve a
fragment
within the referenceddocument
.- Parameters
- document
object
The referent document. Typically a
collections.abc.Mapping
(e.g., a dict) orcollections.abc.Sequence
, but if fragment is#
, then the document is returned unchanged.- fragment
str
a URI fragment to resolve within it
- document
- Returns
object
The part of the document referred to
- owmeta_core.json_schema.resolve_json_pointer(document, pointer)[source]¶
Resolve a
fragment
within the referenceddocument
.- Parameters
- document
object
The referent document. Typically a
collections.abc.Mapping
(e.g., a dict) orcollections.abc.Sequence
, but if fragment is#
, then the document is returned unchanged.- pointer
str
a JSON pointer to resolve in the document
- document
- Returns
object
The part of the document referred to