A small, minimalist application json configuration tool for small projects.
Uses only stdlib if that matters to you.
Table of Contents
Fair question.
I originally started this project as an excuse to use Hatch and write a pyproject.toml no-distutils/setuptools python project and see how green the grass was.
I've typically been unhappy with the configuration options for python in a way I can't articulate very well, and I just wanted a simple json file read with some guiderails. Some quick user-side discovery to find my platform's config path and get going.
This project isn't meant to be a fully featured configuration system to slap into your next big webservice, it's just a small system to slap on your next prototype or small project.
pip install keiconf
from keiconf import KeiConf
k = KeiConf(filepath="path/to/file.json")
k.get("your_key")
k.get("path.to.your.nested_key")
k.start() # start watching config file for changes
KeiConf(filepath, indent=2, fail_on_missing_key=False, create_if_not_exist=False, watch_for_changes=False)
filepath:str
- The path to your target configuration file
- ex.
/path/to/file.json
indent:int
- The json indentation to use for the written file
- ex.
2
fail_on_missing_keys:bool
- If
True
, will raise a KeyError if a key is missing, which you can catch and exit on. - If
False
, will return nothing
- If
create_if_not_exist:bool
- if
True
, will create the missing path directories and empty configuration file if it does not exist - if
False
will fail naturally on opening nonexistent configuration file
- if
watch_for_changes:bool
- if
True
will launch the config file watcher on init - if
False
will not launch the config file watcher on init - See Config File Watcher for more information
- if
If your KeiConf instance is started with watch_for_changes=True
then a threaded file watcher will load changes to your configuration file as it sees them (within keiconf._CHANGE_WATCH_INTERVAL).
If your KeiConf instance is started with watch_for_changes=False
then the threaded file watcher is not created automatically.
You can stop and start the file watcher manually via the start() and stop() functions. It can be start()'ed even if you launched with watch_for_changes=False
k = KeiConf(...)
k.start() # starts the file watcher if it isn't already
k.stop() # stops a running file watcher if it is running
The file watcher and loader is protected by a threading.Lock()
Development/Testing/Contribution requires hatch. Hatch provides the project management, environment abstraction, dependency resolution, and dev/test entrypoints.
python3 -m pip install hatch
although it is recommended to install hatch via pipx
python3 -m pip install pipx
python3 -m pipx ensurepath
python3 -m pipx install hatch
- Need to investigate behavior with lists
- Why not dot notation / attribute reference?
- A: I looked into this as I was interested in possibly doing it, but it looked rather lengthy to implement and involved modifying the base class quite a bit or using wrappers and I just wasn't sure that was in the spirit of minimalism. The get() function uses a string dot notation
get("path.to.key")
as a sort of middle ground. --- I may revisit in the future.
- A: I looked into this as I was interested in possibly doing it, but it looked rather lengthy to implement and involved modifying the base class quite a bit or using wrappers and I just wasn't sure that was in the spirit of minimalism. The get() function uses a string dot notation
keiconf
is distributed under the terms of the MIT license.