Wraper to manage configurations
Project description
WrapConfig
WrapConfig is a lightweight and flexible Python configuration management library that simplifies the handling of configuration data. It provides a consistent API to manage settings in various formats, including JSON, YAML, and TOML, as well as in-memory configurations.
Note: YAML and TOML support are optional. Install the extras if you need them.
Features
- Simple API: Easily set, get, update, fill, and clear configuration values.
- Multiple Formats: Built-in support for JSON file-based configurations with optional YAML and TOML.
- Nested Configurations: Manage complex, nested configuration structures effortlessly.
- In-Memory Configurations: Use configurations without persisting data to disk.
- Sub-Configurations: Access and manipulate subsections of your configuration data via a dedicated
SubConfigclass.
Installation
You can install WrapConfig using pip:
pip install WrapConfig
For optional YAML or TOML support, install with the corresponding extras:
pip install WrapConfig[yaml]
pip install WrapConfig[toml]
To install all optional features:
pip install WrapConfig[all]
Usage
Basic Example with JSON Configuration
Below is an example of how to use WrapConfig with a JSON configuration file:
from wrapconfig import JSONWrapConfig
# Initialize the configuration manager with a JSON file path
config = JSONWrapConfig("config.json")
# Set configuration values using nested keys
config.set("database", "host", value="localhost")
config.set("database", "port", value=5432)
# Retrieve configuration values
db_host = config.get("database", "host")
db_port = config.get("database", "port")
print(f"Database Host: {db_host}, Port: {db_port}")
# Update configuration with multiple values at once
config.update({
"logging": {
"level": "DEBUG",
"file": "app.log"
}
})
# Save changes (if auto-save is disabled, you can manually call save)
config.save()
In-Memory Configuration
If you prefer not to persist configuration data to a file, you can use the in-memory configuration:
from wrapconfig import InMemoryConfig
# Create an in-memory configuration instance
config = InMemoryConfig()
# Set and save configuration values
config.set("feature_flag", value=True)
config.save()
# Clear current configuration and restore from backup
config.clear()
config.load()
print("Feature Flag:", config.get("feature_flag"))
Using YAML and TOML Configurations
After installing the optional dependencies, you can work with YAML or TOML configuration files.
YAML Example
from wrapconfig import YAMLWrapConfig
# Initialize the YAML configuration manager
config = YAMLWrapConfig("config.yaml")
# Set values and save to the YAML file
config.set("app", "name", value="MyApp")
config.save()
TOML Example
from wrapconfig import TOMLWrapConfig
# Initialize the TOML configuration manager
config = TOMLWrapConfig("config.toml")
# Set values and save to the TOML file
config.set("server", "host", value="127.0.0.1")
config.save()
API Overview
Core Class: WrapConfig
WrapConfig is the abstract base class providing the core functionality:
-
set(*keys, value, save=True)
Sets a configuration value using nested keys. Example: config.set("section", "subsection", value="my_value") If no value is provided as a keyword, the last key is assumed to be the value. In any case at least 2 arguments have to be passed. -
get(*keys, default=None)
Retrieves a configuration value using a nested key path. Returns default if the key does not exist. Example: value = config.get("section", "subsection", default="default_value")
-
update(data, save=True)
Deeply updates the configuration with new data, adding new keys or updating existing ones. -
fill(data, save=True)
Fills in missing configuration values without overwriting existing ones. -
clear(*keys)
Clears configuration values. Without keys, it clears the entire configuration.
data property
Returns a deep copy of the current configuration data.
Sub-Configuration: SubConfig
SubConfig represents a subsection of a parent configuration. It allows you to work with nested configuration sections without having to access the full path. It works as a view to the parents data, so changing values is also manipulating the parent data structure. Note that SubConfig delegates persistence to its parent and does not support direct loading.
Contributing
Contributions to WrapConfig are welcome! If you have suggestions, bug reports, or improvements, please follow these steps:
-
Fork the repository.
-
Create a feature branch:
git checkout -b feature/my-feature
-
Commit your changes:
git commit -am 'Add some feature'
-
Push to the branch:
git push origin feature/my-feature
-
Open a Pull Request explaining your changes.
License
WrapConfig is distributed under the MIT License. See the LICENSE file for details.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wrapconfig-0.2.2.tar.gz.
File metadata
- Download URL: wrapconfig-0.2.2.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c4b945c91cd1c9c5d0c5c4cfc904efe3569de8089033db5ba383724f8658e94
|
|
| MD5 |
205697e682cb2a77460ed4af173673c0
|
|
| BLAKE2b-256 |
37990ee5483d8965239dfb333244448a169306b9b4154cf7f48855ee1340c68e
|
Provenance
The following attestation bundles were made for wrapconfig-0.2.2.tar.gz:
Publisher:
version_publish_main.yml on JulianKimmig/WrapConfig
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
wrapconfig-0.2.2.tar.gz -
Subject digest:
0c4b945c91cd1c9c5d0c5c4cfc904efe3569de8089033db5ba383724f8658e94 - Sigstore transparency entry: 168925463
- Sigstore integration time:
-
Permalink:
JulianKimmig/WrapConfig@b7960ce1c7c0024f9a4d7ee74fa96b55e9766cdd -
Branch / Tag:
refs/heads/main - Owner: https://github.com/JulianKimmig
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
version_publish_main.yml@b7960ce1c7c0024f9a4d7ee74fa96b55e9766cdd -
Trigger Event:
push
-
Statement type:
File details
Details for the file WrapConfig-0.2.2-py3-none-any.whl.
File metadata
- Download URL: WrapConfig-0.2.2-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd00d5c0bedc177efbecccc948c29c3a6033c22e1652bfee8933507807716077
|
|
| MD5 |
91fc2d70cfa7a548056ba90fe65b4ea7
|
|
| BLAKE2b-256 |
573e1fcb850737b183006286c5150ae2de567f7700252849dea8034c6ee08e8e
|
Provenance
The following attestation bundles were made for WrapConfig-0.2.2-py3-none-any.whl:
Publisher:
version_publish_main.yml on JulianKimmig/WrapConfig
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
wrapconfig-0.2.2-py3-none-any.whl -
Subject digest:
fd00d5c0bedc177efbecccc948c29c3a6033c22e1652bfee8933507807716077 - Sigstore transparency entry: 168925465
- Sigstore integration time:
-
Permalink:
JulianKimmig/WrapConfig@b7960ce1c7c0024f9a4d7ee74fa96b55e9766cdd -
Branch / Tag:
refs/heads/main - Owner: https://github.com/JulianKimmig
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
version_publish_main.yml@b7960ce1c7c0024f9a4d7ee74fa96b55e9766cdd -
Trigger Event:
push
-
Statement type: