feat: KIAUH v6 - full rewrite of KIAUH in Python (#428)

This commit is contained in:
dw-0
2024-08-31 19:16:52 +02:00
committed by GitHub
parent 8547942986
commit 0ee0fa3325
159 changed files with 13461 additions and 54 deletions

View File

@@ -0,0 +1,12 @@
# ======================================================================= #
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
from pathlib import Path
EXTENSION_ROOT = Path(__file__).resolve().parents[1].joinpath("extensions")

View File

@@ -0,0 +1,29 @@
# ======================================================================= #
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
from abc import ABC, abstractmethod
from typing import Dict
# noinspection PyUnusedLocal
# noinspection PyMethodMayBeStatic
class BaseExtension(ABC):
def __init__(self, metadata: Dict[str, str]):
self.metadata = metadata
@abstractmethod
def install_extension(self, **kwargs) -> None:
raise NotImplementedError
def update_extension(self, **kwargs) -> None:
raise NotImplementedError
@abstractmethod
def remove_extension(self, **kwargs) -> None:
raise NotImplementedError

View File

@@ -0,0 +1,162 @@
# ======================================================================= #
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
from __future__ import annotations
import importlib
import inspect
import json
import textwrap
from pathlib import Path
from typing import Dict, List, Type
from core.constants import COLOR_CYAN, COLOR_YELLOW, RESET_FORMAT
from core.logger import Logger
from core.menus import Option
from core.menus.base_menu import BaseMenu
from extensions import EXTENSION_ROOT
from extensions.base_extension import BaseExtension
# noinspection PyUnusedLocal
# noinspection PyMethodMayBeStatic
class ExtensionsMenu(BaseMenu):
def __init__(self, previous_menu: Type[BaseMenu] | None = None):
super().__init__()
self.previous_menu: Type[BaseMenu] | None = previous_menu
self.extensions: Dict[str, BaseExtension] = self.discover_extensions()
def set_previous_menu(self, previous_menu: Type[BaseMenu] | None) -> None:
from core.menus.main_menu import MainMenu
self.previous_menu = previous_menu if previous_menu is not None else MainMenu
def set_options(self) -> None:
self.options = {
i: Option(self.extension_submenu, opt_data=self.extensions.get(i))
for i in self.extensions
}
def discover_extensions(self) -> Dict[str, BaseExtension]:
ext_dict = {}
for ext in EXTENSION_ROOT.iterdir():
metadata_json = Path(ext).joinpath("metadata.json")
if not metadata_json.exists():
continue
try:
with open(metadata_json, "r") as m:
# read extension metadata from json
metadata = json.load(m).get("metadata")
module_name = metadata.get("module")
module_path = f"kiauh.extensions.{ext.name}.{module_name}"
# get the class name of the extension
ext_class: Type[BaseExtension] = inspect.getmembers(
importlib.import_module(module_path),
predicate=lambda o: inspect.isclass(o)
and issubclass(o, BaseExtension)
and o != BaseExtension,
)[0][1]
# instantiate the extension with its metadata and add to dict
ext_instance: BaseExtension = ext_class(metadata)
ext_dict[f"{metadata.get('index')}"] = ext_instance
except (IOError, json.JSONDecodeError, ImportError) as e:
print(f"Failed loading extension {ext}: {e}")
return dict(sorted(ext_dict.items()))
def extension_submenu(self, **kwargs):
ExtensionSubmenu(kwargs.get("opt_data"), self.__class__).run()
def print_menu(self) -> None:
header = " [ Extensions Menu ] "
color = COLOR_CYAN
line1 = f"{COLOR_YELLOW}Available Extensions:{RESET_FORMAT}"
count = 62 - len(color) - len(RESET_FORMAT)
menu = textwrap.dedent(
f"""
╔═══════════════════════════════════════════════════════╗
{color}{header:~^{count}}{RESET_FORMAT}
╟───────────────────────────────────────────────────────╢
{line1:<62}
║ ║
"""
)[1:]
print(menu, end="")
for extension in self.extensions.values():
index = extension.metadata.get("index")
name = extension.metadata.get("display_name")
row = f"{index}) {name}"
print(f"{row:<53}")
print("╟───────────────────────────────────────────────────────╢")
# noinspection PyUnusedLocal
# noinspection PyMethodMayBeStatic
class ExtensionSubmenu(BaseMenu):
def __init__(
self, extension: BaseExtension, previous_menu: Type[BaseMenu] | None = None
):
super().__init__()
self.extension = extension
self.previous_menu: Type[BaseMenu] | None = previous_menu
def set_previous_menu(self, previous_menu: Type[BaseMenu] | None) -> None:
self.previous_menu = (
previous_menu if previous_menu is not None else ExtensionsMenu
)
def set_options(self) -> None:
self.options["1"] = Option(self.extension.install_extension)
if self.extension.metadata.get("updates"):
self.options["2"] = Option(self.extension.update_extension)
self.options["3"] = Option(self.extension.remove_extension)
else:
self.options["2"] = Option(self.extension.remove_extension)
def print_menu(self) -> None:
header = f" [ {self.extension.metadata.get('display_name')} ] "
color = COLOR_YELLOW
count = 62 - len(color) - len(RESET_FORMAT)
line_width = 53
description: List[str] = self.extension.metadata.get("description", [])
description_text = Logger.format_content(
description,
line_width,
border_left="",
border_right="",
)
menu = textwrap.dedent(
f"""
╔═══════════════════════════════════════════════════════╗
{color}{header:~^{count}}{RESET_FORMAT}
╟───────────────────────────────────────────────────────╢
"""
)[1:]
menu += f"{description_text}\n"
menu += textwrap.dedent(
"""
╟───────────────────────────────────────────────────────╢
║ 1) Install ║
"""
)[1:]
if self.extension.metadata.get("updates"):
menu += "║ 2) Update ║\n"
menu += "║ 3) Remove ║\n"
else:
menu += "║ 2) Remove ║\n"
menu += "╟───────────────────────────────────────────────────────╢\n"
print(menu, end="")

View File

@@ -0,0 +1,19 @@
# ======================================================================= #
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
from pathlib import Path
EXT_MODULE_NAME = "gcode_shell_command.py"
MODULE_PATH = Path(__file__).resolve().parent
MODULE_ASSETS = MODULE_PATH.joinpath("assets")
KLIPPER_DIR = Path.home().joinpath("klipper")
KLIPPER_EXTRAS = KLIPPER_DIR.joinpath("klippy/extras")
EXTENSION_SRC = MODULE_ASSETS.joinpath(EXT_MODULE_NAME)
EXTENSION_TARGET_PATH = KLIPPER_EXTRAS.joinpath(EXT_MODULE_NAME)
EXAMPLE_CFG_SRC = MODULE_ASSETS.joinpath("shell_command.cfg")

View File

@@ -0,0 +1,94 @@
# Run a shell command via gcode
#
# Copyright (C) 2019 Eric Callahan <arksine.code@gmail.com>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import logging
import os
import shlex
import subprocess
class ShellCommand:
def __init__(self, config):
self.name = config.get_name().split()[-1]
self.printer = config.get_printer()
self.gcode = self.printer.lookup_object("gcode")
cmd = config.get("command")
cmd = os.path.expanduser(cmd)
self.command = shlex.split(cmd)
self.timeout = config.getfloat("timeout", 2.0, above=0.0)
self.verbose = config.getboolean("verbose", True)
self.proc_fd = None
self.partial_output = ""
self.gcode.register_mux_command(
"RUN_SHELL_COMMAND",
"CMD",
self.name,
self.cmd_RUN_SHELL_COMMAND,
desc=self.cmd_RUN_SHELL_COMMAND_help,
)
def _process_output(self, eventime):
if self.proc_fd is None:
return
try:
data = os.read(self.proc_fd, 4096)
except Exception:
pass
data = self.partial_output + data.decode()
if "\n" not in data:
self.partial_output = data
return
elif data[-1] != "\n":
split = data.rfind("\n") + 1
self.partial_output = data[split:]
data = data[:split]
else:
self.partial_output = ""
self.gcode.respond_info(data)
cmd_RUN_SHELL_COMMAND_help = "Run a linux shell command"
def cmd_RUN_SHELL_COMMAND(self, params):
gcode_params = params.get("PARAMS", "")
gcode_params = shlex.split(gcode_params)
reactor = self.printer.get_reactor()
try:
proc = subprocess.Popen(
self.command + gcode_params,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
except Exception:
logging.exception("shell_command: Command {%s} failed" % (self.name))
raise self.gcode.error("Error running command {%s}" % (self.name))
if self.verbose:
self.proc_fd = proc.stdout.fileno()
self.gcode.respond_info("Running Command {%s}...:" % (self.name))
hdl = reactor.register_fd(self.proc_fd, self._process_output)
eventtime = reactor.monotonic()
endtime = eventtime + self.timeout
complete = False
while eventtime < endtime:
eventtime = reactor.pause(eventtime + 0.05)
if proc.poll() is not None:
complete = True
break
if not complete:
proc.terminate()
if self.verbose:
if self.partial_output:
self.gcode.respond_info(self.partial_output)
self.partial_output = ""
if complete:
msg = "Command {%s} finished\n" % (self.name)
else:
msg = "Command {%s} timed out" % (self.name)
self.gcode.respond_info(msg)
reactor.unregister_fd(hdl)
self.proc_fd = None
def load_config_prefix(config):
return ShellCommand(config)

View File

@@ -0,0 +1,7 @@
[gcode_shell_command hello_world]
command: echo hello world
timeout: 2.
verbose: True
[gcode_macro HELLO_WORLD]
gcode:
RUN_SHELL_COMMAND CMD=hello_world

View File

@@ -0,0 +1,131 @@
# ======================================================================= #
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
import os
import shutil
from typing import List
from components.klipper.klipper import Klipper
from core.backup_manager.backup_manager import BackupManager
from core.instance_manager.instance_manager import InstanceManager
from core.logger import Logger
from core.submodules.simple_config_parser.src.simple_config_parser.simple_config_parser import (
SimpleConfigParser,
)
from extensions.base_extension import BaseExtension
from extensions.gcode_shell_cmd import (
EXAMPLE_CFG_SRC,
EXTENSION_SRC,
EXTENSION_TARGET_PATH,
KLIPPER_DIR,
KLIPPER_EXTRAS,
)
from utils.fs_utils import check_file_exist
from utils.input_utils import get_confirm
from utils.instance_utils import get_instances
# noinspection PyMethodMayBeStatic
class GcodeShellCmdExtension(BaseExtension):
def install_extension(self, **kwargs) -> None:
install_example = get_confirm("Create an example shell command?", False, False)
klipper_dir_exists = check_file_exist(KLIPPER_DIR)
if not klipper_dir_exists:
Logger.print_warn(
"No Klipper directory found! Unable to install extension."
)
return
extension_installed = check_file_exist(EXTENSION_TARGET_PATH)
overwrite = True
if extension_installed:
overwrite = get_confirm(
"Extension seems to be installed already. Overwrite?",
True,
False,
)
if not overwrite:
Logger.print_warn("Installation aborted due to user request.")
return
instances = get_instances(Klipper)
InstanceManager.stop_all(instances)
try:
Logger.print_status(f"Copy extension to '{KLIPPER_EXTRAS}' ...")
shutil.copy(EXTENSION_SRC, EXTENSION_TARGET_PATH)
except OSError as e:
Logger.print_error(f"Unable to install extension: {e}")
return
if install_example:
self.install_example_cfg(instances)
InstanceManager.start_all(instances)
Logger.print_ok("Installing G-Code Shell Command extension successful!")
def remove_extension(self, **kwargs) -> None:
extension_installed = check_file_exist(EXTENSION_TARGET_PATH)
if not extension_installed:
Logger.print_info("Extension does not seem to be installed! Skipping ...")
return
question = "Do you really want to remove the extension?"
if get_confirm(question, True, False):
try:
Logger.print_status(f"Removing '{EXTENSION_TARGET_PATH}' ...")
os.remove(EXTENSION_TARGET_PATH)
Logger.print_ok("Extension successfully removed!")
except OSError as e:
Logger.print_error(f"Unable to remove extension: {e}")
Logger.print_warn("PLEASE NOTE:")
Logger.print_warn(
"Remaining gcode shell command will cause Klipper to throw an error."
)
Logger.print_warn("Make sure to remove them from the printer.cfg!")
def install_example_cfg(self, instances: List[Klipper]):
cfg_dirs = [instance.base.cfg_dir for instance in instances]
# copy extension to klippy/extras
for cfg_dir in cfg_dirs:
Logger.print_status(f"Create shell_command.cfg in '{cfg_dir}' ...")
if check_file_exist(cfg_dir.joinpath("shell_command.cfg")):
Logger.print_info("File already exists! Skipping ...")
continue
try:
shutil.copy(EXAMPLE_CFG_SRC, cfg_dir)
Logger.print_ok("Done!")
except OSError as e:
Logger.warn(f"Unable to create example config: {e}")
# backup each printer.cfg before modification
bm = BackupManager()
for instance in instances:
bm.backup_file(
instance.cfg_file,
custom_filename=f"{instance.suffix}.printer.cfg",
)
# add section to printer.cfg if not already defined
section = "include shell_command.cfg"
cfg_files = [instance.cfg_file for instance in instances]
for cfg_file in cfg_files:
Logger.print_status(f"Include shell_command.cfg in '{cfg_file}' ...")
scp = SimpleConfigParser()
scp.read(cfg_file)
if scp.has_section(section):
Logger.print_info("Section already defined! Skipping ...")
continue
scp.add_section(section)
scp.write(cfg_file)
Logger.print_ok("Done!")

View File

@@ -0,0 +1,9 @@
{
"metadata": {
"index": 1,
"module": "gcode_shell_cmd_extension",
"maintained_by": "dw-0",
"display_name": "G-Code Shell Command",
"description": ["Run a shell commands from gcode."]
}
}

View File

@@ -0,0 +1,19 @@
# ======================================================================= #
# Copyright (C) 2023 - 2024 Staubgeborener and Tylerjet #
# https://github.com/Staubgeborener/klipper-backup #
# https://klipperbackup.xyz #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
from pathlib import Path
EXT_MODULE_NAME = "klipper_backup_extension.py"
MODULE_PATH = Path(__file__).resolve().parent
MOONRAKER_CONF = Path.home().joinpath("printer_data", "config", "moonraker.conf")
KLIPPERBACKUP_DIR = Path.home().joinpath("klipper-backup")
KLIPPERBACKUP_CONFIG_DIR = Path.home().joinpath("config_backup")
KLIPPERBACKUP_REPO_URL = "https://github.com/staubgeborener/klipper-backup"

View File

@@ -0,0 +1,127 @@
# ======================================================================= #
# Copyright (C) 2023 - 2024 Staubgeborener and Tylerjet #
# https://github.com/Staubgeborener/klipper-backup #
# https://klipperbackup.xyz #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
import os
import shutil
import subprocess
from core.constants import SYSTEMD
from core.logger import Logger
from pathlib import Path
from extensions.base_extension import BaseExtension
from extensions.klipper_backup import (
KLIPPERBACKUP_CONFIG_DIR,
KLIPPERBACKUP_DIR,
KLIPPERBACKUP_REPO_URL,
MOONRAKER_CONF,
)
from utils.fs_utils import check_file_exist, remove_with_sudo
from utils.git_utils import git_cmd_clone
from utils.input_utils import get_confirm
from utils.sys_utils import cmd_sysctl_manage, remove_system_service, unit_file_exists
class KlipperbackupExtension(BaseExtension):
def remove_extension(self, **kwargs) -> None:
if not check_file_exist(KLIPPERBACKUP_DIR):
Logger.print_info("Extension does not seem to be installed! Skipping ...")
return
def uninstall_service(service_name: str, unit_type: str) -> bool:
try:
full_service_name = f"{service_name}.{unit_type}"
if unit_type == "service":
remove_system_service(full_service_name)
elif unit_type == "timer":
full_service_path: Path = SYSTEMD.joinpath(full_service_name)
Logger.print_status(f"Removing {full_service_name} ...")
remove_with_sudo(full_service_path)
Logger.print_ok(f"{service_name}.{unit_type} successfully removed!")
cmd_sysctl_manage("daemon-reload")
cmd_sysctl_manage("reset-failed")
else:
Logger.print_error(f"Unknown unit type {unit_type} of {full_service_name}")
except:
Logger.print_error(f"Failed to remove {full_service_name}: {str(e)}")
def check_crontab_entry(entry) -> bool:
try:
crontab_content = subprocess.check_output(["crontab", "-l"], stderr=subprocess.DEVNULL, text=True)
except subprocess.CalledProcessError:
return False
return any(entry in line for line in crontab_content.splitlines())
def remove_moonraker_entry():
original_file_path = MOONRAKER_CONF
comparison_file_path = os.path.join(str(KLIPPERBACKUP_DIR), "install-files", "moonraker.conf")
if not (os.path.exists(original_file_path) and os.path.exists(comparison_file_path)):
return False
with open(original_file_path, "r") as original_file, open(comparison_file_path, "r") as comparison_file:
original_content = original_file.read()
comparison_content = comparison_file.read()
if comparison_content in original_content:
Logger.print_status("Removing Klipper-Backup moonraker entry ...")
modified_content = original_content.replace(comparison_content, "").strip()
modified_content = "\n".join(line for line in modified_content.split("\n") if line.strip())
with open(original_file_path, "w") as original_file:
original_file.write(modified_content)
Logger.print_ok("Klipper-Backup moonraker entry successfully removed!")
return True
return False
if get_confirm("Do you really want to remove the extension?", True, False):
# Remove systemd timer and services
service_names = ["klipper-backup-on-boot", "klipper-backup-filewatch", "klipper-backup"]
unit_types = ["timer", "service"]
for service_name in service_names:
for unit_type in unit_types:
if unit_file_exists(service_name, unit_type):
uninstall_service(service_name, unit_type)
# Remnove crontab entry
try:
if check_crontab_entry("/klipper-backup/script.sh"):
Logger.print_status("Removing Klipper-Backup crontab entry ...")
crontab_content = subprocess.check_output(["crontab", "-l"], text=True)
modified_content = "\n".join(line for line in crontab_content.splitlines() if "/klipper-backup/script.sh" not in line)
subprocess.run(["crontab", "-"], input=modified_content + "\n", text=True, check=True)
Logger.print_ok("Klipper-Backup crontab entry successfully removed!")
except subprocess.CalledProcessError:
Logger.print_error("Unable to remove the Klipper-Backup cron entry")
# Remove moonraker entry
try:
remove_moonraker_entry()
except:
Logger.print_error("Unable to remove the Klipper-Backup moonraker entry")
# Remove Klipper-backup extension
Logger.print_status("Removing Klipper-Backup extension ...")
try:
remove_with_sudo(KLIPPERBACKUP_DIR)
if check_file_exist(KLIPPERBACKUP_CONFIG_DIR):
remove_with_sudo(KLIPPERBACKUP_CONFIG_DIR)
Logger.print_ok("Extension Klipper-Backup successfully removed!")
except:
Logger.print_error(f"Unable to remove Klipper-Backup extension")
def install_extension(self, **kwargs) -> None:
if not KLIPPERBACKUP_DIR.exists():
git_cmd_clone(KLIPPERBACKUP_REPO_URL, KLIPPERBACKUP_DIR)
subprocess.run(["chmod", "+x", str(KLIPPERBACKUP_DIR / "install.sh")])
subprocess.run([str(KLIPPERBACKUP_DIR / "install.sh")])
def update_extension(self, **kwargs) -> None:
if not check_file_exist(KLIPPERBACKUP_DIR):
Logger.print_info("Extension does not seem to be installed! Skipping ...")
return
subprocess.run([str(KLIPPERBACKUP_DIR / "install.sh"), "check_updates"])

View File

@@ -0,0 +1,10 @@
{
"metadata": {
"index": 3,
"module": "klipper_backup_extension",
"maintained_by": "Staubgeborener",
"display_name": "Klipper-Backup",
"description": ["Backup all your Klipper files to GitHub"],
"updates": true
}
}

View File

@@ -0,0 +1,189 @@
# ======================================================================= #
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
from __future__ import annotations
import csv
import shutil
import textwrap
import urllib.request
from dataclasses import dataclass
from typing import Any, Dict, List, Type, Union
from components.klipper.klipper import Klipper
from components.klipper.klipper_dialogs import (
DisplayType,
print_instance_overview,
)
from core.constants import COLOR_CYAN, COLOR_YELLOW, RESET_FORMAT
from core.instance_manager.base_instance import BaseInstance
from core.instance_type import InstanceType
from core.logger import Logger
from core.menus import Option
from core.menus.base_menu import BaseMenu
from extensions.base_extension import BaseExtension
from utils.git_utils import git_clone_wrapper
from utils.input_utils import get_selection_input
from utils.instance_utils import get_instances
@dataclass
class ThemeData:
name: str
short_note: str
author: str
repo: str
# noinspection PyMethodMayBeStatic
class MainsailThemeInstallerExtension(BaseExtension):
instances: List[Klipper] = get_instances(Klipper)
def install_extension(self, **kwargs) -> None:
MainsailThemeInstallMenu(self.instances).run()
def remove_extension(self, **kwargs) -> None:
print_instance_overview(
self.instances,
display_type=DisplayType.PRINTER_NAME,
show_headline=True,
show_index=True,
show_select_all=True,
)
printer_list = get_printer_selection(self.instances, True)
if printer_list is None:
return
for printer in printer_list:
Logger.print_status(f"Uninstalling theme from {printer.cfg_dir} ...")
theme_dir = printer.cfg_dir.joinpath(".theme")
if not theme_dir.exists():
Logger.print_info(f"{theme_dir} not found. Skipping ...")
continue
try:
shutil.rmtree(theme_dir)
Logger.print_ok("Theme successfully uninstalled!")
except OSError as e:
Logger.print_error("Unable to uninstall theme")
Logger.print_error(e)
# noinspection PyMethodMayBeStatic
class MainsailThemeInstallMenu(BaseMenu):
THEMES_URL: str = (
"https://raw.githubusercontent.com/mainsail-crew/gb-docs/main/_data/themes.csv"
)
def __init__(self, instances: List[Klipper]):
super().__init__()
self.themes: List[ThemeData] = self.load_themes()
self.instances = instances
def set_previous_menu(self, previous_menu: Type[BaseMenu] | None) -> None:
from extensions.extensions_menu import ExtensionsMenu
self.previous_menu = (
previous_menu if previous_menu is not None else ExtensionsMenu
)
def set_options(self) -> None:
self.options = {
f"{index}": Option(self.install_theme, opt_index=f"{index}")
for index in range(len(self.themes))
}
def print_menu(self) -> None:
header = " [ Mainsail Theme Installer ] "
color = COLOR_YELLOW
line1 = f"{COLOR_CYAN}A preview of each Mainsail theme can be found here:{RESET_FORMAT}"
count = 62 - len(color) - len(RESET_FORMAT)
menu = textwrap.dedent(
f"""
╔═══════════════════════════════════════════════════════╗
{color}{header:~^{count}}{RESET_FORMAT}
╟───────────────────────────────────────────────────────╢
{line1:<62}
║ https://docs.mainsail.xyz/theming/themes ║
╟───────────────────────────────────────────────────────╢
"""
)[1:]
for i, theme in enumerate(self.themes):
j: str = f" {i}" if i < 10 else f"{i}"
row: str = f"{j}) [{theme.name}]"
menu += f"{row:<53}\n"
print(menu, end="")
def load_themes(self) -> List[ThemeData]:
with urllib.request.urlopen(self.THEMES_URL) as response:
themes: List[ThemeData] = []
content: str = response.read().decode()
csv_data: List[str] = content.splitlines()
fieldnames = ["name", "short_note", "author", "repo"]
csv_reader = csv.DictReader(csv_data, fieldnames=fieldnames, delimiter=",")
next(csv_reader) # skip the header of the csv file
for row in csv_reader:
row: Dict[str, str] # type: ignore
theme: ThemeData = ThemeData(**row)
themes.append(theme)
return themes
def install_theme(self, **kwargs: Any):
opt_index: str | None = kwargs.get("opt_index", None)
if not opt_index:
raise ValueError("No option index provided")
index: int = int(opt_index)
theme_data: ThemeData = self.themes[index]
theme_author: str = theme_data.author
theme_repo: str = theme_data.repo
theme_repo_url: str = f"https://github.com/{theme_author}/{theme_repo}"
print_instance_overview(
self.instances,
display_type=DisplayType.PRINTER_NAME,
show_headline=True,
show_index=True,
show_select_all=True,
)
printer_list = get_printer_selection(self.instances, True)
if printer_list is None:
return
for printer in printer_list:
git_clone_wrapper(theme_repo_url, printer.cfg_dir.joinpath(".theme"))
if len(theme_data.short_note) > 1:
Logger.print_warn("Info from the creator:", prefix=False, start="\n")
Logger.print_info(theme_data.short_note, prefix=False, end="\n\n")
def get_printer_selection(
instances: List[InstanceType], is_install: bool
) -> Union[List[BaseInstance], None]:
options = [str(i) for i in range(len(instances))]
options.extend(["a", "b"])
if is_install:
q = "Select the printer to install the theme for"
else:
q = "Select the printer to remove the theme from"
selection = get_selection_input(q, options)
install_for = []
if selection == "b":
return None
elif selection == "a":
install_for.extend(instances)
else:
instance = instances[int(selection)]
install_for.append(instance)
return install_for

View File

@@ -0,0 +1,9 @@
{
"metadata": {
"index": 2,
"module": "mainsail_theme_installer_extension",
"maintained_by": "dw-0",
"display_name": "Mainsail Theme Installer",
"description": ["Install Mainsail Themes maintained by the Mainsail community."]
}
}

View File

@@ -0,0 +1,34 @@
# ======================================================================= #
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
from pathlib import Path
MODULE_PATH = Path(__file__).resolve().parent
# repo
OBICO_REPO = "https://github.com/TheSpaghettiDetective/moonraker-obico.git"
# names
OBICO_SERVICE_NAME = "moonraker-obico.service"
OBICO_ENV_FILE_NAME = "moonraker-obico.env"
OBICO_CFG_NAME = "moonraker-obico.cfg"
OBICO_CFG_SAMPLE_NAME = "moonraker-obico.cfg.sample"
OBICO_LOG_NAME = "moonraker-obico.log"
OBICO_UPDATE_CFG_NAME = "moonraker-obico-update.cfg"
OBICO_UPDATE_CFG_SAMPLE_NAME = "moonraker-obico-update.cfg.sample"
OBICO_MACROS_CFG_NAME = "moonraker_obico_macros.cfg"
# directories
OBICO_DIR = Path.home().joinpath("moonraker-obico")
OBICO_ENV_DIR = Path.home().joinpath("moonraker-obico-env")
# files
OBICO_SERVICE_TEMPLATE = MODULE_PATH.joinpath(f"assets/{OBICO_SERVICE_NAME}")
OBICO_ENV_FILE_TEMPLATE = MODULE_PATH.joinpath(f"assets/{OBICO_ENV_FILE_NAME}")
OBICO_LINK_SCRIPT = OBICO_DIR.joinpath("scripts/link.sh")
OBICO_REQ_FILE = OBICO_DIR.joinpath("requirements.txt")

View File

@@ -0,0 +1 @@
OBICO_ARGS="-m moonraker_obico.app -c %CFG%"

View File

@@ -0,0 +1,16 @@
#Systemd service file for moonraker-obico
[Unit]
Description=Moonraker-Obico
After=network-online.target moonraker.service
[Install]
WantedBy=multi-user.target
[Service]
Type=simple
User=%USER%
WorkingDirectory=%OBICO_DIR%
EnvironmentFile=%ENV_FILE%
ExecStart=%ENV%/bin/python3 $OBICO_ARGS
Restart=always
RestartSec=5

View File

@@ -0,0 +1,16 @@
{
"metadata": {
"index": 6,
"module": "moonraker_obico_extension",
"maintained_by": "Obico",
"display_name": "Obico for Klipper",
"description": [
"Open source 3D Printing cloud and AI",
"- AI-Powered Failure Detection",
"- Free Remote Monitoring and Access",
"- 25FPS High-Def Webcam Streaming",
"- Free 4.9-Star Mobile App"
],
"updates": true
}
}

View File

@@ -0,0 +1,145 @@
# ======================================================================= #
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
from __future__ import annotations
from dataclasses import dataclass, field
from pathlib import Path
from subprocess import CalledProcessError, run
from components.moonraker.moonraker import Moonraker
from core.constants import CURRENT_USER
from core.instance_manager.base_instance import BaseInstance
from core.logger import Logger
from core.submodules.simple_config_parser.src.simple_config_parser.simple_config_parser import (
SimpleConfigParser,
)
from extensions.obico import (
OBICO_CFG_NAME,
OBICO_DIR,
OBICO_ENV_DIR,
OBICO_ENV_FILE_NAME,
OBICO_ENV_FILE_TEMPLATE,
OBICO_LINK_SCRIPT,
OBICO_LOG_NAME,
OBICO_SERVICE_TEMPLATE,
)
from utils.fs_utils import create_folders
from utils.sys_utils import get_service_file_path
# noinspection PyMethodMayBeStatic
@dataclass(repr=True)
class MoonrakerObico:
suffix: str
base: BaseInstance = field(init=False, repr=False)
service_file_path: Path = field(init=False)
log_file_name: str = OBICO_LOG_NAME
dir: Path = OBICO_DIR
env_dir: Path = OBICO_ENV_DIR
data_dir: Path = field(init=False)
cfg_file: Path = field(init=False)
is_linked: bool = False
def __post_init__(self):
self.base: BaseInstance = BaseInstance(Moonraker, self.suffix)
self.base.log_file_name = self.log_file_name
self.service_file_path: Path = get_service_file_path(
MoonrakerObico, self.suffix
)
self.data_dir: Path = self.base.data_dir
self.cfg_file = self.base.cfg_dir.joinpath(OBICO_CFG_NAME)
self.is_linked: bool = self._check_link_status()
def create(self) -> None:
from utils.sys_utils import create_env_file, create_service_file
Logger.print_status("Creating new Obico for Klipper Instance ...")
try:
create_folders(self.base.base_folders)
create_service_file(
name=self.service_file_path.name,
content=self._prep_service_file_content(),
)
create_env_file(
path=self.base.sysd_dir.joinpath(OBICO_ENV_FILE_NAME),
content=self._prep_env_file_content(),
)
except CalledProcessError as e:
Logger.print_error(f"Error creating instance: {e}")
raise
except OSError as e:
Logger.print_error(f"Error creating env file: {e}")
raise
def link(self) -> None:
Logger.print_status(
f"Linking instance for printer {self.data_dir.name} to the Obico server ..."
)
try:
cmd = [f"{OBICO_LINK_SCRIPT} -q -c {self.cfg_file}"]
if self.suffix:
cmd.append(f"-n {self.suffix}")
run(cmd, check=True, shell=True)
except CalledProcessError as e:
Logger.print_error(f"Error during Obico linking: {e}")
raise
def _prep_service_file_content(self) -> str:
template = OBICO_SERVICE_TEMPLATE
try:
with open(template, "r") as template_file:
template_content = template_file.read()
except FileNotFoundError:
Logger.print_error(f"Unable to open {template} - File not found")
raise
service_content = template_content.replace(
"%USER%",
CURRENT_USER,
)
service_content = service_content.replace(
"%OBICO_DIR%",
self.dir.as_posix(),
)
service_content = service_content.replace(
"%ENV%",
self.env_dir.as_posix(),
)
service_content = service_content.replace(
"%ENV_FILE%",
self.base.sysd_dir.joinpath(OBICO_ENV_FILE_NAME).as_posix(),
)
return service_content
def _prep_env_file_content(self) -> str:
template = OBICO_ENV_FILE_TEMPLATE
try:
with open(template, "r") as env_file:
env_template_file_content = env_file.read()
except FileNotFoundError:
Logger.print_error(f"Unable to open {template} - File not found")
raise
env_file_content = env_template_file_content.replace(
"%CFG%",
f"{self.base.cfg_dir}/{self.cfg_file}",
)
return env_file_content
def _check_link_status(self) -> bool:
if not self.cfg_file or not self.cfg_file.exists():
return False
scp = SimpleConfigParser()
scp.read(self.cfg_file)
return scp.get("server", "auth_token", None) is not None

View File

@@ -0,0 +1,367 @@
# ======================================================================= #
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
import shutil
from typing import List
from components.klipper.klipper import Klipper
from components.moonraker.moonraker import Moonraker
from core.instance_manager.instance_manager import InstanceManager
from core.logger import DialogType, Logger
from core.submodules.simple_config_parser.src.simple_config_parser.simple_config_parser import (
SimpleConfigParser,
)
from extensions.base_extension import BaseExtension
from extensions.obico import (
OBICO_CFG_SAMPLE_NAME,
OBICO_DIR,
OBICO_ENV_DIR,
OBICO_MACROS_CFG_NAME,
OBICO_REPO,
OBICO_REQ_FILE,
OBICO_UPDATE_CFG_NAME,
OBICO_UPDATE_CFG_SAMPLE_NAME,
)
from extensions.obico.moonraker_obico import (
MoonrakerObico,
)
from utils.common import check_install_dependencies, moonraker_exists
from utils.config_utils import (
add_config_section,
remove_config_section,
)
from utils.fs_utils import run_remove_routines
from utils.git_utils import git_clone_wrapper, git_pull_wrapper
from utils.input_utils import get_confirm, get_selection_input, get_string_input
from utils.instance_utils import get_instances
from utils.sys_utils import (
cmd_sysctl_manage,
cmd_sysctl_service,
create_python_venv,
install_python_requirements,
parse_packages_from_file,
)
# noinspection PyMethodMayBeStatic
class ObicoExtension(BaseExtension):
server_url: str
def install_extension(self, **kwargs) -> None:
Logger.print_status("Installing Obico for Klipper ...")
# check if moonraker is installed. if not, notify the user and exit
if not moonraker_exists():
return
# if obico is already installed, ask if the user wants to repair an
# incomplete installation or link to the obico server
force_clone = False
obico_instances: List[MoonrakerObico] = get_instances(MoonrakerObico)
if obico_instances:
self._print_is_already_installed()
options = ["l", "r", "b"]
action = get_selection_input("Perform action", option_list=options)
if action.lower() == "b":
Logger.print_info("Exiting Obico for Klipper installation ...")
return
elif action.lower() == "l":
unlinked_instances: List[MoonrakerObico] = [
obico for obico in obico_instances if not obico.is_linked
]
self._link_obico_instances(unlinked_instances)
return
else:
Logger.print_status("Re-Installing Obico for Klipper ...")
force_clone = True
# let the user confirm installation
kl_instances: List[Klipper] = get_instances(Klipper)
mr_instances: List[Moonraker] = get_instances(Moonraker)
self._print_moonraker_instances(mr_instances)
if not get_confirm(
"Continue Obico for Klipper installation?",
default_choice=True,
allow_go_back=True,
):
return
try:
git_clone_wrapper(OBICO_REPO, OBICO_DIR, force=force_clone)
self._install_dependencies()
# ask the user for the obico server url
self._get_server_url()
# create obico instances
for moonraker in mr_instances:
instance = MoonrakerObico(suffix=moonraker.suffix)
instance.create()
cmd_sysctl_service(instance.service_file_path.name, "enable")
# create obico config
self._create_obico_cfg(instance, moonraker)
# create obico macros
self._create_obico_macros_cfg(moonraker)
# create obico update manager
self._create_obico_update_manager_cfg(moonraker)
cmd_sysctl_service(instance.service_file_path.name, "start")
cmd_sysctl_manage("daemon-reload")
# add to klippers config
self._patch_printer_cfg(kl_instances)
InstanceManager.restart_all(kl_instances)
# add to moonraker update manager
self._patch_moonraker_conf(mr_instances)
InstanceManager.restart_all(mr_instances)
# check linking of / ask for linking instances
self._check_and_opt_link_instances()
Logger.print_dialog(
DialogType.SUCCESS,
["Obico for Klipper successfully installed!"],
center_content=True,
)
except Exception as e:
Logger.print_error(f"Error during Obico for Klipper installation:\n{e}")
def update_extension(self, **kwargs) -> None:
Logger.print_status("Updating Obico for Klipper ...")
try:
instances = get_instances(MoonrakerObico)
InstanceManager.stop_all(instances)
git_pull_wrapper(OBICO_REPO, OBICO_DIR)
self._install_dependencies()
InstanceManager.start_all(instances)
Logger.print_ok("Obico for Klipper successfully updated!")
except Exception as e:
Logger.print_error(f"Error during Obico for Klipper update:\n{e}")
def remove_extension(self, **kwargs) -> None:
Logger.print_status("Removing Obico for Klipper ...")
kl_instances: List[Klipper] = get_instances(Klipper)
mr_instances: List[Moonraker] = get_instances(Moonraker)
ob_instances: List[MoonrakerObico] = get_instances(MoonrakerObico)
try:
self._remove_obico_instances(ob_instances)
self._remove_obico_dir()
self._remove_obico_env()
remove_config_section(f"include {OBICO_MACROS_CFG_NAME}", kl_instances)
remove_config_section(f"include {OBICO_UPDATE_CFG_NAME}", mr_instances)
Logger.print_dialog(
DialogType.SUCCESS,
["Obico for Klipper successfully removed!"],
center_content=True,
)
except Exception as e:
Logger.print_error(f"Error during Obico for Klipper removal:\n{e}")
def _obico_server_url_prompt(self) -> None:
Logger.print_dialog(
DialogType.CUSTOM,
custom_title="Obico Server URL",
content=[
"You can use a self-hosted Obico Server or the Obico Cloud. "
"For more information, please visit:",
"https://obico.io.",
"\n\n",
"For the Obico Cloud, leave it as the default:",
"https://app.obico.io.",
"\n\n",
"For self-hosted server, specify:",
"http://server_ip:port",
"For instance, 'http://192.168.0.5:3334'.",
],
)
def _print_moonraker_instances(self, mr_instances: List[Moonraker]) -> None:
mr_names = [f"{moonraker.data_dir.name}" for moonraker in mr_instances]
if len(mr_names) > 1:
Logger.print_dialog(
DialogType.INFO,
[
"The following Moonraker instances were found:",
*mr_names,
"\n\n",
"The setup will apply the same names to Obico!",
],
)
def _print_is_already_installed(self) -> None:
Logger.print_dialog(
DialogType.INFO,
[
"Obico is already installed!",
"It is safe to run the installer again to link your "
"printer or repair any issues.",
"\n\n",
"You can perform the following actions:",
"L) Link printer to the Obico server",
"R) Repair installation",
],
)
def _get_server_url(self) -> None:
self._obico_server_url_prompt()
pattern = r"^(http|https)://[a-zA-Z0-9./?=_%:-]*$"
self.server_url = get_string_input(
"Obico Server URL",
regex=pattern,
default="https://app.obico.io",
)
def _install_dependencies(self) -> None:
# install dependencies
script = OBICO_DIR.joinpath("install.sh")
package_list = parse_packages_from_file(script)
check_install_dependencies({*package_list})
# create virtualenv
if create_python_venv(OBICO_ENV_DIR):
install_python_requirements(OBICO_ENV_DIR, OBICO_REQ_FILE)
def _create_obico_macros_cfg(self, moonraker: Moonraker) -> None:
macros_cfg = OBICO_DIR.joinpath(f"include_cfgs/{OBICO_MACROS_CFG_NAME}")
macros_target = moonraker.base.cfg_dir.joinpath(OBICO_MACROS_CFG_NAME)
if not macros_target.exists():
shutil.copy(macros_cfg, macros_target)
else:
Logger.print_info(
f"Obico's '{OBICO_MACROS_CFG_NAME}' in {moonraker.base.cfg_dir} already exists! Skipped ..."
)
def _create_obico_update_manager_cfg(self, moonraker: Moonraker) -> None:
update_cfg = OBICO_DIR.joinpath(OBICO_UPDATE_CFG_SAMPLE_NAME)
update_cfg_target = moonraker.base.cfg_dir.joinpath(OBICO_UPDATE_CFG_NAME)
if not update_cfg_target.exists():
shutil.copy(update_cfg, update_cfg_target)
else:
Logger.print_info(
f"Obico's '{OBICO_UPDATE_CFG_NAME}' in {moonraker.base.cfg_dir} already exists! Skipped ..."
)
def _create_obico_cfg(
self, current_instance: MoonrakerObico, moonraker: Moonraker
) -> None:
cfg_template = OBICO_DIR.joinpath(OBICO_CFG_SAMPLE_NAME)
cfg_target_file = current_instance.cfg_file
if not cfg_template.exists():
Logger.print_error(
f"Obico config template file {cfg_target_file} does not exist!"
)
return
if not cfg_target_file.exists():
shutil.copy(cfg_template, cfg_target_file)
self._patch_obico_cfg(moonraker, current_instance)
else:
Logger.print_info(
f"Obico config in {current_instance.base.cfg_dir} already exists! Skipped ..."
)
def _patch_obico_cfg(self, moonraker: Moonraker, obico: MoonrakerObico) -> None:
scp = SimpleConfigParser()
scp.read(obico.cfg_file)
scp.set("server", "url", self.server_url)
scp.set("moonraker", "port", str(moonraker.port))
scp.set(
"logging",
"path",
obico.base.log_dir.joinpath(obico.log_file_name).as_posix(),
)
scp.write(obico.cfg_file)
def _patch_printer_cfg(self, klipper: List[Klipper]) -> None:
add_config_section(
section=f"include {OBICO_MACROS_CFG_NAME}", instances=klipper
)
def _patch_moonraker_conf(self, instances: List[Moonraker]) -> None:
add_config_section(
section=f"include {OBICO_UPDATE_CFG_NAME}", instances=instances
)
def _link_obico_instances(self, unlinked_instances) -> None:
for obico in unlinked_instances:
obico.link()
def _check_and_opt_link_instances(self) -> None:
Logger.print_status("Checking link status of Obico instances ...")
ob_instances: List[MoonrakerObico] = get_instances(MoonrakerObico)
unlinked_instances: List[MoonrakerObico] = [
obico for obico in ob_instances if not obico.is_linked
]
if unlinked_instances:
Logger.print_dialog(
DialogType.INFO,
[
"The Obico instances for the following printers are not "
"linked to the server:",
*[f"{obico.data_dir.name}" for obico in unlinked_instances],
"\n\n",
"It will take only 10 seconds to link the printer to the Obico server.",
"For more information visit:",
"https://www.obico.io/docs/user-guides/klipper-setup/",
"\n\n",
"If you don't want to link the printer now, you can restart the "
"linking process later by running this installer again.",
],
)
if not get_confirm("Do you want to link the printers now?"):
Logger.print_info("Linking to Obico server skipped ...")
return
self._link_obico_instances(unlinked_instances)
def _remove_obico_instances(
self,
instance_list: List[MoonrakerObico],
) -> None:
if not instance_list:
Logger.print_info("No Obico instances found. Skipped ...")
return
for instance in instance_list:
Logger.print_status(
f"Removing instance {instance.service_file_path.stem} ..."
)
InstanceManager.remove(instance)
def _remove_obico_dir(self) -> None:
Logger.print_status("Removing Obico for Klipper directory ...")
if not OBICO_DIR.exists():
Logger.print_info(f"'{OBICO_DIR}' does not exist. Skipped ...")
return
run_remove_routines(OBICO_DIR)
def _remove_obico_env(self) -> None:
Logger.print_status("Removing Obico for Klipper environment ...")
if not OBICO_ENV_DIR.exists():
Logger.print_info(f"'{OBICO_ENV_DIR}' does not exist. Skipped ...")
return
run_remove_routines(OBICO_ENV_DIR)

View File

@@ -0,0 +1,19 @@
# PrettyGCode website configuration
# copy this file to /etc/nginx/sites-available/pgcode.local.conf
# then to enable:
# sudo ln -s /etc/nginx/sites-available/pgcode.local.conf /etc/nginx/sites-enabled/pgcode.local.conf
# then restart ngninx:
# sudo systemctl reload nginx
server {
listen %PORT%;
listen [::]:%PORT%;
server_name pgcode.local;
root %ROOT_DIR%;
index pgcode.html;
location / {
try_files $uri $uri/ =404;
}
}

View File

@@ -0,0 +1,10 @@
{
"metadata": {
"index": 5,
"module": "pretty_gcode_extension",
"maintained_by": "Kragrathea",
"display_name": "PrettyGCode for Klipper",
"description": ["3D G-Code viewer for Klipper"],
"updates": true
}
}

View File

@@ -0,0 +1,101 @@
# ======================================================================= #
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
import shutil
from pathlib import Path
from components.webui_client.client_utils import create_nginx_cfg
from core.constants import NGINX_SITES_AVAILABLE, NGINX_SITES_ENABLED
from core.logger import DialogType, Logger
from extensions.base_extension import BaseExtension
from utils.common import check_install_dependencies
from utils.fs_utils import (
remove_file,
)
from utils.git_utils import git_clone_wrapper, git_pull_wrapper
from utils.input_utils import get_number_input
from utils.sys_utils import cmd_sysctl_service, get_ipv4_addr
MODULE_PATH = Path(__file__).resolve().parent
PGC_DIR = Path.home().joinpath("pgcode")
PGC_REPO = "https://github.com/Kragrathea/pgcode"
PGC_CONF = "pgcode.local.conf"
# noinspection PyMethodMayBeStatic
class PrettyGcodeExtension(BaseExtension):
def install_extension(self, **kwargs) -> None:
Logger.print_status("Installing PrettyGCode for Klipper ...")
Logger.print_dialog(
DialogType.ATTENTION,
[
"Make sure you don't select a port which is already in use by "
"another application. Your input will not be validated! Choosing a port "
"which is already in use by another application may cause issues!",
"The default port is 7136.",
],
)
port = get_number_input(
"On which port should PrettyGCode run",
min_count=0,
default=7136,
allow_go_back=True,
)
check_install_dependencies({"nginx"})
try:
if PGC_DIR.exists():
shutil.rmtree(PGC_DIR)
git_clone_wrapper(PGC_REPO, PGC_DIR)
create_nginx_cfg(
"PrettyGCode for Klipper",
cfg_name=PGC_CONF,
template_src=MODULE_PATH.joinpath(f"assets/{PGC_CONF}"),
ROOT_DIR=PGC_DIR,
PORT=port,
)
cmd_sysctl_service("nginx", "restart")
log = f"Open PrettyGCode now on: http://{get_ipv4_addr()}:{port}"
Logger.print_ok("PrettyGCode installation complete!", start="\n")
Logger.print_ok(log, prefix=False, end="\n\n")
except Exception as e:
Logger.print_error(
f"Error during PrettyGCode for Klipper installation: {e}"
)
def update_extension(self, **kwargs) -> None:
Logger.print_status("Updating PrettyGCode for Klipper ...")
try:
git_pull_wrapper(PGC_REPO, PGC_DIR)
except Exception as e:
Logger.print_error(f"Error during PrettyGCode for Klipper update: {e}")
def remove_extension(self, **kwargs) -> None:
try:
Logger.print_status("Removing PrettyGCode for Klipper ...")
# remove pgc dir
shutil.rmtree(PGC_DIR)
# remove nginx config
remove_file(NGINX_SITES_AVAILABLE.joinpath(PGC_CONF), True)
remove_file(NGINX_SITES_ENABLED.joinpath(PGC_CONF), True)
# restart nginx
cmd_sysctl_service("nginx", "restart")
Logger.print_ok("PrettyGCode for Klipper removed!")
except Exception as e:
Logger.print_error(f"Error during PrettyGCode for Klipper removal: {e}")

View File

@@ -0,0 +1,29 @@
# ======================================================================= #
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
from pathlib import Path
MODULE_PATH = Path(__file__).resolve().parent
# repo
TG_BOT_REPO = "https://github.com/nlef/moonraker-telegram-bot.git"
# names
TG_BOT_CFG_NAME = "telegram.conf"
TG_BOT_LOG_NAME = "telegram.log"
TG_BOT_SERVICE_NAME = "moonraker-telegram-bot.service"
TG_BOT_ENV_FILE_NAME = "moonraker-telegram-bot.env"
# directories
TG_BOT_DIR = Path.home().joinpath("moonraker-telegram-bot")
TG_BOT_ENV = Path.home().joinpath("moonraker-telegram-bot-env")
# files
TG_BOT_SERVICE_TEMPLATE = MODULE_PATH.joinpath(f"assets/{TG_BOT_SERVICE_NAME}")
TG_BOT_ENV_FILE_TEMPLATE = MODULE_PATH.joinpath(f"assets/{TG_BOT_ENV_FILE_NAME}")
TG_BOT_REQ_FILE = TG_BOT_DIR.joinpath("scripts/requirements.txt")

View File

@@ -0,0 +1 @@
TELEGRAM_BOT_ARGS="%TELEGRAM_BOT_DIR%/bot/main.py -c %CFG% -l %LOG%"

View File

@@ -0,0 +1,16 @@
[Unit]
Description=Moonraker Telegram Bot SV1 %INST%
Documentation=https://github.com/nlef/moonraker-telegram-bot/wiki
After=network-online.target
[Install]
WantedBy=multi-user.target
[Service]
Type=simple
User=%USER%
WorkingDirectory=%TELEGRAM_BOT_DIR%
EnvironmentFile=%ENV_FILE%
ExecStart=%ENV%/bin/python $TELEGRAM_BOT_ARGS
Restart=always
RestartSec=10

View File

@@ -0,0 +1,11 @@
{
"metadata": {
"index": 4,
"module": "moonraker_telegram_bot_extension",
"maintained_by": "nlef",
"display_name": "Moonraker Telegram Bot",
"description": ["Control your printer with the Telegram messenger app."],
"project_url": "https://github.com/nlef/moonraker-telegram-bot",
"updates": true
}
}

View File

@@ -0,0 +1,127 @@
# ======================================================================= #
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
from __future__ import annotations
from dataclasses import dataclass, field
from pathlib import Path
from subprocess import CalledProcessError
from components.moonraker.moonraker import Moonraker
from core.constants import CURRENT_USER
from core.instance_manager.base_instance import BaseInstance
from core.logger import Logger
from extensions.telegram_bot import (
TG_BOT_CFG_NAME,
TG_BOT_DIR,
TG_BOT_ENV,
TG_BOT_ENV_FILE_NAME,
TG_BOT_ENV_FILE_TEMPLATE,
TG_BOT_LOG_NAME,
TG_BOT_SERVICE_TEMPLATE,
)
from utils.fs_utils import create_folders
from utils.sys_utils import get_service_file_path
# noinspection PyMethodMayBeStatic
@dataclass(repr=True)
class MoonrakerTelegramBot:
suffix: str
base: BaseInstance = field(init=False, repr=False)
service_file_path: Path = field(init=False)
log_file_name: str = TG_BOT_LOG_NAME
bot_dir: Path = TG_BOT_DIR
env_dir: Path = TG_BOT_ENV
data_dir: Path = field(init=False)
cfg_file: Path = field(init=False)
def __post_init__(self):
self.base: BaseInstance = BaseInstance(Moonraker, self.suffix)
self.base.log_file_name = self.log_file_name
self.service_file_path: Path = get_service_file_path(
MoonrakerTelegramBot, self.suffix
)
self.data_dir: Path = self.base.data_dir
self.cfg_file = self.base.cfg_dir.joinpath(TG_BOT_CFG_NAME)
def create(self) -> None:
from utils.sys_utils import create_env_file, create_service_file
Logger.print_status("Creating new Moonraker Telegram Bot Instance ...")
try:
create_folders(self.base.base_folders)
create_service_file(
name=self.service_file_path.name,
content=self._prep_service_file_content(),
)
create_env_file(
path=self.base.sysd_dir.joinpath(TG_BOT_ENV_FILE_NAME),
content=self._prep_env_file_content(),
)
except CalledProcessError as e:
Logger.print_error(f"Error creating instance: {e}")
raise
except OSError as e:
Logger.print_error(f"Error creating env file: {e}")
raise
def _prep_service_file_content(self) -> str:
template = TG_BOT_SERVICE_TEMPLATE
try:
with open(template, "r") as template_file:
template_content = template_file.read()
except FileNotFoundError:
Logger.print_error(f"Unable to open {template} - File not found")
raise
service_content = template_content.replace(
"%USER%",
CURRENT_USER,
)
service_content = service_content.replace(
"%TELEGRAM_BOT_DIR%",
self.bot_dir.as_posix(),
)
service_content = service_content.replace(
"%ENV%",
self.env_dir.as_posix(),
)
service_content = service_content.replace(
"%ENV_FILE%",
self.base.sysd_dir.joinpath(TG_BOT_ENV_FILE_NAME).as_posix(),
)
return service_content
def _prep_env_file_content(self) -> str:
template = TG_BOT_ENV_FILE_TEMPLATE
try:
with open(template, "r") as env_file:
env_template_file_content = env_file.read()
except FileNotFoundError:
Logger.print_error(f"Unable to open {template} - File not found")
raise
env_file_content = env_template_file_content.replace(
"%TELEGRAM_BOT_DIR%",
self.bot_dir.as_posix(),
)
env_file_content = env_file_content.replace(
"%CFG%",
f"{self.base.cfg_dir}/printer.cfg",
)
env_file_content = env_file_content.replace(
"%LOG%",
self.base.log_dir.joinpath(self.log_file_name).as_posix(),
)
return env_file_content

View File

@@ -0,0 +1,225 @@
# ======================================================================= #
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
import shutil
from subprocess import run
from typing import List
from components.moonraker.moonraker import Moonraker
from core.instance_manager.instance_manager import InstanceManager
from core.logger import DialogType, Logger
from extensions.base_extension import BaseExtension
from extensions.telegram_bot import TG_BOT_REPO, TG_BOT_REQ_FILE
from extensions.telegram_bot.moonraker_telegram_bot import (
TG_BOT_DIR,
TG_BOT_ENV,
MoonrakerTelegramBot,
)
from utils.common import check_install_dependencies
from utils.config_utils import add_config_section, remove_config_section
from utils.fs_utils import remove_file
from utils.git_utils import git_clone_wrapper, git_pull_wrapper
from utils.input_utils import get_confirm
from utils.instance_utils import get_instances
from utils.sys_utils import (
cmd_sysctl_manage,
cmd_sysctl_service,
create_python_venv,
install_python_requirements,
parse_packages_from_file,
)
# noinspection PyMethodMayBeStatic
class TelegramBotExtension(BaseExtension):
def install_extension(self, **kwargs) -> None:
Logger.print_status("Installing Moonraker Telegram Bot ...")
mr_instances: List[Moonraker] = get_instances(Moonraker)
if not mr_instances:
Logger.print_dialog(
DialogType.WARNING,
[
"No Moonraker instances found!",
"Moonraker Telegram Bot requires Moonraker to be installed. "
"Please install Moonraker first!",
],
)
return
instance_names = [
f"{instance.service_file_path.name}" for instance in mr_instances
]
Logger.print_dialog(
DialogType.INFO,
[
"The following Moonraker instances were found:",
*instance_names,
"\n\n",
"The setup will apply the same names to Telegram Bot!",
],
)
if not get_confirm(
"Continue Moonraker Telegram Bot installation?",
default_choice=True,
allow_go_back=True,
):
return
create_example_cfg = get_confirm("Create example telegram.conf?")
try:
git_clone_wrapper(TG_BOT_REPO, TG_BOT_DIR)
self._install_dependencies()
# create and start services / create bot configs
show_config_dialog = False
tb_names = [mr_i.suffix for mr_i in mr_instances]
for name in tb_names:
instance = MoonrakerTelegramBot(suffix=name)
instance.create()
cmd_sysctl_service(instance.service_file_path.name, "enable")
if create_example_cfg:
Logger.print_status(
f"Creating Telegram Bot config in {instance.base.cfg_dir} ..."
)
template = TG_BOT_DIR.joinpath("scripts/base_install_template")
target_file = instance.cfg_file
if not target_file.exists():
show_config_dialog = True
run(["cp", template, target_file], check=True)
else:
Logger.print_info(
f"Telegram Bot config in {instance.base.cfg_dir} already exists! Skipped ..."
)
cmd_sysctl_service(instance.service_file_path.name, "start")
cmd_sysctl_manage("daemon-reload")
# add to moonraker update manager
self._patch_bot_update_manager(mr_instances)
# restart moonraker
InstanceManager.restart_all(mr_instances)
if show_config_dialog:
Logger.print_dialog(
DialogType.ATTENTION,
[
"During the installation of the Moonraker Telegram Bot, "
"a basic config was created per instance. You need to edit the "
"config file to set up your Telegram Bot. Please refer to the "
"following wiki page for further information:",
"https://github.com/nlef/moonraker-telegram-bot/wiki",
],
margin_bottom=1,
)
Logger.print_ok("Telegram Bot installation complete!")
except Exception as e:
Logger.print_error(
f"Error during installation of Moonraker Telegram Bot:\n{e}"
)
def update_extension(self, **kwargs) -> None:
Logger.print_status("Updating Moonraker Telegram Bot ...")
instances = get_instances(MoonrakerTelegramBot)
InstanceManager.stop_all(instances)
git_pull_wrapper(TG_BOT_REPO, TG_BOT_DIR)
self._install_dependencies()
InstanceManager.start_all(instances)
def remove_extension(self, **kwargs) -> None:
Logger.print_status("Removing Moonraker Telegram Bot ...")
mr_instances: List[Moonraker] = get_instances(Moonraker)
tb_instances: List[MoonrakerTelegramBot] = get_instances(MoonrakerTelegramBot)
try:
self._remove_bot_instances(tb_instances)
self._remove_bot_dir()
self._remove_bot_env()
remove_config_section("update_manager moonraker-telegram-bot", mr_instances)
self._delete_bot_logs(tb_instances)
except Exception as e:
Logger.print_error(f"Error during removal of Moonraker Telegram Bot:\n{e}")
Logger.print_ok("Moonraker Telegram Bot removed!")
def _install_dependencies(self) -> None:
# install dependencies
script = TG_BOT_DIR.joinpath("scripts/install.sh")
package_list = parse_packages_from_file(script)
check_install_dependencies({*package_list})
# create virtualenv
if create_python_venv(TG_BOT_ENV):
install_python_requirements(TG_BOT_ENV, TG_BOT_REQ_FILE)
def _patch_bot_update_manager(self, instances: List[Moonraker]) -> None:
env_py = f"{TG_BOT_ENV}/bin/python"
add_config_section(
section="update_manager moonraker-telegram-bot",
instances=instances,
options=[
("type", "git_repo"),
("path", str(TG_BOT_DIR)),
("orgin", TG_BOT_REPO),
("env", env_py),
("requirements", "scripts/requirements.txt"),
("install_script", "scripts/install.sh"),
],
)
def _remove_bot_instances(
self,
instance_list: List[MoonrakerTelegramBot],
) -> None:
for instance in instance_list:
Logger.print_status(
f"Removing instance {instance.service_file_path.stem} ..."
)
InstanceManager.remove(instance)
def _remove_bot_dir(self) -> None:
if not TG_BOT_DIR.exists():
Logger.print_info(f"'{TG_BOT_DIR}' does not exist. Skipped ...")
return
try:
shutil.rmtree(TG_BOT_DIR)
except OSError as e:
Logger.print_error(f"Unable to delete '{TG_BOT_DIR}':\n{e}")
def _remove_bot_env(self) -> None:
if not TG_BOT_ENV.exists():
Logger.print_info(f"'{TG_BOT_ENV}' does not exist. Skipped ...")
return
try:
shutil.rmtree(TG_BOT_ENV)
except OSError as e:
Logger.print_error(f"Unable to delete '{TG_BOT_ENV}':\n{e}")
def _delete_bot_logs(self, instances: List[MoonrakerTelegramBot]) -> None:
all_logfiles = []
for instance in instances:
all_logfiles = list(instance.base.log_dir.glob("telegram_bot.log*"))
if not all_logfiles:
Logger.print_info("No Moonraker Telegram Bot logs found. Skipped ...")
return
for log in all_logfiles:
Logger.print_status(f"Remove '{log}'")
remove_file(log)