Skip to main content

Layered configuration loader merging env vars, files, and defaults

Project description

philiprehberger-config-kit

Tests PyPI version Last updated

Layered configuration loader merging env vars, files, and defaults.

Installation

pip install philiprehberger-config-kit

Usage

Basic Setup

from philiprehberger_config_kit import Config

config = Config(
    sources=[
        Config.defaults({"port": 3000, "debug": False, "log_level": "info"}),
        Config.json_file("config.json", optional=True),
        Config.env_file(".env", optional=True),
        Config.env(prefix="APP_"),
    ]
)

# Typed access
port = config.get_int("port")
debug = config.get_bool("debug")
db_url = config.get_str("database_url")
timeout = config.get_float("timeout", default=5.0)
hosts = config.get_list("allowed_hosts")

# Nested access (dot notation)
redis_host = config.get_str("redis.host")

# Validation
config.require("database_url", "secret_key")  # raises ConfigError if missing

# Check existence
if config.has("cache_ttl"):
    ttl = config.get_int("cache_ttl")

Source Priority

Sources are applied in order -- later sources override earlier ones:

Config(sources=[
    Config.defaults({...}),       # lowest priority
    Config.json_file("..."),      # overrides defaults
    Config.env_file(".env"),      # overrides JSON
    Config.env(prefix="APP_"),    # highest priority
])

Typed List Getters

Parse comma-separated values into typed lists:

config = Config(sources=[
    Config.defaults({"ports": "8080,8081,8082", "rates": "1.5,2.0,3.7"}),
])

ports = config.get_int_list("ports")      # [8080, 8081, 8082]
rates = config.get_float_list("rates")    # [1.5, 2.0, 3.7]

# Custom separator
config2 = Config(sources=[Config.defaults({"ids": "1|2|3"})])
config2.get_int_list("ids", sep="|")      # [1, 2, 3]

Config Flattening

Export nested config as a flat dictionary with dot-notation keys:

config = Config(sources=[
    Config.defaults({"db": {"host": "localhost", "port": 5432}, "debug": True}),
])

flat = config.flatten()
# {"db.host": "localhost", "db.port": "5432", "debug": "True"}

# With a prefix
flat = config.flatten(prefix="app")
# {"app.db.host": "localhost", "app.db.port": "5432", "app.debug": "True"}

Environment Variables

With prefix="APP_", env vars are mapped:

  • APP_PORT -> port
  • APP_DATABASE__HOST -> database.host (double underscore = nested)

.env Files

DATABASE_URL=postgresql://localhost/mydb
SECRET_KEY="my-secret"
DEBUG=true

Bool Coercion

get_bool() accepts: true/false, 1/0, yes/no, on/off

API

Function / Class Description
Config(sources) Layered configuration with typed access via get(), get_str(), get_int(), get_float(), get_bool(), get_list()
Config.get_int_list(key, sep) Split a string value and convert each element to int
Config.get_float_list(key, sep) Split a string value and convert each element to float
Config.flatten(prefix) Export nested config as flat dict with dot-notation keys
ConfigError(missing) Raised when required config keys are missing

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

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

philiprehberger_config_kit-0.2.1.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_config_kit-0.2.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_config_kit-0.2.1.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_config_kit-0.2.1.tar.gz
Algorithm Hash digest
SHA256 c0e0a88b4575a39535eacd761a44d77ad5216b0df225fe464ff456ca21594cd6
MD5 5f2a4fa97e8ad8b762201c7904804487
BLAKE2b-256 c08499e4ab3bf62544fe89baec9b88440514bd2d9f53587559b4511120c6affc

See more details on using hashes here.

File details

Details for the file philiprehberger_config_kit-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_config_kit-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 99d3120bbe2436a1c1a1926f39e0d57b789ee7f1734043283229bd7a604f7b6a
MD5 d07caf47bcd3d83a0b7fa34135708064
BLAKE2b-256 62855f0534d79d6f49856686caaa1930b57ebeff6adaa6add32d1940576eda2a

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