symforce.codegen.template_util module

class FileType(value)[source]

Bases: Enum

An enumeration.

CPP = 1
PYTHON = 2
PYTHON_INTERFACE = 3
CUDA = 4
LCM = 5
MAKEFILE = 6
TYPESCRIPT = 7
static from_extension(extension)[source]
Parameters:

extension (str) –

Return type:

FileType

static from_template_path(template_path)[source]
Parameters:

template_path (Path) –

Return type:

FileType

comment_prefix()[source]

Return the comment prefix for this file type.

Return type:

str

autoformat(file_contents, template_name, output_path=None)[source]

Format code of this file type.

Parameters:
  • file_contents (str) –

  • template_name (Union[str, PathLike]) –

  • output_path (Union[str, PathLike, None]) –

Return type:

str

class RelEnvironment(block_start_string='{%', block_end_string='%}', variable_start_string='{{', variable_end_string='}}', comment_start_string='{#', comment_end_string='#}', line_statement_prefix=None, line_comment_prefix=None, trim_blocks=False, lstrip_blocks=False, newline_sequence='\\n', keep_trailing_newline=False, extensions=(), optimized=True, undefined=<class 'jinja2.runtime.Undefined'>, finalize=None, autoescape=False, loader=None, cache_size=400, auto_reload=True, bytecode_cache=None, enable_async=False)[source]

Bases: Environment

Override join_path() to enable relative template paths. Modified from the below post.

https://stackoverflow.com/questions/8512677/how-to-include-a-template-with-relative-path-in-jinja2

Parameters:
  • block_start_string (str) –

  • block_end_string (str) –

  • variable_start_string (str) –

  • variable_end_string (str) –

  • comment_start_string (str) –

  • comment_end_string (str) –

  • line_statement_prefix (Optional[str]) –

  • line_comment_prefix (Optional[str]) –

  • trim_blocks (bool) –

  • lstrip_blocks (bool) –

  • newline_sequence (te.Literal['n', 'rn', 'r']) –

  • keep_trailing_newline (bool) –

  • extensions (Sequence[Union[str, Type[ForwardRef]]]) –

  • optimized (bool) –

  • undefined (Type[Undefined]) –

  • finalize (Optional[Callable[…, Any]]) –

  • autoescape (Union[bool, Callable[[Optional[str]], bool]]) –

  • loader (Optional[ForwardRef]) –

  • cache_size (int) –

  • auto_reload (bool) –

  • bytecode_cache (Optional[ForwardRef]) –

  • enable_async (bool) –

join_path(template, parent)[source]

Join a template with the parent. By default all the lookups are relative to the loader root so this method returns the template parameter unchanged, but if the paths should be relative to the parent template, this function can be used to calculate the real template name.

Subclasses may override this method and implement template path joining here.

Parameters:
  • template (Union[Template, str]) –

  • parent (str) –

Return type:

str

add_preamble(source, name, comment_prefix, custom_preamble)[source]
Parameters:
  • source (str) –

  • name (Path) –

  • comment_prefix (str) –

  • custom_preamble (str) –

Return type:

str

jinja_env(template_dir)[source]

Helper function to cache the Jinja environment, which enables caching of loaded templates

Parameters:

template_dir (Union[str, PathLike]) –

Return type:

RelEnvironment

render_template(template_path, data, config, *, template_dir, output_path=None)[source]

Boiler plate to render template. Returns the rendered string and optionally writes to file.

Parameters:
  • template_path (Union[str, PathLike]) – file path of the template to render

  • data (Dict[str, Any]) – dictionary of inputs for template

  • config (RenderTemplateConfig) – configuration options for template rendering (see RenderTemplateConfig for more information)

  • template_dir (Union[str, PathLike]) – Base directory where templates are found

  • output_path (Union[str, PathLike, None]) – If provided, writes to file

Return type:

str

class TemplateList(template_dir=None)[source]

Bases: object

Helper class to keep a list of (template_path, data, config, template_dir, output_path) and render all templates in one go.

Parameters:

template_dir (Union[str, PathLike, None]) –

class TemplateListEntry(template_path, data, config, template_dir, output_path)[source]

Bases: object

Parameters:
  • template_path (Union[str, PathLike]) –

  • data (Dict[str, Any]) –

  • config (RenderTemplateConfig) –

  • template_dir (Union[str, PathLike]) –

  • output_path (Union[str, PathLike, None]) –

template_path: Union[str, PathLike]
data: Dict[str, Any]
config: RenderTemplateConfig
template_dir: Union[str, PathLike]
output_path: Optional[Union[str, PathLike]]
__init__(template_path, data, config, template_dir, output_path)
Parameters:
  • template_path (Union[str, PathLike]) –

  • data (Dict[str, Any]) –

  • config (RenderTemplateConfig) –

  • template_dir (Union[str, PathLike]) –

  • output_path (Union[str, PathLike, None]) –

__init__(template_dir=None)[source]
Parameters:

template_dir (Union[str, PathLike, None]) –

add(template_path, data, config, *, template_dir=None, output_path=None)[source]
Parameters:
  • template_path (Union[str, PathLike]) –

  • data (Dict[str, Any]) –

  • config (RenderTemplateConfig) –

  • template_dir (Union[str, PathLike, None]) –

  • output_path (Union[str, PathLike, None]) –

Return type:

None

render()[source]
Return type:

List[str]