Skip to main content

tmple

General files or content generator, using template and data/variables.

Content of the file will be controlled by the template (in Jinja), and you can control the template using data/variables (in Yaml).

Use Cases

👨‍👩‍👦 Generate simple file, based on a template

Let say you want to generate a repeated SQL file like this:

WITH 
top_user (SELECT name, phone, address, gender, birth_date FROM top_user LIMIT 10),
mid_user (SELECT name, phone, address, gender, birth_date FROM mid_user LIMIT 10),
low_user (SELECT name, phone, address, gender, birth_date FROM low_user LIMIT 10),
base_user (SELECT name, phone, address, gender, birth_date FROM base_user LIMIT 10)

SELECT * top_user UNION ALL
SELECT * mid_user UNION ALL
SELECT * low_user UNION ALL
SELECT * base_user

*just imagine it has 1000 lines with another repetitive complex query 😂

Now, what you need to do, is having a template! a Jinja template!

WITH 
{%- for tb in tables %}
{{ tb.name }} AS (SELECT {% for c in columns %}{{ c }}{{ ',' if not loop.last }} {% endfor %}FROM {{ tb.name }} LIMIT {{ tb.limit }}){{ ',' if not loop.last }}
{%- endfor %}
{% for tb in tables %}
SELECT * FROM {{ tb.name }} {{ 'UNION ALL' if not loop.last }}
{%- endfor %}

Save it to .jinja2 file to your folder working_dir/sample.jinja2

But for sure that 👆 template can't generate by it self, it needs a data or variables to work on, right? Let's just create a yaml file with this format:

var: #hold all variables/dictionary that needed in your jinja files
    tables:
        - name: top_user
        limit: 10
        - name: mid_user
        limit: 20
        - name: low_user
        limit: 10
        - name: base_user
        limit: 30

    columns:
        - name
        - phone
        - address
        - gender
        - birth_date

generator:
    - template: sample.jinja2 #where you save your jinja file
      destination:
        - output.sql
        - log # to also print the result on your terminal

Save it to .yaml file to your folder as working_dir/recipe.yaml Done! above yaml is what we call recipe, for more detail, please take a look Recipe Concept section. Now, lets run your recipe! and see the result!

cd working_dir

tmple --recipe recipe.yaml

Uh oh, you can't run tmple yet? install first man! 😂😂

Generate list of files based on multiple templates

When you expect to generate multiple files, you can do setup on the recipe it self!

You can do write to multiple files for the same template, it will generate the same content but in different files

var: variables

generator:
    - template: samples/templates/sample.jinja2
      destination:
        - samples/results/sample.sql
        - /Users/personname/folder/tmple/src/results/simple.sql
        - sample/result/log.txt

but if let say you need to generate with different template, then do this instead:

var: variables

generator:
    - template: samples/templates/sample.jinja2
      destination:
        - samples/results/sample.sql
        - /Users/personname/folder/tmple/src/results/simple.sql
    - template: samples/templates/sample2.jinja2
      destination:
        - samples/results/sample2.sql
    - template: samples/templates/sample3.jinja2
      destination:
        - samples/results/sample3.sql

When do this, make sure you have all variables setup in var: that needed from all your template.

Just print, not generate files

This will generate content from sample.jinja2 but will not generate a file, it will only do log in your terminal. You need add destination: to generate a file.

var: variables

generator:
    - template: samples/templates/sample.jinja2

This also only do log/print only

var: variables

generator:
    - template: samples/templates/sample.jinja2
      destination: log

Recipe concept

Recipe is a config file to tell the tmple about:

  • all the variables that needed in your templates (var:)
  • which template that you gonna use to generate file (generator:template:)
  • where to save the generated content from your template (generator:destination:)
var: # mandatory
    any_variable: you need
    you_can:
        - write
        - down
        - here
    and_also:
        it_support: 
            nested: variables!
# above variables it just mentionable in the Jinja files
# like {{ it_also.support.nested }}

extend: # optional
    - samples/recipes/parent_recipe.yaml
    # do inherit
    # all variables or generator written from this parent_recipe.yaml 
    # will be also extended into current recipe


generator: # mandatory
    - template: samples/templates/sample.jinja2
      destination:
        - samples/results/sample.sql # directly create a file in this relateve path
        - /Users/personname/folder/tmple/src/results/simple.sql #absolute path to place to some folder
        - log # just print in the log of your terminal

    # you could also generate multiple files in different template using the same variables
    - template: samples/templates/sample2.jinja2
    # if there is no destination key here, then it will only print the generated content

Installation

Install using pip / pip3

pip install tmple

Usage

Running from terminal

tmple -r path/your_recipe.yaml

or

tmple --recipe path/your_recipe.yaml

Running from python code

from tmple.recipe import Recipe
recipe = Recipe.from_path('your_recipe_path.yaml')
recipe.generate()

Running from tmple repository

cd tmple
python3 main.py -r your_recipe.yaml

Metadata

Release files for tmple 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for tmple 0.1.0
File Interpreter ABI Platform
tmple-0.1.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Release files / tmple-0.1.0-py2.py3-none-any.whl

Download URL tmple-0.1.0-py2.py3-none-any.whl
Size 7.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
b8397c824ec681de26cffded7ae9a3ac4a015651cd63caf2f940820968bbb291
BLAKE2b-256 checksum
How to use checksums
4b9625adf0761b82981a596fdb64bcee6912d1d03f162d970bfe8d6f1a6cd773
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.7.1 requests/2.26.0 setuptools/60.5.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.10

Release history Release notifications | RSS feed

This release

0.1.0 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page