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

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

Built distribution (wheel)

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

Total release size: 74.6 kB

Release files / dwgriffin_config-0.1.3.tar.gz

Download URL dwgriffin_config-0.1.3.tar.gz
Size 43.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2c2e0720d689ea9bc18d3023c3571d8dc443d0ef22e6eb288b30484f3e1ba0b1
BLAKE2b-256 checksum
How to use checksums
b30b5fc788ea4f3b71521184694657096bf8204093f3cbf40ed5214c35962db9
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.3-py3-none-any.whl

Download URL dwgriffin_config-0.1.3-py3-none-any.whl
Size 30.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
98bb464be3e98735f1cf648a721c89c6773f4b8cc7e9f99fb3561e0ec7db4d96
BLAKE2b-256 checksum
How to use checksums
c535b0ba4df92e432561a6d7bb6727bda624c00b0e04f68267db920b58142575
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.3

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