Skip to main content

Vars collector and template runner.

Project description


Supported template Jinja, Mako, Template.

Tools that collect some vars and get them to a template engine.

Supported template: Jinja and Mako.

Use c2c-template --help to get the command line help.

Vars file

The vars collector gets the vars from YAML files like this one:

extends: inherit.yaml

     string_var: a string
     int_var: 42
     interpreted_var: __import__('datetime')
     combined_var: <s>'Today:</s> <s>{interpreted_var:%Y-%m-%d}'</s>
     facter_json: /usr/bin/facter --json
     facter_yaml: /usr/bin/facter --yaml
     pi: console.log(Math.PI.toPrecision(3))
         v1: 1
         v2: <s>'2'</s>
         v3: [1, 2, 3]

     - interpreted_var
     - facter_json
     - facter_yaml
     - facter_json
     - facter_yaml
         vars: [<s>"pi"</s>]
         cmd: [<s>"node"</s>, <s>"-e"</s>]

 - obj

The inherit.yaml is an other file with the same syntax that will provide initial vars.

The vars section is where we define the vars values, the YAML files support typing, than 42 will be an integer.

The interpreted configuration to interpret some vars, python, bash, environ, json, yaml are predefined interpreter, node is a custom interpreter.

The update_path is a list of ‘.’-separated paths that will be updated (for dicts) or appended (for lists), instead of overwritten. The sub path will be implicitly added.

We can reuse predefined variables and format them (see combined_var), See: str.format().

Example of usage

Interpret variable in a template

c2c-template --vars vars.yaml --engine jinja --files template.jinja

The result will be stored in a file named template.

Get the vars

It can be useful to get the variable outside.

`c2c-template --vars vars.yaml --get-vars INT_VAR=int_var string_var`

That will set the bash variable INT_VAR to 42, and STRING_VAR to ‘a string’.

Get a configuration file

c2c-template --vars vars.yaml --get-config config.yaml string-var int-var combined-var

Will create a file named config.yaml this:

string-var: a string
int-var: 42
combined-var: Today: 2014-12-12

Build a set of file based on a template

Create the following vars file (vars.yaml):

    var1: common
    - name: one
      var2: first
    - name: two
      var2: second

And the following template (template.jinja):

var1: {{ var1 }}
var2: {{ var2 }}

And run the following command:

c2c-template --vars vars.yaml --files-builder template.jinja {name}.txt iter

This will create two files:

the one.txt file, with:

var1: common
var2: first

The two.txt file, with:

var1: common
var2: second

Project details

Download files

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

Built Distribution

c2c.template-2.3.0-py3-none-any.whl (11.3 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page