jinjyaml.functions module#

jinjyaml.functions.extract(obj: Any, loader_type: TYamlLoaderTypes, env: 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 Data instances.

    It may be:

    • A mapping or sequence object returned by PyYAML Loader.

      In this case, the function does:

      1. Recursively search inside obj for Data objects.

      2. Render Data.source as string source of jinja2.Template of each found Data object.

      3. Parse the rendered string with the PyYAML Loader who loads obj.

      4. Return the whole obj with Data objects replaced with corresponding parsed Python object.

    • A single Data object.

      In this case, the function does:

      1. Render it’s Data.source() as string source of jinja2.Template.

      2. Parse the rendered string with the PyYAML Loader who loads obj.

      3. Return the parsed Python object.

    • Other scalar objects returned by a PyYAML Loader:

      In this case, the function directly returns obj with noting changed.

  • loader_type (TYamlLoaderTypes) – The PyYAML Loader who loads obj

  • env (Environment | None) – Jinja2 environment for template rendering.

  • context (Mapping[str, Any] | None) – Variables name-value pairs for Jinja2 template rendering.

  • inplace (bool) –

    Whether to make an in-place replacement on Data objects inside obj.

    • When True: In-place replace every Data object with corresponding parsed Python object inside the passed-in obj.

    • When False (default): render and parse every Data object with corresponding parsed Python object, without modify the passed-in object.

    Tip

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

    Note

    When the passed-in obj argument is an instance of Data, it won’t be changed, even inplace was set to True. But if there was a mutable dict or list like object pared by YAML loader, which has cascade Data in it, the cascade part would be replaced. However, return value is just the parsed result.

Returns:

Final extracted Python object

Return type:

Any