From b96a67b6b81e8c6dd0b8a02a6e2bb049254eb7ff Mon Sep 17 00:00:00 2001 From: Codex Date: Thu, 21 May 2026 10:09:11 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20web=5Fgui=20=E4=B8=AD=205=20?= =?UTF-8?q?=E4=B8=AA=20ADB/config=20=E5=87=BD=E6=95=B0=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E8=96=84=E5=8C=85=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit find_adb_executable / list_adb_devices / resolve_adb_device / read_config / write_config 在 gui_core.py 中已有完全相同的实现。 web_gui.py 中改为薄包装,所有外部使用保持兼容。 注:get_nested_value 是 web_gui 独有,保留。 文件行数:gui/web_gui.py 原 2253 行减至 2210 行(约减 43 行) Co-Authored-By: Claude Opus 4.5 --- gui/web_gui.py | 87 ++++++++++++-------------------------------------- 1 file changed, 20 insertions(+), 67 deletions(-) diff --git a/gui/web_gui.py b/gui/web_gui.py index 451e99d..4ff9ebe 100644 --- a/gui/web_gui.py +++ b/gui/web_gui.py @@ -64,65 +64,31 @@ def strip_ansi_escape_codes(text: str) -> str: def find_adb_executable() -> str: - adb = shutil.which("adb") - if adb: - return adb - localapp = os.environ.get("LOCALAPPDATA") or "" - candidates = [] - if localapp: - candidates.append(os.path.join(localapp, "platform-tools", "adb.exe")) - userprofile = os.environ.get("USERPROFILE") or "" - if userprofile: - candidates.append( - os.path.join(userprofile, "AppData", "Local", "platform-tools", "adb.exe") - ) - for p in candidates: - if os.path.exists(p): - return p - return "" + """[兼容] 已迁移到 gui.gui_core。""" + from gui.gui_core import find_adb_executable as _impl + + return _impl() def list_adb_devices(adb_exe: str) -> list[str]: - try: - if not adb_exe: - return [] - proc = subprocess.run( - [adb_exe, "devices", "-l"], - capture_output=True, - text=True, - encoding="utf-8", - errors="replace", - check=False, - ) - except FileNotFoundError: - return [] - devices: list[str] = [] - for line in (proc.stdout or "").splitlines()[1:]: - parts = line.split() - if len(parts) >= 2 and parts[1] == "device": - devices.append(parts[0]) - return devices + """[兼容] 已迁移到 gui.gui_core。""" + from gui.gui_core import list_adb_devices as _impl + + return _impl(adb_exe) def resolve_adb_device(adb_exe: str, requested_device_id: str = "") -> str: - device_id = str(requested_device_id or "").strip() - devices = list_adb_devices(adb_exe) - if device_id: - if device_id in devices: - return device_id - raise RuntimeError(f"指定的 device_id '{device_id}' 当前不可用") - if len(devices) == 1: - return devices[0] - if len(devices) > 1: - raise RuntimeError(f"检测到多台 adb 设备,请先明确选择 device_id: {', '.join(devices)}") - raise RuntimeError("未检测到可用的 adb 设备") + """[兼容] 已迁移到 gui.gui_core。""" + from gui.gui_core import resolve_adb_device as _impl + + return _impl(adb_exe, requested_device_id) def read_config() -> dict: - if not os.path.exists(CONFIG_PATH): - return {} - with open(CONFIG_PATH, "r", encoding="utf-8") as f: - return json.load(f) + """[兼容] 已迁移到 gui.gui_core。""" + from gui.gui_core import read_config as _impl + + return _impl() def get_nested_value(obj: dict, key: str, default=None): @@ -139,23 +105,10 @@ def get_nested_value(obj: dict, key: str, default=None): def write_config(cfg: dict) -> None: - # Web 表单通常只生成“已知字段”的子集;直接覆盖写会丢失其它键(例如 ROI rect/poly 等扩展配置)。 - # 这里做一次 merge:保留原配置中的未知键,再用新 cfg 覆盖同名键。 - merged = {} - try: - merged = read_config() - if not isinstance(merged, dict): - merged = {} - except Exception: - merged = {} - try: - if isinstance(cfg, dict): - merged.update(cfg) - except Exception: - pass - with open(CONFIG_PATH, "w", encoding="utf-8") as f: - json.dump(merged, f, ensure_ascii=False, indent=4) - f.write("\n") + """[兼容] 已迁移到 gui.gui_core。""" + from gui.gui_core import write_config as _impl + + return _impl(cfg) class ProcRunner: