Files
douyi_guaji_main/gui/components/config_io_helpers.py
T
2026-06-03 17:39:53 +08:00

150 lines
8.5 KiB
Python

"""config_io 的辅助函数:文本域与 debug/logging 配置映射。"""
from __future__ import annotations
import tkinter as tk
from typing import Dict, List, Tuple
def _read_lines(widget: tk.Text) -> List[str]:
return [x.strip() for x in widget.get("1.0", "end").splitlines() if x.strip()]
def collect_text_lines(texts_: Dict[str, tk.Text]) -> Tuple[List[str], List[str], List[str], List[str], List[str]]:
want_lines = _read_lines(texts_["want_text"])
not_want_lines = _read_lines(texts_["not_want_text"])
yg_comment_text_lines = _read_lines(texts_["yg_comment_texts"])
yg_like_kw_lines = _read_lines(texts_["yg_like_keywords_text"])
yg_fav_kw_lines = _read_lines(texts_["yg_fav_keywords_text"])
return (
want_lines,
not_want_lines,
yg_comment_text_lines,
yg_like_kw_lines,
yg_fav_kw_lines,
)
def populate_text_widgets(
texts_: Dict[str, tk.Text],
want: List[str],
not_want: List[str],
yg_comment_texts_list: List[str],
yg_like_keywords_list: List[str],
yg_fav_keywords_list: List[str],
) -> None:
texts_["want_text"].delete("1.0", "end")
texts_["want_text"].insert("1.0", "\n".join(want))
texts_["not_want_text"].delete("1.0", "end")
texts_["not_want_text"].insert("1.0", "\n".join(not_want))
texts_["yg_comment_texts"].delete("1.0", "end")
texts_["yg_comment_texts"].insert("1.0", "\n".join(yg_comment_texts_list))
texts_["yg_like_keywords_text"].delete("1.0", "end")
texts_["yg_like_keywords_text"].insert("1.0", "\n".join(yg_like_keywords_list))
texts_["yg_fav_keywords_text"].delete("1.0", "end")
texts_["yg_fav_keywords_text"].insert("1.0", "\n".join(yg_fav_keywords_list))
def apply_debug_captures_from_cfg(vars_: Dict[str, tk.Variable], cfg: dict) -> None:
debug_captures_config = cfg.get("debug_captures", {})
if isinstance(debug_captures_config, dict):
vars_["debug_captures_enabled_var"].set(bool(debug_captures_config.get("enabled", False)))
vars_["debug_captures_auto_cleanup_var"].set(bool(debug_captures_config.get("auto_cleanup", True)))
vars_["debug_captures_keep_days_var"].set(int(debug_captures_config.get("keep_days", 7) or 7))
vars_["debug_captures_max_files_var"].set(int(debug_captures_config.get("max_files", 2000) or 2000))
vars_["debug_captures_async_save_var"].set(bool(debug_captures_config.get("async_save", True)))
vars_["debug_captures_log_on_complete_var"].set(bool(debug_captures_config.get("log_on_complete", False)))
else:
vars_["debug_captures_auto_cleanup_var"].set(True)
vars_["debug_captures_keep_days_var"].set(7)
vars_["debug_captures_max_files_var"].set(2000)
def apply_logging_from_cfg(vars_: Dict[str, tk.Variable], cfg: dict) -> None:
logging_config = cfg.get("logging", {})
if not isinstance(logging_config, dict):
return
log_format_mode = _normalize_log_format_mode(logging_config.get("log_format_mode", "simple"))
derived_logging = _derive_logging_detail_fields(log_format_mode)
vars_["logging_beautify_var"].set(bool(logging_config.get("beautify_output", True)))
vars_["logging_color_var"].set(bool(logging_config.get("color_output", True)))
vars_["logging_show_timestamps_var"].set(bool(logging_config.get("show_timestamps", True)))
vars_["logging_use_dated_logs_var"].set(bool(logging_config.get("use_dated_logs", True)))
vars_["logging_enable_monitor_var"].set(bool(logging_config.get("enable_monitor", True)))
vars_["log_unified_enabled_var"].set(bool(cfg.get("log_unified_enabled", True)))
vars_["log_unified_path_var"].set(str(cfg.get("log_unified_path", "logs/unified.log") or "logs/unified.log"))
vars_["logging_keep_days_var"].set(int(logging_config.get("keep_days", 30) or 30))
vars_["logging_compress_logs_var"].set(bool(logging_config.get("compress_logs", True)))
vars_["logging_max_total_size_gb_var"].set(float(logging_config.get("max_total_size_gb", 10.0) or 10.0))
vars_["logging_compress_after_days_var"].set(int(logging_config.get("compress_after_days", 7) or 7))
vars_["logging_debug_level_var"].set(derived_logging["debug_detail_level"])
vars_["logging_error_level_var"].set(derived_logging["error_detail_level"])
vars_["logging_action_format_var"].set(derived_logging["action_log_format"])
vars_["logging_format_mode_var"].set(log_format_mode)
perf_cfg = logging_config.get("performance_tracking", {})
if isinstance(perf_cfg, dict):
vars_["logging_performance_enabled_var"].set(bool(perf_cfg.get("enabled", True)))
vars_["logging_track_functions_var"].set(bool(perf_cfg.get("track_function_calls", True)))
vars_["logging_monitor_warning_mb_var"].set(float(logging_config.get("monitor_warning_mb", 500.0) or 500.0))
vars_["logging_monitor_critical_mb_var"].set(float(logging_config.get("monitor_critical_mb", 2000.0) or 2000.0))
def write_debug_captures_to_cfg(cfg2: dict, vars_: Dict[str, tk.Variable]) -> None:
if "debug_captures" not in cfg2 or not isinstance(cfg2["debug_captures"], dict):
cfg2["debug_captures"] = {}
cfg2["debug_captures"]["enabled"] = bool(vars_["debug_captures_enabled_var"].get())
cfg2["debug_captures"]["auto_cleanup"] = bool(vars_["debug_captures_auto_cleanup_var"].get())
cfg2["debug_captures"]["keep_days"] = int(vars_["debug_captures_keep_days_var"].get() or 7)
cfg2["debug_captures"]["max_files"] = int(vars_["debug_captures_max_files_var"].get() or 2000)
cfg2["debug_captures"]["async_save"] = bool(vars_["debug_captures_async_save_var"].get())
cfg2["debug_captures"]["log_on_complete"] = bool(vars_["debug_captures_log_on_complete_var"].get())
def write_logging_to_cfg(cfg2: dict, vars_: Dict[str, tk.Variable]) -> None:
if "logging" not in cfg2 or not isinstance(cfg2["logging"], dict):
cfg2["logging"] = {}
log_format_mode = _normalize_log_format_mode(vars_["logging_format_mode_var"].get())
derived_logging = _derive_logging_detail_fields(log_format_mode)
cfg2["logging"]["beautify_output"] = bool(vars_["logging_beautify_var"].get())
cfg2["logging"]["color_output"] = bool(vars_["logging_color_var"].get())
cfg2["logging"]["show_timestamps"] = bool(vars_["logging_show_timestamps_var"].get())
cfg2["logging"]["use_dated_logs"] = bool(vars_["logging_use_dated_logs_var"].get())
cfg2["logging"]["enable_monitor"] = bool(vars_["logging_enable_monitor_var"].get())
cfg2["logging"]["keep_days"] = int(vars_["logging_keep_days_var"].get() or 30)
cfg2["logging"]["compress_logs"] = bool(vars_["logging_compress_logs_var"].get())
cfg2["logging"]["max_total_size_gb"] = float(vars_["logging_max_total_size_gb_var"].get() or 10.0)
cfg2["logging"]["compress_after_days"] = int(vars_["logging_compress_after_days_var"].get() or 7)
cfg2["logging"]["debug_detail_level"] = derived_logging["debug_detail_level"]
cfg2["logging"]["error_detail_level"] = derived_logging["error_detail_level"]
cfg2["logging"]["action_log_format"] = derived_logging["action_log_format"]
cfg2["logging"]["log_format_mode"] = log_format_mode
cfg2["logging"]["stage_diag_verbose"] = bool(vars_["stage_diag_verbose_var"].get())
cfg2["logging"]["monitor_warning_mb"] = float(vars_["logging_monitor_warning_mb_var"].get() or 500.0)
cfg2["logging"]["monitor_critical_mb"] = float(vars_["logging_monitor_critical_mb_var"].get() or 2000.0)
if "performance_tracking" not in cfg2["logging"] or not isinstance(cfg2["logging"]["performance_tracking"], dict):
cfg2["logging"]["performance_tracking"] = {}
cfg2["logging"]["performance_tracking"]["enabled"] = bool(vars_["logging_performance_enabled_var"].get())
cfg2["logging"]["performance_tracking"]["track_function_calls"] = bool(vars_["logging_track_functions_var"].get())
cfg2["log_unified_enabled"] = bool(vars_["log_unified_enabled_var"].get())
cfg2["log_unified_path"] = str(vars_["log_unified_path_var"].get() or "logs/unified.log")
def _normalize_log_format_mode(value: object) -> str:
mode = str(value or "simple").strip().lower()
return mode if mode in {"simple", "detailed"} else "simple"
def _derive_logging_detail_fields(log_format_mode: str) -> dict:
if _normalize_log_format_mode(log_format_mode) == "detailed":
return {
"debug_detail_level": "detailed",
"error_detail_level": "full",
"action_log_format": "detailed",
}
return {
"debug_detail_level": "normal",
"error_detail_level": "full",
"action_log_format": "concise",
}