A CLI tool to generate type-hinted Python config classes from YAML files with nested structure support.
Project description
yaml2py
A CLI tool to generate type-hinted Python config classes from YAML files with nested structure support, automatic file watching and hot reloading capabilities.
Features
- 🔧 Auto-generate type-hinted Python classes from YAML configuration files
- 🏗️ Nested structure support - handle complex YAML hierarchies with ease
- 🔍 Intelligent type inference (int, float, boolean, string, list, dict)
- 🔄 Hot reloading - automatically reload configuration when files change
- 🛡️ Sensitive data masking - automatically hide passwords and API keys
- 🎯 Smart path detection - automatically find config files in common locations
- 💡 IDE-friendly - full autocomplete and type hints support
- 🚀 Singleton pattern - ensure single configuration instance across your app
Installation
pip install yaml2py
Quick Start
1. Create a config.yaml file
system:
mode: development
debug: true
port: 8080
timeout: 30.5
database:
host: localhost
port: 5432
name: myapp
user: admin
password: secret123
options:
pool_size: 10
retry_attempts: 3
redis:
host: 127.0.0.1
port: 6379
db: 0
features:
- name: authentication
enabled: true
config:
session_timeout: 3600
max_attempts: 5
- name: logging
enabled: false
config:
level: info
format: json
ai_service:
api_key: sk-1234567890abcdef
model: gpt-4
endpoints:
- path: /chat
method: POST
rate_limit: 100
- path: /completions
method: POST
rate_limit: 50
2. Generate Python config classes
yaml2py --config config.yaml --output ./src/config
Or use it interactively (auto-detects config files):
yaml2py
3. Use in your code
from src.config.manager import ConfigManager
# Get singleton instance
config = ConfigManager()
# Access with full type hints and autocomplete
print(config.system.mode) # 'development'
print(config.system.debug) # True (as boolean)
print(config.database.port) # 5432 (as int)
print(config.system.timeout) # 30.5 (as float)
# Access nested structures
print(config.database.options.pool_size) # 10
print(config.database.options.retry_attempts) # 3
# Access lists with type safety
for feature in config.features:
print(f"{feature.name}: {feature.enabled}")
if feature.enabled:
print(f" Timeout: {feature.config.session_timeout}")
# Sensitive data is automatically masked
print(config.database.password) # '********'
print(config.ai_service.api_key) # '********'
# Hot reloading - config updates automatically
# Edit config.yaml and changes are reflected immediately!
Advanced Features
Nested Structure Support
yaml2py excels at handling complex nested structures:
app:
name: MyApp
services:
cache:
provider: redis
settings:
ttl: 3600
max_entries: 1000
queue:
provider: rabbitmq
settings:
prefetch: 10
durable: true
Access nested values with full type safety:
config.app.services.cache.settings.ttl # Full IDE support!
List Handling
Automatically generates typed classes for lists of objects:
servers:
- name: web-1
host: 10.0.0.1
port: 80
- name: web-2
host: 10.0.0.2
port: 80
for server in config.servers:
# server has full type hints
print(f"{server.name}: {server.host}:{server.port}")
Hot Reloading
Configuration automatically reloads when files change:
# Start your app
config = ConfigManager()
print(config.system.debug) # False
# Edit config.yaml and set debug: true
# No restart needed!
print(config.system.debug) # True
Type Safety
All configurations are properly typed:
config.system.debug # bool
config.database.port # int
config.system.timeout # float
config.database.name # str
config.features # List[FeatureSchema]
config.database.options # OptionsSchema
CLI Options
yaml2py --help
Options:
-c, --config PATH Path to YAML configuration file
-o, --output PATH Output directory for generated files
--help Show this message and exit
Generated File Structure
output_dir/
├── __init__.py # Package initialization
├── schema.py # Configuration classes with type hints
└── manager.py # Singleton manager with hot reload
Development
Running Tests
python -m pytest tests/
Code Quality
make lint # Run linting
make format # Format code
make test # Run tests
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License - see LICENSE file for details.
TODO
- Support for custom type validators
- YAML anchors and references
- Environment variable interpolation
- Multiple config file merging
- Config inheritance
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 yaml2py-0.1.0.tar.gz.
File metadata
- Download URL: yaml2py-0.1.0.tar.gz
- Upload date:
- Size: 28.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
205255c1894821a4cc81328de35e6195c5554b1a7a6d8bf9d00d9ff46ebdf74b
|
|
| MD5 |
5ce367e703f2799ac7f787b4ec08af00
|
|
| BLAKE2b-256 |
ba685b0bcd50872d782185603f01ae12051bd9b36a068f3e3df06264ade78796
|
File details
Details for the file yaml2py-0.1.0-py3-none-any.whl.
File metadata
- Download URL: yaml2py-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a13689e0ea6f2159be2dda0161a8a2596fa8990cda0090308a49f805bc8ef102
|
|
| MD5 |
39c2ae875b62e3d8331bb8d8cc4c9743
|
|
| BLAKE2b-256 |
149073cf35aae077ef7de4406211cd27e3cf8773b4a5fdfc6bf0960cfeaea2a5
|