feat(Klipper): create example printer.cfg if wanted
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
@@ -9,8 +9,11 @@
|
|||||||
# This file may be distributed under the terms of the GNU GPLv3 license #
|
# This file may be distributed under the terms of the GNU GPLv3 license #
|
||||||
# ======================================================================= #
|
# ======================================================================= #
|
||||||
|
|
||||||
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
MODULE_PATH = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
KLIPPER_DIR = f"{Path.home()}/klipper"
|
KLIPPER_DIR = f"{Path.home()}/klipper"
|
||||||
KLIPPER_ENV_DIR = f"{Path.home()}/klippy-env"
|
KLIPPER_ENV_DIR = f"{Path.home()}/klippy-env"
|
||||||
KLIPPER_REQUIREMENTS_TXT = f"{KLIPPER_DIR}/scripts/klippy-requirements.txt"
|
KLIPPER_REQUIREMENTS_TXT = f"{KLIPPER_DIR}/scripts/klippy-requirements.txt"
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ class Klipper(BaseInstance):
|
|||||||
super().__init__(instance_type=self, suffix=suffix)
|
super().__init__(instance_type=self, suffix=suffix)
|
||||||
self.klipper_dir = KLIPPER_DIR
|
self.klipper_dir = KLIPPER_DIR
|
||||||
self.env_dir = KLIPPER_ENV_DIR
|
self.env_dir = KLIPPER_ENV_DIR
|
||||||
self._cfg_file = f"{self.cfg_dir}/printer.cfg"
|
self._cfg_file = self._get_cfg()
|
||||||
self._log = f"{self.log_dir}/klippy.log"
|
self._log = f"{self.log_dir}/klippy.log"
|
||||||
self._serial = f"{self.comms_dir}/klippy.serial"
|
self._serial = f"{self.comms_dir}/klippy.serial"
|
||||||
self._uds = f"{self.comms_dir}/klippy.sock"
|
self._uds = f"{self.comms_dir}/klippy.sock"
|
||||||
@@ -156,8 +156,16 @@ class Klipper(BaseInstance):
|
|||||||
env_file_content = env_template_file_content.replace(
|
env_file_content = env_template_file_content.replace(
|
||||||
"%KLIPPER_DIR%", self.klipper_dir
|
"%KLIPPER_DIR%", self.klipper_dir
|
||||||
)
|
)
|
||||||
env_file_content = env_file_content.replace("%CFG%", self._cfg_file)
|
env_file_content = env_file_content.replace(
|
||||||
|
"%CFG%", f"{self.cfg_dir}/printer.cfg"
|
||||||
|
)
|
||||||
env_file_content = env_file_content.replace("%SERIAL%", self._serial)
|
env_file_content = env_file_content.replace("%SERIAL%", self._serial)
|
||||||
env_file_content = env_file_content.replace("%LOG%", self._log)
|
env_file_content = env_file_content.replace("%LOG%", self._log)
|
||||||
env_file_content = env_file_content.replace("%UDS%", self._uds)
|
env_file_content = env_file_content.replace("%UDS%", self._uds)
|
||||||
return env_file_content
|
return env_file_content
|
||||||
|
|
||||||
|
def _get_cfg(self):
|
||||||
|
cfg_file_loc = f"{self.cfg_dir}/printer.cfg"
|
||||||
|
if Path(cfg_file_loc).is_file():
|
||||||
|
return cfg_file_loc
|
||||||
|
return None
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ from kiauh.modules.klipper.klipper_utils import (
|
|||||||
handle_disruptive_system_packages,
|
handle_disruptive_system_packages,
|
||||||
check_user_groups,
|
check_user_groups,
|
||||||
handle_single_to_multi_conversion,
|
handle_single_to_multi_conversion,
|
||||||
|
create_example_printer_cfg,
|
||||||
)
|
)
|
||||||
from kiauh.core.repo_manager.repo_manager import RepoManager
|
from kiauh.core.repo_manager.repo_manager import RepoManager
|
||||||
from kiauh.utils.input_utils import (
|
from kiauh.utils.input_utils import (
|
||||||
@@ -106,6 +107,8 @@ def install_klipper(
|
|||||||
Logger.print_status(EXIT_KLIPPER_SETUP)
|
Logger.print_status(EXIT_KLIPPER_SETUP)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
create_example_cfg = get_confirm("Create example printer.cfg?")
|
||||||
|
|
||||||
if len(instance_list) < 1:
|
if len(instance_list) < 1:
|
||||||
setup_klipper_prerequesites()
|
setup_klipper_prerequesites()
|
||||||
|
|
||||||
@@ -117,13 +120,24 @@ def install_klipper(
|
|||||||
|
|
||||||
for name in instance_names:
|
for name in instance_names:
|
||||||
if convert_single_to_multi:
|
if convert_single_to_multi:
|
||||||
handle_single_to_multi_conversion(instance_manager, name)
|
current_instance = handle_single_to_multi_conversion(instance_manager, name)
|
||||||
convert_single_to_multi = False
|
convert_single_to_multi = False
|
||||||
else:
|
else:
|
||||||
instance_manager.current_instance = Klipper(suffix=name)
|
current_instance = Klipper(suffix=name)
|
||||||
|
|
||||||
|
instance_manager.current_instance = current_instance
|
||||||
instance_manager.create_instance()
|
instance_manager.create_instance()
|
||||||
instance_manager.enable_instance()
|
instance_manager.enable_instance()
|
||||||
|
|
||||||
|
if create_example_cfg:
|
||||||
|
cfg_dir = current_instance.cfg_dir
|
||||||
|
Logger.print_status(f"Creating example printer.cfg in '{cfg_dir}'")
|
||||||
|
if current_instance.cfg_file is None:
|
||||||
|
create_example_printer_cfg(current_instance)
|
||||||
|
Logger.print_ok(f"Example printer.cfg created in '{cfg_dir}'")
|
||||||
|
else:
|
||||||
|
Logger.print_info(f"printer.cfg in '{cfg_dir}' already exists.")
|
||||||
|
|
||||||
instance_manager.start_instance()
|
instance_manager.start_instance()
|
||||||
|
|
||||||
instance_manager.reload_daemon()
|
instance_manager.reload_daemon()
|
||||||
|
|||||||
@@ -12,12 +12,15 @@
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import grp
|
import grp
|
||||||
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import textwrap
|
import textwrap
|
||||||
|
|
||||||
from typing import List, Union
|
from typing import List, Union
|
||||||
|
|
||||||
|
from kiauh.core.config_manager.config_manager import ConfigManager
|
||||||
from kiauh.core.instance_manager.instance_manager import InstanceManager
|
from kiauh.core.instance_manager.instance_manager import InstanceManager
|
||||||
|
from kiauh.modules.klipper import MODULE_PATH
|
||||||
from kiauh.modules.klipper.klipper import Klipper
|
from kiauh.modules.klipper.klipper import Klipper
|
||||||
from kiauh.modules.klipper.klipper_dialogs import (
|
from kiauh.modules.klipper.klipper_dialogs import (
|
||||||
print_missing_usergroup_dialog,
|
print_missing_usergroup_dialog,
|
||||||
@@ -92,7 +95,7 @@ def handle_existing_multi_instance_names(
|
|||||||
|
|
||||||
def handle_single_to_multi_conversion(
|
def handle_single_to_multi_conversion(
|
||||||
instance_manager: InstanceManager, name: str
|
instance_manager: InstanceManager, name: str
|
||||||
) -> None:
|
) -> Klipper:
|
||||||
instance_list = instance_manager.instances
|
instance_list = instance_manager.instances
|
||||||
instance_manager.current_instance = instance_list[0]
|
instance_manager.current_instance = instance_list[0]
|
||||||
old_data_dir_name = instance_manager.instances[0].data_dir
|
old_data_dir_name = instance_manager.instances[0].data_dir
|
||||||
@@ -103,6 +106,7 @@ def handle_single_to_multi_conversion(
|
|||||||
new_data_dir_name = instance_manager.current_instance.data_dir
|
new_data_dir_name = instance_manager.current_instance.data_dir
|
||||||
try:
|
try:
|
||||||
os.rename(old_data_dir_name, new_data_dir_name)
|
os.rename(old_data_dir_name, new_data_dir_name)
|
||||||
|
return instance_manager.current_instance
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
log = f"Cannot rename {old_data_dir_name} to {new_data_dir_name}:\n{e}"
|
log = f"Cannot rename {old_data_dir_name} to {new_data_dir_name}:\n{e}"
|
||||||
Logger.print_error(log)
|
Logger.print_error(log)
|
||||||
@@ -188,3 +192,18 @@ def has_custom_names(instance_list: List[Klipper]) -> bool:
|
|||||||
def get_highest_index(instance_list: List[Klipper]) -> int:
|
def get_highest_index(instance_list: List[Klipper]) -> int:
|
||||||
indices = [int(instance.suffix.split("-")[-1]) for instance in instance_list]
|
indices = [int(instance.suffix.split("-")[-1]) for instance in instance_list]
|
||||||
return max(indices)
|
return max(indices)
|
||||||
|
|
||||||
|
|
||||||
|
def create_example_printer_cfg(instance: Klipper) -> None:
|
||||||
|
source = os.path.join(MODULE_PATH, "res", "printer.cfg")
|
||||||
|
target = os.path.join(instance.cfg_dir, "printer.cfg")
|
||||||
|
try:
|
||||||
|
shutil.copy(source, target)
|
||||||
|
except OSError as e:
|
||||||
|
Logger.print_error(f"Unable to create example printer.cfg:\n{e}")
|
||||||
|
return
|
||||||
|
|
||||||
|
cm = ConfigManager(target)
|
||||||
|
cm.read_config()
|
||||||
|
cm.set_value("virtual_sdcard", "path", instance.gcodes_dir)
|
||||||
|
cm.write_config()
|
||||||
|
|||||||
11
kiauh/modules/klipper/res/printer.cfg
Normal file
11
kiauh/modules/klipper/res/printer.cfg
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
[mcu]
|
||||||
|
serial: /dev/serial/by-id/<your-mcu-id>
|
||||||
|
|
||||||
|
[virtual_sdcard]
|
||||||
|
path: %GCODES_DIR%
|
||||||
|
on_error_gcode: CANCEL_PRINT
|
||||||
|
|
||||||
|
[printer]
|
||||||
|
kinematics: none
|
||||||
|
max_velocity: 1000
|
||||||
|
max_accel: 1000
|
||||||
Reference in New Issue
Block a user