jinjyaml package¶
Submodules¶
Module contents¶
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[source]¶
Bases:
objectConstructor class for
jinja2.TemplateYAML tags.When parsing a YAML string, this class constructs template tags into
Dataobjects.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 invokingyaml.add_constructor, thetagparameter must include a single leading"!".The content of the tag must be text.
- class jinjyaml.Data(source: str)[source]¶
Bases:
objectA custom PyYAML tag that stores the 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[source]¶
Recursively render and parse template tag objects in a YAML document tree.
This function processes an object that may contain
Datainstances, such as lists or dictionaries. It can handle the following types of input:- A mapping or sequence object returned by a PyYAML Loader:
Recursively searches for
Dataobjects withinobj.Renders the
Data.sourceattribute as a string source for ajinja2.Template.Parses the rendered string using the same PyYAML Loader that loaded
obj.Returns the entire
objwithDataobjects replaced by their corresponding parsed Python objects.
- A single
Dataobject: Renders its
Data.sourceattribute as a string source for ajinja2.Template.Parses the rendered string using the same PyYAML Loader that loaded
obj.Returns the parsed Python object.
- A single
- Other scalar objects returned by a PyYAML Loader:
Directly returns
objwithout any changes.
Note
- Parameters:
obj (Any) – An object that may contain
Datainstances.loader_type (Type[Union[_Loader, _CLoader]]) – The PyYAML Loader used to load
obj.env (Optional[jinja2.Environment]) – The
jinja2.Environmentfor template rendering (optional).context (Optional[Mapping[str, Any]]) – A dictionary of variable name-value pairs for
jinja2template rendering (optional).inplace (bool) –
Whether to perform an in-place replacement of
Dataobjects withinobj.
- Returns:
The final parsed and extracted Python object.
- Return type:
Any
- class jinjyaml.Representer(tag: str)[source]¶
Bases:
objectRepresenter for
jinja2.Templatetags.When dumping an object into a YAML string, this class converts
Dataobjects to their string representation.To add the representer to a PyYAML dumper, use the following code:
representer = jinjyaml.Representer("j2") # Note: No "!" here! yaml.add_representer(Node, representer)
Attention
The tag name passed to the Representer constructor MUST NOT include the leading “!”. This is because PyYAML automatically adds the “!” when registering the representer.
Ensure that Node is the correct type for the objects you want to represent.
- Parameters:
tag (str)