Skip to main content

Custom tags to extend YAML for managing advanced configurations easily within a file. Environment variables, includes, and jinja templating.

Project description

yaml-config-tags

PyPI Python Versions License

Provides a set of custom tags to extend YAML for managing advanced configurations easily within a file. It supports:

  • Environment variables: !env
  • File includes: !include
  • Jinja templating: !jinja

Installation

pip install yaml-config-tags

Usage

Simply load the configuration file using config_load and pass a context dictionary to the loader.

config_load(
    path: str | Path,
    context: dict[str, Any] | None = None,
    jinja_settings: dict[str, Any] | None = None,
) -> Any

Example

from yaml_config import config_load

context = {
    'name': 'John Doe'
}
config = config_load('config.yml', context)
# config.yml
database:
  user: !env DB_USER
  password: !env DB_PASSWORD

advanced: !include advanced.yaml

greeting: !jinja |
  Hello, {{ name }}!

Features

Environment Variables

You can use environment variables in your configuration file by using the !env tag.

There are three ways to use environment variables:

  • !env VAR_NAME - Load the environment variable VAR_NAME. If it is not set, an exception will be raised.
  • !env [VAR_NAME, default_value] - Load the environment variable VAR_NAME with a default value if it is not set.
  • !env [VAR_NAME, FALLBACK_VAR1, .., FALLBACK_VARn, default_value] - Load the environment variable VAR_NAME, if it is not set, try to load the fallback variables in order. If none of them are set, use the default value.

Includes

You can include other files in your configuration file by using the !include tag.

# config.yml
advanced: !include advanced.yaml

Three types of files are supported, specified as a tag suffix:

  • yaml - Load the file as a YAML file.
  • json - Load the file as a JSON file.
  • txt - Load the file as a plain text file.

If no suffix is specified, the file will be loaded as a YAML file.

text_data: !include:txt text.txt
json_data: !include:json data.json

Relative paths are resolved relative to the directory of the file that contains the include.

Glob Patterns

You can use glob patterns in the file path, and all matching files will be included as a list.

files: !include:yaml "data/*.yaml"

Jinja Templating

You can use Jinja templating in your configuration file by using the !jinja tag. The context available to the template is passed as an argument to the loader.

greeting: !jinja |
  Hello, {{ name }}!

The !jinja tag is a short form for !jinja:str, a jinja template rendered as a string.

Native objects

In addition to plain text templates, you can also render a template to native objects.

connection:
  url: !env DB_URL
  token_provider: !jinja:obj |
    {{token_provider}}

When using !jinja:obj the template is rendered using NativeEnvironment and the result is evaluated as a native object instead of a string.

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

yaml_config_tags-0.1.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

yaml_config_tags-0.1.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file yaml_config_tags-0.1.0.tar.gz.

File metadata

  • Download URL: yaml_config_tags-0.1.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.11.10 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for yaml_config_tags-0.1.0.tar.gz
Algorithm Hash digest
SHA256 010bc62f0ea06d7b59ec09acb8a1e7745bde5be1b138dd3ceb6918a087fcf76d
MD5 77ce71975abc57bc6cc2b6b865e9c66c
BLAKE2b-256 18d32fa2ccf25348629a6fba4c01b2aea19e392c9d5103c72b5a1ebb59060100

See more details on using hashes here.

File details

Details for the file yaml_config_tags-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: yaml_config_tags-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.11.10 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for yaml_config_tags-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4aca8268fff15e1a3d54502cd456d633186d6eb5d01a654d81117fbe1b94678e
MD5 7b0f6007bf3b1c26e5ffe7104ca4579d
BLAKE2b-256 2f3de98ffb0b7a175ff9bdb95d07dd30c3d8b3caebb95bfcde405c5098dde591

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