Skip to main content

Generate files from C/C++ headers using jinja2 templates

Project description

header2whatever (h2w)

Generate arbitrary files from C/C++ header files using CppHeaderParser to read the input files and Jinja2 templates to generate the outputs.

This grew out of a desire to generate pybind11 wrapping code from C++ header files. pybind11gen was created, but then I wanted to generate more things…

There are still rough edges, and the documentation is mostly nonexistent, but pull requests with fixes/improvements are very welcome!

As of 0.3.0, h2w requires Python 3.3+

Install

pip install header2whatever

Usage

First, you need to create a jinja2 template that represents whatever you want to generate from the header file. For example, maybe you want to describe the functions in yaml:

---
{% for header in headers %}
{% for fn in header.functions %}
{{ fn.name }}:
  returns: {{ fn.returns }}
  params:
  {% for param in fn.parameters %}
  - { name: {{ param.name }}, type: "{{ param.type }}" }
  {% endfor %}

{% endfor %}
{% endfor %}

And let’s say you have the following header file foo.h:

void some_fn(int i);
int returns_int(int p1, char* p2);

You can execute the following:

h2w foo.h -o foo.yml

And you’ll get the following output:

---
returns_int:
  returns: int
  params:
  - { name: p1, type: "int" }
  - { name: p2, type: "char *" }

some_fn:
  returns: void
  params:
  - { name: i, type: "int" }

As you can see, while this is a silly example, this approach is very flexible and fairly powerful.

Currently, the data structure passed to the template isn’t documented – but it’s a filtered version of whatever CppHeaderParser outputs when it parses a header.

See the examples folder for more examples.

Batch mode

If you need to process multiple files, or just want to record the parameters for autogenerating a file without writing a shell script, batch mode is useful. You pass two parameters: a yaml file with the configuration, and an output directory to write the files to.

Using data from external sources

Sometimes you want to mix in data that CppHeaderParser can’t give you. If you pass the --yaml option, it will load the yaml into a dictionary and make it available to the template as the ‘data’ variable.

You can also pass key=value parameters via the --param option, and the specified keys will be available to the template.

Custom processing

When you need to do more complex logic that a jinja2 template just isn’t appropriate for, you can specify a python file to load custom hooks from.

See [the default hooks](header2whatever/default_hooks.py) for documentation.

License

Apache 2

Author

Dustin Spicuzza (dustin@virtualroadside.com)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

header2whatever-0.4.4.tar.gz (78.3 kB view details)

Uploaded Source

Built Distribution

header2whatever-0.4.4-py3-none-any.whl (81.0 kB view details)

Uploaded Python 3

File details

Details for the file header2whatever-0.4.4.tar.gz.

File metadata

  • Download URL: header2whatever-0.4.4.tar.gz
  • Upload date:
  • Size: 78.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for header2whatever-0.4.4.tar.gz
Algorithm Hash digest
SHA256 afe49188f99b628c7fa74159f1d80298979f2a6effea32cc3fbea081966ba30d
MD5 e69bf0141ac6fce4422039f0b0384450
BLAKE2b-256 3536de8d3f0c386f59d229f6683d7ca3ab7051d8c060c0aaec467d5b689a290d

See more details on using hashes here.

File details

Details for the file header2whatever-0.4.4-py3-none-any.whl.

File metadata

  • Download URL: header2whatever-0.4.4-py3-none-any.whl
  • Upload date:
  • Size: 81.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for header2whatever-0.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 664e5926189faa3fda3e455e515e31f5e599dd55247e8d0f013589bb9ea872b9
MD5 c6a15ff74ce8d15b32db1d80afb00bd3
BLAKE2b-256 33d4cce84783d2d35b888c31e0202caf99ba73d1b561275ea1cd889e61d0af29

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page