Skip to main content

A CLI tool for generating files using Jinja2 templates with smart delimiters

Project description

Jinjagen 🌀

A CLI tool for generating files using Jinja2 templates with smart delimiters and dynamic data.

Features ✨

  • Auto-detects delimiters for code/file types (e.g., # for Python/Shell, /*% for C/JS).
  • Supports JSON/YAML data inputs for dynamic templates.
  • Lightweight CLI with stdin/stdout support.

Usage 🛠️

Basic Example

Generate a file from a template and JSON data:

python -m jinjagen template.txt.j2 output.txt -d data.json

Real-World Use Case: Bash Script Generation

1. Template (backup.sh.j2):

#!/bin/bash
#jinjagen: -D#
BACKUP_PATHS=(#% for path in backup_paths %# "#{ path }#" #% endfor %#)

#% if notify.enabled %#
EMAIL="#{ notify.email }#"
#% endif %#

2. Data (config.json):

{
  "backup_paths": ["/home/user/docs", "/etc/nginx"],
  "notify": { "enabled": true, "email": "admin@example.com" }
}

3. Generate Script:

python -m jinjagen backup.sh.j2 backup.sh -d config.json

Output (backup.sh):

#!/bin/bash
BACKUP_PATHS=( "/home/user/docs"  "/etc/nginx" )
EMAIL="admin@example.com"

Advanced Options ⚙️

Flag Description Example
-d/--data FILE Load variables from JSON/YAML. -d config.yml
-D/--delimiters Force delimiters (#, /, or .). -D# (for Bash/Python)
-t/--templates Specify template directory. -t ./templates
INPUT - OUTPUT Use - for stdin/stdout. template.j2 - -d data.json

Delimiters Explained 🔠

Jinjagen automatically adapts Jinja2 delimiters to avoid conflicts with different file types. You can also override this behavior manually.

Default Behavior (Auto-Detection)

The tool guesses delimiters based on file extensions:

File Type Example Extensions Delimiters
C/JS/Java .c, .js, .java /*% %*/ (blocks)
/*{ }*/ (variables)
Python/Shell .py, .sh, .yaml #% %# (blocks)
#{ }# (variables)
Generic/Other .txt, .html {% %} (blocks)
{{ }} (variables)

Example: For a .c file, Jinjagen uses /_% if x %_/instead of{% if x %}.

Manual Override

Force specific delimiters with -D/--delimiters:

# Use #-style (for Python/Shell)
python -m jinjagen template.txt output.txt -D#

# Use /-style (for C/JS)
python -m jinjagen template.c output.c -D/

# Use default Jinja2 delimiters (.)
python -m jinjagen template.html output.html -D.

Custom Delimiter Logic

The delimiter system handles these cases:

  1. Conflict Prevention:
    Avoids syntax clashes (e.g., {{ }} in Vue.js or {% %} in Django).
  2. Comment Safety:
    Respects language comment styles (e.g., # in Bash won’t break #% blocks).
  3. Edge Cases:
    If no extension matches (or for .txt), falls back to standard Jinja2 delimiters.

Practical Usage 🛠️

Jinjagen shines in generating configs, scripts, and code files. Here are common scenarios:

1. Generate Config Files

Scenario: Create an nginx.conf with dynamic ports and SSL settings.
Template (nginx.conf.j2):

#jinjagen: -D#
server {
  listen #{ port }#;
  server_name #{ domain }#;

  #% if ssl_enabled %#
  ssl_certificate #{ ssl_cert }#;
  ssl_certificate_key #{ ssl_key }#;
  #% endif %#
}

Data (deploy.yml):

port: 443
domain: "example.com"
ssl_enabled: true
ssl_cert: "/etc/ssl/certs/example.crt"
ssl_key: "/etc/ssl/private/example.key"

Command:

python -m jinjagen nginx.conf.j2 nginx.conf -d deploy.yml

2. Dynamic Script Generation

Scenario: Create a backup script with conditional email alerts.
Template (backup.sh.j2):

#!/bin/bash
#jinjagen: -D#
BACKUP_DIRS=(#% for dir in backup_dirs %# "#{ dir }#" #% endfor %#)

#% if notify %#
echo "Backup done" | mail -s "Backup Log" #{ email }#
#% endif %#

Data (config.json):

{
  "backup_dirs": ["/home", "/etc"],
  "notify": true,
  "email": "admin@example.com"
}

Command:

python -m jinjagen backup.sh.j2 backup.sh -d config.json -D#

3. CI/CD Pipeline Integration

Scenario: Generate environment-specific .env files during deployment.
Template (.env.j2):

#jinjagen: -D.
DB_HOST=#{ db_host }#
DB_USER=#{ db_user }#
#% if env == "prod" %#
DB_PASSWORD=#{ "vault.get('db_prod_pass')" }#
#% else %#
DB_PASSWORD=devpass
#% endif %#

Command (using environment variables):

python -m jinjagen .env.j2 .env -D# -d <(echo '{"env":"prod","db_host":"10.0.0.1","db_user":"admin"}')

4. Code Generation

Scenario: Generate a Python class from a template.
Template (model.py.j2):

#jinjagen: -D#
class #{ class_name }#:
    def __init__(self):
        #% for field in fields %#
        self.#{ field }# = None
        #% endfor %#

Data (model.json):

{
  "class_name": "UserModel",
  "fields": ["id", "name", "email"]
}

Command:

python -m jinjagen model.py.j2 UserModel.py -d model.json

Pro Tips 💡

  • Use - for stdin/stdout: Pipe templates/data between tools.
    echo "Hello #{ name }#" | python -m jinjagen -D# - -d <(echo '{"name":"World"}')
    
  • Override delimiters when auto-detection fails:
    python -m jinjagen template.sql output.sql -D/  # Force /*% style for SQL
    
  • Template directories: Use -t ./templates to enable {% include %}.

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

jinjagen-0.0.1.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

jinjagen-0.0.1-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file jinjagen-0.0.1.tar.gz.

File metadata

  • Download URL: jinjagen-0.0.1.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.17

File hashes

Hashes for jinjagen-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d16c56359815a717cce295daf80adac9d830caf0abbec4646106c5da52936ce1
MD5 d09c0602502a9cf766b551dd5a0108f5
BLAKE2b-256 0a434ff8d9fc27c28b048d7f779324a0957f461a133971d71b2ddd2a2a5c1203

See more details on using hashes here.

File details

Details for the file jinjagen-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: jinjagen-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.17

File hashes

Hashes for jinjagen-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 513e8249cd3ba1167bb1f23d7592e08222840f488e03ebc9404078086345b4d9
MD5 a4b62f4b176417a059abe8fb4f42a1e6
BLAKE2b-256 7c1caa1ad821ecf61875e3e6bc76b554818955ec69ab94d58391f4118ff751c5

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