This commit is contained in:
Hraulein
2026-05-09 20:00:30 +08:00
parent ab1fc775e9
commit d2fcb0df76
12 changed files with 604 additions and 235 deletions
+42 -20
View File
@@ -1,6 +1,7 @@
# python/core/task_mgr.py
# src/core/task.py
from pathlib import Path
from .logger import log
class TaskManager:
def __init__(self):
@@ -9,16 +10,18 @@ class TaskManager:
'rename_season_dir': [], 'rename_root_dir': []
}
self.dir_renames = set()
self.reserved_paths = set() # 全局路径防撞中心
self.reserved_paths = set()
def reserve_path(self, desired_path: Path) -> Path:
"""保证生成的路径全局唯一,冲突则自动追加数字 (如 .zh-CN.2.srt)"""
final_path = desired_path
idx = 2
while final_path.exists() or final_path in self.reserved_paths:
# 冲突时,在语言代码后缀前追加序号
final_path = desired_path.with_name(f"{desired_path.stem}.{idx}{desired_path.suffix}")
idx += 1
if final_path != desired_path:
log.debug(f"检测到路径冲突,已自动调整: {final_path.name}")
self.reserved_paths.add(final_path)
return final_path
@@ -43,38 +46,57 @@ class TaskManager:
self.tasks['rename_root_dir'].append((desc, old_dir.rename, new_dir))
self.dir_renames.add(old_dir)
def preview_and_confirm(self) -> bool:
def preview_and_confirm(self, auto_execute=False) -> bool:
all_empty = True
print("\n================== 待执行任务预览 ==================")
if not auto_execute:
log.info("" + "="*20 + " 待执行任务预览 " + "="*20)
labels = [
('extract', '🛠️ 阶段 1: 提取非SRT内置文本字幕'),
('extract', '🛠️ 阶段 1: 提取内置文本字幕'),
('clean', '🧹 阶段 2: 清理元数据广告'),
('rename_file', '📄 阶段 3: 视频与外挂字幕规范化重命名'),
('rename_season_dir', '📁 阶段 4: 季集目录规范化'),
('rename_root_dir', '🎬 阶段 5: 剧集/电影根目录规范化 (补充年份与标题)')
('rename_root_dir', '🎬 阶段 5: 剧集/电影根目录规范化')
]
for cat, label in labels:
if self.tasks[cat]:
all_empty = False
print(f"\n{label}")
for desc, _, _ in self.tasks[cat]:
print(f" - {desc}")
if not auto_execute:
log.info(f"\n{label}")
for desc, _, _ in self.tasks[cat]:
log.info(f" - {desc}")
print("\n====================================================")
if not auto_execute:
log.info("\n" + "="*56)
if all_empty:
print("无需执行任何操作,您的媒体库目前非常完美!")
if not auto_execute: log.info("✨ 您的媒体库目前处于完美状态,无需任何操作。")
return False
ans = input("⚠️ 请确认是否真实执行以上所有操作?(y/n): ")
return ans.lower() == 'y'
# 💡 守护模式接管,免除手工 y/n 敲击
if auto_execute:
log.info("🤖 守护模式已接管:发现待规范文件,自动确认并开始执行物理变更。")
return True
ans = input("⚠️ 是否确认执行以上物理变更?(y/n): ")
if ans.lower() == 'y':
log.debug("用户确认执行任务。")
return True
else:
log.debug("用户取消任务执行。")
return False
def execute(self):
stages = ['extract', 'clean', 'rename_file', 'rename_season_dir', 'rename_root_dir']
for cat in stages:
if not self.tasks[cat]: continue
for desc, func, arg in self.tasks[cat]:
print(f"执行 -> {desc.split(' -->')[0]}")
if isinstance(arg, tuple):
func(*arg)
else:
func(arg)
action_name = desc.split(' -->')[0]
log.info(f"正在执行 -> {action_name}")
try:
if isinstance(arg, tuple): func(*arg)
else: func(arg)
except Exception as e:
log.error(f"任务执行失败: {action_name} | 错误: {e}")