import os import yaml # 模块级单例 _config = None _engine = None def load_config(): global _config if _config is not None: return _config config_path = 'config.yaml' template_path = 'config_template.yaml' if not os.path.exists(config_path): if os.path.exists(template_path): # 如果配置文件不存在但模板存在,则复制模板 import shutil shutil.copy(template_path, config_path) print(f"已从 {template_path} 创建新的配置文件 {config_path},请完善其中的配置信息后再运行。") exit(1) else: print(f"错误:配置文件 {config_path} 和模板 {template_path} 均不存在。") print(f"请创建 {template_path} 文件后再运行。") exit(1) with open(config_path, 'r') as f: _config = yaml.safe_load(f) return _config