Module for defining a config schema and importing it from environment variables.
Project description
anenvconf
A python3 module for defining a config schema and importing it from environment variables.
Install
python3 -m pip install anenvconf
Usage
Import the modules key classes, define the config schema and create a Config object with the schema.
from anenvconf import Config, ConfigValueType
config_schema = {
'jsonvar1': {
'type': ConfigValueType.JSON,
'required_key': 'important_key'
},
'stringvar1': {},
'stringvar2': {
'default': "Default value in case setting this variable is not mandatory"
},
'boolvar1': {
'type': ConfigValueType.BOOL,
'default': True
}
}
config = None
try:
config = Config(config_schema)
except anenvconf.exceptions.InvalidValueException as e:
print("Got an invalid value: ", e.text)
except anenvconf.exceptions.EnvironmentVarMissingException as e:
print("A required environment variable is missing: ", e.text)
except anenvconf.exceptions.RequiredVarMissingException as e:
print("A required value inside a variable is missing: ", e.text)
if config is not None:
print(config.get_value("stringvar2"))
Schema
The schema is given as a dict, where the keys are the environment variables to read, and each of their values is an another dict that defines details about how to handle that variable. The dict that defines the handling can have the following keys:
type
is the data type of the variable value, defined as ConfigValueType
. Supported types are str
(default),
bool
, int
and json
.
default
is the default value of the variable. If default
is not set, the environment variable is treated
as mandatory. If you want to have optional variables, you can for example set default
to None
.
required_key
defines a key that is required in the keys of a json
type of value. If the key is not found, an exception is raised.
Example:
example_config_schema = {
# Use "type" to define ConfigValueType, string is assumed by default.
'env_variable_name1': {
'type': ConfigValueType.JSON
},
# Default data type is string
'env_variable_name2': {},
# If default value exists, value from environment variable is not required.
# Otherwise the key-value pair is expected to be found in the env variables.
'env_variable_name3': {
'type': ConfigValueType.JSON,
'default': '{}'
}
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file anenvconf-1.0.2.tar.gz
.
File metadata
- Download URL: anenvconf-1.0.2.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 09dfc6fa9c1b2ad41d4d1a46a1e1c48cad64cf1a62184cb7f5b0e49b822f24a6 |
|
MD5 | 455f704a08a7fa58ed9fea195e460c8e |
|
BLAKE2b-256 | ca27aca3945fb18f48d2a13cb7256b311e9ea8169e395de59b30360d9c600f88 |
File details
Details for the file anenvconf-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: anenvconf-1.0.2-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | da9dd79989b8b78416de74a813f3d2b05b628ff68f9f9cf08bc9904ff4c9456b |
|
MD5 | 223983053022c1062ce35ffc133bc841 |
|
BLAKE2b-256 | 6da71ccf48fb21699b023da3d2154fe4bcea17ccc534e6bece417726c7fef644 |