# MyICR_Suite/app/modules/report_reti/run.py import sys import os import threading import time import socket from pathlib import Path # Fix PYTHONPATH _myicr_root = Path(__file__).resolve().parent.parent.parent.parent _module_dir = Path(__file__).resolve().parent for _p in (str(_myicr_root), str(_module_dir)): if _p not in sys.path: sys.path.insert(0, _p) import webview from loguru import logger from server import app # noqa: E402 (relative import after path fix) _PORT = 5003 def _setup_logging(): log_dir = _myicr_root / 'logs' log_dir.mkdir(exist_ok=True) logger.remove() logger.add( str(log_dir / 'report_reti.log'), rotation='5 MB', retention='7 days', format='{time:YYYY-MM-DD HH:mm:ss} | {level:<8} | {name}:{line} - {message}', level='INFO', ) def _wait_for_server(host='127.0.0.1', port=_PORT, timeout=10) -> bool: t0 = time.time() while time.time() - t0 < timeout: try: s = socket.socket() s.settimeout(1) s.connect_ex((host, port)) s.close() return True except Exception: pass time.sleep(0.1) return False def _run_flask(): app.run(host='127.0.0.1', port=_PORT, debug=False, use_reloader=False) if __name__ == '__main__': _setup_logging() logger.info('SisterCo MFE avvio') t = threading.Thread(target=_run_flask, daemon=True) t.start() _wait_for_server() win = webview.create_window( 'SisterCo MFE — MyICR Suite', f'http://127.0.0.1:{_PORT}/', width=820, height=620, resizable=True, min_size=(700, 500), maximized=True, ) def _bring_to_front(): try: import ctypes, time time.sleep(0.5) hwnd = ctypes.windll.user32.FindWindowW(None, 'SisterCo MFE — MyICR Suite') if hwnd: fg = ctypes.windll.user32.GetForegroundWindow() fg_tid = ctypes.windll.user32.GetWindowThreadProcessId(fg, None) our_tid = ctypes.windll.kernel32.GetCurrentThreadId() ctypes.windll.user32.AttachThreadInput(fg_tid, our_tid, True) ctypes.windll.user32.BringWindowToTop(hwnd) ctypes.windll.user32.ShowWindow(hwnd, 3) # SW_SHOWMAXIMIZED ctypes.windll.user32.SetForegroundWindow(hwnd) ctypes.windll.user32.AttachThreadInput(fg_tid, our_tid, False) except Exception: pass webview.start(_bring_to_front)