Skip to main content

A generic generator framework using Jinja2 and user-defined parsers.

Project description

pytemplify

Text file generator framework using parsed dictionary data and Jinja2 templates.

How to create your generator using pytemplify

Install uv:

curl -Ls https://astral.sh/uv/install.sh | sh

Install pytemplify:

pip install pytemplify

Generate the first skeleton of your generator using mygen-init:

cd <your-repo-path>
mygen-init

Complete the TODOs in modules; main implementation module is parser_<your-generator-name>.py.

Try to run:

uv pip install -r requirements.txt
uv venv
source .venv/bin/activate
<your-generator-name>
uv pip install nox
nox

Running Tests and Linters with nox

To run all sessions (formatting, linting, and tests):

nox

To run only tests:

nox -s tests

To run only linting:

nox -s lint

To run only code formatting:

nox -s format_code

Publishing to PyPI with uv

  1. Build the package:
uv build
  1. Publish to PyPI:
uv publish

For test PyPI, use:

uv publish --repository testpypi

Using TemplateRenderer

The TemplateRenderer class is a powerful utility for rendering Jinja2 templates with special features like manual sections preservation and content injection.

Basic Usage

from pytemplify.renderer import TemplateRenderer
from pathlib import Path

# Create a renderer with data
data = {"project_name": "MyProject", "version": "1.0.0"}
renderer = TemplateRenderer(data)

# Render a string template
template = "Project: {{ project_name }}, Version: {{ version }}"
result = renderer.render_string(template)
print(result)  # Output: "Project: MyProject, Version: 1.0.0"

# Render a template file
renderer.render_file(Path("templates/config.ini.j2"))

# Generate files from a template directory
renderer.generate(Path("templates"), Path("output"))

Manual Sections

Manual sections allow you to preserve user-modified content between template regenerations:

template = """
# Configuration
project_name = {{ project_name }}

# Custom settings
MANUAL SECTION START: custom_settings
# Add your custom settings here
debug = True
MANUAL SECTION END
"""

# Previously rendered content with user modifications
previous = """
# Configuration
project_name = OldProject

# Custom settings
MANUAL SECTION START: custom_settings
# User added these settings
debug = True
verbose = True
log_level = DEBUG
MANUAL SECTION END
"""

# The manual section will be preserved in the new render
result = renderer.render_string(template, previous)

Content Injection

You can inject content into specific parts of existing files:

template = """
<!-- injection-pattern: imports -->
pattern: (?P<injection>import .*)
<!-- injection-string-start -->
import os
import sys
import json
<!-- injection-string-end -->
"""

existing_file = """
import os
import sys
# Other code here
"""

# Will inject the new import statements
result = renderer.inject_string(template, existing_file)

Template Directory Generation

Generate an entire directory structure from templates:

# Will process all .j2 files in templates/ and generate the corresponding 
# structure in output/ with rendered content
renderer.generate(Path("templates"), Path("output"))

For more details, see the API documentation in the code.

TIPs

Activate uv virtual environment:

source .venv/bin/activate

Deactivate uv virtual environment:

deactivate

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

pytemplify-0.1.15.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

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

pytemplify-0.1.15-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file pytemplify-0.1.15.tar.gz.

File metadata

  • Download URL: pytemplify-0.1.15.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.15

File hashes

Hashes for pytemplify-0.1.15.tar.gz
Algorithm Hash digest
SHA256 cbdc14cc4fcf9d9e4dd40080bba48775ce3b45b5f724346102a93518e87ff10b
MD5 26c1872b35dc377e793bc39415eb374a
BLAKE2b-256 9388bf4d7ddc1cb79e5142f4d3c68b6fa8b5d1eaf88a123749b76efd7d8f9b25

See more details on using hashes here.

File details

Details for the file pytemplify-0.1.15-py3-none-any.whl.

File metadata

File hashes

Hashes for pytemplify-0.1.15-py3-none-any.whl
Algorithm Hash digest
SHA256 50853b07fbb002bdfbbd6562c361099a07a93adf83499a8afed4039b0d192c8c
MD5 82ec4ab3e20edd0abe1d8adf8934fcc1
BLAKE2b-256 717c05d64efe6c222822477bd2432f887a7b45b35e04125af151e1795d2b5c5b

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