pyfcstm.render.render
State Machine Code Renderer Module
This module provides functionality for rendering state machine models into code using templates. It uses Jinja2 templating engine to transform state machine models into various programming languages and formats based on template configurations.
- Template Directory Structure:
The template directory should contain:
config.yaml: Configuration file for the renderer*.j2files: Jinja2 templates that will be renderedOther files: Will be copied directly to the output directory
- Configuration File (config.yaml) Structure:
expr_styles: Dictionary defining expression rendering styles -default: Default style configuration (will use ‘dsl’ as base_lang if not specified)[style_name]: Additional named styles - base_lang: Base language style (‘dsl’, ‘c’, ‘cpp’, ‘python’) - [additional options]: Extra rendering options for the style
globals: Dictionary of global variables to be added to the Jinja2 environment Each entry can be:type: template: A template renderer function - params: List[str], means the parameter list of this template rendering function, e.g.['a', 'b']. - template: str, means the Jinja2-format text render template, e.g.{{ a + b * 2 }}.type: import: An imported object - from: str, means the import position, e.g. math.sin.type: value: A direct value - value: Any, means any possible value, e.g.1,'Hello World'.Other values (e.g.
1,'Hello World') means directly this value itself.
filters: Dictionary of filter functions to be added to the Jinja2 environment (Same format as globals)tests: Dictionary of test functions to be added to the Jinja2 environment (Same format as globals)ignores: List of file patterns to ignore (using gitignore syntax, e.g..git,*.md, etc)
- Expression Rendering:
The expr_styles configuration allows customizing how expressions are rendered in different language styles. The base_lang determines the starting template set, which can be extended or overridden with additional configuration.
And you can use these pre-defined styles in
expr_renderfunction/filter in the template. When use{{ expression | expr_render }}it will usedefaultstyle to render your expression. When use{{ expression | expr_render(style='c') }}it will usecstyle to render your expression.
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 the rendering of state machine models into code using Jinja2 templates. It supports custom expression styles, global functions, filters, and tests through a configuration file.
- Parameters:
template_dir (str) – Directory containing the templates and configuration
config_file (str, default: 'config.yaml') – Name of the configuration file within the template directory
- __init__(template_dir: str, config_file: str = 'config.yaml')[source]
Initialize the StateMachineCodeRenderer.
- Parameters:
template_dir (str) – Directory containing the templates and configuration
config_file (str, default: 'config.yaml') – Name of the configuration file within the template directory
- copy_one_file(model: StateMachine, output_file: str, src_file: str)[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’s an error copying the file
- render(model: StateMachine, output_dir: str, clear_previous_directory: bool = False)[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, default: False) – Whether to clear the output directory before rendering
- Raises:
IOError – If there’s 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)[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’s an error in the template
IOError – If there’s an error reading or writing files