把巨型文件 douyin/handlers/douyin_screenshot_manager.py 按职责拆成 douyin/handlers/screenshot_manager/ 子包: - _common (79) 共享 logger / 全局状态 / 异步计数器 - paths (205) OCR 目录、调试存档目录、各类路径 - save_files (173) save_latest_attempt / save_last_success / save_*_from_file / cleanup_ocr_dir - async_save (360) _debug_save_loop + 工作线程启停(global 状态走 _common._state.X) - cleanup (293) debug_captures 目录清理与轮转 - save_image (731) save_debug_image 主入口(最大头) 原文件改为 79 行 re-export shim,30 个公共符号全部保留。 外部 29 处 save_debug_image / 6 处 save_latest_attempt / 等 import 路径不变,零调用方修改。 回归: - 子包 7 文件 + shim compileall 通过 - pytest 353 passed / 1 skipped(与拆分前完全一致) - 旧 import 路径与新子包路径返回同一函数对象 - AST 验证 30 个期望符号全部正确暴露 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
80 lines
2.8 KiB
Python
80 lines
2.8 KiB
Python
from __future__ import annotations
|
|
|
|
"""screenshot_manager._common - 共享 logger + 全局状态 + 异步保存计数器。
|
|
|
|
被本子包的 async_save / save_files / cleanup / save_image 共享:
|
|
- _LOGGER / _mgr_log:统一日志出口
|
|
- _debug_save_queue / _debug_save_worker_started / _debug_save_worker_lock
|
|
/ _debug_save_shutdown_event:异步保存的工作线程状态
|
|
- _async_save_*_count / _async_save_counters_lock:异步计数器
|
|
- _SKIP_DEBUG_SAVE_IN_HOTLOOPS:高频路径跳过开关
|
|
"""
|
|
|
|
import os
|
|
import threading
|
|
from queue import Queue
|
|
from typing import Optional, Tuple
|
|
|
|
import logging as _logging
|
|
from PIL import Image
|
|
|
|
# 模块级 logger:原代码用 print 输出截图管理 / 调试图像清理日志,统一走 logging。
|
|
# 保留消息中已有的 "[截图管理]"/"[调试图像清理]" 前缀,避免改变历史搜索习惯。
|
|
_LOGGER = _logging.getLogger("douyin.handlers.douyin_screenshot_manager")
|
|
|
|
|
|
def _mgr_log(msg: str, level: str = "INFO") -> None:
|
|
"""统一截图管理日志出口;失败兜底 stdout。"""
|
|
try:
|
|
lv = str(level or "INFO").upper()
|
|
if lv == "ERROR":
|
|
_LOGGER.error(str(msg))
|
|
elif lv == "WARNING":
|
|
_LOGGER.warning(str(msg))
|
|
elif lv == "DEBUG":
|
|
_LOGGER.debug(str(msg))
|
|
else:
|
|
_LOGGER.info(str(msg))
|
|
except Exception:
|
|
try:
|
|
print(str(msg))
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
# 背景保存队列与工作线程(用于异步保存调试图像,避免阻塞主流程)
|
|
_debug_save_queue: "Queue[Tuple[Image.Image, str, bool, Optional[Tuple[int,int,int,int]], Optional[dict], str, str, str]]" = (Queue())
|
|
_debug_save_worker_started = False
|
|
_debug_save_worker_lock = threading.Lock() # 保护 worker 启动的双检查锁
|
|
_debug_save_shutdown_event = threading.Event() # 用于优雅关闭
|
|
|
|
# 异步保存计数(用于 GUI 显示)
|
|
_async_save_success_count = 0
|
|
_async_save_failure_count = 0
|
|
_async_save_counters_lock = threading.Lock()
|
|
|
|
# 环境开关:在高频路径中跳过调试图像保存以提升性能(默认启用跳过)
|
|
_SKIP_DEBUG_SAVE_IN_HOTLOOPS = bool(
|
|
os.environ.get("SKIP_DEBUG_SAVE_IN_HOTLOOPS", "1") == "1"
|
|
)
|
|
|
|
|
|
def get_async_save_counters() -> tuple[int, int]:
|
|
"""返回 (success_count, failure_count)"""
|
|
try:
|
|
with _async_save_counters_lock:
|
|
return int(_async_save_success_count), int(_async_save_failure_count)
|
|
except Exception:
|
|
return 0, 0
|
|
|
|
|
|
def reset_async_save_counters() -> None:
|
|
"""重置计数器(GUI 可调用)"""
|
|
try:
|
|
with _async_save_counters_lock:
|
|
global _async_save_success_count, _async_save_failure_count
|
|
_async_save_success_count = 0
|
|
_async_save_failure_count = 0
|
|
except Exception:
|
|
pass
|