Added libs
This commit is contained in:
51
app/common/args.py
Normal file
51
app/common/args.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import sys
|
||||
from typing import Dict, List, Any
|
||||
|
||||
def read_kargs(argv: List[str] = None) -> Dict[str, Any]:
|
||||
"""
|
||||
Parse command-line arguments in the format key=value.
|
||||
|
||||
Example:
|
||||
python start.py updateApp=True port=8080
|
||||
|
||||
Returns:
|
||||
dict: {
|
||||
"updateApp": True,
|
||||
"port": "8080"
|
||||
}
|
||||
"""
|
||||
if argv is None:
|
||||
argv = sys.argv
|
||||
|
||||
kargs: Dict[str, Any] = {}
|
||||
for param in argv:
|
||||
if "=" in param:
|
||||
key, value = param.split("=", 1)
|
||||
|
||||
# Normalize booleans
|
||||
if value.lower() == "true":
|
||||
value = True
|
||||
elif value.lower() == "false":
|
||||
value = False
|
||||
|
||||
kargs[key] = value
|
||||
|
||||
return kargs
|
||||
|
||||
|
||||
def kargs_to_array(kargs: Dict[str, Any] = None) -> List[str]:
|
||||
"""
|
||||
Convert a dictionary of arguments back to an array of key=value.
|
||||
|
||||
Example:
|
||||
{"updateApp": True, "port": 8080}
|
||||
Returns:
|
||||
["updateApp=True", "port=8080"]
|
||||
"""
|
||||
if not kargs:
|
||||
kargs = read_kargs()
|
||||
|
||||
array: List[str] = []
|
||||
for key, value in kargs.items():
|
||||
array.append(f"{key}={value}")
|
||||
return array
|
||||
56
app/common/config.py
Normal file
56
app/common/config.py
Normal file
@@ -0,0 +1,56 @@
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Dict, Any
|
||||
from collections import OrderedDict
|
||||
|
||||
from .paths import LIBS_DIR
|
||||
|
||||
class Config:
|
||||
def __init__(self):
|
||||
self.libs: Dict[str, Dict[str, Any]] = {}
|
||||
self.load_libs_config()
|
||||
|
||||
def load_libs_config(self):
|
||||
for lib_path in LIBS_DIR.iterdir():
|
||||
if lib_path.is_dir():
|
||||
info_path = lib_path / "info.json"
|
||||
config_path = lib_path / "config.json"
|
||||
lib_id = lib_path.name
|
||||
|
||||
info_data = {}
|
||||
config_data = {}
|
||||
|
||||
if info_path.exists():
|
||||
with open(info_path) as f:
|
||||
info_data = json.load(f, object_pairs_hook=OrderedDict)
|
||||
|
||||
if config_path.exists():
|
||||
with open(config_path) as f:
|
||||
config_data = json.load(f, object_pairs_hook=OrderedDict)
|
||||
|
||||
config_data["info"] = info_data
|
||||
self.libs[lib_id] = config_data
|
||||
|
||||
def get(self, lib: str, *keys, default=None):
|
||||
"""
|
||||
Retrieves values from the config.
|
||||
|
||||
Example:
|
||||
config.get("app", "libs", "app", "update", "version", default="1.0")
|
||||
|
||||
Parameters:
|
||||
- lib: the name of the library
|
||||
- *keys: a sequence of keys to navigate through nested dictionaries
|
||||
- default: the value to return if any key in the chain does not exist
|
||||
|
||||
Returns:
|
||||
- The value found at the nested key path, or `default` if a key is missing.
|
||||
"""
|
||||
value = self.libs.get(lib, default)
|
||||
for key in keys:
|
||||
if isinstance(value, dict) and key in value:
|
||||
value = value[key]
|
||||
else:
|
||||
return default
|
||||
return value
|
||||
173
app/common/logging.py
Normal file
173
app/common/logging.py
Normal file
@@ -0,0 +1,173 @@
|
||||
import os
|
||||
import logging
|
||||
import inspect
|
||||
from collections import deque
|
||||
from typing import Deque
|
||||
from io import StringIO
|
||||
from pathlib import Path
|
||||
from .paths import ROOT_DIR, LOGS_DIR
|
||||
|
||||
|
||||
class CustomLoggingFormatter(logging.Formatter):
|
||||
"""
|
||||
Custom logging formatter with ANSI colors for console output
|
||||
and standard format for file output.
|
||||
"""
|
||||
grey = "\x1b[0;37m"
|
||||
green = "\x1b[1;32m"
|
||||
yellow = "\x1b[1;33m"
|
||||
red = "\x1b[1;31m"
|
||||
purple = "\x1b[1;35m"
|
||||
blue = "\x1b[1;34m"
|
||||
light_blue = "\x1b[1;36m"
|
||||
bold_red = "\x1b[31;1m"
|
||||
reset = "\x1b[0m"
|
||||
|
||||
prefix = light_blue + '%(asctime)s' + reset + ' |'
|
||||
colored_level = '%(levelname)-8s'
|
||||
message = '| %(message)s'
|
||||
suffix = purple + ' (%(name)s %(filename)s:%(lineno)d)' + reset
|
||||
|
||||
FORMATS = {
|
||||
logging.DEBUG: prefix + grey + colored_level + reset + message + suffix,
|
||||
logging.INFO: prefix + blue + colored_level + reset + message + suffix,
|
||||
logging.WARNING: prefix + yellow + colored_level + reset + message + suffix,
|
||||
logging.ERROR: prefix + red + colored_level + reset + message + suffix,
|
||||
logging.CRITICAL: prefix + bold_red + colored_level + reset + message + suffix,
|
||||
}
|
||||
|
||||
file_format = '%(asctime)s | %(levelname)-8s | %(message)s | %(name)s (%(filename)s:%(lineno)d)'
|
||||
|
||||
def format(self, record):
|
||||
log_fmt = self.FORMATS.get(record.levelno, self.file_format)
|
||||
formatter = logging.Formatter(log_fmt)
|
||||
return formatter.format(record)
|
||||
|
||||
def add_logging_console_handler(logger: logging.Logger, logging_level=logging.DEBUG):
|
||||
"""
|
||||
Add console handler with colored formatter to a logger.
|
||||
"""
|
||||
sh = logging.StreamHandler()
|
||||
sh.setLevel(logging_level)
|
||||
sh.setFormatter(CustomLoggingFormatter())
|
||||
logger.addHandler(sh)
|
||||
|
||||
def add_logging_file_handler(logger: logging.Logger, directory: str, file_name: str, logging_level=logging.DEBUG):
|
||||
"""
|
||||
Add file handler with plain formatter to a logger.
|
||||
"""
|
||||
file_path = LOGS_DIR / directory / f"{file_name}.log"
|
||||
file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
fh = logging.FileHandler(file_path, encoding="utf-8")
|
||||
fh.setLevel(logging_level)
|
||||
fh.setFormatter(logging.Formatter(CustomLoggingFormatter().file_format))
|
||||
logger.addHandler(fh)
|
||||
|
||||
class BufferHandler(logging.Handler):
|
||||
"""
|
||||
Logging handler that stores log records in a limited deque.
|
||||
Allows multiple listeners to be notified when a new log line is added.
|
||||
"""
|
||||
def __init__(self, maxlen: int = 200):
|
||||
super().__init__()
|
||||
self.buffer: Deque[str] = deque(maxlen=maxlen)
|
||||
self.formatter = CustomLoggingFormatter()
|
||||
self._listeners: list[callable] = []
|
||||
|
||||
def emit(self, record: logging.LogRecord):
|
||||
for listener in self._listeners:
|
||||
try:
|
||||
listener(record)
|
||||
except Exception as e:
|
||||
print(f"BufferHandler listener error: {e}")
|
||||
|
||||
def get_logs(self):
|
||||
return list(self.buffer)
|
||||
|
||||
def clear(self):
|
||||
self.buffer.clear()
|
||||
|
||||
def register_listener(self, listener: callable):
|
||||
"""
|
||||
Register a listener callback that will be called with
|
||||
each new log line.
|
||||
"""
|
||||
if listener not in self._listeners:
|
||||
self._listeners.append(listener)
|
||||
|
||||
def unregister_listener(self, listener: callable):
|
||||
"""Remove a previously registered listener."""
|
||||
if listener in self._listeners:
|
||||
self._listeners.remove(listener)
|
||||
|
||||
_LOGGER_BUFFERS: dict[str, BufferHandler] = {}
|
||||
|
||||
def add_logging_buffer_handler(logger: logging.Logger, logging_level=logging.DEBUG, maxlen: int = 300) -> BufferHandler:
|
||||
"""
|
||||
Add buffer handler with plain formatter to a logger.
|
||||
Returns the buffer so it can be accessed later.
|
||||
"""
|
||||
bh = BufferHandler(maxlen=maxlen)
|
||||
logger.addHandler(bh)
|
||||
_LOGGER_BUFFERS[logger.name] = bh
|
||||
|
||||
def get_logger(name: str = "", buffer: bool = False) -> logging.Logger:
|
||||
"""
|
||||
Returns a logger instance with console, file, and optional buffer handlers.
|
||||
Logger name is automatically derived from caller module path.
|
||||
"""
|
||||
file_path = os.path.normpath(inspect.stack()[1].filename)
|
||||
head = os.path.split(file_path)[0]
|
||||
|
||||
# Determine library name
|
||||
if head == os.path.normpath(ROOT_DIR):
|
||||
lib = "root"
|
||||
else:
|
||||
after_lib = head.replace(os.path.join(ROOT_DIR, "libs"), "")
|
||||
lib = after_lib.split(os.sep)[1] if len(after_lib.split(os.sep)) > 1 else "unknown"
|
||||
|
||||
# Build logger name
|
||||
if name == "root":
|
||||
logger_name = name
|
||||
elif name:
|
||||
logger_name = f"{lib}.{name}"
|
||||
else:
|
||||
logger_name = lib
|
||||
|
||||
file_name = name if name else lib
|
||||
|
||||
logger = logging.getLogger(logger_name)
|
||||
logger.propagate = False
|
||||
|
||||
if not logger.hasHandlers():
|
||||
logger.setLevel(logging.DEBUG)
|
||||
add_logging_console_handler(logger=logger, logging_level=logging.DEBUG)
|
||||
add_logging_file_handler(logger=logger, directory=lib, file_name=file_name, logging_level=logging.DEBUG)
|
||||
if buffer and logger_name not in _LOGGER_BUFFERS:
|
||||
add_logging_buffer_handler(logger, logging_level=logging.DEBUG)
|
||||
|
||||
return logger
|
||||
|
||||
def get_logger_buffer(logger_name: str):
|
||||
"""
|
||||
Returns the buffer of a previously configured logger.
|
||||
Raises KeyError if the logger does not have a buffer handler configured or does not exist.
|
||||
"""
|
||||
if logger_name in _LOGGER_BUFFERS:
|
||||
return _LOGGER_BUFFERS[logger_name]
|
||||
raise KeyError(f"Logger '{logger_name}' does not have a buffer handler configured or does not exist.")
|
||||
|
||||
def list_loggers() -> list[str]:
|
||||
"""
|
||||
Returns a list of all currently registered logger names.
|
||||
"""
|
||||
return sorted([
|
||||
name for name, obj in logging.Logger.manager.loggerDict.items()
|
||||
if isinstance(obj, logging.Logger)
|
||||
])
|
||||
|
||||
def config_root_logger() -> logging.Logger:
|
||||
"""
|
||||
Configure and return the root logger.
|
||||
"""
|
||||
return get_logger(name="root")
|
||||
25
app/common/network_utils.py
Normal file
25
app/common/network_utils.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import requests
|
||||
from requests.exceptions import RequestException, Timeout
|
||||
|
||||
|
||||
def check_url(url: str, timeout: int = 10, retries: int = 3, verify_ssl: bool = False) -> bool:
|
||||
"""
|
||||
Check if a given URL is reachable.
|
||||
|
||||
Args:
|
||||
url (str): The URL to check.
|
||||
timeout (int): Timeout for each request in seconds.
|
||||
retries (int): Number of retry attempts before failing.
|
||||
verify_ssl (bool): Verify SSL.
|
||||
|
||||
Returns:
|
||||
bool: True if the URL is reachable, False otherwise.
|
||||
"""
|
||||
for attempt in range(retries):
|
||||
try:
|
||||
response = requests.head(url, timeout=timeout, allow_redirects=True, verify=verify_ssl)
|
||||
if 200 <= response.status_code < 400:
|
||||
return True
|
||||
except (RequestException, Timeout):
|
||||
continue
|
||||
return False
|
||||
8
app/common/paths.py
Normal file
8
app/common/paths.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from pathlib import Path
|
||||
|
||||
# Root directory of the project
|
||||
ROOT_DIR = Path(__file__).resolve().parents[3]
|
||||
|
||||
LOGS_DIR = ROOT_DIR / "logs"
|
||||
LIBS_DIR = ROOT_DIR / "libs"
|
||||
FILES_DIR = ROOT_DIR / "files"
|
||||
97
app/common/process.py
Normal file
97
app/common/process.py
Normal file
@@ -0,0 +1,97 @@
|
||||
import subprocess
|
||||
import sys
|
||||
import os
|
||||
import platform
|
||||
import shutil
|
||||
from typing import List, Optional
|
||||
from .paths import ROOT_DIR # centralized project paths
|
||||
|
||||
|
||||
def _find_terminal_emulator() -> Optional[str]:
|
||||
"""
|
||||
Try to find an available terminal emulator on Linux.
|
||||
Returns the command name if found, otherwise None.
|
||||
"""
|
||||
candidates = [
|
||||
"x-terminal-emulator",
|
||||
"gnome-terminal",
|
||||
"konsole",
|
||||
"xfce4-terminal",
|
||||
"lxterminal",
|
||||
"mate-terminal",
|
||||
"tilix",
|
||||
"terminator",
|
||||
"xterm"
|
||||
]
|
||||
for term in candidates:
|
||||
if shutil.which(term):
|
||||
return term
|
||||
return None
|
||||
|
||||
|
||||
def new_python_process(
|
||||
script_path: str,
|
||||
python_exec: str = sys.executable,
|
||||
args: Optional[List[str]] = None,
|
||||
cwd: str = str(ROOT_DIR),
|
||||
wait: bool = False,
|
||||
new_console: bool = False
|
||||
) -> int:
|
||||
"""
|
||||
Start a new Python process running the given script.
|
||||
|
||||
Args:
|
||||
script_path (str): Path to the Python script to execute.
|
||||
python_exec (str): Path to the Python executable.
|
||||
args (List[str], optional): Arguments to pass to the script. Defaults to [].
|
||||
cwd (str): Working directory for the process. Defaults to project root.
|
||||
wait (bool): If True, waits for process completion. Defaults to False.
|
||||
new_console (bool): If True, tries to open process in a new console window.
|
||||
|
||||
Returns:
|
||||
int: PID of the created process.
|
||||
"""
|
||||
if not os.path.isabs(python_exec):
|
||||
python_exec = os.path.normpath(os.path.join(cwd, python_exec))
|
||||
|
||||
if not os.path.isabs(script_path):
|
||||
script_path = os.path.normpath(os.path.join(cwd, script_path))
|
||||
|
||||
if args is None:
|
||||
args = []
|
||||
|
||||
command = [python_exec, script_path] + args
|
||||
|
||||
creation_flags = 0
|
||||
shell = False
|
||||
|
||||
system = platform.system().lower()
|
||||
|
||||
if new_console:
|
||||
if system == "windows":
|
||||
creation_flags = subprocess.CREATE_NEW_CONSOLE
|
||||
elif system == "linux":
|
||||
term = _find_terminal_emulator()
|
||||
if term:
|
||||
command = [term, "-e"] + command
|
||||
else:
|
||||
# fallback: run in same terminal
|
||||
pass
|
||||
elif system == "darwin": # MacOS
|
||||
osa_cmd = f'tell app "Terminal" to do script "{python_exec} {script_path} {" ".join(args)}"'
|
||||
process = subprocess.Popen(["osascript", "-e", osa_cmd], cwd=cwd)
|
||||
if wait:
|
||||
process.wait()
|
||||
return process.pid
|
||||
|
||||
process = subprocess.Popen(
|
||||
command,
|
||||
cwd=cwd,
|
||||
creationflags=creation_flags,
|
||||
shell=shell
|
||||
)
|
||||
|
||||
if wait:
|
||||
process.wait()
|
||||
|
||||
return process.pid
|
||||
75
app/common/store.py
Normal file
75
app/common/store.py
Normal file
@@ -0,0 +1,75 @@
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import List, Dict, Any, Optional
|
||||
|
||||
from libs.app.common.paths import FILES_DIR
|
||||
|
||||
class DataStore:
|
||||
def __init__(self, path: str = "data/data.json", default_data:Optional[Dict[str, List[Any]]] = None, autosave:bool = True):
|
||||
if default_data is None:
|
||||
default_data = {"data": []}
|
||||
if not os.path.isabs(path):
|
||||
path = os.path.normpath(os.path.join(FILES_DIR, path))
|
||||
self.file_path = Path(path)
|
||||
self.default_data = default_data
|
||||
self.data = default_data.copy()
|
||||
self.autosave = autosave
|
||||
|
||||
self.load()
|
||||
|
||||
def load(self):
|
||||
if self.file_path.exists():
|
||||
with open(self.file_path, "r", encoding="utf-8") as f:
|
||||
self.data = json.load(f)
|
||||
else:
|
||||
os.makedirs(self.file_path.parent, exist_ok=True)
|
||||
self._save()
|
||||
|
||||
def save(self):
|
||||
with open(self.file_path, "w", encoding="utf-8") as f:
|
||||
json.dump(self.data, f, indent=2)
|
||||
|
||||
def _save(self):
|
||||
if not self.autosave:
|
||||
return
|
||||
self.save()
|
||||
|
||||
def clear(self):
|
||||
self.data = self.default_data.copy()
|
||||
self._save()
|
||||
|
||||
|
||||
def add_item(self, parent:str, data: Dict[str, Any], unique: bool = False, id_field:Optional[str]=None, id: Optional[str]=None):
|
||||
if unique and id and id_field:
|
||||
if any(item.get(id_field) == id for item in self.data.get(parent, [])):
|
||||
raise ValueError(f"Item already exists in {parent}. {id_field}:{id}")
|
||||
|
||||
self.data.setdefault(parent, []).append(data)
|
||||
self._save()
|
||||
|
||||
def get_item(self, parent:str, id_field:str, id:str) -> Optional[Dict[str, Any]]:
|
||||
for item in self.data.get(parent, []):
|
||||
if item.get(id_field) == id:
|
||||
return item
|
||||
return None
|
||||
|
||||
def update_item(self, parent: str, id_field: str, id: str, updates: Dict[str, Any]) -> bool:
|
||||
for item in self.data.get(parent, []):
|
||||
if item.get(id_field) == id:
|
||||
item.update(updates)
|
||||
self._save()
|
||||
return True
|
||||
return False
|
||||
|
||||
def remove_item(self, parent:str, id_field:str, id:str) -> bool:
|
||||
items = self.data.get(parent, [])
|
||||
new_items = [item for item in items if item.get(id_field) != id]
|
||||
if len(new_items) != len(items):
|
||||
self.data[parent] = new_items
|
||||
self._save()
|
||||
return True
|
||||
return False
|
||||
|
||||
def list_items(self, parent:str) -> List[Dict[str, Any]]:
|
||||
return self.data.get(parent, [])
|
||||
Reference in New Issue
Block a user