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
schemadict

The annotated schema

assign(obj, name, value)[source]

Assign the given value to a property with the given name on the object

Parameters
objobject

The object to receive the assignment

namestr

The name on the object to assign to

valueobject

The value to assign

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
instancedict

The JSON object to create from

contextowmeta_core.context.Context

The context in which the object should be created

Raises
ValidationException

Raised when there’s an error with the given instance compared to the schema

fill_in(target, instance, ident=None)[source]

“Fill-in” an already existing target object with JSON matching a schema

make_instance(owm_type)[source]

Make an instance of the given type

Parameters
owm_typetype

The type for which an instance should be made

class owmeta_core.json_schema.DataObjectTypeCreator(*args, module, context=None, **kwargs)[source]

Bases: TypeCreator

Creates DataObject types from a JSON Schema

Attributes
cdictdict

Map from paths in the schema to the dictionaries that will be passed into the class definition. The path is the same as passed into create_type

modulestr

The module in which classes will be defined

Parameters
modulestr

The module in which classes will be defined

contextowmeta_core.context.Context or str

The class context in which the various types will be declared

determine_property_type(path, k, v)[source]

Determine the type of property created by proc_prop

select_base_types(path, schema)[source]

Returns the base types for create_type

Parameters
pathtuple

The path to the sub-schema

schemadict

The sub-schema at the path location

class owmeta_core.json_schema.DataSourceTypeCreator(*args, module, context=None, **kwargs)[source]

Bases: DataObjectTypeCreator

Creates DataSource types from a JSON Schema

Parameters
modulestr

The module in which classes will be defined

contextowmeta_core.context.Context or str

The class context in which the various types will be declared

select_base_types(path, schema)[source]

Returns the base types for create_type

Parameters
pathtuple

The path to the sub-schema

schemadict

The sub-schema at the path location

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
namestr

The name of the root class and the base-name for all classes derived from a schema’s properties

schemadict

A JSON schema as would be returned by json.load()

definition_base_namestr

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

annotate()[source]

Returns the annotated JSON schema

create_type(path, schema)[source]

Create the OWM type.

At this point, the properties for the schema will already be created.

Parameters
pathtuple

The path to the type

schemadict

The JSON schema that applies to this type

extract_name(path)[source]

Generates a class name from the path to the sub-schema

Parameters
pathtuple

Path to the sub-schema

proc_prop(path, key, value)[source]

Process property named key with the given value.

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', ...}) for data, but for data_data, it would be called like .proc_prop(('properties', 'data'), 'data_data', {'type': 'string'})

Parameters
pathtuple

The path to the given property.

keystr

The name of the property

valuedict

the definition of the property

classmethod retrieve_type(annotated_schema, pointer='')[source]

Look up the type created for the object at the given JSON pointer location

Parameters
annotated_schemadict

Annotated schema as returned from annotate

pointerstr, optional

JSON pointer to the schema/sub-schema

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 referenced document.

Parameters
documentobject

The referent document. Typically a collections.abc.Mapping (e.g., a dict) or collections.abc.Sequence, but if fragment is #, then the document is returned unchanged.

fragmentstr

a URI fragment to resolve within it

Returns
object

The part of the document referred to

owmeta_core.json_schema.resolve_json_pointer(document, pointer)[source]

Resolve a fragment within the referenced document.

Parameters
documentobject

The referent document. Typically a collections.abc.Mapping (e.g., a dict) or collections.abc.Sequence, but if fragment is #, then the document is returned unchanged.

pointerstr

a JSON pointer to resolve in the document

Returns
object

The part of the document referred to