Command-line tool for rendering Jinja2 templates
Project description
template-specialize
template-specialize is a command-line tool for rendering Jinja2 templates.
For example, consider this Markdown name tag template, "nametag.md":
## Hello, my name is
# {{name}}
{% if title is defined %}
### {{title}}
{% endif %}
To render the template, execute template-specialize as follows:
$ template-specialize nametag.md nametag-roy.md -k name 'Roy Hobbs' -k title 'The best there ever was'
Afterward, the output file contains the rendered template:
## Hello, my name is
# Roy Hobbs
### The best there ever was
You can also render directories of templates to an output directory:
$ template-specialize template/ output/ -k name value
Environment Files
template-specialize was originally created to "specialize" web service configuration files for different runtime environments. Environment files are YAML files that allow for the definition of inheritable, structured template configuration values. Consider the following environments file:
base:
values:
service_name: my-service
test_base:
parents: [base]
values:
db_host: test-db-host
test:
parents: [test_base]
values:
db_name: test-db
live:
parents: [base]
values:
db_host: live-db-host
db_name: live-db
To render a template file using an environment, specify the environment file (or files) and the environment name with which to render the template:
$ template-specialize config-template.yaml config.yaml -c environments.yaml -e test
To view the template configuration data use the "--dump" argument:
$ template-specialize config-template.yaml config.yaml -c environments.yaml -e test --dump
db_host: test-db-host
db_name: test-db
service_name: my-service
Renaming and Deleting Output Files
When specializing a template directory, it is sometimes necessary to rename an output file or directory. For example, consider a Python project template with the following structure:
.
|-- README.md
|-- package-name.txt
|-- setup.py
`-- src
|-- __init__.py
|-- package_name
| |-- __init__.py
| `-- package_name.py
`-- tests
|-- __init__.py
`-- test_package_name.py
As part of the specialization, we'd like to rename the "package_name" directory, the "package_name.py" file, and the "test_package_name.py" file to the specialized package name. To accomplish this, we add the "package-name.txt' utility file and call the "template_specialize_rename" Jinja2 extension:
{# Rename template files #}
{% template_specialize_rename 'src/tests/test_package_name.py', 'test_' + package_name + '.py' %}
{% template_specialize_rename 'src/package_name/package_name.py', package_name + '.py' %}
{% template_specialize_rename 'src/package_name', package_name %}
{# Delete the package-name.txt utility template file #}
{% template_specialize_rename 'package-name.txt' %}
First, the "template_specialize_rename" extension is used to rename the package output files and directories. Finally, since we don't want the empty utility file in the output, we delete it using the "template_specialize_rename" extension with no second argument. Here's an example usage of our Python project template:
$ template-specialize python-package my-package -k package_name my_package
This command produces the following specialized template output with appropriately named package source directory and source files:
.
|-- README.md
|-- setup.py
`-- src
|-- __init__.py
|-- my_package
| |-- __init__.py
| `-- my_package.py
`-- tests
|-- __init__.py
`-- test_my_package.py
AWS Parameter Store
template-specialize can retrieve template values from AWS Parameter Store using botocore.
Here's an example of a YAML configuration file with a Parameter Store secret:
my_secret: {% filter tojson %}{% aws_parameter_store 'parameter-name' %}{% endfilter %}
botocore is usually configured using environment variables.
Usage
usage: template-specialize [-h] [-c FILE] [-e ENV] [-k KEY VALUE] [--dump]
SRC DST
positional arguments:
SRC the source template file or directory
DST the destination file or directory
options:
-h, --help show this help message and exit
-c FILE the environment files
-e ENV the environment name
-k KEY VALUE, --key KEY VALUE
add a template key and value
--dump dump the template variables
Development
This project is developed using Python Build.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file template-specialize-1.3.1.tar.gz
.
File metadata
- Download URL: template-specialize-1.3.1.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c02439950fe668d79ec1cf1d4e9f4e4d0924a9c3b3e5294a0785eff55196475f |
|
MD5 | 4c24dee60bd7067e59b5ef6d7dfef1c4 |
|
BLAKE2b-256 | c29b0f537b47a62af0d609108df06519dae2c7defd2208288a9614e3ef8de4cd |