refactor: 统一导出文件命名规则,使用文件前缀变量

提取file_prefix变量统一管理导出文件的命名前缀,
确保所有导出的Excel和DXF文件使用一致的命名规则,
提升代码可维护性和文件命名的一致性。
This commit is contained in:
dmy
2026-01-05 10:35:09 +08:00
parent f28e087cd2
commit 05ac7a3388

23
gui.py
View File

@@ -212,11 +212,12 @@ def index():
if not state["results"] or not refs["export_row"]: if not state["results"] or not refs["export_row"]:
return return
# 获取带 _result 后缀的文件名 # 获取文件名基础前缀
file_prefix = "wind_farm"
download_name = "wind_farm_design_result.xlsx" download_name = "wind_farm_design_result.xlsx"
if state.get("original_filename"): if state.get("original_filename"):
name_no_ext = os.path.splitext(state["original_filename"])[0] file_prefix = os.path.splitext(state["original_filename"])[0]
download_name = f"{name_no_ext}_result.xlsx" download_name = f"{file_prefix}_result.xlsx"
# 寻找推荐方案:优先 Scenario 1 的最低成本,否则取全局最低成本 # 寻找推荐方案:优先 Scenario 1 的最低成本,否则取全局最低成本
scenario1_results = [r for r in state["results"] if "Scenario 1" in r["name"]] scenario1_results = [r for r in state["results"] if "Scenario 1" in r["name"]]
@@ -241,7 +242,7 @@ def index():
if c.isalnum() or c in (" ", "-", "_") if c.isalnum() or c in (" ", "-", "_")
] ]
).strip() ).strip()
dxf_name = f"design_best_{safe_name}.dxf" dxf_name = f"{file_prefix}_best_{safe_name}.dxf"
export_to_dxf( export_to_dxf(
best_res["turbines"], best_res["turbines"],
@@ -278,7 +279,7 @@ def index():
if c.isalnum() or c in (" ", "-", "_") if c.isalnum() or c in (" ", "-", "_")
] ]
).strip() ).strip()
dxf_name = f"design_{safe_name}.dxf" dxf_name = f"{file_prefix}_{safe_name}.dxf"
export_to_dxf( export_to_dxf(
selected_res["turbines"], selected_res["turbines"],
@@ -308,21 +309,17 @@ def index():
import zipfile import zipfile
# 1. 确定文件名 # 1. 确定文件名
zip_filename = "all_designs_result.zip" zip_filename = f"{file_prefix}_all_results.zip"
excel_result_name = "wind_farm_design_result.xlsx" excel_result_name = f"{file_prefix}_summary.xlsx"
# 推断 main.py 生成的原始 Excel 路径 # 推断 main.py 生成的原始 Excel 路径
generated_excel_path = "wind_farm_design.xlsx" generated_excel_path = "wind_farm_design.xlsx"
if state.get("original_filename"): if state.get("original_filename"):
name_no_ext = os.path.splitext(state["original_filename"])[0]
zip_filename = f"{name_no_ext}_result.zip"
excel_result_name = f"{name_no_ext}_result.xlsx"
if state.get("excel_path"): if state.get("excel_path"):
dir_name = os.path.dirname(state["excel_path"]) dir_name = os.path.dirname(state["excel_path"])
generated_excel_path = os.path.join( generated_excel_path = os.path.join(
dir_name, f"{name_no_ext}_design.xlsx" dir_name, f"{file_prefix}_design.xlsx"
) )
try: try:
@@ -353,7 +350,7 @@ def index():
if c.isalnum() or c in (" ", "-", "_") if c.isalnum() or c in (" ", "-", "_")
] ]
).strip() ).strip()
dxf_name = f"design_{safe_name}.dxf" dxf_name = f"{file_prefix}_{safe_name}.dxf"
export_to_dxf( export_to_dxf(
res["turbines"], res["turbines"],
state["substation"], state["substation"],