Skip to main content

Generate user-friendly configuration files from Pydantic models

Project description

confdantic

Tests PyPI version Supported versions Downloads license

Confdantic is a Python library that enhances Pydantic's capabilities for working with JSON, YAML, and TOML formats by preserving field descriptions as comments when serializing to YAML or TOML.

Installation

Using pip

pip install confdantic

Using uv

uv add confdantic

From source

pip install git+https://github.com/zigai/confdantic.git
# or
uv add git+https://github.com/zigai/confdantic.git

Example

from typing import Literal
from pydantic import Field
from confdantic import Confdantic

class 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: str

class 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

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

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

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

confdantic-0.1.2.tar.gz (54.6 kB view details)

Uploaded Source

Built Distribution

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

confdantic-0.1.2-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file confdantic-0.1.2.tar.gz.

File metadata

  • Download URL: confdantic-0.1.2.tar.gz
  • Upload date:
  • Size: 54.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for confdantic-0.1.2.tar.gz
Algorithm Hash digest
SHA256 8a049dd5773f25ad508a749eb2c719789efff18852874e67980953db328ecdd7
MD5 ecb3a07d21aec33381c9a71c53e1458e
BLAKE2b-256 a8269b4dde20ffd60b8b13252f3bc4f39c353b04cb0ec4c0c08ff315f231c921

See more details on using hashes here.

File details

Details for the file confdantic-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: confdantic-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for confdantic-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 83d6293bc7625f74f600eae53e542e4a93df347d962cb75d92a9f6a5690a796c
MD5 fe9f45324c6bfbab99c17931d4dd0839
BLAKE2b-256 ee067b18186052b1f0e06c69010d94ebe26b23c2d9fa4a0abdc7b9dd489b9919

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