Read and generalize the configuration
Project description
generalize-config
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
- All results are returned as argparse.Namespace.
- json, yaml, cfg files are supported.
- argparse.Namespace merge.
- Environment variable filtering and file reader.
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
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)
License
See the LICENSE file for details. In summary, generalize-config is licensed under the MIT license.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for generalize_config-1.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8cb855b75e14b5b0706c1cc8ff3a176ebffceb226db9ebdae9f88cd78ca139aa |
|
MD5 | eae0516aeaff48441abcb33d15f975d3 |
|
BLAKE2b-256 | 73f4f9ebfc95363a13f66335347e67f330cea38b4c0e1d0077a86cd8b4eb4a9b |