Skip to main content

dwgriffin-config

Lightweight Python library for merging configuration settings from multiple sources into a single settings object.

ConfigManager merges settings from the following, lowest to highest priority.

  1. Defaults. A provided dict or object with attributes
  2. Configuration file using INI format
  3. Environment variables
  4. CLI arguments

Settings defined in "defaults" determine which settings are recognized and merged. ConfigManager has no settings of its own. Settings read from other sources will also be type cast to match the default source for bool, int, float, list, and tuple.

Installation

python3 -m pip install dwgriffin-config

Use

import os
from argparse import Namespace
from pathlib import Path
from tempfile import NamedTemporaryFile

from dwgriffin_config import ConfigManager

# Default config settings and values.
defaults = {
    "timeout": 300,
    "flags": False,
    "host": "example.com",
    "retries": 4,
}

# Config file
with NamedTemporaryFile(mode="w", suffix=".ini", delete=False) as file:
    file.write(
        "[DEFAULT]\n"
        "timeout = 500\n"
        "host = config-example.com\n"
        "retries = 2\n"
    )
config_path = Path(file.name)

# Environment modules
os.environ["APP_TIMEOUT"] = "700"

# ClI args
args = Namespace(host="cli-example.com", timeout=None, flags=None)

config = ConfigManager(
    defaults=defaults,
    config_file = config_path,
    env_prefix = "APP_",
    cli_args = args,
)
for key in ("timeout", "flags", "hostname", "retries"):
    print(f"{key:10} = {config.get(key)!r:30} (from {config.source(key)})")

Produces:

timeout    = 700                            (from env)
flags      = False                          (from default)
host       = 'cli-example.com'              (from cli)
retries    = 2                              (from config)

API

ConfigManager(
    defaults,
    config_file=None,
    ini_section=None,
    env_prefix=None,
    cli_args=None,
    required=None,
)
Argument Type Description
defaults dict[str, Any] Defines known settings and their default values and types.
config_file str | Path | None Path to optional INI configuration file.
ini_section str | None Optional INI section to read from. If None, include all sections
env_prefix str | None Prefix for Environment variables. e.g. APP_ maps timeout to APP_TIMEOUT.
cli_args argparse.Namespace | dict[str, Any] | None Parsed CLI arguments.
required list[str] | None Optional list of required keys.

Methods and access patterns

  • Dot access — config.key returns the resolved value, or raises AttributeError if the key isn't known.
  • config.get(key) — returns the resolved value, or None if unknown.
  • config.all() — returns a dict copy of every resolved setting.
  • config.source(key) — returns where a setting's value came from: cli, env, config, default, or none.

License

GNU General Public License v3.0 or later

Metadata

Release files for dwgriffin-config 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dwgriffin-config 0.1.6
File Size Uploaded Provenance Actions
dwgriffin_config-0.1.6.tar.gz 43.8 kB Without provenance Details for dwgriffin_config-0.1.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dwgriffin-config 0.1.6
File Interpreter ABI Platform Actions
dwgriffin_config-0.1.6-py3-none-any.whl Python 3 none any Details for dwgriffin_config-0.1.6-py3-none-any.whl

Total release size: 74.6 kB

Release files / dwgriffin_config-0.1.6.tar.gz

Download URL dwgriffin_config-0.1.6.tar.gz
Size 43.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b2047b1810a2f97ff47370c017e86ff0f29ae861fd50aa8e4006b367f83584bf
BLAKE2b-256 checksum
How to use checksums
edd0cfe2d798b9c4665dabe1bd91565b727550a7d35fafb944c62087c4553546
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.13

Release files / dwgriffin_config-0.1.6-py3-none-any.whl

Download URL dwgriffin_config-0.1.6-py3-none-any.whl
Size 30.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5b91aed70b6f4699edad60244ed3cd05a739dcff0d392c3ebd860da16d345cf2
BLAKE2b-256 checksum
How to use checksums
b405b34deb1bc999a2ca1d1ab24122ffae8e5bb30fe6b41d4cf3c9264f589198
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.13

Security for dwgriffin-config 0.1.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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