55 lines
2.0 KiB
Python
55 lines
2.0 KiB
Python
from __future__ import annotations
|
|
|
|
import hashlib
|
|
from pathlib import Path
|
|
|
|
import yaml
|
|
from django.conf import settings
|
|
|
|
|
|
CONFIG_PATH = Path(__file__).resolve().parents[1] / "templates" / "regulatory_info_package_templates_v1.yaml"
|
|
|
|
|
|
def load_template_config(path: str | Path | None = None) -> dict:
|
|
config_path = Path(path) if path else CONFIG_PATH
|
|
with config_path.open("r", encoding="utf-8") as handle:
|
|
payload = yaml.safe_load(handle) or {}
|
|
if payload.get("source_dir"):
|
|
payload["source_dir"] = str((Path(settings.BASE_DIR) / payload["source_dir"]).resolve())
|
|
return payload
|
|
|
|
|
|
def compute_config_hash(path: str | Path | None = None) -> str:
|
|
config_path = Path(path) if path else CONFIG_PATH
|
|
digest = hashlib.sha256()
|
|
digest.update(config_path.read_bytes())
|
|
return digest.hexdigest()
|
|
|
|
|
|
def validate_template_config(config: dict) -> list[str]:
|
|
errors: list[str] = []
|
|
source_dir = Path(config.get("source_dir") or "")
|
|
if not source_dir.exists():
|
|
errors.append(f"模板源目录不存在:{source_dir}")
|
|
templates = config.get("templates") or []
|
|
if len(templates) != 7:
|
|
errors.append("第1章监管信息模板配置必须包含 7 个模板。")
|
|
seen: set[str] = set()
|
|
for template in templates:
|
|
code = str(template.get("code") or "")
|
|
if not code:
|
|
errors.append("模板 code 不能为空。")
|
|
elif code in seen:
|
|
errors.append(f"模板 code 重复:{code}")
|
|
seen.add(code)
|
|
source_file = str(template.get("source_file") or "")
|
|
output_name = str(template.get("output_name") or "")
|
|
if not source_file:
|
|
errors.append(f"模板 {code} 缺少 source_file。")
|
|
elif source_dir.exists() and not (source_dir / source_file).exists():
|
|
errors.append(f"模板源文件不存在:{source_file}")
|
|
if not output_name:
|
|
errors.append(f"模板 {code} 缺少 output_name。")
|
|
return errors
|
|
|