Skip to main content

YAMLpp: a macro language for dynamic YAML preprocessing

Project description

YAML File Preprocessor (YAMLpp)

Problem

YAML is an excellent file format but it is essentially static. Sometimes, the content of a YAML file must change according to circumstances (typically when the environment changes or when you have different configuratons for test or production, etc.).

Manually maintaining different versions can be time-consuming and error-prone.

Introducing YAMLpp

What if we had a way to generate a new YAML file (or more than one) according to a single pattern?

The purpose of YAML Preprocessor (YAMLpp) is to help programmers prepare YAML files from a template, with rules that produce the YAML tree according to source data. It extends standard YAML with constructs for variable declaration, conditionals, iteration, functions, importing and exporting YAML files, and importing Python modules.

YAMLpp is a macro language, since it manipulates the YAML tree on which it resides.

Here is a simple example:

YAMLpp:

.context:
  name: "Alice"

message: "Hello, {{ name }}!"

Output:

message: "Hello, Alice!"

General principles

The language is composed of constructs, which are denoted keys starting with a dot (.), such as .context, .if, .switch, etc.

The YAMLpp preprocessor uses these constructs modify the tree, and the constructs disappear.

The result is pure YAML.

YAMLpp obeys the rules of YAML syntax:

  • It provides declarative constructs without breaking YAML syntax.
  • It allows modular, reusable, and expressive constructs that create YAML files

🚀 Quickstart

Installation

pip install yamlpp

Command-line usage

yamlpp input.yaml -o output.yaml
  • input.yaml → your YAML file with YAMLpp directives
  • output.yaml → the fully expanded YAML after preprocessing

To consult the help:

yamlpp --help

Python API

from yamlpp import Interpreter

FILENAME = 'my_file.yaml'
i = Interpreter()

# the initial (source) tree:
initial_tree = i.load(FILENAME)

# the destination (target) tree (pure YAML):
tree = i.render()

# print the resulting YAML
print(i.yaml)

🔧 YAMLpp Constructs (Quick Reference)

Construct Purpose Minimal Example
.context Define local variables valid for siblings and descendants. .context:
name: "Alice"
message: "Hello {{ name }}"
.do Execute a sequence or map of instructions. .do:
- step: "Init"
- step: "Run"
.foreach Iterate over values with a loop body. .context:
items: [1,2]
.foreach:
.values: [x, items]
.do:
- val: "{{ x }}"
.switch Branch to a different node based on an expression and cases. .switch:
.expr: "{{ color }}"
.cases:
red: {msg: "Stop"}
.default: {msg: "?"}
.if Conditional node creation with then and else. .if:
.cond: "{{ x>0 }}"
.then: {res: "Pos"}
.else: {res: "Neg"}
.insert Insert and preprocess another YAMLpp (or YAML) file. .import: "other.yaml"
.function Define a reusable block with arguments and a body. .function:
.name: "greet"
.args: ["n"]
.do:
- msg: "Hi {{ n }}"
.call Invoke a previously defined function with arguments. .call:
.name: "greet"
.args: ["Bob"]
.import Import a Python module exposing functions, filters, and variables. .module: "module.py"
.export Export a portion of the tree into an external file. .export:
.filename: "out.yaml"
.do:
- foo: "bar"

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

yamlpp_lang-0.2.4.tar.gz (28.9 kB view details)

Uploaded Source

Built Distribution

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

yamlpp_lang-0.2.4-py3-none-any.whl (33.6 kB view details)

Uploaded Python 3

File details

Details for the file yamlpp_lang-0.2.4.tar.gz.

File metadata

  • Download URL: yamlpp_lang-0.2.4.tar.gz
  • Upload date:
  • Size: 28.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for yamlpp_lang-0.2.4.tar.gz
Algorithm Hash digest
SHA256 2becd7f38f28595d41a47c5b12b8e6a2b4f41e80a608bce4ff4666f5eee1cfd8
MD5 5b8a0ddb22a71cc53baf52a24c2a04d3
BLAKE2b-256 187f8193ef695e5ec7a43b462f4095eeb0c2a8f5ba281d30a5e07858b40b48be

See more details on using hashes here.

File details

Details for the file yamlpp_lang-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: yamlpp_lang-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 33.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for yamlpp_lang-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 9c78d7d557bc7d8f4986b8bae69ffe1aa23b0eb085538f1dbdfe5631aeac973d
MD5 d8818840cf52cc68d17e8e7bcfd85a13
BLAKE2b-256 496b940b894770306a39b4980c16dd055959585ac6aec37fd29f839aa607127b

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