A configuration management library
Project description
CatenaConf
Introduction
CatenaConf is a lightweight Python library designed for managing and operating configurations. It extends the Python dictionary type to manage configurations using key-value pairs and provides flexible operation functionalities.
Features
- Lightweight: Depends only on the Python standard library, without any third-party dependencies.
- Based on Python dictionaries to create configurations.
- Access and modify configuration values via attributes.
- Flexible update and merge mechanisms.
- Ability to reference other configuration values within configuration values.
Installation
Install using pip:
pip install catenaconf
Usage
Creating Configuration
from catenaconf import Catenaconf
config = {
"database": {
"host": "localhost",
"port": 3306
}
}
cfg = Catenaconf.create(config)
- Use the
Catenaconf.create
method to create a configuration from a dictionary. - The method returns a
KvConfig
instance.
Updating Configuration
Catenaconf.update(cfg, "database.user", "root") # Adds a user key-value pair in database
Catenaconf.update(cfg, "database", {"root": "root"}) # Replaces the value of database with {"root": "root"}
Catenaconf.update(cfg, "database", {"root": "root"}, merge=True) # Adds a root key-value pair in database
- Use the
Catenaconf.update
method to update the configuration. - The first parameter is the
KvConfig
instance to be updated, the second parameter specifies the location of the value to be updated, the third parameter is the new value, and the fourth parameter (merge) indicates whether to merge or not. - merge parameter: Defaults to True. When set to True, if both the new value and the existing value are dictionaries, they will be merged instead of replacing the existing value. When set to False, the new value will directly replace the existing value.
Merging Configurations
config1 = {"database": {"host": "localhost"}}
config2 = {"database": {"port": 3306}}
merged_cfg = Catenaconf.merge(config1, config2)
- Use the
Catenaconf.merge
method to merge multiple configurations. - Returns a merged
KvConfig
instance.
References and Resolving References
config = {
"info": {
"path": "/data",
"filename": "a.txt"
},
"backup_path": "@{info.path}/backup/@{info.filename}"
}
cfg = Catenaconf.create(config)
Catenaconf.resolve(cfg)
- Use the
@{}
format to reference other configuration values. - Use the
Catenaconf.resolve
method to resolve references within the configuration.
Converting to Dictionary
dict_config = Catenaconf.to_container(cfg, resolve=True)
dict_config = Catenaconf.to_container(cfg, resolve=False) # References within will not be resolved
- Use the
Catenaconf.to_container
method to convert aKvConfig
instance into a regular dictionary. - resolve parameter: Defaults to True. When set to True, internal references are resolved. When set to False, internal references are not resolved.
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
catenaconf-0.1.1.tar.gz
(10.5 kB
view details)
Built Distribution
File details
Details for the file catenaconf-0.1.1.tar.gz
.
File metadata
- Download URL: catenaconf-0.1.1.tar.gz
- Upload date:
- Size: 10.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
448c46984b172524fdbbbf995654ac3346691252dc0ff6bd0fe9da70e484a072
|
|
MD5 |
5e7debb91cc0a3c1f7bb6f3916a746b0
|
|
BLAKE2b-256 |
ce2b92218952a65ac08c932b1b38edcbeb5a1e08bf681ad9cc49be448d9d18a1
|
File details
Details for the file catenaconf-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: catenaconf-0.1.1-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
54a8c9ca7094d3b6be6b2b06e24ef3a40b9d461a74896603a9b8027f49e11eae
|
|
MD5 |
5067a2d14953e03164a8cee2929e68cb
|
|
BLAKE2b-256 |
b2480002e0c5151e1648048249548a517d8da2364c9d354f88aaf8b074a5f006
|