Skip to main content

J2Escape

.github/workflows/python-package.yml Python 3.7 Python 3.8 Python 3.9 Python 3.10 Python 3.11

Installation

pip install j2escape

Overview

This module, written in Python, facilitates the storage of Jinja2 templates within a project managed by Cookiecutter or Cruft.

Cookiecutter utilizes Jinja templates internally when substituting input variables in the source code. However, this can result in errors if a Jinja template, such as {% if config.allow_duplicates %}, is intended for the application rather than Cookiecutter or Cruft.

To circumvent this problem, this module employs the jinja2.lex() function to parse and appropriately escape the template blocks.

The transformation process is idempotent, meaning that an already escaped template will remain unchanged when escaped again.

Example:

Template Escaped Template
{{ variable }} {{ '{{' }} variable {{ '}}' }}
{% if config.allow_duplicates %} {{ '{%' }} if config.allow_duplicates {{ '%}' }}

The module also provides a command-line interface that can be accessed using the j2escape command. This command can be utilized to escape jinja2 tags within a directory of templates.

j2escape usage

j2escape --help
usage: j2escape [-h] [-t TEMPLATES] [-o OUTPUT_DIR] [--overwrite] [-c] [-l {DEBUG,INFO,WARNING,ERROR,CRITICAL}] [-f LOGFILE]

Escape jinja2 tags in a directory of templates.

options:
  -h, --help            show this help message and exit
  -t TEMPLATES, --templates TEMPLATES
                        A comma-separated string of Jinja Templates (*.j2) or a directory with *.j2 files.
  -o OUTPUT_DIR, --output-dir OUTPUT_DIR
                        Specifies the directory path where the escaped templates will be stored.
  --overwrite           Replaces the original templates. This is necessary if the --output-dir is not provided.
  -c, --create-ok       Generates the output directory if it doesn’t already exist.
  -l {DEBUG,INFO,WARNING,ERROR,CRITICAL}, --loglevel {DEBUG,INFO,WARNING,ERROR,CRITICAL}
                        log level
  -f LOGFILE, --logfile LOGFILE
                        Specifies the logfile. If not provided, the default is None

To use the module in Python code, you can import it as follows:

import j2escape

template_directory = "./source-dir"
output_directory = "./excaped-templates"
allow_create_output_dir = True

j2e = J2Escape(template_directory)
j2e.save_to_directory(outputdir=output_directory, create_ok=allow_create_output_dir)

The static method get_escaped() can be used to escape templates in memory:

escaped_tamplate_string = J2Escape.get_escaped(plain_template_string)

Development

Install poetry and pre-commit to manage the development environment.

Clone the repository and install the development dependencies:

git clone https://github.com/jifox/j2escape.git
cd j2escape
poetry install

pre-commit hooks are used to ensure code quality. To install the pre-commit hooks, run the following command:

pre-commit install

Use pre-commit to run the hooks on all files:

pre-commit run --all-files

Metadata

Release files for j2escape 1.0.2

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

Source distribution (sdist)

Source distribution for j2escape 1.0.2
File Size Uploaded Provenance Actions
j2escape-1.0.2.tar.gz 9.6 kB Without provenance Details for j2escape-1.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for j2escape 1.0.2
File Interpreter ABI Platform Actions
j2escape-1.0.2-py3-none-any.whl Python 3 none any Details for j2escape-1.0.2-py3-none-any.whl

Total release size: 18.8 kB

Release files / j2escape-1.0.2.tar.gz

Download URL j2escape-1.0.2.tar.gz
Size 9.6 kB
Tags Source
SHA-256 checksum
How to use checksums
1e92ce72ad63723cf05f71665ae82faeae37166b655d83d631942cfdb885cb51
BLAKE2b-256 checksum
How to use checksums
fb2629145f0151260da552ec89dadbceef2be6b4fcd0cedbf03565a15aada726
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.6 Linux/5.15.0-84-generic

Release files / j2escape-1.0.2-py3-none-any.whl

Download URL j2escape-1.0.2-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6cc7a2c65c204947a2407f58f147173eab729702ce54e5e57be117a540ff9f46
BLAKE2b-256 checksum
How to use checksums
b8bc3cf4346a48718543c537c71dd397617fddab26eb3d6048b5907b405bfc62
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.6 Linux/5.15.0-84-generic

Security for j2escape 1.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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