Use Jinja 2 templates to generate the `metadata` file
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 20 Aug 2020 19:52:48 +0000 (15:52 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 20 Aug 2020 19:52:48 +0000 (15:52 -0400)
commitde49021eecdfe156dc8b575447bc512982d8bbc7
tree5b8d7965b08b2058b15d94dda56cdd2e258d20f2
parenta9a8908e33db37cd257c7ff310903ffc9cc93758
Use Jinja 2 templates to generate the `metadata` file

The goal of this patch is to, as much as possible, and as long as
everything remains as readable as possible, move any `metadata` file
templating outside of pure Python code to improve template readability,
maintenability, and reuse.

This patch does the following:

1. Adds a Jinja 2 dependency to `pyproject.toml`.

2. Adds the `template` module which contains helpers to create and
   render a Jinja 2 template.

   The _create_template() function uses a `jinja2.PackageLoader` object
   to load a named package from the `barectf` packet's `templates`
   directory.

   It creates an environment with common parameters and common
   filters:

   `indent_tab`:
       Like Jinja 2's built-in `indent`, but uses tabs instead of
       spaces.

   `escape_sq`:
       Escapes a double-quoted string (backslashes and double quotes).

   Jinja 2 templates can expect to have access to the `barectf_config`
   and `barectf_version` modules, as well as, possibly, the current
   barectf configuration as the `cfg` variable.

   The _render_template() function renders a given template with a given
   context, keeping a single newline at the end if `is_file_template` is
   `True`.

3. Adds Jinja 2 templates to generate the `metadata` file:

   `metadata.j2`:
       Top-level template for the whole file.

   `metadata-enum-ft.j2`:
       Enumeration field type block template.

   `metadata-int-ft.j2`:
       Integer field type block template.

   `metadata-real-ft.j2`:
       Real field type block template.

   `metadata-str-ft.j2`:
       String field type block template.

   `metadata-struct-ft.j2`:
       Structure field type block template.

4. Changes `barectf/tsdl182gen.py` so as to use 2. with 3.

   `barectf/tsdl182gen.py` defines custom Jinja 2 filters to help the
   templates of 3.:

   `bo_str`:
       Converts a `barectf.ByteOrder` value to the `le` or `be` string.

   `disp_base_int`:
       Converts a `barectf.DisplayBase` value to an equivalent integer.

   `int_ft_str`:
       Converts an integer field type object to its TSDL string
       equivalent.

       The first line is not indented.

   `ft_str`:
       Converts any field type object to its TSDL string equivalent.

       The first line is not indented.

5. Adds the `barectf.TraceType.clock_types` property to easily access
   the set of required clock types (by all stream types).

   This is required by 3.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
barectf/config.py
barectf/template.py [new file with mode: 0644]
barectf/templates/metadata-enum-ft.j2 [new file with mode: 0644]
barectf/templates/metadata-int-ft.j2 [new file with mode: 0644]
barectf/templates/metadata-real-ft.j2 [new file with mode: 0644]
barectf/templates/metadata-str-ft.j2 [new file with mode: 0644]
barectf/templates/metadata-struct-ft.j2 [new file with mode: 0644]
barectf/templates/metadata.j2 [new file with mode: 0644]
barectf/tsdl182gen.py
poetry.lock
pyproject.toml
This page took 0.023872 seconds and 4 git commands to generate.