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.
Metadata
Release files for WrapConfig 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| wrapconfig-0.2.2.tar.gz | 11.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| WrapConfig-0.2.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.3 kB
Release files / wrapconfig-0.2.2.tar.gz
| Download URL | wrapconfig-0.2.2.tar.gz |
|---|---|
| Size | 11.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0c4b945c91cd1c9c5d0c5c4cfc904efe3569de8089033db5ba383724f8658e94
|
|
BLAKE2b-256 checksum How to use checksums |
37990ee5483d8965239dfb333244448a169306b9b4154cf7f48855ee1340c68e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 5, 2025.
Transparency logRelease files / WrapConfig-0.2.2-py3-none-any.whl
| Download URL | WrapConfig-0.2.2-py3-none-any.whl |
|---|---|
| Size | 10.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fd00d5c0bedc177efbecccc948c29c3a6033c22e1652bfee8933507807716077
|
|
BLAKE2b-256 checksum How to use checksums |
573e1fcb850737b183006286c5150ae2de567f7700252849dea8034c6ee08e8e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 5, 2025.
Transparency log