diff --git a/app/code_generate_interface.py b/app/code_generate_interface.py index 3b70ee9..6a307f1 100644 --- a/app/code_generate_interface.py +++ b/app/code_generate_interface.py @@ -4,6 +4,7 @@ from qfluentwidgets import TitleLabel, BodyLabel, PushButton, TreeWidget, Fluent from app.tools.analyzing_ioc import analyzing_ioc from app.code_page.bsp_interface import bsp from app.data_interface import DataInterface +from app.tools.code_generator import CodeGenerator import os import csv @@ -159,9 +160,7 @@ class CodeGenerateInterface(QWidget): """生成所有代码,包括未加载页面""" try: # 先收集所有页面名(从CSV配置文件读取) - script_dir = os.path.dirname(os.path.abspath(__file__)) - csv_path = os.path.join(script_dir, "../assets/User_code/config.csv") - csv_path = os.path.abspath(csv_path) + csv_path = os.path.join(CodeGenerator.get_assets_dir("User_code"), "config.csv") all_class_names = [] if os.path.exists(csv_path): with open(csv_path, newline='', encoding='utf-8') as f: @@ -238,9 +237,7 @@ class CodeGenerateInterface(QWidget): return "未找到.ioc文件" def _load_csv_and_build_tree(self): - script_dir = os.path.dirname(os.path.abspath(__file__)) - csv_path = os.path.join(script_dir, "../assets/User_code/config.csv") - csv_path = os.path.abspath(csv_path) + csv_path = os.path.join(CodeGenerator.get_assets_dir("User_code"), "config.csv") print(f"加载CSV路径: {csv_path}") if not os.path.exists(csv_path): print(f"配置文件未找到: {csv_path}") diff --git a/app/code_page/bsp_interface.py b/app/code_page/bsp_interface.py index c1a2501..8826768 100644 --- a/app/code_page/bsp_interface.py +++ b/app/code_page/bsp_interface.py @@ -37,6 +37,8 @@ def save_with_preserve(path, new_code): with open(path, "r", encoding="utf-8") as f: old_code = f.read() new_code = preserve_all_user_regions(new_code, old_code) + # 确保目录存在 + os.makedirs(os.path.dirname(path), exist_ok=True) with open(path, "w", encoding="utf-8") as f: f.write(new_code) @@ -47,7 +49,7 @@ class BspSimplePeripheral(QWidget): self.peripheral_name = peripheral_name self.template_names = template_names # 加载描述 - describe_path = os.path.join(os.path.dirname(__file__), "../../assets/User_code/bsp/describe.csv") + describe_path = os.path.join(CodeGenerator.get_assets_dir("User_code/bsp"), "describe.csv") self.descriptions = load_descriptions(describe_path) self._init_ui() self._load_config() @@ -123,7 +125,7 @@ class BspPeripheralBase(QWidget): self.get_available_func = get_available_func self.available_list = [] # 新增:加载描述 - describe_path = os.path.join(os.path.dirname(__file__), "../../assets/User_code/bsp/describe.csv") + describe_path = os.path.join(CodeGenerator.get_assets_dir("User_code/bsp"), "describe.csv") self.descriptions = load_descriptions(describe_path) self._init_ui() self._load_config() diff --git a/app/code_page/component_interface.py b/app/code_page/component_interface.py index d7a2753..1a5fc8c 100644 --- a/app/code_page/component_interface.py +++ b/app/code_page/component_interface.py @@ -105,7 +105,7 @@ class ComponentSimple(QWidget): self.component_manager = component_manager # 加载描述和依赖信息 - component_dir = os.path.dirname(__file__) + "/../../assets/User_code/component" + component_dir = CodeGenerator.get_assets_dir("User_code/component") describe_path = os.path.join(component_dir, "describe.csv") dependencies_path = os.path.join(component_dir, "dependencies.csv") self.descriptions = load_descriptions(describe_path) @@ -172,13 +172,7 @@ class ComponentSimple(QWidget): return True def _get_component_template_dir(self): - current_dir = os.path.dirname(os.path.abspath(__file__)) - while os.path.basename(current_dir) != 'MRobot' and current_dir != '/': - current_dir = os.path.dirname(current_dir) - if os.path.basename(current_dir) == 'MRobot': - return os.path.join(current_dir, "assets/User_code/component") - else: - return os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "assets/User_code/component") + return CodeGenerator.get_assets_dir("User_code/component") def _save_config(self): config_path = os.path.join(self.project_path, "User/component/component_config.yaml") @@ -286,7 +280,7 @@ class component(QWidget): def generate_component(project_path, pages): """生成所有组件代码,处理依赖关系""" # 自动添加 component.h - src_component_h = os.path.join(os.path.dirname(__file__), "../../assets/User_code/component/component.h") + src_component_h = os.path.join(CodeGenerator.get_assets_dir("User_code/component"), "component.h") dst_component_h = os.path.join(project_path, "User/component/component.h") os.makedirs(os.path.dirname(dst_component_h), exist_ok=True) if os.path.exists(src_component_h): diff --git a/app/code_page/device_interface.py b/app/code_page/device_interface.py index 358352c..fc294c5 100644 --- a/app/code_page/device_interface.py +++ b/app/code_page/device_interface.py @@ -37,7 +37,7 @@ def get_available_bsp_devices(project_path, bsp_type, gpio_type=None): def generate_device_header(project_path, enabled_devices): """生成device.h文件""" - device_dir = os.path.join(os.path.dirname(__file__), "../../assets/User_code/device") + device_dir = CodeGenerator.get_assets_dir("User_code/device") template_path = os.path.join(device_dir, "device.h") # 读取模板文件 @@ -274,16 +274,7 @@ class DeviceSimple(QWidget): def _get_device_template_dir(self): """获取设备模板目录""" - current_dir = os.path.dirname(os.path.abspath(__file__)) - # 向上找到 MRobot 根目录 - while os.path.basename(current_dir) != 'MRobot' and current_dir != '/': - current_dir = os.path.dirname(current_dir) - - if os.path.basename(current_dir) == 'MRobot': - return os.path.join(current_dir, "assets/User_code/device") - else: - # 如果找不到,使用相对路径作为备选 - return os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "assets/User_code/device") + return CodeGenerator.get_assets_dir("User_code/device") def _save_config(self): """保存配置""" @@ -316,7 +307,7 @@ class DeviceSimple(QWidget): def get_device_page(device_name, project_path): """根据设备名返回对应的页面类""" # 加载设备配置 - device_dir = os.path.join(os.path.dirname(__file__), "../../assets/User_code/device") + device_dir = CodeGenerator.get_assets_dir("User_code/device") config_path = os.path.join(device_dir, "config.yaml") device_configs = load_device_config(config_path) diff --git a/app/tools/code_generator.py b/app/tools/code_generator.py index e3a45b5..dbc0f6e 100644 --- a/app/tools/code_generator.py +++ b/app/tools/code_generator.py @@ -78,4 +78,33 @@ class CodeGenerator: if not os.path.exists(template_dir): print(f"警告:模板目录不存在: {template_dir}") - return template_dir \ No newline at end of file + return template_dir + + @staticmethod + def get_assets_dir(sub_path=""): + """获取assets目录路径,兼容打包环境 + Args: + sub_path: 子路径,如 "User_code/component" 或 "User_code/device" + Returns: + str: 完整的assets路径 + """ + if getattr(sys, 'frozen', False): + # 打包后的环境 + base_path = sys._MEIPASS + assets_dir = os.path.join(base_path, "assets") + else: + # 开发环境 + current_dir = os.path.dirname(os.path.abspath(__file__)) + while os.path.basename(current_dir) != 'MRobot' and current_dir != '/': + current_dir = os.path.dirname(current_dir) + assets_dir = os.path.join(current_dir, "assets") + + if sub_path: + full_path = os.path.join(assets_dir, sub_path) + else: + full_path = assets_dir + + if not os.path.exists(full_path): + print(f"警告:资源目录不存在: {full_path}") + + return full_path \ No newline at end of file