优化设置机器配置的方式
This commit is contained in:
parent
59f4ed643d
commit
2f4091470a
@ -102,27 +102,35 @@ class ModelConfig:
|
|||||||
|
|
||||||
def wirte_printer_config(self, device_name):
|
def wirte_printer_config(self, device_name):
|
||||||
if 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:
|
try:
|
||||||
with open(self.printer_config_path, "r+") as file:
|
if os.path.islink(target_base_path) or os.path.exists(target_base_path):
|
||||||
lines = file.readlines()
|
os.remove(target_base_path)
|
||||||
file.seek(0)
|
os.symlink(source_base_path, target_base_path)
|
||||||
found_printer_section = False
|
logging.info(f"Created config symlink for {device_name}.")
|
||||||
for i, line in enumerate(lines):
|
except FileExistsError:
|
||||||
if line.strip().startswith("[include ../../klipper/config/"):
|
logging.error(f"Failed to create config symlink for {device_name}.")
|
||||||
lines[i] = f"[include ../../klipper/config/{device_name}.cfg]\n"
|
except PermissionError:
|
||||||
found_printer_section = True
|
logging.error(f"No permission to create symlink for {device_name}.")
|
||||||
break
|
except Exception as e:
|
||||||
if not found_printer_section:
|
logging.error(f"Error creating symlink for{device_name}:{e}")
|
||||||
lines.insert(0, f"[include ../../klipper/config/{device_name}.cfg]\n")
|
|
||||||
file.truncate(0)
|
|
||||||
file.writelines(lines)
|
|
||||||
|
|
||||||
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):
|
def generate_config(self, model):
|
||||||
model_name = model
|
model_name = model
|
||||||
|
Loading…
x
Reference in New Issue
Block a user