pyfcstm.render.render
State machine code renderer for Jinja2 template-based code generation.
This module provides the StateMachineCodeRenderer class, which renders
state machine models into code files using a template directory. The renderer
supports configuration-driven expression styles, Jinja2 globals, filters, and
tests. Template files with the .j2 extension are rendered, while all other
files are copied directly to the output directory.
The template directory is expected to contain:
config.yaml- Configuration file for the renderer (see below)*.j2files - Jinja2 templates to renderOther files - Copied verbatim to the output directory
Configuration file (config.yaml) structure:
expr_styles- Mapping of expression rendering styles. Each style is a mapping with abase_langkey and optional template overrides. Thedefaultstyle is always created if absent.stmt_styles- Mapping of statement rendering styles. Each style is a mapping with abase_langkey and optional overrides such asdeclare_tempandtemp_type_aliasesfor static-language backends.globals- Mapping of Jinja2 globals (seepyfcstm.render.func.process_item_to_object())filters- Mapping of Jinja2 filters (same structure asglobals)tests- Mapping of Jinja2 tests (same structure asglobals)ignores- Git-style ignore patterns to skip files in the template directory
Expression rendering:
Use
{{ expression | expr_render }}to render with thedefaultstyleUse
{{ expression | expr_render(style='c') }}to render with a named style
The module depends on jinja2, pyyaml, and pathspec for
templating, YAML configuration, and ignore pattern handling.
Example:
>>> from pyfcstm.render.render import StateMachineCodeRenderer
>>> from pyfcstm.model import StateMachine
>>> renderer = StateMachineCodeRenderer('./templates')
>>> renderer.render(StateMachine(defines={}, root_state=some_state), './output')
StateMachineCodeRenderer
- class pyfcstm.render.render.StateMachineCodeRenderer(template_dir: str, config_file: str = 'config.yaml')[source]
Renderer for generating code from state machine models using templates.
This class handles rendering of state machine models into code by combining a template directory with a configuration file. It creates a Jinja2 environment, registers expression rendering styles, and maps template files to rendering operations or file copying operations.
- Parameters:
template_dir (str) – Directory containing the templates and configuration
config_file (str, optional) – Name of the configuration file within the template directory, defaults to
'config.yaml'
- Variables:
template_dir (str) – Absolute path to the template directory
config_file (str) – Absolute path to the configuration file
env (jinja2.Environment) – Jinja2 environment used for rendering
_ignore_patterns (List[str]) – List of git-style ignore patterns
_file_mappings (Dict[str, Callable]) – Mapping of relative template paths to render/copy callables
Example:
>>> renderer = StateMachineCodeRenderer('./templates') >>> renderer.render(my_state_machine, './output', clear_previous_directory=True)
- __init__(template_dir: str, config_file: str = 'config.yaml') None[source]
Initialize the StateMachineCodeRenderer.
- Parameters:
template_dir (str) – Directory containing the templates and configuration
config_file (str, optional) – Name of the configuration file within the template directory, defaults to
'config.yaml'
- copy_one_file(model: StateMachine, output_file: str, src_file: str) None[source]
Copy a single file to the output directory.
- Parameters:
model (StateMachine) – The state machine model (unused in this method)
output_file (str) – Path to the output file
src_file (str) – Path to the source file
- Raises:
IOError – If there is an error copying the file
- render(model: StateMachine, output_dir: str, clear_previous_directory: bool = False) None[source]
Render the state machine model to the output directory.
This method processes all template files and copies all other files from the template directory to the output directory according to the configured mappings.
- Parameters:
model (StateMachine) – The state machine model to render
output_dir (str) – Directory where the rendered files will be placed
clear_previous_directory (bool, optional) – Whether to clear the output directory before rendering, defaults to
False
- Raises:
IOError – If there is an error accessing or writing to the output directory
Example:
>>> renderer = StateMachineCodeRenderer('./templates') >>> renderer.render(my_state_machine, './output', clear_previous_directory=True)
- render_one_file(model: StateMachine, output_file: str, template_file: str) None[source]
Render a single template file.
- Parameters:
model (StateMachine) – The state machine model to render
output_file (str) – Path to the output file
template_file (str) – Path to the template file
- Raises:
jinja2.exceptions.TemplateError – If there is an error in the template
IOError – If there is an error reading or writing files