jinjyaml.functions module

jinjyaml.functions.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 Data instances, such as lists or dictionaries. It can handle the following types of input:

  • A mapping or sequence object returned by a PyYAML Loader:
    1. Recursively searches for Data objects within obj.

    2. Renders the Data.source attribute as a string source for a jinja2.Template.

    3. Parses the rendered string using the same PyYAML Loader that loaded obj.

    4. Returns the entire obj with Data objects replaced by their corresponding parsed Python objects.

  • A single Data object:
    1. Renders its Data.source attribute as a string source for a jinja2.Template.

    2. Parses the rendered string using the same PyYAML Loader that loaded obj.

    3. Returns the parsed Python object.

  • Other scalar objects returned by a PyYAML Loader:

    Directly returns obj without any changes.

Note

  • obj must be a mutable dict or list-like object if inplace is True.

  • If obj is an instance of Data, it will not be changed, even if inplace is set to True.

    However, nested Data objects within mutable structures will still be replaced.

  • The return value is always the parsed result.

Parameters:
  • obj (Any) – An object that may contain Data instances.

  • loader_type (Type[Union[_Loader, _CLoader]]) – The PyYAML Loader used to load obj.

  • env (Optional[jinja2.Environment]) – The jinja2.Environment for template rendering (optional).

  • context (Optional[Mapping[str, Any]]) – A dictionary of variable name-value pairs for jinja2 template rendering (optional).

  • inplace (bool) –

    Whether to perform an in-place replacement of Data objects within obj.

    • When True: Replaces every Data object with its corresponding parsed Python object within the passed-in obj.

    • When False (default): Renders and parses every Data object with its corresponding parsed Python object without modifying the passed-in object.

Returns:

The final parsed and extracted Python object.

Return type:

Any