2025-04-04 15:24:02 +08:00
|
|
|
import os
|
2025-04-19 01:41:15 +08:00
|
|
|
|
|
|
|
import yaml
|
2025-04-05 22:48:31 +08:00
|
|
|
|
2025-04-06 12:44:18 +08:00
|
|
|
# 模块级单例
|
|
|
|
_config = None
|
|
|
|
_engine = None
|
|
|
|
|
2025-04-04 15:24:02 +08:00
|
|
|
|
|
|
|
def load_config():
|
2025-04-06 12:44:18 +08:00
|
|
|
global _config
|
|
|
|
if _config is not None:
|
|
|
|
return _config
|
|
|
|
|
2025-04-04 15:24:02 +08:00
|
|
|
config_path = 'config.yaml'
|
2025-04-19 13:11:41 +08:00
|
|
|
template_path = 'config_template.yaml'
|
|
|
|
|
2025-04-04 15:24:02 +08:00
|
|
|
if not os.path.exists(config_path):
|
2025-04-19 13:11:41 +08:00
|
|
|
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)
|
2025-04-05 01:02:15 +08:00
|
|
|
|
2025-04-04 15:24:02 +08:00
|
|
|
with open(config_path, 'r') as f:
|
2025-04-06 12:44:18 +08:00
|
|
|
_config = yaml.safe_load(f)
|
|
|
|
return _config
|
2025-04-04 15:24:02 +08:00
|
|
|
|