Skip to content

Commit

Permalink
Allow setting support_templates_dir additionally to templates_dir (Op…
Browse files Browse the repository at this point in the history
…enCyphal#326)

Using an external template for support code is currently not possible,
as it was explicitly disabled to fix a bug:

OpenCyphal@c7db1bd

I was working around that bug in the past, by just calling nunavut
twice, for support and templates, e.g.:

```
nnvg --templates my_template/support --target-language c --generate-support never ...
nnvg --templates my_template/templates --target-language c --generate-support only ...
```

Since version 2.1.0, this is not possible anymore. This PR implements
this use-case properly again, by adding an argument
`--support-templates` to separate support from struct serialization. So
now it becomes:

```
nnvg --support-templates my_template/support  --templates my_template/templates --target-language c ...
```
  • Loading branch information
swuerl authored Jan 23, 2024
1 parent a1aa72d commit cac61d6
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def __init__(self, test_file: str, keep_temporaries: bool, node_name: str):
search_dir = search_dir.parent
self.root_dir = search_dir
self.templates_dir = self.test_dir / pathlib.Path("templates")
self.support_templates_dir = self.test_dir / pathlib.Path("support")
self.dsdl_dir = self.test_dir / pathlib.Path("dsdl")
self.lang_src_dir = self.root_dir / pathlib.Path("src") / pathlib.Path("nunavut") / pathlib.Path("lang")

Expand Down
14 changes: 14 additions & 0 deletions src/nunavut/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ def _make_parser() -> argparse.ArgumentParser:
).lstrip(),
)

parser.add_argument(
"--support-templates",
help=textwrap.dedent(
"""
Paths to a directory containing templates to use when generating support code.
Templates found under these paths will override the built-in support templates for a
given language.
"""
).lstrip(),
)

def extension_type(raw_arg: str) -> str:
if len(raw_arg) > 0 and not raw_arg.startswith("."):
return "." + raw_arg
Expand Down
3 changes: 3 additions & 0 deletions src/nunavut/cli/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ def __init__(self, args: argparse.Namespace, extra_includes: typing.Optional[typ
YesNoDefault.YES if self._args.generate_namespace_types else YesNoDefault.DEFAULT
),
"templates_dir": (pathlib.Path(self._args.templates) if self._args.templates is not None else None),
"support_templates_dir": (
pathlib.Path(self._args.support_templates) if self._args.support_templates is not None else None
),
"trim_blocks": self._args.trim_blocks,
"lstrip_blocks": self._args.lstrip_blocks,
"post_processors": self._build_post_processor_list_from_args(),
Expand Down
17 changes: 15 additions & 2 deletions src/nunavut/jinja/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ class CodeGenerator(nunavut._generators.AbstractGenerator):
where the names are the same. See :class:`jinja2.ChoiceLoader` for rules
on the lookup hierarchy.
:type templates_dir: typing.Optional[typing.Union[pathlib.Path,typing.List[pathlib.Path]]]
:param support_templates_dir: Directories containing jinja templates for the support files. These will be
available along with any built-in templates provided by the target language.
The templates at these paths will take precedence masking any built-in
templates where the names are the same. See :class:`jinja2.ChoiceLoader` for
rules on the lookup hierarchy.
:type support_templates_dir: typing.Optional[typing.Union[pathlib.Path,typing.List[pathlib.Path]]]
:param bool use_support_templates_dir: If True use the 'support_templates_dir' param for jinja templates, otherwise
use the 'templates_dir' param. Defaults to False.
:param bool followlinks: If True then symbolic links will be followed when
searching for templates.
:param bool trim_blocks: If this is set to True the first newline after a
Expand Down Expand Up @@ -148,6 +156,8 @@ def __init__(
namespace: nunavut.Namespace,
generate_namespace_types: YesNoDefault = YesNoDefault.DEFAULT,
templates_dir: typing.Optional[typing.Union[pathlib.Path, typing.List[pathlib.Path]]] = None,
support_templates_dir: typing.Optional[typing.Union[pathlib.Path, typing.List[pathlib.Path]]] = None,
use_support_templates_dir: bool = False,
followlinks: bool = False,
trim_blocks: bool = False,
lstrip_blocks: bool = False,
Expand All @@ -164,14 +174,17 @@ def __init__(
if templates_dir is not None and not isinstance(templates_dir, list):
templates_dir = [templates_dir]

if support_templates_dir is not None and not isinstance(support_templates_dir, list):
support_templates_dir = [support_templates_dir]

language_context = self._namespace.get_language_context()
target_language = language_context.get_target_language()

if package_name_for_templates is None:
package_name_for_templates = target_language.get_templates_package_name()

self._dsdl_template_loader = DSDLTemplateLoader(
templates_dirs=templates_dir,
templates_dirs=support_templates_dir if use_support_templates_dir else templates_dir,
package_name_for_templates=package_name_for_templates,
followlinks=followlinks,
builtin_template_path=builtin_template_path,
Expand Down Expand Up @@ -783,7 +796,7 @@ class SupportGenerator(CodeGenerator):
"""

def __init__(self, namespace: nunavut.Namespace, **kwargs: typing.Any):
kwargs.update(templates_dir=None)
kwargs.update(use_support_templates_dir=True)
super().__init__(namespace, builtin_template_path="support", **kwargs)

target_language = self.language_context.get_target_language()
Expand Down
1 change: 1 addition & 0 deletions test/gentest_nnvg/support/serialization.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
19 changes: 19 additions & 0 deletions test/gentest_nnvg/test_nnvg.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,3 +610,22 @@ def test_list_configuration(gen_paths: typing.Any, run_nnvg: typing.Callable) ->
)
assert len(parsed_config[default_target_section_name]) > 0
print(yaml.dump(parsed_config))

def test_support_templates_dir(gen_paths: typing.Any, run_nnvg: typing.Callable) -> None:
"""
Use the --support-templates option to find templates for support generation
"""
nnvg_args = [
"--templates",
gen_paths.templates_dir.as_posix(),
"--support-templates",
gen_paths.support_templates_dir.as_posix(),
"-O",
gen_paths.out_dir.as_posix(),
"-I",
(gen_paths.dsdl_dir / pathlib.Path("scotec")).as_posix(),
"--list-inputs",
(gen_paths.dsdl_dir / pathlib.Path("uavcan")).as_posix(),
]

run_nnvg(gen_paths, nnvg_args).stdout.decode("utf-8").split(";")

0 comments on commit cac61d6

Please sign in to comment.