fix:实现基础功能
This commit is contained in:
44
config/config.py
Normal file
44
config/config.py
Normal file
@@ -0,0 +1,44 @@
|
||||
# 尝试导入 toml 解析库 (兼容 Python 3.11+ 和旧版本)
|
||||
import os
|
||||
import sys
|
||||
|
||||
try:
|
||||
import tomllib as toml # Python 3.11+ 内置
|
||||
except ImportError:
|
||||
try:
|
||||
import tomli as toml # 需要 pip install tomli
|
||||
except ImportError:
|
||||
print("错误: 缺少 TOML 解析库。请运行: pip install tomli")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
# ==========================================
|
||||
# 1. 配置加载 (Config Loader)
|
||||
# ==========================================
|
||||
def load_config(config_path="config.toml"):
|
||||
"""读取 TOML 配置文件并转换为扁平字典"""
|
||||
if not os.path.exists(config_path):
|
||||
print(f"错误: 找不到配置文件 {config_path}")
|
||||
sys.exit(1)
|
||||
|
||||
try:
|
||||
with open(config_path, "rb") as f:
|
||||
data = toml.load(f)
|
||||
|
||||
# 将 TOML 的层级结构映射回原本的扁平结构,保持代码其余部分不用大改
|
||||
config = {
|
||||
"source_file": data["paths"]["source_file"],
|
||||
"output_folder": data["paths"]["output_folder"],
|
||||
"excel_file": data["paths"]["excel_file"],
|
||||
"image_folder": data["paths"]["image_folder"],
|
||||
"fonts_dir": data["paths"]["fonts_dir"],
|
||||
"class_name": data["class_info"]["class_name"],
|
||||
"teachers": data["class_info"]["teachers"],
|
||||
"default_comment": data["defaults"].get("default_comment", "暂无评语"),
|
||||
"age_group": data["defaults"].get("age_group", "大班上学期"),
|
||||
"ai": data["ai"]
|
||||
}
|
||||
return config
|
||||
except Exception as e:
|
||||
print(f"读取配置文件出错: {e}")
|
||||
sys.exit(1)
|
||||
Reference in New Issue
Block a user