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.5

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.5
File Size Uploaded Provenance Actions
dwgriffin_config-0.1.5.tar.gz 43.8 kB Without provenance Details for dwgriffin_config-0.1.5.tar.gz

Built distribution (wheel)

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

Total release size: 74.6 kB

Release files / dwgriffin_config-0.1.5.tar.gz

Download URL dwgriffin_config-0.1.5.tar.gz
Size 43.8 kB
Tags Source
SHA-256 checksum
How to use checksums
034d92b1cba4785729992ab49045853e9ccdf8d956a1e763b2423367978a7562
BLAKE2b-256 checksum
How to use checksums
9597912f0f5d2bc75071da676504bb38de7cb564365cb16e23a1f3243432612a
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.5-py3-none-any.whl

Download URL dwgriffin_config-0.1.5-py3-none-any.whl
Size 30.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6b969163c21735572393d745855f77c95b4122ec989411296d42dbe4efb23a83
BLAKE2b-256 checksum
How to use checksums
949c1b8982edd5398d56d997b2a3c4f9781b2547a34ac4146e639636da32f2f4
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.5

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