Skip to main content

A CLI tool for generating files from Jinja2 templates and environment variables.

Project description

pipeline status Latest version on PyPI Project license PyPI Stats

Supported Python implementations Supported Python versions

A CLI tool for generating files from Jinja2 templates and environment variables.

Installation

pip install template[all]

This will install Template along with the dependencies for all of the filters available with it. However, you can use narrower specifiers if you want to avoid a specific dependency (the filters listed below include the needed specifier). In previous releases of Template all of the dependencies were required. To not break existing templates, for the foreseeable future those dependencies will remain a requirement and new dependencies for specific filters will be optional. This will change in the 0.9 release where all filter specific dependencies will be optional. Please update your project before that happens.

Examples

$ template -h
usage: template [-h] [-o OUTPUT] [filename]

A CLI tool for generating files from Jinja2 templates and environment
variables.

positional arguments:
  filename              Input filename

  optional arguments:
    -h, --help            show this help message and exit
    -o OUTPUT, --output OUTPUT
                          Output to filename
$ export name='John'
$ echo 'Hello {{ name if name is defined else 'world' }}. | template
Hello John.
$ echo '{{ USER }}' > username.j2
$ template --output username.txt username.j2
$ cat username.txt
John

Jinja filters

The following Jinja filters were added:

  • combine: Merges 2 dictionaries with the 2nd overriding the 1st. Returns the result.

  • to_yaml: Convert to yaml (requires the yaml package specifier).

  • from_yaml: Convert from yaml (requires the yaml package specifier).

  • to_json: Convert to json.

  • from_json: Convert from json.

  • to_toml: Convert to toml (requires the toml package specifier).

  • from_toml: Convert from toml (requires the toml package specifier).

  • jmespath: Queries data using the JMESPath query language (requires the jmespath package specifier).

  • run: Runs a command and returns the stdout, stderr and returncode using run. This filter is replaced with the run function and will be removed in the 0.10 release.

  • ipaddress: Returns an IPAddress object from the netaddr library (requires the netaddr package specifier).

  • ipnetwork: Returns an IPNetwork object from the netaddr library (requires the netaddr package specifier).

  • iprange: Returns an IPRange object from the netaddr library. (requires the netaddr package specifier).

  • ipglob: Returns an IPGlob object from the netaddr library (requires the netaddr package specifier).

  • ipset: Returns an IPSet object from the netaddr library (requires the netaddr package specifier).

Example usage can be seen in tests and for specific filters in the docstrings in template/filters.py.

Jinja functions

  • run: Runs a command and returns the stdout, stderr and returncode using run. This function replaces the run filter.

Example usage can be seen in tests and for specific filters in the docstrings in template/functions.py.

Testing

Tests require Pipenv and Bats. Run the tests with the following commands:

pipenv run lint  # Pre-commit hooks.
pipenv run doctest  # Doc tests.
pipenv run bats  # Bats tests.
pipenv run check  # Twine check.

Also, GitLab CI is setup for this project so every push to this repository is checked with all Python 2.7, Python 3.6 and later and all supported versions of PyPy.

Release

Release requires Pipenv. To bump the version run pipenv run bumpversion major|minor|patch to update the version and git commit and tag the changes, then run git push --follow-tags to push the git commit and tag. The GitLab CI will then build and upload a release to PyPI. To manually upload to PyPI run pipenv run build to build the Python package and pipenv run upload -s dist/* to upload a signed version.

License

This software is licensed under the AGPL 3+ license (see the LICENSE.txt file).

Author

Nimrod Adar, contact me or visit my website. Patches are welcome via git send-email. The repository is located at: https://git.shore.co.il/nimrod/.

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

template-0.7.5.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

template-0.7.5-py2.py3-none-any.whl (19.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file template-0.7.5.tar.gz.

File metadata

  • Download URL: template-0.7.5.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.1 requests/2.26.0 setuptools/39.2.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.6.8

File hashes

Hashes for template-0.7.5.tar.gz
Algorithm Hash digest
SHA256 482a0aafd2845cbed08912070ac8ff07506109d61aad8d5b64d26d7701dc42e3
MD5 7fc60a9f8f888516bb1821d5372abc1c
BLAKE2b-256 7135102d3ff90f6781180a3ce3499e064ca1ecbd01a5ee29eaed2ca08c78b8f0

See more details on using hashes here.

File details

Details for the file template-0.7.5-py2.py3-none-any.whl.

File metadata

  • Download URL: template-0.7.5-py2.py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.1 requests/2.26.0 setuptools/39.2.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.6.8

File hashes

Hashes for template-0.7.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1abef34b79fd3af6974f69aed6ef8372cc7270be4a0f8da4d182b91d15904847
MD5 99db2988e6840db4448402459221f597
BLAKE2b-256 eb653979fd18869d31536e770d6796ec3202611860c2bbc474ae66acd3be53c8

See more details on using hashes here.

Supported by

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