83 lines
2.7 KiB
Python
83 lines
2.7 KiB
Python
#!/usr/bin/env python3
|
|
|
|
from Utils.Check import Check
|
|
from typing import Any, Self, Optional
|
|
from Utils.Patterns import RE
|
|
from re import Match as REMatch
|
|
from inspect import stack as get_stack
|
|
from inspect import FrameInfo
|
|
|
|
class Utils:
|
|
|
|
@classmethod
|
|
def get_keys(cls:type[Self], *items:list[Any|None]) -> list[str]:
|
|
return [key for item in items for key in (
|
|
(item,) if Check.is_key(item) else
|
|
cls.get_keys(*item) if Check.is_array(item) else
|
|
tuple())]
|
|
|
|
@classmethod
|
|
def get_dictionaries(cls:type[Self], *items:list[Any|None]) -> list[dict[str, Any|None]]:
|
|
return [dictionary for item in items for dictionary in (
|
|
(item,) if Check.is_dictionary(item) else
|
|
cls.get_dictionaries(*item) if Check.is_array(item) else
|
|
tuple())]
|
|
|
|
@classmethod
|
|
def get_dictionary(cls:type[Self], *items:list[Any|None]) -> dict[str, Any|None]:
|
|
return {key : value for item in items for key, value in (
|
|
item if Check.is_dictionary(item) else
|
|
cls.get_dictionary(*item) if Check.is_array(item) else
|
|
{}).items()}
|
|
|
|
@classmethod
|
|
def get_values(cls:type[Self], keys:str|list|tuple, inputs:dict[str, Any|None]|list[Any|None]|tuple[Any|None], _default:Optional[Any] = None) -> Any|None:
|
|
|
|
keys = cls.get_keys(keys)
|
|
|
|
if len(keys):
|
|
|
|
subinputs:dict[str, Any|None]
|
|
|
|
for subinputs in cls.get_dictionaries(inputs):
|
|
|
|
key:str
|
|
|
|
for key in keys:
|
|
if key in subinputs:
|
|
return subinputs[key]
|
|
return _default
|
|
|
|
@classmethod
|
|
def string_variables(cls:type[Self], string:str, variables:dict[str, Any|None], _default:Optional[str] = None) -> str|None:
|
|
|
|
variables = cls.get_dictionary(variables)
|
|
|
|
def callback(matches:REMatch) -> str:
|
|
|
|
key:str = matches.group(1)
|
|
|
|
return (
|
|
str(variables[key]) if key in variables else
|
|
str(_default) if _default is not None else
|
|
matches.group(0))
|
|
|
|
return RE.STRING_VARIABLES.sub(callback, str(string))
|
|
|
|
@staticmethod
|
|
def get_action_data(i:int = 0) -> dict[str, str|int]:
|
|
|
|
stack:FrameInfo = get_stack()[1 + i]
|
|
|
|
return {
|
|
"file" : stack.filename,
|
|
"method" : stack.function,
|
|
"line" : stack.lineno
|
|
}
|
|
|
|
@classmethod
|
|
def get_strings(cls:type[Self], *items:list[Any|None]) -> list[str]:
|
|
return [string for item in items for string in (
|
|
cls.get_strings(*item) if Check.is_array(item) else (item,)
|
|
) if Check.is_string(string)]
|
|
|