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:
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 (str | None) –

  • line_comment_prefix (str | None) –

  • trim_blocks (bool) –

  • lstrip_blocks (bool) –

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

  • keep_trailing_newline (bool) –

  • extensions (Sequence[str | Type[Extension]]) –

  • optimized (bool) –

  • undefined (Type[Undefined]) –

  • finalize (Callable[[...], Any] | None) –

  • autoescape (bool | Callable[[str | None], bool]) –

  • loader (BaseLoader | None) –

  • cache_size (int) –

  • auto_reload (bool) –

  • bytecode_cache (BytecodeCache | None) –

  • 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 (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, search_paths=())[source]#

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

Parameters:
Return type:

RelEnvironment

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

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

Parameters:
  • template_path (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 (str | PathLike) – Base directory where templates are found

  • output_path (str | PathLike | None) – If provided, writes to file

  • search_paths (Iterable[str | PathLike]) – Additional directories jinja should search when resolving imports

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 (T.Optional[T.Openable]) –

class TemplateListEntry(template_path: 'T.Openable', data: 'T.Dict[str, T.Any]', config: 'RenderTemplateConfig', template_dir: 'T.Openable', output_path: 'T.Optional[T.Openable]')[source]#

Bases: object

Parameters:
template_path: str | PathLike#
data: Dict[str, Any]#
config: RenderTemplateConfig#
template_dir: str | PathLike#
output_path: str | PathLike | None#
add(template_path, data, config, *, template_dir=None, output_path=None)[source]#
Parameters:
Return type:

None

render(search_paths=())[source]#
Parameters:

search_paths (Iterable[str | PathLike]) –

Return type:

List[str]