Compare commits

..

5 Commits

Author SHA1 Message Date
Clemens Schwaighofer
447034046e v0.12.3: settings loader error message improvement 2025-07-14 16:50:36 +09:00
Clemens Schwaighofer
0770ac0bb4 Better error handling in the settings loader for entry not found in block 2025-07-14 16:49:37 +09:00
Clemens Schwaighofer
aa2fbd4f70 v0.12.2: Fix mandatory for settings loader 2025-07-14 16:25:21 +09:00
Clemens Schwaighofer
58c8447531 Settings loader mandatory fixes
- mandatory empty check if empty list ([''])
- skip regex check if replace value is None -> allowed empty as empty if not mandatory
2025-07-14 16:23:55 +09:00
Clemens Schwaighofer
bcca43d774 v0.12.1: settings loader update, regex constants added 2025-07-14 16:01:54 +09:00
6 changed files with 47 additions and 7 deletions

View File

@@ -1,7 +1,7 @@
# MARK: Project info
[project]
name = "corelibs"
version = "0.12.0"
version = "0.12.3"
description = "Collection of utils for Python scripts"
readme = "README.md"
requires-python = ">=3.13"

View File

@@ -94,6 +94,13 @@ class SettingsLoader:
try:
# load all data as is, validation is done afterwards
settings[config_id] = dict(self.config_parser[config_id])
except KeyError as e:
self.__print(
f"[!] Cannot read [{config_id}] block in the {self.config_file}: {e}",
'CRITICAL', raise_exception=True
)
sys.exit(1)
try:
for key, checks in config_validate.items():
skip = True
split_char = self.DEFAULT_ELEMENT_SPLIT_CHAR
@@ -145,7 +152,7 @@ class SettingsLoader:
]
except KeyError as e:
self.__print(
f"[!] Cannot read [{config_id}] block in the {self.config_file}: {e}",
f"[!] Cannot read [{config_id}] block because the entry [{e}] could not be found",
'CRITICAL', raise_exception=True
)
sys.exit(1)
@@ -175,7 +182,9 @@ class SettingsLoader:
# - length: for string length
# - range: for int/float range check
# mandatory check
if check == "mandatory:yes" and not settings[config_id].get(entry):
if check == "mandatory:yes" and (
not settings[config_id].get(entry) or settings[config_id].get(entry) == ['']
):
error = True
self.__print(f"[!] Missing content entry for: {entry}", 'ERROR')
# skip if empty none
@@ -392,7 +401,8 @@ class SettingsLoader:
clean: re.Pattern[str] | None = None
if regex_clean is not None:
clean = re.compile(regex_clean, re.VERBOSE)
if not check.search(value):
# value must be set if clean is None, else empty value is allowed and will fail
if (clean is None and value or clean) and not check.search(value):
self.__print(
f"[!] Invalid content for '{entry}' with check '{validate}' and data: {value}",
'ERROR', print_error

View File

@@ -3,6 +3,9 @@ Dict helpers
"""
from typing import Any
def mask(
data_set: dict[str, str],
mask_keys: list[str] | None = None,
@@ -34,4 +37,22 @@ def mask(
for key, value in data_set.items()
}
def set_entry(dict_set: dict[str, Any], key: str, value_set: Any) -> dict[str, Any]:
"""
set a new entry in the dict set
Arguments:
key {str} -- _description_
dict_set {dict[str, Any]} -- _description_
value_set {Any} -- _description_
Returns:
dict[str, Any] -- _description_
"""
if not dict_set.get(key):
dict_set[key] = {}
dict_set[key] = value_set
return dict_set
# __END__

View File

@@ -1,6 +1,7 @@
[TestA]
foo=bar
foobar=1
bar=st
some_match=foo
some_match_list=foo,bar
test_list=a,b,c,d f, g h
@@ -21,5 +22,6 @@ match_source_list=foo,bar
element_a=Static energy
element_b=123.5
element_c=True
email=foo@bar.com,other+bar-fee@domain-com.cp
email_bad=@bar.com
email=foo@bar.com,other+bar-fee@domain-com.cp,
email_not_mandatory=
email_bad=gii@bar.com

View File

@@ -42,8 +42,10 @@ def main():
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": [
@@ -82,6 +84,11 @@ def main():
"mandatory:yes",
"check:string.email.basic"
],
"email_not_mandatory": [
"split:,",
# "mandatory:yes",
"check:string.email.basic"
],
"email_bad": [
"split:,",
"mandatory:yes",

2
uv.lock generated
View File

@@ -44,7 +44,7 @@ wheels = [
[[package]]
name = "corelibs"
version = "0.12.0"
version = "0.12.2"
source = { editable = "." }
dependencies = [
{ name = "jmespath" },