Configurationlib
![Python Tests](https://github.com/kokofixcomputers/configurationlib/actions/workflows/python-tests.yaml/badge.svg)
A simple configuration manager for Python that allows you to easily manage nested configurations using JSON files.
Features
- Load and save configurations from/to a JSON, YAML or .env file.
- Create nested dictionaries dynamically.
- Retrieve configuration values easily.
Installation
You can install the package via pip:
pip install configurationlib
Note
If the file specified does not exist, it will be created.
Usage
[!WARNING]
Please do note, if you specify the wrong file with the wrong format, it may load incorrectly or may break your file.
Here is a simple example of the usage of this module:
import configurationlib
config = configurationlib.Instance(file="config.json")
config.save()["dic1"] = {}
config.save()["dic1"]["afewmoredic"] = {}
config.save()["dic1"]["afewmoredic"]["key"] = "value"
config.save()['weird'] = True
retrieved_value = config.get()["dic1"]["afewmoredic"]["key"]
print(retrieved_value)
config.save()
Changing formats
If you want, You can change the format of the saved file (YAML, JSON, dotENV) the default already is JSON so if you want json, you don't need to do anything.
Here is how you can change it to YAML:
import configurationlib
config = configurationlib.Instance(file="config.json", format=configurationlib.Format.YAML)
config.save()["dic1"] = {}
config.save()["dic1"]["afewmoredic"] = {}
config.save()["dic1"]["afewmoredic"]["key"] = "value"
retrieved_value = config.get()["dic1"]["afewmoredic"]["key"]
print(retrieved_value)
config.save()
The only line that changes is config = configurationlib.Instance(file="config.json", format=configurationlib.Format.YAML
nothing else changes. It will automatically save the data into the data you'd like! If you remove format argument, it will default to JSON
.
Hot Reloading
[!NOTE]
Hot reloading is disabled by default
If you want to enable hot reloading, Use this:
config = configurationlib.Instance(file="config.json", format=configurationlib.Format.YAML, hot_reloading=True)
Hot reloading will check the file for changes and update the variable.