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
  • Reading parameters from environment variables
  • Builds the configuration from files (build_from_files method) and from strinngs (build_from_str method)
  • If the environment variable prefix is provided, merges the corresponding environment variables into the configuration.

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

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

Precendence

  1. Defaults
  2. Configuration from given files or string
  3. Environment variables, if variable prefix is provided
  4. Overrides

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 }}"
override: test
# app.py

import json
from configtpl.main import ConfigTpl

builder = ConfigTpl(
  defaults={"default": "default_value"},
  env_var_prefix="MY_APP",
  overrides={"override": "overridden"},
)
cfg = builder.build_from_files(files=["my_first_config.cfg", "my_second_config.cfg"])
print(json.dumps(cfg, indent=2))
# Execution

MY_ENV_VAR=testing MY_APP__NESTED__VAR=hello python ./app.py

# output
{
  "app": {
    "param_env": "testing",
    "param1": "abc",
    "param2": "def",
    "param3": "abc123"
  },
  "default": "default_value",
  "hash": "900150983cd24fb0d6963f7d28e17f72",
  "nested": {
    "var": "hello"
  },
  "override": "overridden"
}

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-2.0.1.tar.gz (6.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-2.0.1-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: configtpl-2.0.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.2 cpython/3.13.9 HTTPX/0.28.1

File hashes

Hashes for configtpl-2.0.1.tar.gz
Algorithm Hash digest
SHA256 ee5bde55f3fa87b0404c8ea124e55b6152cf2dc385e9056487380996f1b74387
MD5 b4e72a552dd932ee43c8f7eef5226b78
BLAKE2b-256 377a7130051544c20e7dee4e7023a6409f977d412db3c9e2469facb2fa55a637

See more details on using hashes here.

File details

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

File metadata

  • Download URL: configtpl-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.2 cpython/3.13.9 HTTPX/0.28.1

File hashes

Hashes for configtpl-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 586b94b620be93734d50ec62f6e8f3055edbe183ef95e977e394d19c137be4ee
MD5 1c49df954f07083ddd3d012c975ec2fe
BLAKE2b-256 17affbbbaa8db9b0c5c482311315b0f82b440f9af89f92499b70b847ee973024

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