1
0
Files
PPTX/validators/resource.py
lanyuanxiaoyao f1aae96a04 refactor: 重构外部模板系统,改为单文件模板库模式
主要变更:
- 将 templates_dir 参数改为 template_file,支持单个模板库 YAML 文件
- 添加模板库 YAML 验证功能
- 为模板添加 base_dir 支持,正确解析相对路径资源
- 内联模板与外部模板同名时改为警告(内联优先)
- 移除模板缓存机制,直接使用模板库字典
- 更新所有相关测试以适配新的模板加载方式

此重构简化了模板管理,使模板资源的路径解析更加清晰明确。
2026-03-05 13:27:12 +08:00

221 lines
6.9 KiB
Python

"""
资源验证器
验证 YAML 文件引用的外部资源是否存在。
"""
from pathlib import Path
from validators.result import ValidationIssue
from loaders.yaml_loader import load_yaml_file, validate_template_yaml, validate_template_library_yaml
class ResourceValidator:
"""资源验证器"""
def __init__(self, yaml_dir: Path, template_file: Path = None, yaml_data: dict = None):
"""
初始化资源验证器
Args:
yaml_dir: YAML 文件所在目录
template_file: 模板库文件路径(可选)
yaml_data: YAML 数据(用于检查内联模板)
"""
self.yaml_dir = yaml_dir
self.template_file = template_file
self.yaml_data = yaml_data or {}
# 加载模板库(如果提供)
self.template_library = None
self.template_base_dir = None
if self.template_file:
self.template_base_dir = self.template_file.parent
try:
self.template_library = load_yaml_file(self.template_file)
validate_template_library_yaml(self.template_library, str(self.template_file))
except Exception:
# 如果加载失败,在 validate_template 中会报错
pass
def validate_image(self, element, slide_index: int, elem_index: int) -> list:
"""
验证图片文件是否存在
Args:
element: 图片元素对象
slide_index: 幻灯片索引(从 1 开始)
elem_index: 元素索引(从 1 开始)
Returns:
验证问题列表
"""
issues = []
location = f"幻灯片 {slide_index}, 元素 {elem_index}"
if not hasattr(element, "src"):
return issues
src = element.src
if not src:
return issues
# 解析路径(支持相对路径和绝对路径)
src_path = Path(src)
if not src_path.is_absolute():
src_path = self.yaml_dir / src
# 检查文件是否存在
if not src_path.exists():
issues.append(
ValidationIssue(
level="ERROR",
message=f"图片文件不存在: {src}",
location=location,
code="IMAGE_FILE_NOT_FOUND",
)
)
return issues
def validate_template(self, slide_data: dict, slide_index: int) -> list:
"""
验证模板文件是否存在且结构正确
Args:
slide_data: 幻灯片数据字典
slide_index: 幻灯片索引(从 1 开始)
Returns:
验证问题列表
"""
issues = []
location = f"幻灯片 {slide_index}"
if "template" not in slide_data:
return issues
template_name = slide_data["template"]
if not template_name:
return issues
# 检查是否为内联模板
inline_templates = self.yaml_data.get("templates", {})
has_inline = template_name in inline_templates
has_external = False
# 检查外部模板是否存在
if self.template_library:
has_external = template_name in self.template_library.get('templates', {})
# 情况 1: 内联和外部模板同名 - WARNING
if has_inline and has_external:
issues.append(
ValidationIssue(
level="WARNING",
message=f"模板名称冲突: '{template_name}' 同时存在于内联模板和外部模板库,将优先使用内联模板",
location=location,
code="TEMPLATE_NAME_CONFLICT",
)
)
return issues # 优先使用内联模板,不再检查外部模板
# 情况 2: 仅有内联模板
if has_inline:
# 内联模板已在 validate_presentation_yaml 中验证
return issues
# 情况 3: 检查外部模板
if not self.template_file:
# 未提供模板库文件
issues.append(
ValidationIssue(
level="ERROR",
message=f"使用了模板但未指定模板库文件: {template_name}",
location=location,
code="TEMPLATE_FILE_NOT_SPECIFIED",
)
)
return issues
# 检查模板库文件是否存在
if not self.template_file.exists():
issues.append(
ValidationIssue(
level="ERROR",
message=f"模板库文件不存在: {self.template_file}",
location=location,
code="TEMPLATE_LIBRARY_FILE_NOT_FOUND",
)
)
return issues
# 检查模板名称是否在模板库中
if not has_external:
issues.append(
ValidationIssue(
level="ERROR",
message=f"模板库中找不到指定模板名称: {template_name}",
location=location,
code="TEMPLATE_NOT_FOUND_IN_LIBRARY",
)
)
return issues
return issues
def validate_template_vars(self, slide_data: dict, slide_index: int) -> list:
"""
验证模板变量是否满足要求
Args:
slide_data: 幻灯片数据字典
slide_index: 幻灯片索引(从 1 开始)
Returns:
验证问题列表
"""
issues = []
location = f"幻灯片 {slide_index}"
if "template" not in slide_data:
return issues
template_name = slide_data["template"]
if not template_name:
return issues
# 检查是否为内联模板
inline_templates = self.yaml_data.get("templates", {})
if template_name in inline_templates:
template_data = inline_templates[template_name]
else:
# 外部模板
if not self.template_library:
return issues
if template_name not in self.template_library.get('templates', {}):
return issues
template_data = self.template_library['templates'][template_name]
template_vars = template_data.get("vars", [])
required_vars = [v["name"] for v in template_vars if v.get("required", False)]
if not required_vars:
return issues
user_vars = slide_data.get("vars", {})
missing_vars = [v for v in required_vars if v not in user_vars]
if missing_vars:
issues.append(
ValidationIssue(
level="ERROR",
message=f"缺少模板必需变量: {', '.join(missing_vars)}",
location=location,
code="MISSING_TEMPLATE_REQUIRED_VARS",
)
)
return issues