Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7119844313 | ||
|
|
5763f57830 |
@@ -1,7 +1,7 @@
|
||||
# MARK: Project info
|
||||
[project]
|
||||
name = "corelibs"
|
||||
version = "0.12.5"
|
||||
version = "0.12.6"
|
||||
description = "Collection of utils for Python scripts"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.13"
|
||||
|
||||
@@ -5,7 +5,6 @@ Additional check for override settings as arguments
|
||||
"""
|
||||
|
||||
import re
|
||||
import sys
|
||||
import configparser
|
||||
from typing import Any, Tuple, Sequence, cast
|
||||
from pathlib import Path
|
||||
@@ -109,11 +108,10 @@ class SettingsLoader:
|
||||
except KeyError as e:
|
||||
if allow_not_exist is True:
|
||||
return {}
|
||||
self.__print(
|
||||
raise ValueError(self.__print(
|
||||
f"[!] Cannot read [{config_id}] block in the {self.config_file}: {e}",
|
||||
'CRITICAL', raise_exception=True
|
||||
)
|
||||
sys.exit(1)
|
||||
'CRITICAL'
|
||||
)) from e
|
||||
try:
|
||||
for key, checks in config_validate.items():
|
||||
skip = True
|
||||
@@ -124,20 +122,16 @@ class SettingsLoader:
|
||||
try:
|
||||
[_, convert_to] = check.split(":")
|
||||
if convert_to not in self.CONVERT_TO_LIST:
|
||||
self.__print(
|
||||
raise ValueError(self.__print(
|
||||
f"[!] In [{config_id}] the convert type is invalid {check}: {convert_to}",
|
||||
'CRITICAL',
|
||||
raise_exception=True
|
||||
)
|
||||
sys.exit(1)
|
||||
'CRITICAL'
|
||||
))
|
||||
self.entry_convert[key] = convert_to
|
||||
except ValueError as e:
|
||||
self.__print(
|
||||
raise ValueError(self.__print(
|
||||
f"[!] In [{config_id}] the convert type setup for entry failed: {check}: {e}",
|
||||
'CRITICAL',
|
||||
raise_exception=True
|
||||
)
|
||||
sys.exit(1)
|
||||
'CRITICAL'
|
||||
)) from e
|
||||
if check.startswith('empty:'):
|
||||
try:
|
||||
[_, empty_set] = check.split(":")
|
||||
@@ -146,12 +140,10 @@ class SettingsLoader:
|
||||
self.entry_set_empty[key] = empty_set
|
||||
except ValueError as e:
|
||||
print(f"VALUE ERROR: {key}")
|
||||
self.__print(
|
||||
raise ValueError(self.__print(
|
||||
f"[!] In [{config_id}] the empty set type for entry failed: {check}: {e}",
|
||||
'CRITICAL',
|
||||
raise_exception=True
|
||||
)
|
||||
sys.exit(1)
|
||||
'CRITICAL'
|
||||
)) from e
|
||||
# split char, also check to not set it twice, first one only
|
||||
if check.startswith("split:") and not self.entry_split_char.get(key):
|
||||
try:
|
||||
@@ -168,12 +160,10 @@ class SettingsLoader:
|
||||
self.entry_split_char[key] = split_char
|
||||
skip = False
|
||||
except ValueError as e:
|
||||
self.__print(
|
||||
raise ValueError(self.__print(
|
||||
f"[!] In [{config_id}] the split character setup for entry failed: {check}: {e}",
|
||||
'CRITICAL',
|
||||
raise_exception=True
|
||||
)
|
||||
sys.exit(1)
|
||||
'CRITICAL'
|
||||
)) from e
|
||||
if skip:
|
||||
continue
|
||||
settings[config_id][key] = [
|
||||
@@ -181,16 +171,14 @@ class SettingsLoader:
|
||||
for __value in settings[config_id][key].split(split_char)
|
||||
]
|
||||
except KeyError as e:
|
||||
self.__print(
|
||||
raise ValueError(self.__print(
|
||||
f"[!] Cannot read [{config_id}] block because the entry [{e}] could not be found",
|
||||
'CRITICAL', raise_exception=True
|
||||
)
|
||||
sys.exit(1)
|
||||
'CRITICAL'
|
||||
)) from e
|
||||
else:
|
||||
# ignore error if arguments are set
|
||||
if not self.__check_arguments(config_validate, True):
|
||||
self.__print(f"[!] Cannot find file: {self.config_file}", 'CRITICAL', raise_exception=True)
|
||||
sys.exit(1)
|
||||
raise ValueError(self.__print(f"[!] Cannot find file: {self.config_file}", 'CRITICAL'))
|
||||
else:
|
||||
# base set
|
||||
settings[config_id] = {}
|
||||
@@ -271,8 +259,7 @@ class SettingsLoader:
|
||||
):
|
||||
error = True
|
||||
if error is True:
|
||||
self.__print("[!] Missing or incorrect settings data. Cannot proceed", 'CRITICAL', raise_exception=True)
|
||||
sys.exit(1)
|
||||
raise ValueError(self.__print("[!] Missing or incorrect settings data. Cannot proceed", 'CRITICAL'))
|
||||
# set empty
|
||||
for [entry, empty_set] in self.entry_set_empty.items():
|
||||
# if set, skip, else set to empty value
|
||||
@@ -326,22 +313,20 @@ class SettingsLoader:
|
||||
try:
|
||||
[__from, __to] = check.split('-')
|
||||
if (__from and not is_float(__from)) or (__to and not is_float(__to)):
|
||||
self.__print(
|
||||
raise ValueError(self.__print(
|
||||
f"[{entry}] Check value for length is not in: {check}",
|
||||
'CRITICAL', raise_exception=True
|
||||
)
|
||||
sys.exit(1)
|
||||
'CRITICAL'
|
||||
))
|
||||
if len(__from) == 0:
|
||||
__from = None
|
||||
if len(__to) == 0:
|
||||
__to = None
|
||||
except ValueError:
|
||||
except ValueError as e:
|
||||
if not is_float(__equal := check):
|
||||
self.__print(
|
||||
raise ValueError(self.__print(
|
||||
f"[{entry}] Check value for length is not a valid integer: {check}",
|
||||
'CRITICAL', raise_exception=True
|
||||
)
|
||||
sys.exit(1)
|
||||
'CRITICAL'
|
||||
)) from e
|
||||
if len(__equal) == 0:
|
||||
__equal = None
|
||||
# makre sure this is all int or None
|
||||
@@ -477,11 +462,10 @@ class SettingsLoader:
|
||||
# get the check settings
|
||||
__check_settings = SettingsLoaderCheck.CHECK_SETTINGS.get(check)
|
||||
if __check_settings is None:
|
||||
self.__print(
|
||||
raise ValueError(self.__print(
|
||||
f"[{entry}] Cannot get SettingsLoaderCheck.CHECK_SETTINGS for {check}",
|
||||
'CRITICAL', raise_exception=True
|
||||
)
|
||||
sys.exit(1)
|
||||
'CRITICAL'
|
||||
))
|
||||
# either removes or replaces invalid characters in the list
|
||||
if isinstance(setting_value, list):
|
||||
# clean up invalid characters
|
||||
@@ -549,7 +533,7 @@ class SettingsLoader:
|
||||
return self.args.get(entry)
|
||||
|
||||
# MARK: error print
|
||||
def __print(self, msg: str, level: str, print_error: bool = True, raise_exception: bool = False):
|
||||
def __print(self, msg: str, level: str, print_error: bool = True) -> str:
|
||||
"""
|
||||
print out error, if Log class is set then print to log instead
|
||||
|
||||
@@ -563,12 +547,11 @@ class SettingsLoader:
|
||||
if self.log is not None:
|
||||
if not Log.validate_log_level(level):
|
||||
level = 'ERROR'
|
||||
self.log.logger.log(Log.get_log_level_int(level), msg)
|
||||
self.log.logger.log(Log.get_log_level_int(level), msg, stacklevel=2)
|
||||
if self.log is None or self.always_print:
|
||||
if print_error:
|
||||
print(msg)
|
||||
if raise_exception:
|
||||
raise ValueError(msg)
|
||||
return msg
|
||||
|
||||
|
||||
# __END__
|
||||
|
||||
@@ -74,8 +74,8 @@ class CustomConsoleFormatter(logging.Formatter):
|
||||
return f"{color}{message}{reset}"
|
||||
|
||||
|
||||
# TODO: add custom handlers for stack_correct, if not set fill with %(filename)s:%(funcName)s:%(lineno)d
|
||||
# hasattr(record, 'stack_correct')
|
||||
# TODO: add custom handlers for stack_trace, if not set fill with %(filename)s:%(funcName)s:%(lineno)d
|
||||
# hasattr(record, 'stack_trace')
|
||||
|
||||
|
||||
# MARK: Log class
|
||||
@@ -367,7 +367,7 @@ class Log:
|
||||
|
||||
return root_logger
|
||||
|
||||
# FIXME: all below will only work if we add a custom format interface for the stack_correct part
|
||||
# FIXME: we need to add a custom formater to add stack level listing if we want to
|
||||
# Important note, although they exist, it is recommended to use self.logger.NAME directly
|
||||
# so that the correct filename, method and row number is set
|
||||
# for > 50 use logger.log(LoggingLevel.<LEVEL>.value, ...)
|
||||
@@ -379,8 +379,8 @@ class Log:
|
||||
raise ValueError('Logger is not yet initialized')
|
||||
if extra is None:
|
||||
extra = {}
|
||||
extra['stack_correct'] = traceback_call_str(start=3)
|
||||
self.logger.log(level, msg, *args, extra=extra)
|
||||
extra['stack_trace'] = traceback_call_str(start=3)
|
||||
self.logger.log(level, msg, *args, extra=extra, stacklevel=2)
|
||||
|
||||
# MARK: DEBUG 10
|
||||
def debug(self, msg: object, *args: object, extra: MutableMapping[str, object] | None = None) -> None:
|
||||
@@ -389,8 +389,8 @@ class Log:
|
||||
raise ValueError('Logger is not yet initialized')
|
||||
if extra is None:
|
||||
extra = {}
|
||||
extra['stack_correct'] = traceback_call_str(start=3)
|
||||
self.logger.debug(msg, *args, extra=extra)
|
||||
extra['stack_trace'] = traceback_call_str(start=3)
|
||||
self.logger.debug(msg, *args, extra=extra, stacklevel=2)
|
||||
|
||||
# MARK: INFO 20
|
||||
def info(self, msg: object, *args: object, extra: MutableMapping[str, object] | None = None) -> None:
|
||||
@@ -399,8 +399,8 @@ class Log:
|
||||
raise ValueError('Logger is not yet initialized')
|
||||
if extra is None:
|
||||
extra = {}
|
||||
extra['stack_correct'] = traceback_call_str(start=3)
|
||||
self.logger.info(msg, *args, extra=extra)
|
||||
extra['stack_trace'] = traceback_call_str(start=3)
|
||||
self.logger.info(msg, *args, extra=extra, stacklevel=2)
|
||||
|
||||
# MARK: WARNING 30
|
||||
def warning(self, msg: object, *args: object, extra: MutableMapping[str, object] | None = None) -> None:
|
||||
@@ -409,8 +409,8 @@ class Log:
|
||||
raise ValueError('Logger is not yet initialized')
|
||||
if extra is None:
|
||||
extra = {}
|
||||
extra['stack_correct'] = traceback_call_str(start=3)
|
||||
self.logger.warning(msg, *args, extra=extra)
|
||||
extra['stack_trace'] = traceback_call_str(start=3)
|
||||
self.logger.warning(msg, *args, extra=extra, stacklevel=2)
|
||||
|
||||
# MARK: ERROR 40
|
||||
def error(self, msg: object, *args: object, extra: MutableMapping[str, object] | None = None) -> None:
|
||||
@@ -419,8 +419,8 @@ class Log:
|
||||
raise ValueError('Logger is not yet initialized')
|
||||
if extra is None:
|
||||
extra = {}
|
||||
extra['stack_correct'] = traceback_call_str(start=3)
|
||||
self.logger.error(msg, *args, extra=extra)
|
||||
extra['stack_trace'] = traceback_call_str(start=3)
|
||||
self.logger.error(msg, *args, extra=extra, stacklevel=2)
|
||||
|
||||
# MARK: CRITICAL 50
|
||||
def critical(self, msg: object, *args: object, extra: MutableMapping[str, object] | None = None) -> None:
|
||||
@@ -429,8 +429,8 @@ class Log:
|
||||
raise ValueError('Logger is not yet initialized')
|
||||
if extra is None:
|
||||
extra = {}
|
||||
extra['stack_correct'] = traceback_call_str(start=3)
|
||||
self.logger.critical(msg, *args, extra=extra)
|
||||
extra['stack_trace'] = traceback_call_str(start=3)
|
||||
self.logger.critical(msg, *args, extra=extra, stacklevel=2)
|
||||
|
||||
# MARK: ALERT 55
|
||||
def alert(self, msg: object, *args: object, extra: MutableMapping[str, object] | None = None) -> None:
|
||||
@@ -440,8 +440,8 @@ class Log:
|
||||
# extra_dict = dict(extra)
|
||||
if extra is None:
|
||||
extra = {}
|
||||
extra['stack_correct'] = traceback_call_str(start=3)
|
||||
self.logger.log(LoggingLevel.ALERT.value, msg, *args, extra=extra)
|
||||
extra['stack_trace'] = traceback_call_str(start=3)
|
||||
self.logger.log(LoggingLevel.ALERT.value, msg, *args, extra=extra, stacklevel=2)
|
||||
|
||||
# MARK: EMERGECNY: 60
|
||||
def emergency(self, msg: object, *args: object, extra: MutableMapping[str, object] | None = None) -> None:
|
||||
@@ -450,8 +450,8 @@ class Log:
|
||||
raise ValueError('Logger is not yet initialized')
|
||||
if extra is None:
|
||||
extra = {}
|
||||
extra['stack_correct'] = traceback_call_str(start=3)
|
||||
self.logger.log(LoggingLevel.EMERGENCY.value, msg, *args, extra=extra)
|
||||
extra['stack_trace'] = traceback_call_str(start=3)
|
||||
self.logger.log(LoggingLevel.EMERGENCY.value, msg, *args, extra=extra, stacklevel=2)
|
||||
|
||||
# MARK: EXCEPTION: 70
|
||||
def exception(self, msg: object, *args: object, extra: MutableMapping[str, object] | None = None) -> None:
|
||||
@@ -467,8 +467,8 @@ class Log:
|
||||
raise ValueError('Logger is not yet initialized')
|
||||
if extra is None:
|
||||
extra = {}
|
||||
extra['stack_correct'] = traceback_call_str(start=3)
|
||||
self.logger.log(LoggingLevel.EXCEPTION.value, msg, *args, exc_info=True, extra=extra)
|
||||
extra['stack_trace'] = traceback_call_str(start=3)
|
||||
self.logger.log(LoggingLevel.EXCEPTION.value, msg, *args, exc_info=True, extra=extra, stacklevel=2)
|
||||
|
||||
# MARK: break line
|
||||
def break_line(self, info: str = "BREAK"):
|
||||
|
||||
Reference in New Issue
Block a user