Skip to main content

Configuration builder which combines features of Jinja and YAML

Project description

configtpl

This library builds configuration in two stages:

  1. Renders the provided configuration as Jinja templates
  2. Parses the rendered data as YAML file

Features

  • Uses Jinja2 and Yaml capabilities to build a dynamic configuration
  • Multiple configuration files might be passed. The library merges all of them into single config.
  • Basic confuration includes Jinja functions and filters for general-purpose tasks:
    • Reading the environment variables
    • Execution of system commands
    • Hashing

Standard features

Filters

In addition to Jinja buildin filters, the library provides the following ones:

Filter Description
base64 Base64 encoding
base64_decode Base64 decoding
md5 MD5 hash
sha256 SHA-256 hash
sha512 SHA-512 hash
split_space Splits a string with space separator into list of strings

Functions

See also List of Global Functions on Jinja page

            "cmd": jinja_globals.jinja_global_cmd,
            "cwd": jinja_globals.jinja_global_cwd,
            "env": jinja_globals.jinja_global_env,
            "file": jinja_globals.jinja_global_file,
            "uuid": jinja_globals.jinja_global_uuid,
Function Description
cmd(cmd: str) Executes a system command and returns the standard output
cwd() Returns the current working directory
env(name: str, default: str) Returns the value of enviroment variable name if it exists,
or falls back to default value otherwise
file(path: str) Reads the file and returns the contents
uuid Generates a UUID e.g 1f6c868d-f9b7-4d3f-b7c9-48048b065019

Examples

You can check the functional tests folder for more examples.

A very simple example of usage is provided below:

# my_first_config.cfg

{% set my_val = "abc" %}
app:
  param_env: "{{ env('MY_ENV_VAR', 'default') }}"
  param1: "{{ my_val }}"
# my_second_config.cfg

app:
  param2: def
  param3: "{{ app.param1 }}123"
hash: "{{ app.param1 | md5 }}"
# app.py

import json
from configtpl.config_builder import ConfigBuilder

builder = ConfigBuilder()
cfg = builder.build_from_files("my_first_config.cfg:my_second_config.cfg")
print(json.dumps(cfg, indent=2))
# Execution

MY_ENV_VAR=testing python ./app.py

# output
{
  "app": {
    "param_env": "testing",
    "param1": "abc",
    "param2": "def",
    "param3": "abc123"
  },
  "hash": "900150983cd24fb0d6963f7d28e17f72"
}

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

configtpl-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.

configtpl-1.0.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: configtpl-1.0.1.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for configtpl-1.0.1.tar.gz
Algorithm Hash digest
SHA256 c679cc6d452f87fc6cec24ceb0ffccf77b1e0f2c455097caca1eb399b7c57c28
MD5 1ccf8c03ac8a6732511f4739f9aaedd3
BLAKE2b-256 f3ff0986888fe8527ee6ac46c0c6773f501ce69321242d7eee5a6245231d1208

See more details on using hashes here.

File details

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

File metadata

  • Download URL: configtpl-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for configtpl-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b5c3c46f264238ffe0746079c3c3b5229f7fa7985f0532b0da81c3a83dad9fdf
MD5 bad5af3639703bca5fe113db00ebf990
BLAKE2b-256 7bd071a0df9c21ee981894fef2ed11719cb2d9a6abfc70808db4c8252094b1cb

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