Skip to main content

Powerful and clean YAML-based configuration library for Python 3.11+

Project description

ymlmaster

ymlmaster is a configuration loading utility for Python 3.11+. It provides a unified interface for loading deeply structured YAML configuration files (optionally combined with .env overrides) into statically typed Python classes using either dataclass or pydantic.

Features

  • Schema generation from YAML into @dataclass or Pydantic models
  • Nested structures supported automatically
  • Optional .env merging with environment variable fallback
  • Profile support (e.g., dev, release)
  • CLI generator for schema models

Installation

Installation is done in the project using pip or poetry:

pip install ymlmaster
poetry add ymlmaster

Model Generation CLI

To generate data models from a YAML configuration file:

poetry run generate-schema
  --settings settings.yml
  --output settings_model.py
  --type dataclass # or --type pydantic,  default: dataclass
  --profile dev # or custom name block,  default: dev
  --url-fields postgresql redis # added url services

This will generate Python code like:

@dataclass
class Postgresql:
    host: Optional[str] = None
    user: Optional[str] = None
    password: Optional[str] = None
    port: Optional[str] = None
    db: Optional[str] = None

@dataclass
class Redis:
    host: Optional[str] = None
    port: Optional[str] = None

@dataclass
class Application:
    token: Optional[str] = None
    admin_id: Optional[str] = None

@dataclass
class Settings:
    postgresql: Postgresql = None
    redis: Redis = None
    application: Application = None
    postgresql_url: Optional[str] = None
    redis_url: Optional[str] = None

Using the Loader

You can then load values from settings.yml and .env into your model:

from pathlib import Path
from ymlmaster import SettingsLoader
from <settings-model> import Settings

loader = SettingsLoader(
    settings_path=Path("settings.yml"),
    env_path=Path(".env"),
    model_class=Settings,
    use_release=False, # true - release block, false - dev
    profile=None, # specify the exact loading block
    url_templates={
        "postgresql": "postgresql+asyncpg",
        "redis": "redis",
        "nats": "nats"
    }, # url generation instructions   <block name>:<circuit name>
    env_alias_map={"OLD_NAME": "NEW__NAME"}
)

config = loader.load()

print(config.redis.host)
print(config.application.admin_id)

Parameter Description:

  • settings_path - (pathlib.Path) - Path to the file with the YAML schema of the configuration, in my case settings.yml
  • env_path - (pathlib.Path) - Path to the .env file that contains all the data for the configuration
  • model_class - (dataclasses.dataclass | pydantic.BaseModel) - The generated class from the YAML schema of the configuration
  • use_release - (bool) - Parameter to automatically define dev/release configuration, more details below*
  • profile - (str) - Name of the block in the YAML configuration schema which configuration data to take (example: dev, release, stage, development) The default is dev.
  • url_templates - (dict) - Dictionary schema for generating URL services (example: postgresql, redis, nats, celery, rabbitmq, ...)
  • env_alias_map - (dict) - Aliases map for faster implementation (or library testing) in the existing configuration

* The use_release parameter is used to automatically determine where the project is launched. I use the following method: on the local machine there is a file .developer which is located in .gitignore, in SettingsLoader I write use_release=not Path(‘.developer’), it means, if the file is not found - it will be True and since profile is not specified, the block release will be automatically pulled up. If the file is found, so we are on a local machine in development mode, it will be False and therefore the dev configuration will be pulled. This is handy to use when you have one clear configuration for development and one for your sell.

Environment Variable Override Behavior

  • Values from .env are injected only if the YAML value is null
  • Nested overrides use __ as separator:
    • For application.tokenAPPLICATION__TOKEN
    • For redis.portREDIS__PORT

If the key ends with port and is an integer, a default IP of 127.0.0.1: is prepended unless already present.

This separation helps to use sensitive data in Dockerfile/DockerCompose and in your project at once Example docker-compose.yml:

services:
  q3s2j0pj0fuj:
    image: "postgres:17"
    container_name: "q3s2j0pj0fuj"
    environment:
      POSTGRES_DB: ${POSTGRESQL__DB}
      POSTGRES_USER: ${POSTGRESQL__USER}
      POSTGRES_PASSWORD: ${POSTGRESQL__PASSWORD}
    ports:
      - "${POSTGRESQL__PORT}:5432"
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
    networks:
      - Network
    restart: always

MIT LICENSE

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

ymlmaster-0.0.2.4.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

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

ymlmaster-0.0.2.4-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file ymlmaster-0.0.2.4.tar.gz.

File metadata

  • Download URL: ymlmaster-0.0.2.4.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for ymlmaster-0.0.2.4.tar.gz
Algorithm Hash digest
SHA256 9f3d17495abdd78a786a68be9f170d5d8194b5ed5d527b45c8f85fa4df94286f
MD5 07559a6b5c9e2380fb2df42a8b2857d9
BLAKE2b-256 6ac632edf2244479af4a36690288f5f20cfa7dee71bed55a52d4765c8d514166

See more details on using hashes here.

File details

Details for the file ymlmaster-0.0.2.4-py3-none-any.whl.

File metadata

  • Download URL: ymlmaster-0.0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for ymlmaster-0.0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fb255efde49cef163367540c358e62867208dd073609b4a15d6434cad4c3873d
MD5 9ce766344eed279e617f242d71430ef9
BLAKE2b-256 dc98b7aca1a92ab2d531aa565ce9b5d2ec96e56d73d2d1512ba72ae4d59c98f9

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