pyfcstm.render.func

Utilities for converting configuration items into callable objects.

This module provides a single public function, process_item_to_object(), which transforms dictionary-based configuration items into callable render functions, imported objects, or extracted values. It is designed to integrate with Jinja2 templates and dynamic imports, enabling flexible runtime behavior from declarative configurations.

The module contains the following main component:

Note

This module mutates dictionary inputs by popping configuration keys such as type, template, params, from, and value. If the original dictionary should be preserved, pass a copy.

Example:

>>> import jinja2
>>> env = jinja2.Environment()
>>> template_config = {
...     'type': 'template',
...     'template': 'Hello {{ name }}',
...     'params': ['name'],
... }
>>> renderer = process_item_to_object(template_config, env)
>>> renderer('World')
'Hello World'

>>> import_config = {'type': 'import', 'from': 'math.sqrt'}
>>> sqrt_fn = process_item_to_object(import_config, env)
>>> sqrt_fn(16)
4.0

process_item_to_object

pyfcstm.render.func.process_item_to_object(f: Any, env: Environment) Any[source]

Process a configuration item into an object based on its type.

This function converts dictionary configurations into different types of objects:

  • 'template': Creates a callable Jinja2 template renderer function

  • 'import': Imports an object from a specified module

  • 'value': Extracts and returns a value from the configuration

  • Any other type or non-dictionary input is returned unchanged

When the configuration is a dictionary, the function mutates it by removing the keys it consumes (such as type, template, params, from, or value).

Parameters:
  • f (Any) – Configuration item to process; typically a dictionary with a type key, or any other object to return unchanged.

  • env (jinja2.Environment) – Jinja2 environment used for template rendering.

Returns:

The processed object (callable, imported object, extracted value, or the original input).

Return type:

Any

Raises:

ImportError – If type is 'import' and the target cannot be imported.

Example:

>>> import jinja2
>>> env = jinja2.Environment()
>>> template_config = {
...     'type': 'template',
...     'template': 'Hello {{ name }}',
...     'params': ['name'],
... }
>>> renderer = process_item_to_object(template_config, env)
>>> renderer('World')
'Hello World'

>>> import_config = {'type': 'import', 'from': 'math.sqrt'}
>>> sqrt_fn = process_item_to_object(import_config, env)
>>> sqrt_fn(16)
4.0