Nunavut Filters

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:

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):

 /*
    * 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!

1 Like

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!

1 Like

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:

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.

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?

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.

1 Like

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

nnvg -l c ../my_dsdl_root_namespace/sirius_cyber_corp/

I opened this issue:

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!

1 Like

Okay, I’ve found two more issues:

I suspect both are trivial. Thanks again.

Hi Scott! That seemed quite an adventure on your part! :rofl:
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.

1 Like

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.

2 Likes

Thanks Scott. Appreciate the assistance!