Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b74ed1f30e | ||
|
|
8082ab78a1 | ||
|
|
c69076f517 | ||
|
|
648ab001b6 | ||
|
|
447034046e | ||
|
|
0770ac0bb4 |
@@ -1,7 +1,7 @@
|
|||||||
# MARK: Project info
|
# MARK: Project info
|
||||||
[project]
|
[project]
|
||||||
name = "corelibs"
|
name = "corelibs"
|
||||||
version = "0.12.2"
|
version = "0.12.4"
|
||||||
description = "Collection of utils for Python scripts"
|
description = "Collection of utils for Python scripts"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.13"
|
requires-python = ">=3.13"
|
||||||
|
|||||||
@@ -51,11 +51,14 @@ class SettingsLoader:
|
|||||||
self.always_print = always_print
|
self.always_print = always_print
|
||||||
# entries that have to be split
|
# entries that have to be split
|
||||||
self.entry_split_char: dict[str, str] = {}
|
self.entry_split_char: dict[str, str] = {}
|
||||||
|
# entries that should be converted
|
||||||
self.entry_convert: dict[str, str] = {}
|
self.entry_convert: dict[str, str] = {}
|
||||||
|
# default set entries
|
||||||
|
self.entry_set_empty: dict[str, str | None] = {}
|
||||||
# config parser, load config file first
|
# config parser, load config file first
|
||||||
self.config_parser: configparser.ConfigParser | None = self.__load_config_file()
|
self.config_parser: configparser.ConfigParser | None = self.__load_config_file()
|
||||||
# all settings
|
# all settings
|
||||||
self.settings: dict[str, dict[str, Any]] | None = None
|
self.settings: dict[str, dict[str, None | str | int | float | bool]] | None = None
|
||||||
# remove file name and get base path and check
|
# remove file name and get base path and check
|
||||||
if not self.config_file.parent.is_dir():
|
if not self.config_file.parent.is_dir():
|
||||||
raise ValueError(f"Cannot find the config folder: {self.config_file.parent}")
|
raise ValueError(f"Cannot find the config folder: {self.config_file.parent}")
|
||||||
@@ -79,6 +82,7 @@ class SettingsLoader:
|
|||||||
- in: the right side is another KEY value from the settings where this value must be inside
|
- in: the right side is another KEY value from the settings where this value must be inside
|
||||||
- split: character to split entries, if set check:list+ must be set if checks are needed
|
- split: character to split entries, if set check:list+ must be set if checks are needed
|
||||||
- convert: convert to int, float -> if element is number convert, else leave as is
|
- convert: convert to int, float -> if element is number convert, else leave as is
|
||||||
|
- empty: convert empty to, if nothing set on the right side then convert to None type
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
config_id (str): what block to load
|
config_id (str): what block to load
|
||||||
@@ -94,6 +98,13 @@ class SettingsLoader:
|
|||||||
try:
|
try:
|
||||||
# load all data as is, validation is done afterwards
|
# load all data as is, validation is done afterwards
|
||||||
settings[config_id] = dict(self.config_parser[config_id])
|
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():
|
for key, checks in config_validate.items():
|
||||||
skip = True
|
skip = True
|
||||||
split_char = self.DEFAULT_ELEMENT_SPLIT_CHAR
|
split_char = self.DEFAULT_ELEMENT_SPLIT_CHAR
|
||||||
@@ -108,6 +119,8 @@ class SettingsLoader:
|
|||||||
'CRITICAL',
|
'CRITICAL',
|
||||||
raise_exception=True
|
raise_exception=True
|
||||||
)
|
)
|
||||||
|
sys.exit(1)
|
||||||
|
self.entry_convert[key] = convert_to
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
self.__print(
|
self.__print(
|
||||||
f"[!] In [{config_id}] the convert type setup for entry failed: {check}: {e}",
|
f"[!] In [{config_id}] the convert type setup for entry failed: {check}: {e}",
|
||||||
@@ -115,6 +128,20 @@ class SettingsLoader:
|
|||||||
raise_exception=True
|
raise_exception=True
|
||||||
)
|
)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
if check.startswith('empty:'):
|
||||||
|
try:
|
||||||
|
[_, empty_set] = check.split(":")
|
||||||
|
if not empty_set:
|
||||||
|
empty_set = None
|
||||||
|
self.entry_set_empty[key] = empty_set
|
||||||
|
except ValueError as e:
|
||||||
|
print(f"VALUE ERROR: {key}")
|
||||||
|
self.__print(
|
||||||
|
f"[!] In [{config_id}] the empty set type for entry failed: {check}: {e}",
|
||||||
|
'CRITICAL',
|
||||||
|
raise_exception=True
|
||||||
|
)
|
||||||
|
sys.exit(1)
|
||||||
# split char, also check to not set it twice, first one only
|
# split char, also check to not set it twice, first one only
|
||||||
if check.startswith("split:") and not self.entry_split_char.get(key):
|
if check.startswith("split:") and not self.entry_split_char.get(key):
|
||||||
try:
|
try:
|
||||||
@@ -145,7 +172,7 @@ class SettingsLoader:
|
|||||||
]
|
]
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
self.__print(
|
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
|
'CRITICAL', raise_exception=True
|
||||||
)
|
)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
@@ -236,8 +263,14 @@ class SettingsLoader:
|
|||||||
if error is True:
|
if error is True:
|
||||||
self.__print("[!] Missing or incorrect settings data. Cannot proceed", 'CRITICAL', raise_exception=True)
|
self.__print("[!] Missing or incorrect settings data. Cannot proceed", 'CRITICAL', raise_exception=True)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
# set empty
|
||||||
|
for [entry, empty_set] in self.entry_set_empty.items():
|
||||||
|
# if set, skip, else set to empty value
|
||||||
|
if settings[config_id].get(entry) or isinstance(settings[config_id].get(entry), list):
|
||||||
|
continue
|
||||||
|
settings[config_id][entry] = empty_set
|
||||||
# Convert input
|
# Convert input
|
||||||
for [entry, convert_type] in self.entry_convert:
|
for [entry, convert_type] in self.entry_convert.items():
|
||||||
if convert_type in ["int", "any"] and is_int(settings[config_id][entry]):
|
if convert_type in ["int", "any"] and is_int(settings[config_id][entry]):
|
||||||
settings[config_id][entry] = int(settings[config_id][entry])
|
settings[config_id][entry] = int(settings[config_id][entry])
|
||||||
elif convert_type in ["float", "any"] and is_float(settings[config_id][entry]):
|
elif convert_type in ["float", "any"] and is_float(settings[config_id][entry]):
|
||||||
@@ -256,6 +289,7 @@ class SettingsLoader:
|
|||||||
'ERROR'
|
'ERROR'
|
||||||
)
|
)
|
||||||
# string is always string
|
# string is always string
|
||||||
|
# TODO: empty and int/float/bool: set to none?
|
||||||
|
|
||||||
return settings[config_id]
|
return settings[config_id]
|
||||||
|
|
||||||
@@ -324,6 +358,9 @@ class SettingsLoader:
|
|||||||
(__from, __to, __equal) = check
|
(__from, __to, __equal) = check
|
||||||
valid = True
|
valid = True
|
||||||
for value_raw in convert_to_list(values):
|
for value_raw in convert_to_list(values):
|
||||||
|
# skip no tset values for range check
|
||||||
|
if not value_raw:
|
||||||
|
continue
|
||||||
value = 0
|
value = 0
|
||||||
error_mark = ''
|
error_mark = ''
|
||||||
if check_type == 'length':
|
if check_type == 'length':
|
||||||
|
|||||||
@@ -3,6 +3,9 @@ Dict helpers
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
def mask(
|
def mask(
|
||||||
data_set: dict[str, str],
|
data_set: dict[str, str],
|
||||||
mask_keys: list[str] | None = None,
|
mask_keys: list[str] | None = None,
|
||||||
@@ -34,4 +37,22 @@ def mask(
|
|||||||
for key, value in data_set.items()
|
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__
|
# __END__
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ other_list=a|b|c|d|
|
|||||||
third_list=xy|ab|df|fg
|
third_list=xy|ab|df|fg
|
||||||
str_length=foobar
|
str_length=foobar
|
||||||
int_range=20
|
int_range=20
|
||||||
|
int_range_not_set=
|
||||||
|
int_range_not_set_empty_set=5
|
||||||
#
|
#
|
||||||
match_target=foo
|
match_target=foo
|
||||||
match_target_list=foo,bar,baz
|
match_target_list=foo,bar,baz
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ def main():
|
|||||||
config_data = sl.load_settings(
|
config_data = sl.load_settings(
|
||||||
config_load,
|
config_load,
|
||||||
{
|
{
|
||||||
|
# "doesnt": ["split:,"],
|
||||||
"foo": ["mandatory:yes"],
|
"foo": ["mandatory:yes"],
|
||||||
"foobar": ["check:int"],
|
"foobar": ["check:int"],
|
||||||
"bar": ["mandatory:yes"],
|
"bar": ["mandatory:yes"],
|
||||||
@@ -62,6 +63,12 @@ def main():
|
|||||||
"int_range": [
|
"int_range": [
|
||||||
"range:2-50"
|
"range:2-50"
|
||||||
],
|
],
|
||||||
|
"int_range_not_set": [
|
||||||
|
"range:2-50"
|
||||||
|
],
|
||||||
|
"int_range_not_set_empty_set": [
|
||||||
|
"empty:"
|
||||||
|
],
|
||||||
"match_target": ["matching:foo"],
|
"match_target": ["matching:foo"],
|
||||||
"match_target_list": ["split:,", "matching:foo|bar|baz",],
|
"match_target_list": ["split:,", "matching:foo|bar|baz",],
|
||||||
"match_source_a": ["in:match_target"],
|
"match_source_a": ["in:match_target"],
|
||||||
|
|||||||
Reference in New Issue
Block a user