ConfigLoader is a Python 3.8 package built to make loading and creating configurations a simpler process.
pip install git+https://github.com/NotToDisturb/ConfigLoader.git#egg=ConfigLoader
ConfigLoader
ConfigLoader.validate_nothing
ConfigLoader.validate_not_empty
ConfigLoader.validate_folder
ConfigLoader.validate_file
ConfigLoader.validate_json_file
There are three possible outcomes:
- If the config file doesn't exist, create it using the keys in
validators
and stop the script- If the config file exists but it doesn't pass all the validators, print the validators that fail and stop the script
- If the config file exists and it passes all the validators, create an instance of ConfigLoader, which inherits from
dict
In outcomes 2 and 3, if any unexpected keys are found they will be pointed out but execution won't be interrupted
Ensures that the
key
exists in the config
Validates if the
value
ofkey
is not""
Validates if the
value
ofkey
is a path that exists and points to a folder
Validates if the
value
ofkey
is a path that exists and points to a file
Validates if the
value
ofkey
is a path that exists and points to a correct JSON file
Here is an example of how to use ConfigLoader:
from configloader import ConfigLoader
CONFIG_JSON = "config.json"
validators = {
"hello_world_txt": ConfigLoader.validate_file,
"content": ConfigLoader.validate_not_empty
}
config = ConfigLoader(CONFIG_JSON. validators)
print(config["hello_world_txt"])
The first time this script is run, it will exit after generating config.json
(the path indicated in CONFIG_JSON
).
The generated file will contain all the keys from validators
with ""
assigned to them. Subsequent runs will continue exiting until the configuration file is filled out so that all the validators
pass.