Metadata-Version: 2.4
Name: configtpl
Version: 2.0.1
Summary: Configuration builder which combines features of Jinja and YAML
Project-URL: Homepage, https://github.com/ignytis/configtpl_py
Author-email: Ignytis <155588001+ignytis@users.noreply.github.com>
License-Expression: MIT
License-File: LICENSE
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Requires-Python: >=3.11
Requires-Dist: jinja2<4.0.0,>=3.0.0
Requires-Dist: pyyaml<7.0.0,>=5.1.1
Provides-Extra: dev
Requires-Dist: hatch<1.17.0,>=1.16.0; extra == 'dev'
Requires-Dist: pre-commit; extra == 'dev'
Requires-Dist: pytest; extra == 'dev'
Requires-Dist: ruff==0.14.3; extra == 'dev'
Description-Content-Type: text/markdown

# configtpl

This library builds configuration in two stages:

1. Renders the provided configuration as Jinja templates
1. 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](https://tedboy.github.io/jinja2/templ14.html#list-of-builtin-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](https://tedboy.github.io/jinja2/templ16.html#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
1. Configuration from given files or string
1. Environment variables, if variable prefix is provided
1. Overrides

# Examples

You can check the [functional tests folder](tests/functional) for more examples.

A very simple example of usage is provided below:

```yaml
# my_first_config.cfg

{% set my_val = "abc" %}
app:
  param_env: "{{ env('MY_ENV_VAR', 'default') }}"
  param1: "{{ my_val }}"
```

```yaml
# my_second_config.cfg
app:
  param2: def
  param3: "{{ app.param1 }}123"
hash: "{{ app.param1 | md5 }}"
override: test
```


```python
# 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))

```

```bash
# 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"
}
```
