Skip to main content

A YAML template engine with Python expressions

Project description

YTE - A YAML template engine with Python expressions

test coverage: 100% GitHub Workflow Status PyPI Conda Recipe Conda Downloads Conda Version

YTE is a template engine for YAML format that utilizes the YAML structure in combination with Python expressions for enabling to dynamically build YAML documents.

Syntax

The key idea of YTE is to rely on the YAML structure to enable conditionals, loops and other arbitrary Python expressions to dynamically render YAML files. Python expressions are thereby declared by prepending them with a ? anywhere in the YAML. Any such value will be automatically evaluated by YTE, yielding plain YAML as a result. Importantly, YTE templates are still valid YAML files (for YAML, the ? expressions are just strings).

Examples

Conditionals

Template
?if True:
  foo: 1
?elif False:
  bar: 2
?else:
  bar: 1
Rendered
foo: 1
Template
?if True:
  - a
  - b
Rendered
- a
- b
Template
- foo
- bar
- ?if True:
    baz
  ?else:
    bar
Rendered
- foo
- bar
- baz

Loops

Template
?for i in range(2):
  '?f"key:{i}"': 1  # When expressions in keys or values contain colons, they need to be additionally quoted.
  ?if i == 1:
      foo: true
Rendered
"key:0": 1
"key:1": 1
foo: true

Custom definitions

Template
  # The special keyword __definitions__ allows to define custom statements.
  # It can be used anywhere in the YAML, also repeatedly and inside of ifs or loops.
  __definitions__:
    - from itertools import product
    - |
      def squared(value):
          return value ** 2

  ?for item in product([1, 2], ["a", "b"]):
    - ?f"{item}"

  ?if True:
    - ?squared(2)
Rendered
- 1-a
- 1-b
- 2-a
- 2-b
- 4

Usage

Command line interface

YTE comes with a command line interface. To render any YTE template, just issue

yte < the-template.yaml > the-rendered-version.yaml

Python API

Alternatively, you can invoke YTE via its Python API:

from yte import process_yaml

# set some variables as a Python dictionary
variables = ...

# render a string and obtain the result as a Python dict
result = process_yaml("""
?for i in range(10):
  - ?f"item-{i}"
""", variables=variables)

# render a file and obtain the result as a Python dict
with open("the-template.yaml", "r") as template:
    result = process_yaml(template, variables=variables)

# render a file and write the result as valid YAML
with open("the-template.yaml", "r") as template, open("the-rendered-version.yaml", "w") as outfile:
    result = process_yaml(template, outfile=outfile, variables=variables)

Comparison with other engines

Lots of template engines are available, for example the famous generic jinja2. The reasons to generate a YAML specific engine are

  1. The YAML syntax can be exploited to simplify template expression syntax, and make it feel less foreign (i.e. fewer special characters for control flow needed) while increasing human readability.
  2. Whitespace handling (which is important with YAML since it has a semantic there) becomes unnecessary (e.g. with jinja2, some tuning is required to obtain proper YAML rendering).

Of course, YTE is not the first YAML specific template engine. Others include

The main difference between YTE and these two is that YTE extends YAML with plain Python syntax instead of introducing another specialized language. Of course, the choice is also a matter of taste.

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

yte-1.2.2.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

yte-1.2.2-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file yte-1.2.2.tar.gz.

File metadata

  • Download URL: yte-1.2.2.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0 CPython/3.9.12 Linux/5.13.0-1021-azure

File hashes

Hashes for yte-1.2.2.tar.gz
Algorithm Hash digest
SHA256 bfef905fe4bde475fa00eaa6264e1bd2c3dac9494abe32aaf387db08882c9096
MD5 a08df40b20a3a58391040473472fc30f
BLAKE2b-256 307e86e07223e6a1d0682acfe9c2db0e711fdaa70b6e7f8ea3e8992465a71105

See more details on using hashes here.

Provenance

File details

Details for the file yte-1.2.2-py3-none-any.whl.

File metadata

  • Download URL: yte-1.2.2-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0 CPython/3.9.12 Linux/5.13.0-1021-azure

File hashes

Hashes for yte-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b930b617731a42551085716e09bfd7d931222f45f53a0c0728b6593d98e1e8bb
MD5 0a79fcbf589dd06e45750db9f8331e7c
BLAKE2b-256 52d18675e2da35d767f326402585e347190b98ca847d08478670f353e9bd1dfa

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page