Skip to main content

Tests PyPI version

ApPySetty ⚙️

A simple, type-safe Python library for managing application configuration from environment variables and YAML.

ApPySetty uses Python dataclasses as the single definition of your application configuration. It can load values from configuration files and environment variables and generate documentation from the same definition.

Quick Start

Install:

uv add appysetty

Define your configuration:

from dataclasses import dataclass

from appysetty import AppConfigSource, read_configuration


@dataclass
class Config:
    host: str = "localhost"
    port: int = 8080
    debug: bool = False


config = read_configuration(
    Config,
    AppConfigSource.ENV,
)

Override values with environment variables:

export HOST="0.0.0.0"
export PORT="9000"
export DEBUG="true"

Or load them from YAML:

host: localhost
port: 8080
debug: false
config = read_configuration(
    Config,
    AppConfigSource.YAML,
)

And also document your configuration with an example yaml and a markdown document:

write_configuration_documentation(Config, output_dir=Path("./docs"))

Configuration Sources

ApPySetty currently supports:

  • ENV: environment variables using UPPER_SNAKE_CASE only
  • YAML: YAML configuration files

Multiple sources can be combined. Later sources overwrite values from earlier sources:

config = read_configuration(
    Config,
    [
        AppConfigSource.YAML,
        AppConfigSource.ENV,
    ],
)

By default, YAML files are searched for in:

config.yaml
config.yml
config/config.yaml
config/config.yml

An explicit YAML path can also be provided.

Usage Details

Define Config

First you need to define a dataclass which contains all the configuration options you want to support.

@dataclass
class Config:
    host: str = "localhost"
    port: int = 8080
    debug: bool = False
    timeout: float = 5.0

You can also extend your dataclass with additional information for better documentation and for masking secrets:

@dataclass
class ConfigWithMetadata:
    host: Annotated[
        str,
        AppConfigEntry(description="The application host"),
    ] = "localhost"

    password: Annotated[
        str,
        AppConfigEntry(description="The database password", is_secret=True),
    ] = "secret"

Both variants can be mixed. If no description is provided, the name of the field will be the description.

Read Config

To read the config, use:

cfg = read_configuration(ConfigWithMetadata, AppConfigSource.ENV)

After that cfg should have all configurations with auto-complete ready for you.

You can also pass an instance and use multiple sources, where each source will overwrite the previous one:

cfg = read_configuration(
    ConfigWithMetadata(), [AppConfigSource.ENV, AppConfigSource.YAML]
)

The available sources are:

Key Source Description
AppConfigSource.ENV Environment This will read config from environment, using UPPER_SNAKE_CASE variant of the field name
AppConfigSource.YAML YAML file This will read the config from a yaml file, only matching field name exactly

Options

Options can be used to customize the config:

cfg = read_configuration(
    ConfigWithMetadata,
    [AppConfigSource.ENV, AppConfigSource.YAML],
    AppConfigOptions(
        env_prefix="MY_APP_PREFIX",
        yaml_path="config.dev.yaml",
        overwrite={"port": "8000"},
    ),
)
Option Description
env_prefix A Prefix that will be prepended to all field names using UPPER_SNAKE_CASE to read environment .
yaml_path Setting a specific yaml file to use. It unset, the tool will look for (config)/config.y(a)ml.
overwrite This accepts a mapping. values set with overwrite will always overwrite anything else

Write Documentation

The second feature of this tool is automated creation of a few documentation items for configuration options:

  • config.example.yaml containing an example yaml file with default values and descriptive comments (if descriptions were defined)
  • DefaultConfiguration.md containing a table of all options with ENV variant, a docker environment block for docker compose and a docker run example command with all -e set.

To create the documentation, use:

# Create both documents
write_configuration_documentation(
    ConfigWithMetadata, env_prefix="MY_APP_PREFIX", output_dir=Path()
)

# Only create yaml example
write_config_yaml_example(ConfigWithMetadata, output_dir=Path())

# Only create markdown document
write_config_markdown(ConfigWithMetadata, env_prefix="MY_APP_PREFIX", output_dir=Path())

Development

Clone the repository and install the development dependencies:

git clone https://github.com/SmartFactory-KL/appysetty.git
cd appysetty
uv sync

Run the tests:

uv run pytest

Run tests with coverage:

uv run pytest --cov=appysetty --cov-report=term-missing

Run the examples:

uv run python -m examples.write_documentation
uv run python -m examples.read_documentation

License

ApPySetty is licensed under the MIT License.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

appysetty-0.1.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

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

appysetty-0.1.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file appysetty-0.1.0.tar.gz.

File metadata

  • Download URL: appysetty-0.1.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for appysetty-0.1.0.tar.gz
Algorithm Hash digest
SHA256 66097ae0ab1992d4151a71ace52929524da49b285b31cc106d2ba0f3d9ec8cf8
MD5 221edc86e8193b7d9df6854f1590bf6f
BLAKE2b-256 57438b89b8f3c4a8885c9c5a0ff19aa02205fdfc97db637f4631abeed2dd9eef

See more details on using hashes here.

Provenance

The following attestation bundles were made for appysetty-0.1.0.tar.gz:

Publisher: publish.yml on SmartFactory-KL/appysetty

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file appysetty-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: appysetty-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for appysetty-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e4e45d56ccdc2e4772a770a6f1f4833cefb912ff61213b60a870ae9ba0039b3
MD5 a7f04eb00a0b5011e5216c431ea38d66
BLAKE2b-256 8e588dfacfd71e9dd2967fffe4c591b5189fe3f297959f0960206788b88c4f8b

See more details on using hashes here.

Provenance

The following attestation bundles were made for appysetty-0.1.0-py3-none-any.whl:

Publisher: publish.yml on SmartFactory-KL/appysetty

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page