Application resource configuration library for Python
Project description
resconfig
resconfig is a minimalistic application configuration library for
Python. It is essentially a thin wrapper around nested dict
s and
can:
-
Read from multiple configuration file formats: INI, JSON, TOML, and YAML.
-
Dynamically reload configuration at run time: Watch functions can be attached to any keys within the configuration, so that separate resources can be reloaded and managed.
-
Access nested configuration item with a
.
-delimited string key: The underlying configuration data structure is nested dicts, but no need to manually traverse or use the verboseconfig["foo"]["bar"]
form (can useconfig["foo.bar"]
instead). -
Apply schema (experimental): Type casting can be performed upon loading configuration from files.
Installation
$ pip install resconfig
Basic Usage
from resconfig import ResConfig
config = ResConfig() # create empty config
config.load_from_yaml("pg.yaml") # load config from YAML file
dbname = config.get("pg.dbname") # get value at config["pg"]["dbname"]
config = ResConfig({"pg": {"dbname": "foo"}}) # with default config
config = ResConfig({"pg.dbname": "foo"}}) # this also works
Watching for Configuration Changes
The ResConfig
object is aware of changes to its
configuration. Watch functions can be registered to watch changes
happening at any nested key to act on them. For example,
import signal
from resconfig import Action, ResConfig
config = ResConfig(skip_reload_on_init=True) # do not immediately load config
@config.watch("nested.key")
def act_on_nested_key(action, old, new):
if action == Action.ADDED:
# ... do something when the new value is added to nested.key ...
elif action == Action.MODIFIED:
# ... do something when the value at nested.key is modified ...
elif action == Action.RELOADED:
# ... do something when the value at nested.key is reloaded ...
elif action == Action.REMOVED:
# ... do something when the value at nested.key is removed ...
def reload(signum=None, stack_frame=None):
config.load_from_yaml("myconf.yml")
signal.signal(signal.SIGHUP, reload) # run reload on receiving SIGHUP signal
reload() # ready to do the initial config loading
Here, the act_on_nested_key
function is invoked whenever an action
occurs at the nested.key of the configuration and can decide what to
do with the old
and/or new
values. In this code, the configuration
reload function is also a signal handler for SIGHUP
and is triggered
on the process receiving the signal.
The same watch function above can be registered in a couple more different ways, via the method
config.register("nested.key", act_on_nested_key)
or as an argument to ResConfig
config = ResConfig(watchers={"nested.key": act_on_nested_key})
Development
$ pip install .[dev]
$ pre-commit install
Running Tests
$ python setup.py test
License
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 resconfig-20.4.0a0.tar.gz
.
File metadata
- Download URL: resconfig-20.4.0a0.tar.gz
- Upload date:
- Size: 15.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4daaa2f63bf16890656c244bacff6c2b8936462ffc5ee92e4685a451630e9ca4 |
|
MD5 | 35111548a7e08037b5209bfed3ed4b84 |
|
BLAKE2b-256 | 51e5c3dccd25e9f75121ede962ea9f1a5dff727de0329d56aa7f0cfcf788c51b |
File details
Details for the file resconfig-20.4.0a0-py3-none-any.whl
.
File metadata
- Download URL: resconfig-20.4.0a0-py3-none-any.whl
- Upload date:
- Size: 16.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1947ade6383c64d017447907fd4d6a511c331025b852e543b6c5ec03be9e530a |
|
MD5 | 6d198f8921f2f862bb0cf83f1f6f1784 |
|
BLAKE2b-256 | 0f8adb580d06f141690ae7854529521c4a15cd657706524572b653c921419e16 |