fix:修复模板问题,修复系统操作逻辑,修复系统的一些BUG
This commit is contained in:
@@ -18,24 +18,27 @@ except ImportError:
|
||||
# 如果没安装,提供一个 fallback 提示
|
||||
toml_write = None
|
||||
|
||||
|
||||
def get_base_dir():
|
||||
if getattr(sys, 'frozen', False):
|
||||
if getattr(sys, "frozen", False):
|
||||
return os.path.dirname(sys.executable)
|
||||
else:
|
||||
# 假设当前文件在项目根目录或根目录下的某个文件夹中
|
||||
return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
|
||||
def get_resource_path(relative_path):
|
||||
base_path = get_base_dir()
|
||||
external_path = os.path.join(base_path, relative_path)
|
||||
if os.path.exists(external_path):
|
||||
return external_path
|
||||
if getattr(sys, 'frozen', False):
|
||||
if getattr(sys, "frozen", False):
|
||||
internal_path = os.path.join(sys._MEIPASS, relative_path)
|
||||
if os.path.exists(internal_path):
|
||||
return internal_path
|
||||
return external_path
|
||||
|
||||
|
||||
# ==========================================
|
||||
# 1. 配置加载 (Config Loader)
|
||||
# ==========================================
|
||||
@@ -44,7 +47,7 @@ def load_config(config_filename="config.toml"):
|
||||
|
||||
if not os.path.exists(config_path):
|
||||
# 如果彻底找不到,返回一个最小化的默认值,防止程序奔溃
|
||||
return { "source_file": "", "ai": {"api_key": ""}, "teachers": [] }
|
||||
return {"source_file": "", "ai": {"api_key": ""}, "teachers": []}
|
||||
|
||||
try:
|
||||
with open(config_path, "rb") as f:
|
||||
@@ -59,20 +62,32 @@ def load_config(config_filename="config.toml"):
|
||||
|
||||
config = {
|
||||
"root_path": base_dir,
|
||||
"data_folder": os.path.join(os.path.join("data")),
|
||||
# 扁平化映射
|
||||
"source_file": get_resource_path(os.path.join("templates", paths.get("source_file", ""))),
|
||||
"excel_file": get_resource_path(os.path.join("data", paths.get("excel_file", ""))),
|
||||
"image_folder": get_resource_path(os.path.join("data", paths.get("image_folder", ""))),
|
||||
"source_file": get_resource_path(
|
||||
os.path.join("templates", paths.get("source_file", ""))
|
||||
),
|
||||
"excel_file": get_resource_path(
|
||||
os.path.join("data", paths.get("excel_file", ""))
|
||||
),
|
||||
"image_folder": get_resource_path(
|
||||
os.path.join("data", paths.get("image_folder", ""))
|
||||
),
|
||||
"fonts_dir": get_resource_path(paths.get("fonts_dir", "fonts")),
|
||||
"output_folder": os.path.join(base_dir, paths.get("output_folder", "output")),
|
||||
"signature_image": get_resource_path(os.path.join("data", paths.get("signature_image", ""))),
|
||||
|
||||
"output_folder": os.path.join(
|
||||
base_dir, paths.get("output_folder", "output")
|
||||
),
|
||||
"signature_image": get_resource_path(
|
||||
os.path.join("data", paths.get("signature_image", ""))
|
||||
),
|
||||
"class_name": class_info.get("class_name", "未命名班级"),
|
||||
"teachers": class_info.get("teachers", []),
|
||||
"class_type": class_info.get("class_type", 0),
|
||||
"default_comment": defaults.get("default_comment", "暂无评语"),
|
||||
"age_group": defaults.get("age_group", "大班上学期"),
|
||||
"ai": data.get("ai", {"api_key": "", "api_url": "", "model":"","prompt":""}),
|
||||
"ai": data.get(
|
||||
"ai", {"api_key": "", "api_url": "", "model": "", "prompt": ""}
|
||||
),
|
||||
}
|
||||
return config
|
||||
|
||||
@@ -80,6 +95,7 @@ def load_config(config_filename="config.toml"):
|
||||
print(f"解析配置文件失败: {e}")
|
||||
return {}
|
||||
|
||||
|
||||
# ==========================================
|
||||
# 2. 配置保存 (Config Saver)
|
||||
# ==========================================
|
||||
@@ -95,11 +111,15 @@ def save_config(config_data, config_filename="config.toml"):
|
||||
new_data = {
|
||||
"paths": {
|
||||
"source_file": os.path.basename(config_data.get("source_file", "")),
|
||||
"output_folder": os.path.basename(config_data.get("output_folder", "output")),
|
||||
"output_folder": os.path.basename(
|
||||
config_data.get("output_folder", "output")
|
||||
),
|
||||
"excel_file": os.path.basename(config_data.get("excel_file", "")),
|
||||
"image_folder": os.path.basename(config_data.get("image_folder", "")),
|
||||
"fonts_dir": os.path.basename(config_data.get("fonts_dir", "fonts")),
|
||||
"signature_image": get_resource_path(os.path.join("data", config_data.get("signature_image", ""))),
|
||||
"signature_image": get_resource_path(
|
||||
os.path.join("data", config_data.get("signature_image", ""))
|
||||
),
|
||||
},
|
||||
"class_info": {
|
||||
"class_name": config_data.get("class_name", ""),
|
||||
@@ -110,14 +130,14 @@ def save_config(config_data, config_filename="config.toml"):
|
||||
"default_comment": config_data.get("default_comment", ""),
|
||||
"age_group": config_data.get("age_group", ""),
|
||||
},
|
||||
"ai": config_data.get("ai", {})
|
||||
"ai": config_data.get("ai", {}),
|
||||
}
|
||||
|
||||
# 写入文件
|
||||
with open(save_path, "wb") as f:
|
||||
f.write(toml_write.dumps(new_data).encode("utf-8"))
|
||||
|
||||
|
||||
return True, f"成功保存到: {save_path}"
|
||||
|
||||
except Exception as e:
|
||||
return False, f"写入失败: {str(e)}"
|
||||
return False, f"写入失败: {str(e)}"
|
||||
|
||||
Reference in New Issue
Block a user