Files
douyi_guaji_main/path_config.py
T

241 lines
5.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
路径配置模块:统一管理所有文件路径,便于整理文件夹结构而不影响代码
"""
import os
from typing import Optional
# 项目根目录(所有路径的基准)
PROJECT_ROOT = 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.json")
def get_logs_dir() -> str:
"""返回 logs 目录的路径"""
return os.path.join(PROJECT_ROOT, "logs")
def get_log_file_path() -> str:
"""返回统一日志文件的路径"""
return os.path.join(PROJECT_ROOT, "logs", "unified.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:
"""返回预约队列状态文件的路径"""
return os.path.join(PROJECT_ROOT, "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 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