Simple layered configuraion tool
Project description
Compendium
Overview
Compendium is a layered configuration management tool. It has the capability to manage configuration files writen in JSON, TOML, XML and YAML. Settings from these configuration files can then be managed easily with the help of dpath.
Documentation
https://kuwv.github.io/python-compendium/
Install
pip install compendium
Manage a configuration file
>>> import os
>>> from compendium.loader import ConfigFile
>>> basepath = os.path.join(os.getcwd(), 'tests')
>>> filepath = os.path.join(basepath, 'config.toml')
>>> cfg = ConfigFile()
>>> settings = cfg.load(filepath=filepath)
# Simple lookup for title
>>> settings['/title']
'TOML Example'
# Query values within list
>>> settings.values('/servers/**/ip')
['10.0.0.1', '10.0.0.2']
# Update setting
>>> settings['/database/server']
'192.168.1.1'
>>> settings['/database/server'] = '192.168.1.2'
>>> settings['/database/server']
'192.168.1.2'
# Check the database max connections
>>> settings['/database/connection_max']
5000
# Delete the max connections
>>> del settings['/database/connection_max']
# Check that the max connections have been removed
>>> settings.get('/database/connection_max')
Manage multiple layered configurations
The ConfigManager
is a layered dictionary mapping. It allows multiple
configurations to be loaded from various files. Settings from each file
is overlapped in order so that the first setting found will be used.
>>> from tempfile import NamedTemporaryFile
>>> from textwrap import dedent
>>> from compendium.config_manager import ConfigManager
>>> try:
... # Create first mock config file
... file1 = NamedTemporaryFile(mode='wt', suffix='.toml')
... _ = file1.write(
... dedent(
... """\
... [default]
... foo = "bar"
... foo2 = "bar2"
... """
... )
... )
... _ = file1.seek(0)
...
... # Create first mock config file
... file2 = NamedTemporaryFile(mode='wt', suffix='.toml')
... _ = file2.write(
... dedent(
... """\
... [example.settings]
... foo = "baz"
... """
... )
... )
... _ = file2.seek(0)
...
... # Retrieve settings from config files
... cfg = ConfigManager(name='app', filepaths=[file1.name, file2.name])
...
... # Get using dpath
... cfg.get('/default/foo2')
...
... # Lookup with multi-query
... cfg.lookup('/example/settings/foo', '/default/foo')
...
... finally:
... file1.close()
... file2.close()
'bar2'
'baz'
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
compendium-0.1.2.post5.tar.gz
(17.9 kB
view hashes)
Built Distribution
Close
Hashes for compendium-0.1.2.post5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 846160d3192fbcf562dc0ec6147be37bacc45fffb9bcccbd98552ebd0302f212 |
|
MD5 | 68cd5e283e19a3041dc90ec321c254e6 |
|
BLAKE2b-256 | 7612279a7a77fac344844ba19c8ab15f33f816f875c4ebd2d725310e562d9893 |