refactor: web_gui 中 5 个 ADB/config 函数改为薄包装
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
7d63a36125
commit
b96a67b6b8
1 file changed
+20
-67
+20
-67
@@ -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:
|
||||
|
||||
Reference in new issue
Block a user