Skip to main content

A tiny language-independent text templater for python

Project description

tempest

Syntax-independent text templating library

Easily embed literal python code into any file.

Features:

  • Configurable open/close delimiters
  • Works with any language
  • It's just more python

Big Scary Security Warning

This library will execute arbitrary code contained in the template. Do not allow untrusted templates to be executed.

Install

pip install tempest-fmt

Example

Template

# This is a markdown file

Hello my name is {name=}

My friends are:
{for f in friends:}
    - {f=}

{divisor = 2}
{for i in range(5):}
    {if i % divisor == 0:}
        Did you know {i=} is divisible by {divisor=}

I can even evaluate stuff: {len(name)=}

Python Implementation

import tempest

t = tempest.parse_template_file("myTemplate.md", "{", "}")

values = {
    "name": "Jimmy",
    "friends": ["Carl", "Steve", "Greg"],
}

with open("output.md", mode='w') as f:
    t.generate(f, value)

Output file

# This is a markdown file

Hello my name is Jimmy

My friends are:
- Carl
- Steve
- Greg

Did you know 0 is divisible by 2
Did you know 2 is divisible by 2
Did you know 4 is divisible by 2

I can even evaluate stuff: 5

Syntax specifics

For simplicity, we will assume the delimiters to be { }
All python statements will be contained within the delimiters, any text outside will be considered raw text.

Statements of the form {<expr>=} will be evaluated as str(<expr>) and directly inserted into the text.
All other statements will be considered logical statements. Such statements must follow indentation rules for python and should have no raw text on the line. Any statements following will have indentation stripped, this includes raw text (extra whitespace after the expected indentation is left as-is).

Indentation is important
Leading spaces replaced with dots for this example

{for x in range(3):}
....- {x}

My next thing

Will produce

- 0
- 1
- 2

My next thing

Compared to

{for x in range(3):}
....- {x}
....

My next thing

Will produce

- 0

- 1

- 2

My next thing

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

tempest_fmt-1.0.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

tempest_fmt-1.0.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file tempest_fmt-1.0.1.tar.gz.

File metadata

  • Download URL: tempest_fmt-1.0.1.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for tempest_fmt-1.0.1.tar.gz
Algorithm Hash digest
SHA256 f2aa7ddcd3a42a5b67c3ff29d0d6110734fb52ef06da99306bf55adcd700bec3
MD5 78b697d4ef676ac5b6fbb3ef707127c1
BLAKE2b-256 e3898ec058a35c2e98eecc229a19514ccab6a5c8850f0620ffa44e88a633edef

See more details on using hashes here.

File details

Details for the file tempest_fmt-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: tempest_fmt-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for tempest_fmt-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 12ef4fd476c7190b71db9d169ae3474f296e2e72b07579eb2eb1fe82e9f2008e
MD5 9dcc3df3acbd3b6b04df3054cd6d7b02
BLAKE2b-256 24cda8a1599d71150f2f72b34f59afbc28bda1db0ff6dbfa40d2bc59f2f4fb99

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