yggdrasil.metaschema package

Subpackages

Submodules

yggdrasil.metaschema.encoder module

class yggdrasil.metaschema.encoder.JSONDecoder[source]

Bases: rapidjson.Decoder

Decoder class for Ygg messages.

string(s)[source]

Try to parse string with class.

class yggdrasil.metaschema.encoder.JSONEncoder[source]

Bases: rapidjson.Encoder

Encoder class for Ygg messages.

default(o)[source]

Encoder that allows for expansion types.

class yggdrasil.metaschema.encoder.JSONReadableEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: json.encoder.JSONEncoder

Encoder class for Ygg messages.

default(o)[source]

Encoder that allows for expansion types.

yggdrasil.metaschema.encoder.decode_json(msg, **kwargs)[source]

Decode a Python object from a JSON serialization.

Parameters
  • msg (str) – JSON serialization to decode.

  • **kwargs – Additional keyword arguments are passed to json.loads.

Returns

Deserialized Python object.

Return type

object

yggdrasil.metaschema.encoder.decode_yaml(msg, sorted_dict_type=None, **kwargs)[source]

Decode a Python object from a YAML serialization.

Parameters
  • msg (str) – YAML serialization to decode.

  • sorted_dict_type (type, optional) – Class that should be used to contain mapping objects while preserving order. Defaults to None and is ignored.

  • **kwargs – Additional keyword arguments are passed to yaml.load.

Returns

Deserialized Python object.

Return type

object

yggdrasil.metaschema.encoder.encode_json(obj, fd=None, indent=None, sort_keys=True, **kwargs)[source]

Encode a Python object in JSON format.

Parameters
  • obj (object) – Python object to encode.

  • fd (file, optional) – File descriptor for file that encoded object should be written to. Defaults to None and string is returned.

  • indent (int, str, optional) – Indentation for new lines in encoded string. Defaults to None.

  • sort_keys (bool, optional) – If True, the keys will be output in sorted order. Defaults to True.

  • **kwargs – Additional keyword arguments are passed to json.dumps.

Returns

Encoded object.

Return type

str, bytes

yggdrasil.metaschema.encoder.encode_yaml(obj, fd=None, indent=None, sorted_dict_type=None, **kwargs)[source]

Encode a Python object in YAML format.

Parameters
  • obj (object) – Python object to encode.

  • fd (file, optional) – File descriptor for file that encoded object should be written to. Defaults to None and string is returned.

  • indent (int, str, optional) – Indentation for new lines in encoded string. Defaults to None.

  • **kwargs – Additional keyword arguments are passed to yaml.dump.

Returns

Encoded object.

Return type

str, bytes

yggdrasil.metaschema.encoder.indent_char2int(indent)[source]

Convert a character indent into a number of spaces that should be used. Tabs are set to be equivalent to 4 spaces.

Parameters

indent (str) – String indent.

Returns

Number of whitespaces that is equivalent to the provided string.

Return type

int

yggdrasil.metaschema.encoder.string2import(s)[source]

Import a function/class based on its representation as a string.

Parameters

s (str) – String that may or may not contain a represetnation of an importable class or function.

Returns

Imported class/function if one exists, original

string if not.

Return type

str, class, function

yggdrasil.metaschema.normalizer module

class yggdrasil.metaschema.normalizer.UndefinedProperty[source]

Bases: object

Class to be used as a flag for undefined properties.

class yggdrasil.metaschema.normalizer.UninitializedNormalized[source]

Bases: object

Class to be used as a flag for uninitialized normalized value.

yggdrasil.metaschema.normalizer.create(*args, **kwargs)[source]

Dynamically create a validation/normalization class that subclasses the jsonschema validation class.

Parameters
  • normalizers (dict, optional) – Keys are tuples representing paths that exist within the schema at which the normalization functions stored in lists as their value counterparts should be executed. Defaults to empty dictionary.

  • no_defaults (bool, optional) – If True, defaults will not be set during normalization. Defaults to False.

  • required_defaults (bool, optional) – If True, defaults will be set for required properties, even if no_defaults is True. Defaults to False.

  • *args – Additional arguments are passed to jsonschema.validators.create.

  • **kwargs – Additional keyword arguments are passed to jsonschema.validators.create.

Module contents

exception yggdrasil.metaschema.MetaschemaTypeError[source]

Bases: TypeError

Error that should be raised when a class encounters a type it cannot handle.

yggdrasil.metaschema.create_metaschema(overwrite=False)[source]

Create the meta schema for validating ygg schema.

Parameters

overwrite (bool, optional) – If True, the existing meta schema will be overwritten. If False and the metaschema exists, an error will be raised. Defaults to False.

Returns

Meta schema specifying rules for ygg type schema. This includes

all original JSON schema rules with the addition of types and property definitions.

Return type

dict

Raises

RuntimeError – If the file already exists and overwrite is False.

yggdrasil.metaschema.data2dtype(data)[source]

Get numpy data type for an object.

Parameters

data (object) – Python object.

Returns

Numpy data type.

Return type

np.dtype

yggdrasil.metaschema.definition2dtype(props)[source]

Get numpy data type for a type definition.

Parameters

props (dict) – Type definition properties.

Returns

Numpy data type.

Return type

np.dtype

yggdrasil.metaschema.get_metaschema()[source]

Return the meta schema for validating ygg schema.

Returns

Meta schema specifying rules for ygg type schema. This includes

all original JSON schema rules with the addition of types and property definitions.

Return type

dict

Note

This function should not be called at the module level as it can cause the metaschema (if it dosn’t exist) to be generated before all of the necessary modules have been loaded.

yggdrasil.metaschema.get_validator(overwrite=False, normalizers=None, **kwargs)[source]

Return the validator that includes ygg expansion types.

Parameters
  • overwrite (bool, optional) – If True, the existing validator will be overwritten. Defaults to False.

  • normalizers (dict, optional) – Additional normalizers to add.

  • **kwargs – Additional keyword arguments are passed to normalizer.create.

Returns

JSON schema validator.

Return type

jsonschema.IValidator

yggdrasil.metaschema.normalize_instance(obj, schema, **kwargs)[source]

Normalize an object using the provided schema.

Parameters
  • obj (object) – Object to be normalized using the provided schema.

  • schema (dict) – Schema to use to normalize the provided object.

  • **kwargs – Additional keyword arguments are passed to normalize.

Returns

Normalized instance.

Return type

object

yggdrasil.metaschema.type2numpy(typedef)[source]

Convert a type definition into a numpy dtype.

Parameters

typedef (dict) – Type definition.

Returns

Numpy data type.

Return type

np.dtype

yggdrasil.metaschema.validate_instance(obj, schema, **kwargs)[source]

Validate an instance against a schema.

Parameters
  • obj (object) – Object to be validated using the provided schema.

  • schema (dict) – Schema to use to validate the provided object.

  • **kwargs – Additional keyword arguments are passed to validate.

Raises

ValidationError – If the object is not valid.

yggdrasil.metaschema.validate_schema(obj)[source]

Validate a schema against the metaschema.

Parameters

obj (dict) – Schema to be validated.

Raises

ValidationError – If the schema is not valid.