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.

☕ Support

If you find this project helpful, consider supporting me:

ko-fi

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.3.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.3-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: jinjagen-0.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 ec36f2cf1964e1e66b6afd01315f6f362148c86c866fcc99079507723a8765e4
MD5 ff1443ebc1d8e1b214c11d82146cfa66
BLAKE2b-256 3ea5b9051163ae06e1735a618c3ed78209f6c0b8aa620b14c659b393f924c473

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jinjagen-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 7.6 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bc48e71fd144543c2ee5576e9ea2dee746da0db2b6b9a9e75869e4fd1b596540
MD5 ad47a4cf6dfc3b5cfecafa44e52196d9
BLAKE2b-256 5d054d8ae48976e6376557d670453d57df0bfd6404d405268013927a8b5f63b5

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