Skip to main content

Recursive template file specializer.

Project description

template-specialize

PyPI - Status PyPI GitHub PyPI - Python Version

template-specialize is a command-line tool for rendering Jinja2 templates. It renders individual template files as well as directories of template files.

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 file, execute template-specialize as follows. By default, templates are rendered to the terminal:

$ template-specialize nametag.md --key name --value 'John Doe'
## Hello, my name is

# John Doe

You can render the template file to an output file:

$ template-specialize nametag.md nametag-roy.md --key name --value 'Roy Hobbs' --key title --value 'The best there ever was'

You can also render templates contained within one or more directories to an output directory:

$ template-specialize templates/ output/ --key var --value value

Links

Environment Files

template-specialize was 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 -c environments.yaml -e test --dump
db_host: test-db-host
db_name: test-db
service_name: my-service

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] [--key KEY] [--value VALUE]
                           [--dump] [-v]
                           [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
  --key KEY      add a template key. Must be paired with a template value.
  --value VALUE  add a template value. Must be paired with a template key.
  --dump         dump the template variables
  -v, --version  show version number and quit

Development

This project is developed using Python Build.

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-specialize-1.0.0.tar.gz (8.0 kB view details)

Uploaded Source

File details

Details for the file template-specialize-1.0.0.tar.gz.

File metadata

  • Download URL: template-specialize-1.0.0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for template-specialize-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ff1a0b65f577fbc9fb1d6a46c072c1f1c4aecbad0127cedecc30d6786db054f3
MD5 e177a240baf5b06569006e9222890153
BLAKE2b-256 6d811bae11a69cca9c993fa9d8cdd488481aa87016a62622a2128c5bb6d76de1

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