Skip to main content

generalize-config

PyPI PyPI - Python Version GitHub

Read and generalize the configuration

Overview

A list of variables required for a program's execution environment can be obtained in several ways:

  • Environment Variables
  • Configuration files
  • Command-line arguments
  • etc ...

Especially if you're deploying as a container, you need to reference a file like /run/secrets/... (docker swarm secret) to read the variable.

I developed the generalize-config library because I needed a way to unify environment variables from multiple places into one.

Features

Installation

pip install generalize-config

Usage

Configuration file

Reading extension-based configuration files

envs:
  host: localhost
  port: 8080
from argparse import Namespace
from generalize_config import read_config_file

config = read_config_file("/path/config/file.yml", "envs", encoding="utf-8")
assert isinstance(config, Namespace)
assert "localhost" == config.host
assert 8080 == config.port

The supported extension types are:

  • YAML extensions: .yml, .yaml
  • JSON extensions: .json
  • CFG extensions: .cfg, .ini

⚠️ When using a CFG file, there must be 1 subsection argument.

Environment variable

Environment Variable Filtering

from argparse import Namespace
from generalize_config import read_os_envs

# APP_HTTP_HOST_VALUE=localhost
config = read_os_envs(prefix="APP_", suffix="_VALUE")
assert isinstance(config, Namespace)
assert "localhost" == config.http_host

Filter environment variables and read files:

from argparse import Namespace
from generalize_config import read_os_envs_file

# APP_DATABASE_PASSWORD_FILE=/run/secrets/password
config = read_os_envs_file(prefix="APP_", suffix="_FILE")
assert isinstance(config, Namespace)
assert isinstance(config.database_password, str)

Merge namespaces

from argparse import Namespace, ArgumentParser
from generalize_config import merge_left_first

parser = ArgumentParser()
# add_argument ...
args = parser.parse_known_args()[0]

result = Namespace()
merge_left_first(result, args, ...)
print(result)

Generalized method

The recommended reading order is:

  1. Fixed variable (higher priority)
  2. Command line arguments
  3. Configuration file received as a command line argument
  4. Environment Variables
  5. Configuration file received as environment variable
  6. Environment variable pointing to file
  7. Default variable (low priority)

The function that implements the above is read_generalize_configs:

from argparse import ArgumentParser, Namespace
from generalize_config import read_generalize_configs

parser = ArgumentParser()
# add_argument ...

default_args = Namespace(...)

result = read_generalize_configs(
    parser=parser,
    subsection="application",
    env_prefix="ENV_",
    env_suffix="_FILE",
    config_key="config",
    default=default_args,
)
print(result)

Things to know

When using argparse.ArgumentParser you need to make sure that all values not entered are returned as None. Otherwise, the merge_left_first function may malfunction.

Also, variables acquired through CFG files and environment variables are fixed as string type. To solve this you need to deserialize like this:

Install type-serialize:

pip install type-serialize

Add type annotation to Namespace and then call deserialize:

from argparse import Namespace
from generalize_config import read_generalize_configs
from type_serialize import deserialize

class Config(Namespace):
    host: str
    port: int
    # ...

ns = read_generalize_configs(...)
config = deserialize(ns, Config)
assert isinstance(config, Config)

print(config)

License

See the LICENSE file for details. In summary, generalize-config is licensed under the MIT license.

Metadata

Release files for generalize-config 1.3.0

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

Built distribution (wheel)

Table of built distributions (wheels) for generalize-config 1.3.0
File Interpreter ABI Platform
generalize_config-1.3.0-py3-none-any.whl Python 3 none any Details

Release files / generalize_config-1.3.0-py3-none-any.whl

Download URL generalize_config-1.3.0-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f26ac2ecf7b31e213b9fdaf39b878230a7bed999ae4f4d3dbf93f2d8e883e46e
BLAKE2b-256 checksum
How to use checksums
79eba0e99df1854e78bd80ce3650a0733bd90fb049debc64f35ae64e9fc17503
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.11

Security for generalize-config 1.3.0

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.

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