https://github.com/zigai/confdantic
Generate user-friendly configuration files from Pydantic models
https://github.com/zigai/confdantic
config configuration-management pydantic pydantic-configuration toml toml-configuration yaml yaml-configuration
Last synced: about 1 month ago
JSON representation
Generate user-friendly configuration files from Pydantic models
- Host: GitHub
- URL: https://github.com/zigai/confdantic
- Owner: zigai
- License: mit
- Created: 2024-01-09T19:28:06.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-02-14T12:10:48.000Z (3 months ago)
- Last Synced: 2025-03-24T03:35:54.264Z (2 months ago)
- Topics: config, configuration-management, pydantic, pydantic-configuration, toml, toml-configuration, yaml, yaml-configuration
- Language: Python
- Homepage:
- Size: 34.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Confdantic
[](https://github.com/zigai/confdantic/actions/workflows/tests.yml)
[](https://badge.fury.io/py/confdantic)

[](https://pepy.tech/project/confdantic)
[](https://github.com/zigai/confdantic/blob/master/LICENSE)`Confdantic` is a Python library that enhances Pydantic's capabilities for working with JSON, YAML, and TOML formats.
It preserves field descriptions as comments when serializing to YAML or TOML, making it great for generating user-friendly configuration files.## Installation
#### From PyPi
```
pip install confdantic
```
#### From source
```
pip install git+https://github.com/zigai/confdantic.git
```
## Example
```python
from typing import Literal
from pydantic import Field
from confdantic import Confdanticclass DatabaseConfig(Confdantic):
host: str = Field(
"localhost",
description="The hostname or IP address of the database server",
)
port: int = Field(
5432,
description="The port number on which the database server is listening.",
)
username: str
password: strclass ApplicationConfig(Confdantic):
debug: bool = Field(False, description="Enable debug mode")
log_level: Literal["DEBUG", "INFO", "WARNING", "ERROR"] = Field(
"INFO", description="Logging level"
)
database: DatabaseConfig
allowed_hosts: list[str] = Field(
default_factory=list,
description="A list of host/domain names that this application can serve.",
)config = ApplicationConfig(
database=DatabaseConfig(username="admin", password="secret"),
allowed_hosts=["example.com"],
)
config.save("config.yaml", comments=True)
config.save("config.toml", comments=True)
```
`config.yaml`
```yaml
debug: false # Enable debug mode
log_level: INFO # Logging level | choices: DEBUG, INFO, WARNING, ERROR
database:
host: localhost # The hostname or IP address of the database server
port: 5432 # The port number on which the database server is listening.
username: admin
password: secret
allowed_hosts: # A list of host/domain names that this application can serve.
- example.com
```
`config.toml`
```toml
debug = false # Enable debug mode
log_level = "INFO" # Logging level | choices: DEBUG, INFO, WARNING, ERROR
allowed_hosts = ["example.com"] # A list of host/domain names that this application can serve.[database]
host = "localhost" # The hostname or IP address of the database server
port = 5432 # The port number on which the database server is listening.
username = "admin"
password = "secret"
```
## License
[MIT License](https://github.com/zigai/confdantic/blob/master/LICENSE)