With new empty: block if just like this set to None if not set (empty), can also be any value, if list, skip setting default
112 lines
3.3 KiB
Python
112 lines
3.3 KiB
Python
"""
|
|
Settings loader test
|
|
"""
|
|
|
|
from pathlib import Path
|
|
from corelibs.iterator_handling.dump_data import dump_data
|
|
from corelibs.logging_handling.log import Log
|
|
from corelibs.config_handling.settings_loader import SettingsLoader
|
|
|
|
SCRIPT_PATH: Path = Path(__file__).resolve().parent
|
|
ROOT_PATH: Path = SCRIPT_PATH
|
|
CONFIG_DIR: Path = Path("config")
|
|
CONFIG_FILE: str = "settings.ini"
|
|
|
|
|
|
def main():
|
|
"""
|
|
Main run
|
|
"""
|
|
|
|
# for log testing
|
|
script_path: Path = Path(__file__).resolve().parent
|
|
log = Log(
|
|
log_path=script_path.joinpath('log', 'settings_loader.log'),
|
|
log_name="Settings Loader",
|
|
log_settings={
|
|
"log_level_console": 'DEBUG',
|
|
"log_level_file": 'DEBUG',
|
|
}
|
|
)
|
|
log.logger.info('Settings loader')
|
|
|
|
sl = SettingsLoader(
|
|
{
|
|
'foo': 'OVERLOAD'
|
|
},
|
|
ROOT_PATH.joinpath(CONFIG_DIR, CONFIG_FILE),
|
|
log=log
|
|
)
|
|
try:
|
|
config_load = 'TestA'
|
|
config_data = sl.load_settings(
|
|
config_load,
|
|
{
|
|
# "doesnt": ["split:,"],
|
|
"foo": ["mandatory:yes"],
|
|
"foobar": ["check:int"],
|
|
"bar": ["mandatory:yes"],
|
|
"some_match": ["matching:foo|bar"],
|
|
"some_match_list": ["split:,", "matching:foo|bar"],
|
|
"test_list": [
|
|
"check:string.alphanumeric",
|
|
"split:,"
|
|
],
|
|
"other_list": ["split:|"],
|
|
"third_list": [
|
|
"split:|",
|
|
"check:string.alphanumeric"
|
|
],
|
|
"str_length": [
|
|
"length:2-10"
|
|
],
|
|
"int_range": [
|
|
"range:2-50"
|
|
],
|
|
"int_range_not_set": [
|
|
"range:2-50"
|
|
],
|
|
"int_range_not_set_empty_set": [
|
|
"empty:"
|
|
],
|
|
"match_target": ["matching:foo"],
|
|
"match_target_list": ["split:,", "matching:foo|bar|baz",],
|
|
"match_source_a": ["in:match_target"],
|
|
"match_source_b": ["in:match_target_list"],
|
|
"match_source_list": ["split:,", "in:match_target_list"],
|
|
}
|
|
)
|
|
print(f"[{config_load}] Load: {config_load} -> {dump_data(config_data)}")
|
|
except ValueError as e:
|
|
print(f"Could not load settings: {e}")
|
|
|
|
try:
|
|
config_load = 'TestB'
|
|
config_data = sl.load_settings(
|
|
config_load,
|
|
{
|
|
"email": [
|
|
"split:,",
|
|
"mandatory:yes",
|
|
"check:string.email.basic"
|
|
],
|
|
"email_not_mandatory": [
|
|
"split:,",
|
|
# "mandatory:yes",
|
|
"check:string.email.basic"
|
|
],
|
|
"email_bad": [
|
|
"split:,",
|
|
"mandatory:yes",
|
|
"check:string.email.basic"
|
|
]
|
|
}
|
|
)
|
|
print(f"[{config_load}] Load: {config_load} -> {dump_data(config_data)}")
|
|
except ValueError as e:
|
|
print(f"Could not load settings: {e}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|