This is a very basic class without many helper functions added yet Add to the CoreLibs so when we develop it further it can be used by all projects
126 lines
3.8 KiB
Python
126 lines
3.8 KiB
Python
"""
|
|
Settings loader test
|
|
"""
|
|
|
|
import re
|
|
from pathlib import Path
|
|
from corelibs.debug_handling.dump_data import dump_data
|
|
from corelibs.logging_handling.log import Log
|
|
from corelibs.config_handling.settings_loader import SettingsLoader
|
|
from corelibs.config_handling.settings_loader_handling.settings_loader_check import SettingsLoaderCheck
|
|
|
|
SCRIPT_PATH: Path = Path(__file__).resolve().parent
|
|
ROOT_PATH: Path = SCRIPT_PATH
|
|
CONFIG_DIR: Path = Path("config")
|
|
LOG_DIR: Path = Path("log")
|
|
CONFIG_FILE: str = "settings.ini"
|
|
|
|
|
|
def main():
|
|
"""
|
|
Main run
|
|
"""
|
|
|
|
value = "2025/1/1"
|
|
regex_c = re.compile(SettingsLoaderCheck.CHECK_SETTINGS['string.date']['regex'], re.VERBOSE)
|
|
result = regex_c.search(value)
|
|
print(f"regex {regex_c} check against {value} -> {result}")
|
|
|
|
# for log testing
|
|
log = Log(
|
|
log_path=ROOT_PATH.joinpath(LOG_DIR, '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}")
|
|
|
|
try:
|
|
config_load = 'LoadTest'
|
|
config_data = sl.load_settings(config_load)
|
|
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()
|