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

?if True:
  foo: 1
?elif False:
  bar: 2
?else:
  bar: 1
?for i in range(2):
  ?f"key{i}": 1
  ?if i == 1:
      foo: True
?if True:
  - a
  - b
- foo
- bar
- ?if True:
    baz
  ?else:
    bar

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)

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-0.2.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

yte-0.2.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: yte-0.2.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0 CPython/3.9.10 Linux/5.11.0-1028-azure

File hashes

Hashes for yte-0.2.0.tar.gz
Algorithm Hash digest
SHA256 5024b96fbcec4cdb86898a7bca966b133b31ec293e0b0cd63d7f6b60f787091b
MD5 94a02f3ebf593f17a8e56a2cc04c6cb2
BLAKE2b-256 0ee08056aa0d820e7d8360267485c445605cf02ab43fbdb800286ea1d8fe6eff

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: yte-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0 CPython/3.9.10 Linux/5.11.0-1028-azure

File hashes

Hashes for yte-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 06454efde97afe274641855dd0e142e7c2578a852fb144dfcf86120e57a8be35
MD5 7cb50ba2f5836acc518a86126cfbf01d
BLAKE2b-256 b44087131b9bae04042125a5fe4b1c1714ac4874899ffdca4a9f12c136b1aba2

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