import json import os from typing import Dict, Any, List class TemplateManager: """接口配置模板管理类 (Interface Template Management)""" def __init__(self, templates_dir: str): self.templates_dir = templates_dir os.makedirs(self.templates_dir, exist_ok=True) def save_template(self, name: str, mapping_data: Dict[str, Any]): """保存接口配置模板""" file_path = os.path.join(self.templates_dir, f"{name}.json") with open(file_path, 'w', encoding='utf-8') as f: json.dump(mapping_data, f, indent=4, ensure_ascii=False) def load_template(self, name: str) -> Dict[str, Any]: """加载接口配置模板""" file_path = os.path.join(self.templates_dir, f"{name}.json") if not os.path.exists(file_path): raise FileNotFoundError(f"Template {name} not found") with open(file_path, 'r', encoding='utf-8') as f: return json.load(f) def list_templates(self) -> List[str]: """列出所有可用模板""" return [f.replace('.json', '') for f in os.listdir(self.templates_dir) if f.endswith('.json')]