jinjyaml.constructor module

class jinjyaml.constructor.Constructor[source]

Bases: object

Constructor class for jinja2.Template YAML tags.

When parsing a YAML string, this class constructs template tags into Data objects.

To add the constructor to a PyYAML loader, use the following code:

import yaml
import jinjyaml as jy

ctor = jy.Constructor()

# Note: Tag names start with "!"

# Add to default loader
yaml.add_constructor("!j2", ctor)
# or: Add to CLoader
yaml.add_constructor("!j2", ctor, yaml.CLoader)
# or: Add to SafeLoader
yaml.add_constructor("!j2", ctor, yaml.SafeLoader)
# or: Add to other loaders...

Attention

  • Custom YAML tags must start with "!". When invoking yaml.add_constructor, the tag parameter must include a single leading "!".

  • The content of the tag must be text.