1
0

refactor: 重构外部模板系统,改为单文件模板库模式

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

此重构简化了模板管理,使模板资源的路径解析更加清晰明确。
This commit is contained in:
2026-03-05 13:26:29 +08:00
parent bd12fce14b
commit f1aae96a04
27 changed files with 2141 additions and 1988 deletions

View File

@@ -6,25 +6,37 @@
from pathlib import Path
from validators.result import ValidationIssue
from loaders.yaml_loader import load_yaml_file, validate_template_yaml
from loaders.yaml_loader import load_yaml_file, validate_template_yaml, validate_template_library_yaml
class ResourceValidator:
"""资源验证器"""
def __init__(self, yaml_dir: Path, template_dir: Path = None, yaml_data: dict = None):
def __init__(self, yaml_dir: Path, template_file: Path = None, yaml_data: dict = None):
"""
初始化资源验证器
Args:
yaml_dir: YAML 文件所在目录
template_dir: 模板文件目录(可选)
template_file: 模板文件路径(可选)
yaml_data: YAML 数据(用于检查内联模板)
"""
self.yaml_dir = yaml_dir
self.template_dir = template_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:
"""
验证图片文件是否存在
@@ -88,52 +100,66 @@ class ResourceValidator:
# 检查是否为内联模板
inline_templates = self.yaml_data.get("templates", {})
if template_name in inline_templates:
# 内联模板已在 validate_presentation_yaml 中验证,这里不需要额外验证
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
# 检查是否提供了模板目录(外部模板
if not self.template_dir:
# 情况 3: 检查外部模板
if not self.template_file:
# 未提供模板库文件
issues.append(
ValidationIssue(
level="ERROR",
message=f"使用了模板但未指定模板目录: {template_name}",
message=f"使用了模板但未指定模板库文件: {template_name}",
location=location,
code="TEMPLATE_DIR_NOT_SPECIFIED",
code="TEMPLATE_FILE_NOT_SPECIFIED",
)
)
return issues
# 解析模板文件路径
template_path = self.template_dir / template_name
if not template_path.suffix:
template_path = template_path.with_suffix(".yaml")
# 检查模板文件是否存在
if not template_path.exists():
# 检查模板文件是否存在
if not self.template_file.exists():
issues.append(
ValidationIssue(
level="ERROR",
message=f"模板文件不存在: {template_name}",
message=f"模板文件不存在: {self.template_file}",
location=location,
code="TEMPLATE_FILE_NOT_FOUND",
code="TEMPLATE_LIBRARY_FILE_NOT_FOUND",
)
)
return issues
# 验证模板文件结构
try:
template_data = load_yaml_file(template_path)
validate_template_yaml(template_data, str(template_path))
except Exception as e:
# 检查模板名称是否在模板库中
if not has_external:
issues.append(
ValidationIssue(
level="ERROR",
message=f"模板文件结构错误: {template_name} - {str(e)}",
message=f"模板库中找不到指定模板名称: {template_name}",
location=location,
code="TEMPLATE_STRUCTURE_ERROR",
code="TEMPLATE_NOT_FOUND_IN_LIBRARY",
)
)
return issues
return issues
@@ -164,21 +190,13 @@ class ResourceValidator:
template_data = inline_templates[template_name]
else:
# 外部模板
if not self.template_dir:
if not self.template_library:
return issues
template_path = self.template_dir / template_name
if not template_path.suffix:
template_path = template_path.with_suffix(".yaml")
if not template_path.exists():
if template_name not in self.template_library.get('templates', {}):
return issues
try:
template_data = load_yaml_file(template_path)
validate_template_yaml(template_data, str(template_path))
except Exception:
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)]