Files
douyin_guaji-main/gui/gui_core_pkg/config.py
T
CodexandClaude Opus 4.7 ef6ca4a650 拆分 gui_core (1594 → 4 子模块 + 1207 行原文件保留 Runner)
把 gui/gui_core.py 中 12 个独立顶层函数 + RunParams dataclass 拆出到
gui/gui_core_pkg/ 子包;Runner 巨型类(1164 行 / 13 实例方法 / 用 self
状态)原文件保留,仅做 import re-export。

新建子包:
- adb (79)         find_adb_executable / list_adb_devices / resolve_adb_device
- wifi_adb (323)   7 个 WiFi-ADB 函数
- config (46)      CONFIG_PATH / read_config / write_config
- run_params (45)  RunParams dataclass

策略:Runner 类内部用了 find_adb_executable 等顶层函数,所以原文件
``gui/gui_core.py`` 通过 ``from gui.gui_core_pkg import *`` re-export
14 个公共符号供 Runner 内部和外部代码继续使用。

外部 import 路径全部不变:
``from gui.gui_core import find_adb_executable / RunParams / Runner`` 等
所有 14 个公共符号都保留。
- find_adb_executable 20 处外部引用 / read_config 17 处 / list_adb_devices
  16 处 / 其他 1-7 处,全部不需修改

回归:
- 5 个新文件 + 改写后 gui_core.py compileall 通过
- pytest 353 passed / 1 skipped
- find_adb_executable() 调用正常返回 adb.EXE 绝对路径
- read_config() 读到 126 个 config keys
- RunParams.is_dataclass = True,28 个字段
- Runner 类正确暴露

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-21 18:49:05 +08:00

47 lines
1.2 KiB
Python

"""gui_core_pkg.config - 配置文件读写。
- CONFIG_PATH config.json 绝对路径
- read_config 读取并返回 dict
- write_config 深度合并写回(保留未传字段)
"""
from __future__ import annotations
import json
import os
import shutil
from douyin.core import path_config
CONFIG_PATH = path_config.get_config_path()
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)
def write_config(cfg: dict) -> None:
# GUI 表单通常只生成“已知字段”的子集;直接覆盖写会丢失其它键(例如 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")