YAML Codec

Codec for serialising and de-serialising YAML data. Supports both array and objects for mapping into resources or collections of resources.

The YAML codec uses the yaml module and uses the compiled C versions of the library if available.

Methods

odin.codecs.yaml_codec.load(fp, resource=None, full_clean=True, default_to_not_supplied=False)[source]

Load a from a YAML encoded file.

If a resource value is supplied it is used as the base resource for the supplied YAML. I one is not supplied a resource type field $ is used to obtain the type represented by the dictionary. A ValidationError will be raised if either of these values are supplied and not compatible. It is valid for a type to be supplied in the file to be a child object from within the inheritance tree.

Parameters:
  • fp – a file pointer to read YAML data from.
  • resource – A resource type, resource name or list of resources and names to use as the base for creating a resource. If a list is supplied the first item will be used if a resource type is not supplied.
  • full_clean – Do a full clean of the object as part of the loading process.
  • default_to_not_supplied – Used for loading partial resources. Any fields not supplied are replaced with NOT_SUPPLIED.
Returns:

A resource object or object graph of resources loaded from file.

odin.codecs.yaml_codec.loads(fp, resource=None, full_clean=True, default_to_not_supplied=False)

Load a from a YAML encoded file.

If a resource value is supplied it is used as the base resource for the supplied YAML. I one is not supplied a resource type field $ is used to obtain the type represented by the dictionary. A ValidationError will be raised if either of these values are supplied and not compatible. It is valid for a type to be supplied in the file to be a child object from within the inheritance tree.

Parameters:
  • fp – a file pointer to read YAML data from.
  • resource – A resource type, resource name or list of resources and names to use as the base for creating a resource. If a list is supplied the first item will be used if a resource type is not supplied.
  • full_clean – Do a full clean of the object as part of the loading process.
  • default_to_not_supplied – Used for loading partial resources. Any fields not supplied are replaced with NOT_SUPPLIED.
Returns:

A resource object or object graph of resources loaded from file.

odin.codecs.yaml_codec.dump(resource, fp, dumper=<class 'odin.codecs.yaml_codec.OdinDumper'>, **kwargs)[source]

Dump to a JSON encoded file.

Parameters:
  • resource – The root resource to dump to a JSON encoded file.
  • dumper – Dumper to use serializing to a string; default is the OdinDumper.
  • fp – The file pointer that represents the output file.
odin.codecs.yaml_codec.dumps(resource, dumper=<class 'odin.codecs.yaml_codec.OdinDumper'>, **kwargs)[source]

Dump to a JSON encoded string.

Parameters:
  • resource – The root resource to dump to a JSON encoded file.
  • dumper – Dumper to use serializing to a string; default is the OdinDumper.
Returns:

YAML encoded string.

Customising Encoding

Serialisation of Odin resources is handled by a customised yaml.Dumper. Additional data types can be appended to the odin.codecs.yaml_codec.YAML_TYPES dictionary.

Example usage

Loading a resource from a file:

from odin.codecs import yaml_codec

with open('my_resource.yaml') as f:
    resource = yaml_codec.load(f)