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 (YAML, JSON, plain text, YAML front matter)
  • Jinja templating: !jinja
  • Scoped context variables: __vars__

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,
    jinja_filters: dict[str, Callable] | None = None,
    *,
    constructor: type[Any] | None = None,
    jinja_vars_key: str = "__vars__",
) -> 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.

Explicit type

Environment variables are converted using implicit yaml types by default, but you can force a specific data type with tag suffix:

  • !env:str VAR_NAME
  • !env:int VAR_NAME

Valid type suffix are:

  • str
  • int
  • float
  • bool
  • timestamp

You can also combine defaults and fallbacks with type suffix: !env:str [VAR_NAME, default_value]

Includes

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

# config.yml
advanced: !include advanced.yaml

Four 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.
  • fmt - Load the file as text with a YAML front matter header.

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.

YAML Front Matter

You can include files that contain YAML front matter (e.g. Markdown documents with metadata) using the yfm suffix.

Front matter is a YAML block delimited by --- at the start of the file:

---
title: My Document
author: Jane Doe
tags:
  - config
  - example
---
This is the body of the document.

Three variants are available:

  • !include:yfm — returns the body text (shorthand for !include:yfm:body)
  • !include:yfm:body — returns the body text
  • !include:yfm:head — returns the parsed front matter as a dict
content: !include:yfm    docs/page.md       # body text
content: !include:yfm:body docs/page.md     # same
meta:    !include:yfm:head docs/page.md     # { title: "My Document", author: "Jane Doe", ... }

If no front matter is found, yfm:body returns the full file content and yfm:head returns an empty dict.

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.

Scoped Context Variables

You can create scoped context variables within any mapping using the special __vars__ key. This allows you to define variables that are available to Jinja templates within that scope and any nested scopes.

Variables defined in __vars__ are accessed via the vars namespace in Jinja templates: {{ vars.variable_name }}.

Basic Usage

api_config:
  __vars__:
    base_url: "https://api.example.com"
    version: "v2"
  
  endpoint: !jinja "{{ vars.base_url }}/{{ vars.version }}/users"
  auth_url: !jinja "{{ vars.base_url }}/{{ vars.version }}/auth"

Nested Scopes

Nested mappings can define their own __vars__ which inherit from and override parent scope variables:

api_config:
  __vars__:
    base_url: "https://api.example.com"
    version: "v2"
  
  endpoint: !jinja "{{ vars.base_url }}/{{ vars.version }}/users"
  
  settings:
    __vars__:
      timeout: 30
    
    # This scope has access to both parent vars (base_url, version) and its own (timeout)
    connection_timeout: !jinja "{{ vars.timeout }}"
    read_url: !jinja "{{ vars.base_url }}/{{ vars.version }}/read"

Scope Restoration

When exiting a scope, the parent's context is automatically restored:

# Global context
global_value: !jinja "{{ vars.base_url }}"  # Uses global context

api_config:
  __vars__:
    base_url: "https://api.example.com"  # Overrides base_url for this scope
  
  endpoint: !jinja "{{ vars.base_url }}"  # Uses scoped value

# Back to global context
fallback: !jinja "{{ vars.base_url }}"  # Uses global context again

Custom Key Name

You can customize the special key name by passing jinja_vars_key to the loader:

config = config_load('config.yml', context={'vars': {'env': 'prod'}}, jinja_vars_key='_context')
api:
  _context:
    version: "v3"
  url: !jinja "{{ vars.version }}"

Note: The __vars__ key itself is never included in the final loaded configuration.

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.7.3.tar.gz (10.9 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.7.3-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: yaml_config_tags-0.7.3.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.11.14 Linux/6.6.87.2-microsoft-standard-WSL2

File hashes

Hashes for yaml_config_tags-0.7.3.tar.gz
Algorithm Hash digest
SHA256 bd85ebd2bc2884f05c91ab90f08caa7fc3fb4ecd1c8f3fccced28e66c41bf25b
MD5 54951f8dfda790c9ab70cc2edc4ccfee
BLAKE2b-256 fce3d045b3e8bf082bc8725bd18eb2096018d34e8330e9d7ad5f412df3ed51a9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yaml_config_tags-0.7.3-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.11.14 Linux/6.6.87.2-microsoft-standard-WSL2

File hashes

Hashes for yaml_config_tags-0.7.3-py3-none-any.whl
Algorithm Hash digest
SHA256 73ccd25b73b235666b030511fc98f2746e867bae07f6d5346f862e72a8713431
MD5 979ad64e2ad1dd508a17f15724200ca8
BLAKE2b-256 6faa4c0c35041d66fb85e777669ba48055886859f567c327d240312d7263712e

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