Implements comprehensive validation before PPTX conversion to catch errors early. Includes element-level validation (colors, fonts, table consistency) and system-level validation (geometry, resources). Supports standalone check command and automatic validation during conversion.
74 lines
2.3 KiB
Python
74 lines
2.3 KiB
Python
"""
|
||
验证结果数据结构
|
||
|
||
定义验证问题和验证结果的数据类。
|
||
"""
|
||
|
||
from dataclasses import dataclass, field
|
||
from typing import List
|
||
|
||
|
||
@dataclass
|
||
class ValidationIssue:
|
||
"""验证问题"""
|
||
level: str # "ERROR" | "WARNING" | "INFO"
|
||
message: str
|
||
location: str # "幻灯片 2, 元素 3"
|
||
code: str # "ELEMENT_OUT_OF_BOUNDS"
|
||
|
||
|
||
@dataclass
|
||
class ValidationResult:
|
||
"""验证结果"""
|
||
valid: bool # 是否有 ERROR
|
||
errors: List[ValidationIssue] = field(default_factory=list)
|
||
warnings: List[ValidationIssue] = field(default_factory=list)
|
||
infos: List[ValidationIssue] = field(default_factory=list)
|
||
|
||
def has_errors(self) -> bool:
|
||
"""是否有错误"""
|
||
return len(self.errors) > 0
|
||
|
||
def format_output(self) -> str:
|
||
"""格式化为命令行输出"""
|
||
lines = []
|
||
|
||
lines.append("🔍 正在检查 YAML 文件...\n")
|
||
|
||
# 错误
|
||
if self.errors:
|
||
lines.append(f"❌ 错误 ({len(self.errors)}):")
|
||
for issue in self.errors:
|
||
location_str = f"[{issue.location}] " if issue.location else ""
|
||
lines.append(f" {location_str}{issue.message}")
|
||
lines.append("")
|
||
|
||
# 警告
|
||
if self.warnings:
|
||
lines.append(f"⚠️ 警告 ({len(self.warnings)}):")
|
||
for issue in self.warnings:
|
||
location_str = f"[{issue.location}] " if issue.location else ""
|
||
lines.append(f" {location_str}{issue.message}")
|
||
lines.append("")
|
||
|
||
# 提示
|
||
if self.infos:
|
||
lines.append(f"ℹ️ 提示 ({len(self.infos)}):")
|
||
for issue in self.infos:
|
||
location_str = f"[{issue.location}] " if issue.location else ""
|
||
lines.append(f" {location_str}{issue.message}")
|
||
lines.append("")
|
||
|
||
# 总结
|
||
if not self.errors and not self.warnings and not self.infos:
|
||
lines.append("✅ 验证通过,未发现问题")
|
||
else:
|
||
summary_parts = []
|
||
if self.errors:
|
||
summary_parts.append(f"{len(self.errors)} 个错误")
|
||
if self.warnings:
|
||
summary_parts.append(f"{len(self.warnings)} 个警告")
|
||
lines.append(f"检查完成: 发现 {', '.join(summary_parts)}")
|
||
|
||
return "\n".join(lines)
|