Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e2e7882bfa | ||
|
|
4f9c2b9d5f | ||
|
|
5203bcf1ea | ||
|
|
f1e3bc8559 | ||
|
|
b97ca6f064 | ||
|
|
d1ea9874da | ||
|
|
3cd3f87d68 | ||
|
|
582937b866 | ||
|
|
2b8240c156 | ||
|
|
abf4b7ac89 | ||
|
|
9c49f83c16 | ||
|
|
3a625ed0ee | ||
|
|
2cfbf4bb90 | ||
|
|
5767533668 | ||
|
|
24798f19ca |
@@ -1,7 +1,7 @@
|
|||||||
# MARK: Project info
|
# MARK: Project info
|
||||||
[project]
|
[project]
|
||||||
name = "corelibs"
|
name = "corelibs"
|
||||||
version = "0.16.0"
|
version = "0.20.0"
|
||||||
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"
|
||||||
|
|||||||
@@ -4,7 +4,12 @@ Various debug helpers
|
|||||||
|
|
||||||
import traceback
|
import traceback
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
|
from typing import Tuple, Type
|
||||||
|
from types import TracebackType
|
||||||
|
|
||||||
|
# _typeshed.OptExcInfo
|
||||||
|
OptExcInfo = Tuple[None, None, None] | Tuple[Type[BaseException], BaseException, TracebackType]
|
||||||
|
|
||||||
def call_stack(
|
def call_stack(
|
||||||
start: int = 0,
|
start: int = 0,
|
||||||
@@ -41,4 +46,30 @@ def call_stack(
|
|||||||
# print(f"* HERE: {dump_data(stack)}")
|
# print(f"* HERE: {dump_data(stack)}")
|
||||||
return f"{separator}".join(f"{os.path.basename(f.filename)}:{f.name}:{f.lineno}" for f in __stack)
|
return f"{separator}".join(f"{os.path.basename(f.filename)}:{f.name}:{f.lineno}" for f in __stack)
|
||||||
|
|
||||||
|
|
||||||
|
def exception_stack(
|
||||||
|
exc_stack: OptExcInfo | None = None,
|
||||||
|
separator: str = ' -> '
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Exception traceback, if no sys.exc_info is set, run internal
|
||||||
|
|
||||||
|
Keyword Arguments:
|
||||||
|
exc_stack {OptExcInfo | None} -- _description_ (default: {None})
|
||||||
|
separator {str} -- _description_ (default: {' -> '})
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str -- _description_
|
||||||
|
"""
|
||||||
|
if exc_stack is not None:
|
||||||
|
_, _, exc_traceback = exc_stack
|
||||||
|
else:
|
||||||
|
exc_traceback = None
|
||||||
|
_, _, exc_traceback = sys.exc_info()
|
||||||
|
stack = traceback.extract_tb(exc_traceback)
|
||||||
|
if not separator:
|
||||||
|
separator = ' -> '
|
||||||
|
# print(f"* HERE: {dump_data(stack)}")
|
||||||
|
return f"{separator}".join(f"{os.path.basename(f.filename)}:{f.name}:{f.lineno}" for f in stack)
|
||||||
|
|
||||||
# __END__
|
# __END__
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import json
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
def dump_data(data: dict[Any, Any] | list[Any] | str | None) -> str:
|
def dump_data(data: Any) -> str:
|
||||||
"""
|
"""
|
||||||
dump formated output from dict/list
|
dump formated output from dict/list
|
||||||
|
|
||||||
|
|||||||
23
src/corelibs/exceptions/csv_exceptions.py
Normal file
23
src/corelibs/exceptions/csv_exceptions.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
"""
|
||||||
|
Exceptions for csv file reading and processing
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class NoCsvReader(Exception):
|
||||||
|
"""
|
||||||
|
CSV reader is none
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class CsvHeaderDataMissing(Exception):
|
||||||
|
"""
|
||||||
|
The csv reader returned None as headers, the header column in the csv file is missing
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class CompulsoryCsvHeaderCheckFailed(Exception):
|
||||||
|
"""
|
||||||
|
raise if the header is not matching to the excpeted values
|
||||||
|
"""
|
||||||
|
|
||||||
|
# __END__
|
||||||
@@ -2,23 +2,40 @@
|
|||||||
wrapper around search path
|
wrapper around search path
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import Any
|
from typing import Any, TypedDict, NotRequired
|
||||||
|
from warnings import deprecated
|
||||||
|
|
||||||
|
|
||||||
|
class ArraySearchList(TypedDict):
|
||||||
|
"""find in array from list search dict"""
|
||||||
|
key: str
|
||||||
|
value: str | bool | int | float | list[str | None]
|
||||||
|
case_sensitive: NotRequired[bool]
|
||||||
|
|
||||||
|
|
||||||
|
@deprecated("Use find_in_array_from_list()")
|
||||||
def array_search(
|
def array_search(
|
||||||
search_params: list[dict[str, str | bool | list[str | None]]],
|
search_params: list[ArraySearchList],
|
||||||
data: list[dict[str, Any]],
|
data: list[dict[str, Any]],
|
||||||
return_index: bool = False
|
return_index: bool = False
|
||||||
|
) -> list[dict[str, Any]]:
|
||||||
|
"""depreacted, old call order"""
|
||||||
|
return find_in_array_from_list(data, search_params, return_index)
|
||||||
|
|
||||||
|
def find_in_array_from_list(
|
||||||
|
data: list[dict[str, Any]],
|
||||||
|
search_params: list[ArraySearchList],
|
||||||
|
return_index: bool = False
|
||||||
) -> list[dict[str, Any]]:
|
) -> list[dict[str, Any]]:
|
||||||
"""
|
"""
|
||||||
search in an array of dicts with an array of Key/Value set
|
search in an list of dicts with an list of Key/Value set
|
||||||
all Key/Value sets must match
|
all Key/Value sets must match
|
||||||
Value set can be list for OR match
|
Value set can be list for OR match
|
||||||
option: case_senstive: default True
|
option: case_senstive: default True
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
search_params (list): List of search params in "Key"/"Value" lists with options
|
|
||||||
data (list): data to search in, must be a list
|
data (list): data to search in, must be a list
|
||||||
|
search_params (list): List of search params in "key"/"value" lists with options
|
||||||
return_index (bool): return index of list [default False]
|
return_index (bool): return index of list [default False]
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
@@ -32,18 +49,20 @@ def array_search(
|
|||||||
"""
|
"""
|
||||||
if not isinstance(search_params, list): # type: ignore
|
if not isinstance(search_params, list): # type: ignore
|
||||||
raise ValueError("search_params must be a list")
|
raise ValueError("search_params must be a list")
|
||||||
keys = []
|
keys: list[str] = []
|
||||||
|
# check that key and value exist and are set
|
||||||
for search in search_params:
|
for search in search_params:
|
||||||
if not search.get('Key') or not search.get('Value'):
|
if not search.get('key') or not search.get('value'):
|
||||||
raise KeyError(
|
raise KeyError(
|
||||||
f"Either Key '{search.get('Key', '')}' or "
|
f"Either Key '{search.get('key', '')}' or "
|
||||||
f"Value '{search.get('Value', '')}' is missing or empty"
|
f"Value '{search.get('value', '')}' is missing or empty"
|
||||||
)
|
)
|
||||||
# if double key -> abort
|
# if double key -> abort
|
||||||
if search.get("Key") in keys:
|
if search.get("key") in keys:
|
||||||
raise KeyError(
|
raise KeyError(
|
||||||
f"Key {search.get('Key', '')} already exists in search_params"
|
f"Key {search.get('key', '')} already exists in search_params"
|
||||||
)
|
)
|
||||||
|
keys.append(str(search['key']))
|
||||||
|
|
||||||
return_items: list[dict[str, Any]] = []
|
return_items: list[dict[str, Any]] = []
|
||||||
for si_idx, search_item in enumerate(data):
|
for si_idx, search_item in enumerate(data):
|
||||||
@@ -55,20 +74,20 @@ def array_search(
|
|||||||
# lower case left side
|
# lower case left side
|
||||||
# TODO: allow nested Keys. eg "Key: ["Key a", "key b"]" to be ["Key a"]["key b"]
|
# TODO: allow nested Keys. eg "Key: ["Key a", "key b"]" to be ["Key a"]["key b"]
|
||||||
if search.get("case_sensitive", True) is False:
|
if search.get("case_sensitive", True) is False:
|
||||||
search_value = search_item.get(str(search['Key']), "").lower()
|
search_value = search_item.get(str(search['key']), "").lower()
|
||||||
else:
|
else:
|
||||||
search_value = search_item.get(str(search['Key']), "")
|
search_value = search_item.get(str(search['key']), "")
|
||||||
# lower case right side
|
# lower case right side
|
||||||
if isinstance(search['Value'], list):
|
if isinstance(search['value'], list):
|
||||||
search_in = [
|
search_in = [
|
||||||
str(k).lower()
|
str(k).lower()
|
||||||
if search.get("case_sensitive", True) is False else k
|
if search.get("case_sensitive", True) is False else k
|
||||||
for k in search['Value']
|
for k in search['value']
|
||||||
]
|
]
|
||||||
elif search.get("case_sensitive", True) is False:
|
elif search.get("case_sensitive", True) is False:
|
||||||
search_in = str(search['Value']).lower()
|
search_in = str(search['value']).lower()
|
||||||
else:
|
else:
|
||||||
search_in = search['Value']
|
search_in = search['value']
|
||||||
# compare check
|
# compare check
|
||||||
if (
|
if (
|
||||||
(
|
(
|
||||||
|
|||||||
@@ -32,4 +32,6 @@ def jmespath_search(search_data: dict[Any, Any] | list[Any], search_params: str)
|
|||||||
raise ValueError(f"Type error for search_params: {excp}") from excp
|
raise ValueError(f"Type error for search_params: {excp}") from excp
|
||||||
return search_result
|
return search_result
|
||||||
|
|
||||||
|
# TODO: compile jmespath setup
|
||||||
|
|
||||||
# __END__
|
# __END__
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ from pathlib import Path
|
|||||||
from typing import MutableMapping, TextIO, TypedDict, Any, TYPE_CHECKING, cast
|
from typing import MutableMapping, TextIO, TypedDict, Any, TYPE_CHECKING, cast
|
||||||
from corelibs.logging_handling.logging_level_handling.logging_level import LoggingLevel
|
from corelibs.logging_handling.logging_level_handling.logging_level import LoggingLevel
|
||||||
from corelibs.string_handling.text_colors import Colors
|
from corelibs.string_handling.text_colors import Colors
|
||||||
from corelibs.debug_handling.debug_helpers import call_stack
|
from corelibs.debug_handling.debug_helpers import call_stack, exception_stack
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from multiprocessing import Queue
|
from multiprocessing import Queue
|
||||||
@@ -225,11 +225,13 @@ class LogParent:
|
|||||||
if extra is None:
|
if extra is None:
|
||||||
extra = {}
|
extra = {}
|
||||||
extra['stack_trace'] = call_stack(skip_last=2)
|
extra['stack_trace'] = call_stack(skip_last=2)
|
||||||
|
extra['exception_trace'] = exception_stack()
|
||||||
# write to console first with extra flag for filtering in file
|
# write to console first with extra flag for filtering in file
|
||||||
if log_error:
|
if log_error:
|
||||||
self.logger.log(
|
self.logger.log(
|
||||||
LoggingLevel.ERROR.value,
|
LoggingLevel.ERROR.value,
|
||||||
f"<=EXCEPTION> {msg}", *args, extra=dict(extra) | {'console': True}, stacklevel=2
|
f"<=EXCEPTION={extra['exception_trace']}> {msg} [{extra['stack_trace']}]",
|
||||||
|
*args, extra=dict(extra) | {'console': True}, stacklevel=2
|
||||||
)
|
)
|
||||||
self.logger.log(LoggingLevel.EXCEPTION.value, msg, *args, exc_info=True, extra=extra, stacklevel=2)
|
self.logger.log(LoggingLevel.EXCEPTION.value, msg, *args, exc_info=True, extra=extra, stacklevel=2)
|
||||||
|
|
||||||
|
|||||||
20
src/corelibs/requests_handling/auth_helpers.py
Normal file
20
src/corelibs/requests_handling/auth_helpers.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
"""
|
||||||
|
Various HTTP auth helpers
|
||||||
|
"""
|
||||||
|
|
||||||
|
from base64 import b64encode
|
||||||
|
|
||||||
|
|
||||||
|
def basic_auth(username: str, password: str) -> str:
|
||||||
|
"""
|
||||||
|
setup basic auth, for debug
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
username {str} -- _description_
|
||||||
|
password {str} -- _description_
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str -- _description_
|
||||||
|
"""
|
||||||
|
token = b64encode(f"{username}:{password}".encode('utf-8')).decode("ascii")
|
||||||
|
return f'Basic {token}'
|
||||||
52
test-run/iterator_handling/data_search.py
Normal file
52
test-run/iterator_handling/data_search.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
"""
|
||||||
|
Search data tests
|
||||||
|
iterator_handling.data_search
|
||||||
|
"""
|
||||||
|
|
||||||
|
from corelibs.debug_handling.dump_data import dump_data
|
||||||
|
from corelibs.iterator_handling.data_search import find_in_array_from_list, ArraySearchList
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
"""
|
||||||
|
Comment
|
||||||
|
"""
|
||||||
|
data = [
|
||||||
|
{
|
||||||
|
"lookup_value_p": "A01",
|
||||||
|
"lookup_value_c": "B01",
|
||||||
|
"replace_value": "R01",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"lookup_value_p": "A02",
|
||||||
|
"lookup_value_c": "B02",
|
||||||
|
"replace_value": "R02",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
test_foo = ArraySearchList(
|
||||||
|
key = "lookup_value_p",
|
||||||
|
value = "A01"
|
||||||
|
)
|
||||||
|
print(test_foo)
|
||||||
|
search: list[ArraySearchList] = [
|
||||||
|
{
|
||||||
|
"key": "lookup_value_p",
|
||||||
|
"value": "A01"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"key": "lookup_value_c",
|
||||||
|
"value": "B01"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
result = find_in_array_from_list(data, search)
|
||||||
|
|
||||||
|
print(f"Search {dump_data(search)} -> {dump_data(result)}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
||||||
|
# __END__
|
||||||
52
test-run/json_handling/jmespath_helper.py
Normal file
52
test-run/json_handling/jmespath_helper.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
"""
|
||||||
|
jmes path testing
|
||||||
|
"""
|
||||||
|
|
||||||
|
from corelibs.debug_handling.dump_data import dump_data
|
||||||
|
from corelibs.json_handling.jmespath_helper import jmespath_search
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
"""
|
||||||
|
Comment
|
||||||
|
"""
|
||||||
|
__set = {
|
||||||
|
'a': 'b',
|
||||||
|
'foobar': [1, 2, 'a'],
|
||||||
|
'bar': {
|
||||||
|
'a': 1,
|
||||||
|
'b': 'c'
|
||||||
|
},
|
||||||
|
'baz': [
|
||||||
|
{
|
||||||
|
'aa': 1,
|
||||||
|
'ab': 'cc'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'ba': 2,
|
||||||
|
'bb': 'dd'
|
||||||
|
},
|
||||||
|
],
|
||||||
|
'foo': {
|
||||||
|
'a': [1, 2, 3],
|
||||||
|
'b': ['a', 'b', 'c']
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
__get = [
|
||||||
|
'a',
|
||||||
|
'bar.a',
|
||||||
|
'foo.a',
|
||||||
|
'baz[].aa'
|
||||||
|
]
|
||||||
|
for __jmespath in __get:
|
||||||
|
result = jmespath_search(__set, __jmespath)
|
||||||
|
print(f"GET {__jmespath}: {dump_data(result)}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
||||||
|
# __END__
|
||||||
@@ -3,9 +3,11 @@ Log logging_handling.log testing
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# import atexit
|
# import atexit
|
||||||
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
# this is for testing only
|
# this is for testing only
|
||||||
from corelibs.logging_handling.log import Log, Logger
|
from corelibs.logging_handling.log import Log, Logger
|
||||||
|
from corelibs.debug_handling.debug_helpers import exception_stack, call_stack
|
||||||
from corelibs.logging_handling.logging_level_handling.logging_level import LoggingLevel
|
from corelibs.logging_handling.logging_level_handling.logging_level import LoggingLevel
|
||||||
|
|
||||||
|
|
||||||
@@ -78,6 +80,8 @@ def main():
|
|||||||
__test = 5 / 0
|
__test = 5 / 0
|
||||||
print(f"Divied: {__test}")
|
print(f"Divied: {__test}")
|
||||||
except ZeroDivisionError as e:
|
except ZeroDivisionError as e:
|
||||||
|
print(f"** sys.exec_info(): {sys.exc_info()}")
|
||||||
|
print(f"** sys.exec_info(): [{exception_stack()}] | [{exception_stack(sys.exc_info())}] | [{call_stack()}]")
|
||||||
log.logger.critical("Divison through zero: %s", e)
|
log.logger.critical("Divison through zero: %s", e)
|
||||||
log.exception("Divison through zero: %s", e)
|
log.exception("Divison through zero: %s", e)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user