jinjyaml package¶
Application specific tag of Jinja2 template in PyYAML.
It may be useful if you only want to render special tag nodes in the document, instead of whole YAML string as a template.
- class jinjyaml.Constructor¶
Bases:
objectConstructor class for
jinja2.TemplateYAML tags.When parsing YAML string, the class constructs template tags to
Dataobjects.Add the constructor to PyYAML Loader as below:
import yaml import jinjyaml as jy ctor = jy.Constructor() # Attention: tag name starts 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 tag starts with
"!". When we invokeyaml.add_constructor, thetagparameter MUST have a single"!"at the beginning.Content of the tag MUST be text
- class jinjyaml.Data(source: str)¶
Bases:
objectA PyYAML Custom tag stores string source of a
jinja2.Templateobject.- Parameters:
source (str)
- jinjyaml.extract(obj: Any, loader_type: Type[_Loader | _CLoader], env: jinja2.Environment | None = None, context: Mapping[str, Any] | None = None, inplace: bool = False) Any¶
Recursively render and parse template tag objects in a YAML doc-tree.
- Parameters:
obj (Any) –
Object like list or dictionary contains
Datainstances.It may be:
A mapping or sequence object returned by PyYAML Loader.
In this case, the function does:
Recursively search inside
objforDataobjects.Render
Data.sourceas string source ofjinja2.Templateof each foundDataobject.Parse the rendered string with the PyYAML Loader who loads
obj.Return the whole
objwithDataobjects replaced with corresponding parsed Python object.
A single
Dataobject.In this case, the function does:
Render it’s
Data.source()as string source ofjinja2.Template.Parse the rendered string with the PyYAML Loader who loads
obj.Return the parsed Python object.
Other scalar objects returned by a PyYAML Loader:
In this case, the function directly returns
objwith noting changed.
loader_type (Type[Union[_Loader, _CLoader]]) – The PyYAML Loader who loads
objenv (Optional[jinja2.Environment]) – Jinja2 environment for template rendering.
context (Optional[Mapping[str, Any]]) – Variables name-value pairs for Jinja2 template rendering.
inplace (bool) –
Whether to make an in-place replacement on
Dataobjects insideobj.When
True: In-place replace everyDataobject with corresponding parsed Python object inside the passed-inobj.When
False(default): render and parse everyDataobject with corresponding parsed Python object, without modify the passed-in object.
Note
When the passed-in
objargument is an instance ofData, it won’t be changed, eveninplacewas set toTrue. But if there was a mutabledictorlistlike object pared by YAML loader, which has cascadeDatain it, the cascade part would be replaced. However, return value is just the parsed result.
- Returns:
Final extracted Python object
- Return type:
Any
- class jinjyaml.Representer(tag: str)¶
Bases:
objectRepresenter for
jinja2.Templatetags.When dumping an object into YAML string, convert
Datato string.Add the representer to PyYAML Dumper as below:
representer = jinjyaml.Representer("j2") # No "!" here !!! yaml.add_representer(Node, representer)
- Parameters:
tag (str)