_Tests = Mapping[str, _Test]
-def _create_template(name: str, cfg: Optional[barectf_config.Configuration] = None,
- filters: Optional[_Filters] = None,
- tests: Optional[_Test] = None) -> jinja2.Template:
- env = jinja2.Environment(trim_blocks=True, lstrip_blocks=True,
- loader=jinja2.PackageLoader('barectf', 'templates'))
- env.globals.update({
- 'cfg': cfg,
- 'barectf_config': barectf_config,
- 'barectf_version': barectf_version,
- })
+class _Template:
+ def __init__(self, name: str, is_file_template: bool = False,
+ cfg: Optional[barectf_config.Configuration] = None,
+ filters: Optional[_Filters] = None, tests: Optional[_Tests] = None):
+ env = jinja2.Environment(trim_blocks=True, lstrip_blocks=True,
+ loader=jinja2.PackageLoader('barectf', 'templates'))
+ env.globals.update({
+ 'cfg': cfg,
+ 'barectf_config': barectf_config,
+ 'barectf_version': barectf_version,
+ })
- env.filters['indent_tab'] = _filt_indent_tab
- env.filters['escape_dq'] = _filt_escape_dq
+ env.filters['indent_tab'] = _filt_indent_tab
+ env.filters['escape_dq'] = _filt_escape_dq
- if filters is not None:
- env.filters.update(filters)
+ if filters is not None:
+ env.filters.update(filters)
- if tests is not None:
- env.tests.update(tests)
+ if tests is not None:
+ env.tests.update(tests)
- return env.get_template(name)
+ self._templ = env.get_template(name)
+ self._is_file_template = is_file_template
+ def render(self, **kwargs) -> str:
+ text = self._templ.render(**kwargs)
-def _render_template(templ: jinja2.Template, is_file_template: bool = False, **kwargs) -> str:
- text = templ.render(**kwargs)
+ if self._is_file_template:
+ text = text.strip() + '\n'
- if is_file_template:
- text = text.strip() + '\n'
-
- return text
+ return text
def _filt_int_ft_str(ft: barectf_config._FieldType) -> str:
- return barectf_template._render_template(_INT_FT_TEMPL, ft=ft,
- is_signed=isinstance(ft, barectf_config.SignedIntegerFieldType))
+ return _INT_FT_TEMPL.render(ft=ft,
+ is_signed=isinstance(ft, barectf_config.SignedIntegerFieldType))
def _gen_enum_ft(ft: barectf_config._FieldType) -> str:
- return barectf_template._render_template(_ENUM_FT_TEMPL, ft=ft)
+ return _ENUM_FT_TEMPL.render(ft=ft)
def _gen_real_ft(ft: barectf_config._FieldType) -> str:
- return barectf_template._render_template(_REAL_FT_TEMPL, ft=ft)
+ return _REAL_FT_TEMPL.render(ft=ft)
def _gen_str_ft(ft: barectf_config._FieldType) -> str:
- return barectf_template._render_template(_STR_FT_TEMPL, ft=ft)
+ return _STR_FT_TEMPL.render(ft=ft)
def _ft_chain(ft: barectf_config._FieldType) -> List[barectf_config._FieldType]:
def _gen_struct_ft(ft: barectf_config._FieldType) -> str:
- return barectf_template._render_template(_STRUCT_FT_TEMPL, ft=ft, ft_chain=_ft_chain)
+ return _STRUCT_FT_TEMPL.render(ft=ft, ft_chain=_ft_chain)
_FT_CLS_TO_GEN_FT_FUNC = {
}
-def _create_template(name: str,
- cfg: Optional[barectf_config.Configuration] = None) -> jinja2.Template:
- return barectf_template._create_template(name, cfg,
- typing.cast(barectf_template._Filters, _TEMPL_FILTERS))
+def _create_template(name: str, is_file_template: bool = False,
+ cfg: Optional[barectf_config.Configuration] = None) -> barectf_template._Template:
+ return barectf_template._Template(name, is_file_template, cfg,
+ typing.cast(barectf_template._Filters, _TEMPL_FILTERS))
_ENUM_FT_TEMPL = _create_template('metadata-enum-ft.j2')
def _from_config(cfg: barectf_config.Configuration) -> str:
- return barectf_template._render_template(_create_template('metadata.j2', cfg),
- is_file_template=True)
+ return _create_template('metadata.j2', True, cfg).render()