# Nunavut Filters

**URL:** <https://forum.opencyphal.org/t/nunavut-filters/900>\
**Category:** Nunavut\
**Created:** [August 2, 2020, 12:19pm UTC](https://forum.opencyphal.org/t/nunavut-filters/900 "2020-08-02T12:19:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nacansino](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/nacansino/32/535_2.png) [@nacansino](https://forum.opencyphal.org/u/nacansino)\
**Post date:** [August 2, 2020, 12:19pm UTC](https://forum.opencyphal.org/t/nunavut-filters/900/1 "2020-08-02T12:19:00Z")

</div>

Hi!

I’m new to UAVCAN and Nunavut, and for the past few hours I’ve been trying to generate C-headers.

I installed Nunavut using pip, and was using the ff. command in cli to generate my headers:

```bash
nnvg -e c ../my_dsdl_root_namespace/sirius_cyber_corp/ --template c 
_jinja/

```

I’m using the following sample code from the Github README.md as my Jinja template (because other templates are not available):

```auto
 /*
    * UAVCAN data structure definition
    *
    * Auto-generated, do not edit.
    *
    * Source file: {{T.source_file_path}}
    */

    #ifndef {{T.full_name | c.macrofy}}
    #define {{T.full_name | c.macrofy}}

    {%- for constant in T.constants %}
    #define {{ T | c.macrofy }}_{{ constant.name | c.macrofy }} {{ constant | constant_value }}
    {%- endfor %}

    typedef struct
    {
        /*
            Note that we're not handling union types properly in this simplified example.
            Unions take a bit more logic to generate correctly.
        */
        {%- for field in T.fields %}
        {%- if field is not padding %}
            {{ field.data_type | declaration }} {{ field | id }}
            {%- if field.data_type is ArrayType -%}
                [{{ field.data_type.capacity }}]
            {%- endif -%};
        {%- if field is VariableLengthArrayType %}
            {{ typename_unsigned_length }} {{ field | id }}_length;
        {%- endif -%}
        {%- endif -%}
        {%- endfor %}
...

    } {{ composite_type | full_reference_name }};

    #endif // {{T.full_name | c.macrofy}}

```

I’m getting a `no filter named 'declaration'` error, as well as for ‘constant\_value’.

I know that my understanding of how the library works is still limited, but I would really appreciate it if you guys can share some working sample Python codes with c jinja templates against the latest Nunavut release? It can serve as a baseline for learning the library, and I think it would really help beginners (like me) to understand what the library can do.

Thanks and keep up the good work!

---

<div class="post-metadata">

**Author:** ![scottdixon](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/scottdixon/32/39_2.png) [@scottdixon](https://forum.opencyphal.org/u/scottdixon)\
**Post date:** [August 2, 2020, 4:01pm UTC](https://forum.opencyphal.org/t/nunavut-filters/900/2 "2020-08-02T16:01:05Z")

</div>

Sorry. I think my sample is broken. I’ll try to fix it. You can now generate C without supplying a template using the built-in support. Simply omit the --template parameter and it should work.

Opening a bug against the documentation would be a huge help. Thanks!

---

<div class="post-metadata">

**Author:** ![nacansino](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/nacansino/32/535_2.png) [@nacansino](https://forum.opencyphal.org/u/nacansino)\
**Post date:** [August 3, 2020, 2:37pm UTC](https://forum.opencyphal.org/t/nunavut-filters/900/3 "2020-08-03T14:37:51Z")

</div>

Hi Scott! Thank you for responding.  
I tried your suggestion just now.

Using a very simple command `nnvg -e .h ../my_dsdl_root_namespace/sirius_cyber_corp/` yields me the following error:

```bash
Traceback (most recent call last):
  File "c:\users\nielc\appdata\local\programs\python\python38-32\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\users\nielc\appdata\local\programs\python\python38-32\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\nielc\AppData\Local\Programs\Python\Python38-32\Scripts\nnvg.exe\ __main__.py", line 4, in <module>
  File "c:\users\nielc\appdata\local\programs\python\python38-32\lib\site-packages\nunavut\ __main__.py", line 14, in <module>
    sys.exit(main())
  File "c:\users\nielc\appdata\local\programs\python\python38-32\lib\site-packages\nunavut\cli.py", line 415, in main
    return _run(args, extra_includes)
  File "c:\users\nielc\appdata\local\programs\python\python38-32\lib\site-packages\nunavut\cli.py", line 132, in _run
    generator.generate_all(is_dryrun=args.dry_run,
  File "c:\users\nielc\appdata\local\programs\python\python38-32\lib\site-packages\nunavut\jinja\ __init__.py", line 556, in generate_all
    self._generate_type(parsed_type, output_path, is_dryrun, allow_overwrite, self._post_processors)
  File "c:\users\nielc\appdata\local\programs\python\python38-32\lib\site-packages\nunavut\jinja\ __init__.py", line 650, in _generate_type
    template = self._env.get_template(template_name)
  File "c:\users\nielc\appdata\local\programs\python\python38-32\lib\site-packages\nunavut\jinja\jinja2\environment.py", line 832, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "c:\users\nielc\appdata\local\programs\python\python38-32\lib\site-packages\nunavut\jinja\jinja2\environment.py", line 806, in _load_template
    template = self.loader.load(self, name, globals)
  File "c:\users\nielc\appdata\local\programs\python\python38-32\lib\site-packages\nunavut\jinja\jinja2\loaders.py", line 113, in load 
    source, filename, uptodate = self.get_source(environment, name)
  File "c:\users\nielc\appdata\local\programs\python\python38-32\lib\site-packages\nunavut\jinja\jinja2\loaders.py", line 187, in get_source
    raise TemplateNotFound(template)
nunavut.jinja.jinja2.exceptions.TemplateNotFound: ServiceType.j2

```

Also, I don’t think this is related but I noticed I was using an old (0.2.3) version of Nunavut before I reinstalled the 0.3.0. The 0.2.3 seems to come with the pyuavcan development version that I installed after Nunavut. It says they are not compatible.

---

<div class="post-metadata">

**Author:** ![scottdixon](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/scottdixon/32/39_2.png) [@scottdixon](https://forum.opencyphal.org/u/scottdixon)\
**Post date:** [August 3, 2020, 4:52pm UTC](https://forum.opencyphal.org/t/nunavut-filters/900/4 "2020-08-03T16:52:12Z")

</div>

hmm. I’m in the office now so I’ll take a look at this. A couple of questions in case I can’t reproduce:

1. Are you running nnvg from a version you installed via pip or are you running from source?
2. From the namespace it looks like you are using a DSDL example from the alpha specification, section 3.8.3.4?

---

<div class="post-metadata">

**Author:** ![scottdixon](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/scottdixon/32/39_2.png) [@scottdixon](https://forum.opencyphal.org/u/scottdixon)\
**Post date:** [August 3, 2020, 5:04pm UTC](https://forum.opencyphal.org/t/nunavut-filters/900/5 "2020-08-03T17:04:02Z")

</div>

> [@nacansino](#):
>
> `nnvg -e .h ../my_dsdl_root_namespace/sirius_cyber_corp/` y

Confirmed. The current release is busted somehow even though the tests pass. I’m really sorry about this. I’ll work on fixing this today. Expect a fix by tomorrow morning GMT-7.

---

<div class="post-metadata">

**Author:** ![scottdixon](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/scottdixon/32/39_2.png) [@scottdixon](https://forum.opencyphal.org/u/scottdixon)\
**Post date:** [August 3, 2020, 5:15pm UTC](https://forum.opencyphal.org/t/nunavut-filters/900/6 "2020-08-03T17:15:36Z")

</div>

> [@nacansino](#):
>
> ```auto
> nnvg -e .h ../my_dsdl_root_namespace/sirius_cyber_corp/
> 
> ```

Found the problem. It’s fortunately trivial with an available workaround. Try this instead:

```auto
nnvg -l c ../my_dsdl_root_namespace/sirius_cyber_corp/

```

I opened this issue:

[https://github.com/UAVCAN/nunavut/issues/116](https://github.com/UAVCAN/nunavut/issues/116)

The defect is in the auto-detection of the target language by extension (`-e` switch). Its looking for C++ built-in templates which are incomplete at this time. I’ll modify this so `.h` maps to C and `.hpp` maps to C++ by default. `-l, --target-language` will always override this automatic behaviour so you will be able to generate C++ headers with a `.h` extension by combining the `-e` and `-l` switches.

Sorry for the trouble and thank you for reporting this issue!

---

<div class="post-metadata">

**Author:** ![scottdixon](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/scottdixon/32/39_2.png) [@scottdixon](https://forum.opencyphal.org/u/scottdixon)\
**Post date:** [August 3, 2020, 5:29pm UTC](https://forum.opencyphal.org/t/nunavut-filters/900/7 "2020-08-03T17:29:54Z")

</div>

Okay, I’ve found two more issues:

- [https://github.com/UAVCAN/nunavut/issues/118](https://github.com/UAVCAN/nunavut/issues/118)
- [https://github.com/UAVCAN/nunavut/issues/117](https://github.com/UAVCAN/nunavut/issues/117)

I suspect both are trivial. Thanks again.

---

<div class="post-metadata">

**Author:** ![nacansino](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/nacansino/32/535_2.png) [@nacansino](https://forum.opencyphal.org/u/nacansino)\
**Post date:** [August 4, 2020, 2:19pm UTC](https://forum.opencyphal.org/t/nunavut-filters/900/8 "2020-08-04T14:19:22Z")

</div>

Hi Scott! That seemed quite an adventure on your part! 🤣  
Anyway, I was able to successfully generate the code without the template and -e parameter.  
I really appreciate the team’s very quick response to the queries in this forum. It gives me confidence to start adapting UAVCAN on our projects.

---

<div class="post-metadata">

**Author:** ![scottdixon](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/scottdixon/32/39_2.png) [@scottdixon](https://forum.opencyphal.org/u/scottdixon)\
**Post date:** [August 5, 2020, 7:02pm UTC](https://forum.opencyphal.org/t/nunavut-filters/900/9 "2020-08-05T19:02:24Z")

</div>

Nunavut 0.3.1 is up on Pypi which fixes issue 118 and 116. This means your original attempt to use nnvg would now work. Sorry for the trouble.

---

<div class="post-metadata">

**Author:** ![nacansino](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/nacansino/32/535_2.png) [@nacansino](https://forum.opencyphal.org/u/nacansino)\
**Post date:** [August 7, 2020, 10:48am UTC](https://forum.opencyphal.org/t/nunavut-filters/900/10 "2020-08-07T10:48:33Z")

</div>

Thanks Scott. Appreciate the assistance!
