上次冲突解决时跳过了 982f57e (P0 清理) 和 b6ae970 (路径修复) 两个 commit,
本提交补回其中影响运行时正确性和文档一致性的 7 处。
E. 路径错位修复
- douyin/core/douyin_fudai.py: _reservations_state_path 与 room_pos_map
状态目录改为优先调用 path_config.get_reservations_path()
- douyin/core/path_config.py: get_reservations_path 注释明确"实际位置 + 写入方"
- gui/components/mapping_utils.py: 删除错误兜底 douyin/state/reservations.json
- gui/components/mapping_table.py: 同步删除 douyin/state/room_pos_map.json 错误兜底
- gui/web_gui.py: get_reservations() 内的兜底由 douyin/state/ 修正为 douyin/core/state/
- bin/run_cli.py: 移除对已删空文件 douyin_reservation_ops.py 的 shim 加载
- douyin/reservation/douyin_reservation_ops.py: 删除 0 字节空文件
F. .gitignore 补强
- 新增 *.bak_* 拦截带日期后缀的备份命名(修复 *.bak 命中失败的场景)
G. 依赖版本对齐
- requirements-minimal.txt: paddleocr/paddlepaddle 改为 >=3.0.0,与 requirements.txt 一致
- README.md: 安装指引中 paddlepaddle==2.5.2 改为 >=3.0.0
回归:全量 .py compileall 通过;7 项断言(path_config 路径、douyin_fudai 切换、
3 处兜底已清、shim 已移除、空文件已删、.gitignore 补强、版本对齐)全部 PASS;
test_filter_logging / check_web_gui_config_sync 通过。
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
196 lines
5.8 KiB
Python
196 lines
5.8 KiB
Python
"""
|
||
路径配置模块:统一管理所有文件路径,便于整理文件夹结构而不影响代码
|
||
"""
|
||
import os
|
||
from typing import Optional
|
||
|
||
# 项目根目录(所有路径的基准)
|
||
# __file__ 位于 <project_root>/douyin/core/path_config.py,
|
||
# 所以项目根是当前文件向上两级目录。
|
||
PROJECT_ROOT = os.path.dirname(
|
||
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||
)
|
||
|
||
|
||
def get_project_root() -> str:
|
||
"""返回项目根目录(绝对路径)"""
|
||
return PROJECT_ROOT
|
||
|
||
|
||
def get_config_path() -> str:
|
||
"""返回 config.json 的路径"""
|
||
return os.path.join(PROJECT_ROOT, "config", "config.json")
|
||
|
||
|
||
def get_logs_dir() -> str:
|
||
"""返回 logs 目录的路径"""
|
||
return os.path.join(PROJECT_ROOT, "logs")
|
||
|
||
|
||
def get_debug_log_path() -> str:
|
||
"""返回调试日志(debug.log)的路径"""
|
||
return os.path.join(get_logs_dir(), "debug.log")
|
||
|
||
def get_error_log_path() -> str:
|
||
"""返回错误日志(error.log)的路径"""
|
||
return os.path.join(get_logs_dir(), "error.log")
|
||
|
||
def get_log_file_path() -> str:
|
||
"""返回统一日志文件的路径"""
|
||
return os.path.join(PROJECT_ROOT, "logs", "unified.log")
|
||
|
||
|
||
def get_actions_log_path() -> str:
|
||
"""返回行为日志文件 (actions.log) 的路径"""
|
||
return os.path.join(PROJECT_ROOT, "logs", "actions.log")
|
||
|
||
|
||
def get_pic_dir() -> str:
|
||
"""返回 pic 目录的路径(OCR截图、临时截图等)"""
|
||
return os.path.join(PROJECT_ROOT, "pic")
|
||
|
||
|
||
def get_screenshot_path() -> str:
|
||
"""返回 screenshot.png 的路径(当前屏幕截图)"""
|
||
return os.path.join(PROJECT_ROOT, "pic", "screenshot.png")
|
||
|
||
|
||
def get_state_dir() -> str:
|
||
"""返回 state 目录的路径(状态文件)"""
|
||
return os.path.join(PROJECT_ROOT, "state")
|
||
|
||
|
||
def get_reservations_path() -> str:
|
||
"""返回预约队列状态文件的路径(实际位置:douyin/core/state/reservations.json)。
|
||
|
||
该文件由 fudai_analyse 在 douyin/core/douyin_fudai.py 中写入,
|
||
供 WebGUI / 桌面 GUI 跨进程读取。
|
||
"""
|
||
return os.path.join(PROJECT_ROOT, "douyin", "core", "state", "reservations.json")
|
||
|
||
|
||
def get_high_value_rooms_path() -> str:
|
||
"""返回高价值房间状态文件的路径"""
|
||
return os.path.join(PROJECT_ROOT, "state", "high_value_rooms.json")
|
||
|
||
|
||
def get_assets_dir() -> str:
|
||
"""返回 assets 目录的路径(模板图片等)"""
|
||
return os.path.join(PROJECT_ROOT, "assets")
|
||
|
||
|
||
def get_like_template_path() -> str:
|
||
"""返回点赞图标模板的路径"""
|
||
return os.path.join(PROJECT_ROOT, "assets", "like.png")
|
||
|
||
|
||
def get_fav_template_path() -> str:
|
||
"""返回收藏图标模板的路径"""
|
||
return os.path.join(PROJECT_ROOT, "assets", "fav.png")
|
||
|
||
|
||
def get_desktop_template_path() -> str:
|
||
"""返回桌面图标模板的路径"""
|
||
return os.path.join(PROJECT_ROOT, "assets", "desktop.png")
|
||
|
||
|
||
def get_webui_dir() -> str:
|
||
"""返回 webui 目录的路径"""
|
||
return os.path.join(PROJECT_ROOT, "webui")
|
||
|
||
|
||
def get_pic_file_path(filename: str) -> str:
|
||
"""获取 pic 目录下指定文件的路径
|
||
|
||
Args:
|
||
filename: 文件名,可以是子目录/文件名,如 'diag_live_plaza.png' 或 'start/latest.png'
|
||
"""
|
||
return os.path.join(PROJECT_ROOT, "pic", filename)
|
||
|
||
|
||
def get_cut_pic_path(cut_pic_name: str) -> str:
|
||
"""获取裁剪后的图片路径(用于OCR)
|
||
|
||
Args:
|
||
cut_pic_name: 图片名称(不含扩展名),如 '直播广场'
|
||
"""
|
||
return os.path.join(PROJECT_ROOT, "pic", f"{cut_pic_name}.png")
|
||
|
||
|
||
def get_window_dump_path() -> str:
|
||
"""返回 window_dump.xml 的路径"""
|
||
return os.path.join(PROJECT_ROOT, "pic", "window_dump.xml")
|
||
|
||
|
||
def get_robot_verify_pic_path(save_subdir: bool = False) -> str:
|
||
"""返回机器人验证图片的路径
|
||
|
||
Args:
|
||
save_subdir: 是否在 pic/save 子目录中
|
||
"""
|
||
if save_subdir:
|
||
return os.path.join(PROJECT_ROOT, "pic", "save", "机器人验证.png")
|
||
return os.path.join(PROJECT_ROOT, "pic", "机器人验证.png")
|
||
|
||
|
||
def get_run_cli_path() -> str:
|
||
"""返回 run_cli.py 的路径(绝对路径)"""
|
||
return os.path.abspath(os.path.join(PROJECT_ROOT, "bin", "run_cli.py"))
|
||
|
||
|
||
def ensure_dir(path: str) -> None:
|
||
"""确保目录存在,不存在则创建"""
|
||
dir_path = os.path.dirname(path) if os.path.isfile(path) else path
|
||
os.makedirs(dir_path, exist_ok=True)
|
||
|
||
|
||
def find_executable(cmd: str) -> Optional[str]:
|
||
"""尝试在系统环境中定位可执行命令的完整路径。
|
||
|
||
处理策略:
|
||
- 若 `cmd` 已是存在的路径则直接返回;
|
||
- 使用 `shutil.which` 尝试查找;
|
||
- 在 Windows 上回退到 `where`,在类 Unix 上回退到 `command -v` / `which`。
|
||
|
||
返回命令的绝对路径,找不到时返回 `None`。
|
||
"""
|
||
import shutil
|
||
import subprocess
|
||
import sys
|
||
import os
|
||
|
||
if not cmd:
|
||
return None
|
||
# 如果传入的是已有文件路径,直接返回绝对路径
|
||
if os.path.exists(cmd):
|
||
return os.path.abspath(cmd)
|
||
|
||
# 首选 shutil.which
|
||
try:
|
||
found = shutil.which(cmd)
|
||
if found:
|
||
return found
|
||
except Exception:
|
||
pass
|
||
|
||
# 平台回退检查
|
||
try:
|
||
if sys.platform.startswith("win"):
|
||
# 使用 where 查找(Windows)
|
||
proc = subprocess.run(["where", cmd], capture_output=True, text=True)
|
||
if proc.returncode == 0 and proc.stdout:
|
||
first = proc.stdout.splitlines()[0].strip()
|
||
if first:
|
||
return first
|
||
else:
|
||
# 类 Unix:尝试 command -v 再 which
|
||
proc = subprocess.run(["/bin/sh", "-c", f"command -v {cmd} || which {cmd}"], capture_output=True, text=True)
|
||
if proc.returncode == 0 and proc.stdout:
|
||
first = proc.stdout.splitlines()[0].strip()
|
||
if first:
|
||
return first
|
||
except Exception:
|
||
pass
|
||
|
||
return None
|