304 lines
10 KiB
Python
304 lines
10 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
"""Check that desktop GUI and WebUI expose the same config keys.
|
|
|
|
The check compares:
|
|
- keys saved by gui/gui_app.py to config.json
|
|
- keys loaded by gui/gui_app.py from config.json
|
|
- keys bound in webui/index.html through data-key/data-mirror-key
|
|
|
|
It is intentionally static and side-effect free, so it can run in local
|
|
verification without opening the GUI or starting the Web server.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
GUI_APP = ROOT / "gui" / "gui_app.py"
|
|
# load_config_to_ui / save_ui_to_config 已抽到 components/config_io.py,
|
|
# 但 GUI 启动时仍通过 main() 内的薄包装调用它们。本工具的同步检查需要从
|
|
# config_io.py 抽 cfg/cfg2 赋值,从 gui_app.py 抽 gui_cfg 引用(仍在 main 里)。
|
|
GUI_CONFIG_IO = ROOT / "gui" / "components" / "config_io.py"
|
|
GUI_CONFIG_IO_HELPERS = ROOT / "gui" / "components" / "config_io_helpers.py"
|
|
# realtime_tab UI 抽到 components/realtime_tab.py 后,gui_cfg 引用也跟过去了。
|
|
GUI_REALTIME_TAB = ROOT / "gui" / "components" / "realtime_tab.py"
|
|
WEB_INDEX = ROOT / "webui" / "index.html"
|
|
WEB_CONFIG_TEMPLATES = ROOT / "webui" / "js" / "config_panel_templates.js"
|
|
MANIFEST_PATH = ROOT / "tools" / "dev" / "web_gui_config_manifest.json"
|
|
|
|
IGNORED_CONTAINER_KEYS = {
|
|
"debug_captures",
|
|
"logging",
|
|
"logging.performance_tracking",
|
|
"gui",
|
|
"gui.realtime_status",
|
|
}
|
|
|
|
GUI_RUNTIME_ONLY_KEYS = {
|
|
# Local file-log switches are consumed by GUI/Web backends but are not part
|
|
# of gui_app.py's main left-pane config form.
|
|
"log_unified_enabled",
|
|
"log_unified_path",
|
|
"logging.use_dated_logs",
|
|
# These are right-side runtime refresh preferences in gui_app.py.
|
|
"gui.realtime_status.refresh_interval_ms",
|
|
"gui.realtime_status.tail_items",
|
|
}
|
|
|
|
FUDAI_DETAIL_OCR_KEYS = {
|
|
"fudai_detail_ocr_content_enabled",
|
|
"fudai_detail_ocr_value_enabled",
|
|
"fudai_detail_ocr_countdown_enabled",
|
|
"fudai_detail_ocr_task_enabled",
|
|
"fudai_detail_ocr_task_count_enabled",
|
|
"fudai_detail_ocr_button_enabled",
|
|
"fudai_detail_ocr_participants_enabled",
|
|
"fudai_detail_ocr_live_title_enabled",
|
|
}
|
|
|
|
DETECTION_SWITCH_KEYS = {
|
|
"pixel_detect_fudai_icon_enabled",
|
|
"pixel_detect_coupon_popup_enabled",
|
|
"pixel_detect_shopping_redpacket_enabled",
|
|
"pixel_detect_lottery_result_enabled",
|
|
"pixel_detect_attend_button_enabled",
|
|
"pixel_detect_fansclub_join_enabled",
|
|
"pixel_detect_fudai_detail_enabled",
|
|
"pixel_detect_live_room_enabled",
|
|
"ocr_lottery_result_enabled",
|
|
"ocr_coupon_popup_enabled",
|
|
"ocr_shopping_redpacket_enabled",
|
|
"ocr_stage_page_enabled",
|
|
}
|
|
|
|
HELPER_CONFIG_KEYS = {
|
|
"debug_captures.enabled",
|
|
"debug_captures.auto_cleanup",
|
|
"debug_captures.keep_days",
|
|
"debug_captures.max_files",
|
|
"debug_captures.async_save",
|
|
"debug_captures.log_on_complete",
|
|
"logging.beautify_output",
|
|
"logging.color_output",
|
|
"logging.show_timestamps",
|
|
"logging.enable_monitor",
|
|
"logging.keep_days",
|
|
"logging.compress_logs",
|
|
"logging.max_total_size_gb",
|
|
"logging.compress_after_days",
|
|
"logging.debug_detail_level",
|
|
"logging.error_detail_level",
|
|
"logging.action_log_format",
|
|
"logging.log_format_mode",
|
|
"logging.monitor_warning_mb",
|
|
"logging.monitor_critical_mb",
|
|
"logging.performance_tracking.enabled",
|
|
"logging.performance_tracking.track_function_calls",
|
|
}
|
|
|
|
|
|
def _read(path: Path) -> str:
|
|
return path.read_text(encoding="utf-8", errors="ignore")
|
|
|
|
|
|
def _normalize_key(parts: tuple[str | None, ...]) -> str:
|
|
return ".".join(part for part in parts if part)
|
|
|
|
|
|
def extract_gui_saved_keys(source: str) -> set[str]:
|
|
start = source.index("def save_ui_to_config")
|
|
end = source.index("write_config(cfg2)", start)
|
|
block = source[start:end]
|
|
keys: set[str] = set()
|
|
pattern = re.compile(
|
|
r'cfg2\["([^"]+)"\](?:\["([^"]+)"\])?(?:\["([^"]+)"\])?\s*='
|
|
)
|
|
for match in pattern.finditer(block):
|
|
key = _normalize_key(match.groups())
|
|
if key not in IGNORED_CONTAINER_KEYS:
|
|
keys.add(key)
|
|
if "fudai_detail_ocr_content_enabled" in block:
|
|
keys.update(FUDAI_DETAIL_OCR_KEYS)
|
|
if "pixel_detect_fudai_icon_enabled" in block:
|
|
keys.update(DETECTION_SWITCH_KEYS)
|
|
return keys
|
|
|
|
|
|
def extract_gui_loaded_keys(source: str) -> set[str]:
|
|
start = source.index("def load_config_to_ui")
|
|
end = source.index("def save_ui_to_config", start)
|
|
block = source[start:end]
|
|
keys = set(re.findall(r'(?<![A-Za-z0-9_])cfg\.get\("([^"]+)"', block))
|
|
for top_key in ("gui", "logging"):
|
|
pattern = re.compile(rf'cfg\.get\("{top_key}", \{{\}}\)\.get\("([^"]+)"')
|
|
for match in pattern.finditer(block):
|
|
keys.add(f"{top_key}.{match.group(1)}")
|
|
|
|
for group_name, prefix in (
|
|
("debug_captures_config", "debug_captures"),
|
|
("logging_config", "logging"),
|
|
("perf_cfg", "logging.performance_tracking"),
|
|
):
|
|
pattern = re.compile(rf'{group_name}\.get\("([^"]+)"')
|
|
for match in pattern.finditer(block):
|
|
keys.add(f"{prefix}.{match.group(1)}")
|
|
|
|
realtime_pattern = re.compile(r'gui_cfg\.get\("([^"]+)"')
|
|
for match in realtime_pattern.finditer(source):
|
|
keys.add(f"gui.realtime_status.{match.group(1)}")
|
|
|
|
if "fudai_detail_ocr_content_enabled" in block:
|
|
keys.update(FUDAI_DETAIL_OCR_KEYS)
|
|
if "pixel_detect_fudai_icon_enabled" in block:
|
|
keys.update(DETECTION_SWITCH_KEYS)
|
|
return {key for key in keys if key not in IGNORED_CONTAINER_KEYS}
|
|
|
|
|
|
def extract_web_keys(source: str) -> set[str]:
|
|
keys = set(re.findall(r'data-key="([^"]+)"', source))
|
|
keys.update(re.findall(r'data-mirror-key="([^"]+)"', source))
|
|
return keys
|
|
|
|
|
|
def build_manifest() -> dict[str, object]:
|
|
gui_source = _read(GUI_APP)
|
|
config_io_source = _read(GUI_CONFIG_IO)
|
|
config_io_helpers_source = _read(GUI_CONFIG_IO_HELPERS)
|
|
realtime_tab_source = _read(GUI_REALTIME_TAB)
|
|
web_source = _read(WEB_INDEX) + "\n" + _read(WEB_CONFIG_TEMPLATES)
|
|
|
|
# save/load 函数现在在 components/config_io.py 里;gui_cfg 引用在 realtime_tab.py
|
|
gui_saved = extract_gui_saved_keys(config_io_source + "\n" + config_io_helpers_source)
|
|
gui_loaded = extract_gui_loaded_keys(
|
|
config_io_source + "\n" + config_io_helpers_source + "\n" + gui_source + "\n" + realtime_tab_source
|
|
)
|
|
gui_saved.update(HELPER_CONFIG_KEYS)
|
|
gui_loaded.update(HELPER_CONFIG_KEYS)
|
|
web_keys = extract_web_keys(web_source)
|
|
|
|
expected_gui_keys = web_keys - GUI_RUNTIME_ONLY_KEYS
|
|
return {
|
|
"generated_by": "tools/check_web_gui_config_sync.py --sync",
|
|
"shared_keys": sorted(expected_gui_keys),
|
|
"gui_runtime_only_keys": sorted(GUI_RUNTIME_ONLY_KEYS),
|
|
"gui_saved_keys": sorted(gui_saved),
|
|
"gui_loaded_keys": sorted(gui_loaded),
|
|
"web_keys": sorted(web_keys),
|
|
}
|
|
|
|
|
|
def write_manifest() -> None:
|
|
manifest = build_manifest()
|
|
MANIFEST_PATH.write_text(
|
|
json.dumps(manifest, ensure_ascii=False, indent=2) + "\n",
|
|
encoding="utf-8",
|
|
)
|
|
print(f"[OK] Wrote {MANIFEST_PATH.relative_to(ROOT)}")
|
|
|
|
|
|
def load_manifest() -> dict[str, object] | None:
|
|
if not MANIFEST_PATH.exists():
|
|
return None
|
|
try:
|
|
return json.loads(MANIFEST_PATH.read_text(encoding="utf-8"))
|
|
except json.JSONDecodeError:
|
|
return None
|
|
|
|
|
|
def compare_manifest(current: dict[str, object]) -> list[str]:
|
|
stored = load_manifest()
|
|
if stored is None:
|
|
return [
|
|
f"{MANIFEST_PATH.relative_to(ROOT)} is missing or invalid; "
|
|
"run tools/check_web_gui_config_sync.py --sync"
|
|
]
|
|
|
|
diffs: list[str] = []
|
|
for key in (
|
|
"shared_keys",
|
|
"gui_runtime_only_keys",
|
|
"gui_saved_keys",
|
|
"gui_loaded_keys",
|
|
"web_keys",
|
|
):
|
|
if stored.get(key) != current.get(key):
|
|
diffs.append(
|
|
f"{MANIFEST_PATH.relative_to(ROOT)} is stale for {key}; "
|
|
"run tools/check_web_gui_config_sync.py --sync"
|
|
)
|
|
return diffs
|
|
|
|
|
|
def check_sync() -> tuple[list[tuple[str, list[str]]], dict[str, object]]:
|
|
manifest = build_manifest()
|
|
gui_saved = set(manifest["gui_saved_keys"]) # type: ignore[arg-type]
|
|
gui_loaded = set(manifest["gui_loaded_keys"]) # type: ignore[arg-type]
|
|
web_keys = set(manifest["web_keys"]) # type: ignore[arg-type]
|
|
expected_gui_keys = set(manifest["shared_keys"]) # type: ignore[arg-type]
|
|
|
|
problems: list[tuple[str, list[str]]] = []
|
|
|
|
missing_in_web = sorted((gui_saved | gui_loaded) - web_keys - GUI_RUNTIME_ONLY_KEYS)
|
|
missing_in_gui_save = sorted(expected_gui_keys - gui_saved)
|
|
missing_in_gui_load = sorted(expected_gui_keys - gui_loaded)
|
|
saved_not_loaded = sorted(gui_saved - gui_loaded - GUI_RUNTIME_ONLY_KEYS)
|
|
loaded_not_saved = sorted(gui_loaded - gui_saved - GUI_RUNTIME_ONLY_KEYS)
|
|
|
|
if missing_in_web:
|
|
problems.append(("GUI exposes keys missing from WebUI", missing_in_web))
|
|
if missing_in_gui_save:
|
|
problems.append(("WebUI keys missing from gui_app.py save", missing_in_gui_save))
|
|
if missing_in_gui_load:
|
|
problems.append(("WebUI keys missing from gui_app.py load", missing_in_gui_load))
|
|
if saved_not_loaded:
|
|
problems.append(("gui_app.py saves keys it does not load", saved_not_loaded))
|
|
if loaded_not_saved:
|
|
problems.append(("gui_app.py loads keys it does not save", loaded_not_saved))
|
|
|
|
stale_manifest = compare_manifest(manifest)
|
|
if stale_manifest:
|
|
problems.append(("Generated sync manifest is not current", stale_manifest))
|
|
|
|
return problems, manifest
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(
|
|
description="Check or regenerate WebUI/Desktop GUI config sync metadata."
|
|
)
|
|
parser.add_argument(
|
|
"--sync",
|
|
action="store_true",
|
|
help="Regenerate tools/dev/web_gui_config_manifest.json from current code.",
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
if args.sync:
|
|
write_manifest()
|
|
return 0
|
|
|
|
problems, manifest = check_sync()
|
|
if problems:
|
|
print("[FAIL] WebUI and gui_app.py config keys are not synchronized.")
|
|
for title, keys in problems:
|
|
print(f"\n{title}:")
|
|
for key in keys:
|
|
print(f" - {key}")
|
|
return 1
|
|
|
|
print(
|
|
"[OK] WebUI and gui_app.py config keys are synchronized "
|
|
f"({len(manifest['shared_keys'])} shared keys)."
|
|
)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|