上传代码

This commit is contained in:
chentianrui
2025-09-08 17:58:02 +08:00
parent be848c3e78
commit f5f26c5cf8
76 changed files with 839039 additions and 2695 deletions
+51 -30
View File
@@ -395,50 +395,77 @@ def create_equipment_from_node(node: dict[str, any]) -> Equipment:
return equipment
def init_bcl_calculator(software_category, engineering_type, calculation_type, project_type: Optional[str] = None):
def init_bcl_calculator(*args, **kwargs):
"""
初始化BCL计算器
Args:
software_category: 软件类别(主网/配网/技改)
engineering_type: 工程类型(预算/清单)
calculation_type: 计算类型(工程量/人材机)
project_type: 项目类型(如 变电/线路 等);仅用于工程量时筛选要加载的XML文件
新接口(推荐):
init_bcl_calculator(bcl_dir_path: str) -> bool
兼容旧接口:
init_bcl_calculator(software_category, engineering_type, calculation_type, project_type: Optional[str] = None)
兼容极旧/无参调用:
init_bcl_calculator() # 不执行加载,直接返回 True,避免旧代码报错
Returns:
bool: 是否成功初始化
"""
try:
# 构建计算配置路径
# 格式:计算配置/软件类型(主网,配网,技改)/计算类型(工程量,人材机)/工程类型(预算,清单)
config_path = f"equipment_calculation/计算配置/{software_category}/{calculation_type}/{engineering_type}"
print(f"加载计算配置: {config_path}")
# 检查目录是否存在
import os
# 解析参数:优先使用新接口的单一路径
bcl_dir_path: Optional[str] = None
software_category = None
engineering_type = None
calculation_type = None
project_type = None
# 新接口:单一位置参数为路径
if len(args) == 1 and isinstance(args[0], str):
bcl_dir_path = args[0]
# 旧接口:按原有签名位置参数
elif len(args) >= 3:
software_category, engineering_type, calculation_type = args[:3]
project_type = args[3] if len(args) >= 4 else None
else:
# 也尝试从kwargs读取
bcl_dir_path = kwargs.get("bcl_dir_path")
software_category = kwargs.get("software_category")
engineering_type = kwargs.get("engineering_type")
calculation_type = kwargs.get("calculation_type")
project_type = kwargs.get("project_type")
# 无参极旧调用:直接返回 True(不加载任何脚本,避免中断旧流程)
if bcl_dir_path is None and software_category is None and engineering_type is None and calculation_type is None:
print("init_bcl_calculator: 无参数调用,跳过加载(兼容模式)")
return True
# 确定配置目录
if bcl_dir_path:
config_path = bcl_dir_path
print(f"加载计算配置(新接口): {config_path}")
else:
# 保持旧接口逻辑
config_path = f"equipment_calculation/计算配置/{software_category}/{calculation_type}/{engineering_type}"
print(f"加载计算配置(旧接口): {config_path}")
# 目录存在性处理
if not os.path.exists(config_path):
print(f"计算配置目录不存在: {config_path}")
# 尝试创建目录
try:
os.makedirs(config_path, exist_ok=True)
print(f"已创建计算配置目录: {config_path}")
except Exception as e:
print(f"创建计算配置目录失败: {e}")
# 如果目录不存在,使用默认配置
# 旧逻辑下的默认回退
default_path = "equipment_calculation/计算配置/主网/工程量/预算"
print(f"使用默认计算配置: {default_path}")
# 检查默认配置目录是否存在
if not os.path.exists(default_path):
print(f"默认计算配置目录不存在: {default_path}")
# 尝试使用原始默认配置
original_default_path = "equipment_calculation/计算配置/主网/主网预算"
print(f"尝试使用原始默认计算配置: {original_default_path}")
if os.path.exists(original_default_path):
config_path = original_default_path
else:
@@ -448,13 +475,12 @@ def init_bcl_calculator(software_category, engineering_type, calculation_type, p
config_path = default_path
# 加载脚本
# 对于工程量,若提供了project_type且存在映射,则仅加载映射中指定的XML
result = True
use_filtered_files = False
try:
import os
if calculation_type == "工程量" and project_type:
# 仅当旧接口+工程量+提供project_type时,启用按映射筛选
try:
if (not bcl_dir_path) and calculation_type == "工程量" and project_type:
target_list = PROJECT_TYPE_XML_MAP.get(software_category, {}).get(project_type) or []
if target_list:
use_filtered_files = True
@@ -482,7 +508,6 @@ def init_bcl_calculator(software_category, engineering_type, calculation_type, p
else:
print(f"未找到映射项,按目录全部加载: 软件={software_category}, 项目类型={project_type}")
except Exception as _e:
# 映射/筛选流程异常时,退回目录加载
print(f"按项目类型筛选加载发生异常,退回目录加载: {_e}")
use_filtered_files = False
@@ -490,17 +515,13 @@ def init_bcl_calculator(software_category, engineering_type, calculation_type, p
result = calculator.load_scripts_dir(config_path)
if False == result:
print(f"加载脚本错误: {calculator.get_last_error()}")
# 尝试使用默认配置
# 默认回退
default_path = "equipment_calculation/计算配置/主网/工程量/预算"
print(f"尝试使用默认计算配置: {default_path}")
# 检查默认配置目录是否存在
if not os.path.exists(default_path):
print(f"默认计算配置目录不存在: {default_path}")
# 尝试使用原始默认配置
original_default_path = "equipment_calculation/计算配置/主网/主网预算"
print(f"尝试使用原始默认计算配置: {original_default_path}")
if os.path.exists(original_default_path):
result = calculator.load_scripts_dir(original_default_path)
if False == result: