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
@dataclassorPydanticmodels - Nested structures supported automatically
- Optional
.envmerging 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 casesettings.ymlenv_path- (pathlib.Path) - Path to the.envfile that contains all the data for the configurationmodel_class- (dataclasses.dataclass | pydantic.BaseModel) - The generated class from the YAML schema of the configurationuse_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 isdev.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
.envare injected only if the YAML value isnull - Nested overrides use
__as separator:- For
application.token→APPLICATION__TOKEN - For
redis.port→REDIS__PORT
- For
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ymlmaster-0.0.2.5.tar.gz.
File metadata
- Download URL: ymlmaster-0.0.2.5.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
96116dbacdee261bdbfc59e1f8524c3a68195ff32aa1d206c5f9c56274a05dc4
|
|
| MD5 |
ac1002ebc56eac92cf1323d87f7b720e
|
|
| BLAKE2b-256 |
a85fd4822b5f7739c95401bf041b49af06957fc2bb73a788413ed58c998154ab
|
File details
Details for the file ymlmaster-0.0.2.5-py3-none-any.whl.
File metadata
- Download URL: ymlmaster-0.0.2.5-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d20026339af864e0a1b743713e75081d9c79bd62a1a92cbc0e38d685170a6062
|
|
| MD5 |
6573c20c602f154243d9c7ffaf30aaf6
|
|
| BLAKE2b-256 |
79a21fb199f688386b6b9a23cdccf7aaaa5b4493ac0ed706014ae93f8b893cad
|