From 2f4091470a06399cb0ba8ca0baea46c591fe14c8 Mon Sep 17 00:00:00 2001 From: zkk <1007518571@qq.com> Date: Sat, 7 Sep 2024 16:42:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AE=BE=E7=BD=AE=E6=9C=BA?= =?UTF-8?q?=E5=99=A8=E9=85=8D=E7=BD=AE=E7=9A=84=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ks_includes/ModelConfig.py | 44 ++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/ks_includes/ModelConfig.py b/ks_includes/ModelConfig.py index 624744cf..1ec978d3 100644 --- a/ks_includes/ModelConfig.py +++ b/ks_includes/ModelConfig.py @@ -102,27 +102,35 @@ class ModelConfig: def wirte_printer_config(self, device_name): if device_name: + source_path = f"{os.path.expanduser('~')}/klipper/config/{device_name}/" + target_path = f"{os.path.expanduser('~')}/printer_data/config/" + if not os.path.exists(target_path): + os.makedirs(target_path) + source_base_path = os.path.join(source_path, os.path.basename("base.cfg")) + target_base_path = os.path.join(target_path, os.path.basename("base.cfg")) try: - with open(self.printer_config_path, "r+") as file: - lines = file.readlines() - file.seek(0) - found_printer_section = False - for i, line in enumerate(lines): - if line.strip().startswith("[include ../../klipper/config/"): - lines[i] = f"[include ../../klipper/config/{device_name}.cfg]\n" - found_printer_section = True - break - if not found_printer_section: - lines.insert(0, f"[include ../../klipper/config/{device_name}.cfg]\n") - file.truncate(0) - file.writelines(lines) + if os.path.islink(target_base_path) or os.path.exists(target_base_path): + os.remove(target_base_path) + os.symlink(source_base_path, target_base_path) + logging.info(f"Created config symlink for {device_name}.") + except FileExistsError: + logging.error(f"Failed to create config symlink for {device_name}.") + except PermissionError: + logging.error(f"No permission to create symlink for {device_name}.") + except Exception as e: + logging.error(f"Error creating symlink for{device_name}:{e}") - logging.info(f"Setting printer config to {device_name}") + source_printer_path = os.path.join(source_path, os.path.basename("printer.cfg")) + target_printer_path = os.path.join(target_path, os.path.basename("printer.cfg")) + command = ['cp','-f', source_printer_path, target_printer_path] + try: + subprocess.run(command, check=True, text=True, capture_output=True) + logging.info(f"Configuration file copied successfully. {source_printer_path}' to '{target_printer_path}'") + except subprocess.CalledProcessError as e: + logging.error(f"Copy error config file: {e.stderr}") + except Exception as e: + logging.error(f"Copy error printer file: {e.stderr}") - except FileNotFoundError: - logging.error( - f"Configuration file {self.printer_config_path} not found." - ) def generate_config(self, model): model_name = model