把巨型文件 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>
174 lines
4.8 KiB
Python
174 lines
4.8 KiB
Python
from __future__ import annotations
|
||
|
||
"""screenshot_manager.save_files - 双截图记录的保存接口。
|
||
|
||
实时操作截图(latest_attempt_<type>.png)每次都覆盖;
|
||
成功识别存档截图(last_success_<type>.png)仅在成功时保存。
|
||
"""
|
||
|
||
import os
|
||
from PIL import Image
|
||
|
||
from ._common import _mgr_log
|
||
from .paths import (
|
||
ensure_ocr_dir,
|
||
get_latest_attempt_path,
|
||
get_last_success_path,
|
||
get_ocr_dir,
|
||
)
|
||
|
||
|
||
def cleanup_ocr_dir(
|
||
keep_latest_attempt: bool = True,
|
||
keep_last_success: bool = True,
|
||
keep_success_files: bool = True,
|
||
max_files: int = 50,
|
||
) -> int:
|
||
"""清理OCR目录中的旧文件
|
||
|
||
参数:
|
||
keep_latest_attempt: 是否保留所有 latest_attempt_*.png 文件
|
||
keep_last_success: 是否保留所有 last_success_*.png 文件
|
||
keep_success_files: 是否保留所有 _success.png 文件
|
||
max_files: 最多保留的文件数(不包括特殊文件)
|
||
|
||
返回:
|
||
清理的文件数量
|
||
"""
|
||
try:
|
||
ocr_dir = get_ocr_dir()
|
||
if not os.path.exists(ocr_dir):
|
||
return 0
|
||
|
||
files = []
|
||
for f in os.listdir(ocr_dir):
|
||
file_path = os.path.join(ocr_dir, f)
|
||
if os.path.isfile(file_path) and f.lower().endswith(".png"):
|
||
# 检查是否为特殊文件
|
||
is_special = False
|
||
if f.startswith("latest_attempt_") and keep_latest_attempt:
|
||
is_special = True
|
||
elif f.startswith("last_success_") and keep_last_success:
|
||
is_special = True
|
||
elif f.endswith("_success.png") and keep_success_files:
|
||
is_special = True
|
||
|
||
if not is_special:
|
||
files.append((file_path, os.path.getmtime(file_path)))
|
||
|
||
# 按修改时间排序,保留最新的
|
||
files.sort(key=lambda x: x[1], reverse=True)
|
||
|
||
# 删除超出限制的文件
|
||
deleted_count = 0
|
||
for file_path, _ in files[max_files:]:
|
||
try:
|
||
os.remove(file_path)
|
||
deleted_count += 1
|
||
except Exception:
|
||
pass
|
||
|
||
return deleted_count
|
||
except Exception:
|
||
return 0
|
||
|
||
|
||
|
||
|
||
def save_latest_attempt(image: Image.Image, operation_type: str = "") -> bool:
|
||
"""保存实时操作截图(覆盖保存)
|
||
|
||
参数:
|
||
image: PIL Image 对象
|
||
operation_type: 操作类型(用于分类,如 "template_match", "pixel_detect", "ocr", "cut_pic")
|
||
|
||
返回:
|
||
是否保存成功
|
||
"""
|
||
try:
|
||
ensure_ocr_dir()
|
||
path = get_latest_attempt_path(operation_type)
|
||
image.save(path, "PNG")
|
||
return True
|
||
except Exception as e:
|
||
try:
|
||
_mgr_log(f"[截图管理] 保存 latest_attempt_{operation_type}.png 失败: {e}")
|
||
except Exception:
|
||
pass
|
||
return False
|
||
|
||
|
||
|
||
|
||
def save_last_success(image: Image.Image, operation_type: str = "") -> bool:
|
||
"""保存成功识别存档截图(不覆盖,仅成功时保存)
|
||
|
||
参数:
|
||
image: PIL Image 对象
|
||
operation_type: 操作类型(用于分类,如 "template_match", "pixel_detect", "ocr", "cut_pic")
|
||
|
||
返回:
|
||
是否保存成功
|
||
"""
|
||
try:
|
||
ensure_ocr_dir()
|
||
path = get_last_success_path(operation_type)
|
||
image.save(path, "PNG")
|
||
return True
|
||
except Exception as e:
|
||
try:
|
||
_mgr_log(f"[截图管理] 保存 last_success_{operation_type}.png 失败: {e}")
|
||
except Exception:
|
||
pass
|
||
return False
|
||
|
||
|
||
|
||
|
||
def save_attempt_from_file(source_path: str, operation_type: str = "") -> bool:
|
||
"""从文件保存实时操作截图
|
||
|
||
参数:
|
||
source_path: 源图片文件路径
|
||
operation_type: 操作类型
|
||
|
||
返回:
|
||
是否保存成功
|
||
"""
|
||
try:
|
||
if not os.path.exists(source_path):
|
||
return False
|
||
image = Image.open(source_path)
|
||
return save_latest_attempt(image, operation_type)
|
||
except Exception as e:
|
||
try:
|
||
_mgr_log(f"[截图管理] 从文件保存 latest_attempt.png 失败: {e}")
|
||
except Exception:
|
||
pass
|
||
return False
|
||
|
||
|
||
|
||
|
||
def save_success_from_file(source_path: str, operation_type: str = "") -> bool:
|
||
"""从文件保存成功识别存档截图
|
||
|
||
参数:
|
||
source_path: 源图片文件路径
|
||
operation_type: 操作类型
|
||
|
||
返回:
|
||
是否保存成功
|
||
"""
|
||
try:
|
||
if not os.path.exists(source_path):
|
||
return False
|
||
image = Image.open(source_path)
|
||
return save_last_success(image, operation_type)
|
||
except Exception as e:
|
||
try:
|
||
_mgr_log(f"[截图管理] 从文件保存 last_success.png 失败: {e}")
|
||
except Exception:
|
||
pass
|
||
return False
|