template.py: define a `_Template` class instead of two functions
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 27 Aug 2020 15:11:16 +0000 (11:11 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 27 Aug 2020 15:33:20 +0000 (11:33 -0400)
This patch changes `template.py` so as to offer the `_Template` class
instead of the _create_template() and _render_template() functions.

This makes it possible to move the `is_file_template` parameter from
_render_template() to the template's constructor, as this really is a
template-specific option, not a rendering-specific option.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
barectf/template.py
barectf/tsdl182gen.py

index 7329996e3338440225f6d6d065ade112dd974a98..27ade4f244a281f95df71540b56c2c1648bc5822 100644 (file)
@@ -55,33 +55,34 @@ _Test = Callable[[Any], bool]
 _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
index aa53e769ccccca843c94d82968a6bd63280199f6..911160ea93378663bb61e4bf891e484c9d39827e 100644 (file)
@@ -45,20 +45,20 @@ def _filt_disp_base_int(disp_base: barectf_config.DisplayBase) -> int:
 
 
 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]:
@@ -73,7 +73,7 @@ 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 = {
@@ -99,10 +99,10 @@ _TEMPL_FILTERS = {
 }
 
 
-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')
@@ -113,5 +113,4 @@ _STRUCT_FT_TEMPL = _create_template('metadata-struct-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()
This page took 0.025227 seconds and 4 git commands to generate.