From 0dfe7672b846e13cdb98fa184a5021c43441c1ec Mon Sep 17 00:00:00 2001 From: dw-0 Date: Mon, 20 May 2024 12:15:33 +0200 Subject: [PATCH] feat(extension): implement PrettyGCode for Klipper extension Signed-off-by: Dominik Willner --- kiauh/extensions/pretty_gcode/__init__.py | 0 .../pretty_gcode/assets/pgcode.local.conf | 19 ++++ kiauh/extensions/pretty_gcode/metadata.json | 10 ++ .../pretty_gcode/pretty_gcode_extension.py | 104 ++++++++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 kiauh/extensions/pretty_gcode/__init__.py create mode 100644 kiauh/extensions/pretty_gcode/assets/pgcode.local.conf create mode 100644 kiauh/extensions/pretty_gcode/metadata.json create mode 100644 kiauh/extensions/pretty_gcode/pretty_gcode_extension.py diff --git a/kiauh/extensions/pretty_gcode/__init__.py b/kiauh/extensions/pretty_gcode/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/kiauh/extensions/pretty_gcode/assets/pgcode.local.conf b/kiauh/extensions/pretty_gcode/assets/pgcode.local.conf new file mode 100644 index 0000000..eab6162 --- /dev/null +++ b/kiauh/extensions/pretty_gcode/assets/pgcode.local.conf @@ -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; + } +} diff --git a/kiauh/extensions/pretty_gcode/metadata.json b/kiauh/extensions/pretty_gcode/metadata.json new file mode 100644 index 0000000..946c046 --- /dev/null +++ b/kiauh/extensions/pretty_gcode/metadata.json @@ -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 + } +} diff --git a/kiauh/extensions/pretty_gcode/pretty_gcode_extension.py b/kiauh/extensions/pretty_gcode/pretty_gcode_extension.py new file mode 100644 index 0000000..0a1c723 --- /dev/null +++ b/kiauh/extensions/pretty_gcode/pretty_gcode_extension.py @@ -0,0 +1,104 @@ +# ======================================================================= # +# Copyright (C) 2020 - 2024 Dominik Willner # +# # +# 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 extensions.base_extension import BaseExtension +from utils import NGINX_SITES_AVAILABLE, NGINX_SITES_ENABLED +from utils.common import check_install_dependencies +from utils.fs_utils import ( + create_nginx_cfg, + remove_file, +) +from utils.git_utils import git_clone_wrapper, git_pull_wrapper +from utils.input_utils import get_number_input +from utils.logger import DialogType, Logger +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: + # remove any existing pgc dir + if PGC_DIR.exists(): + shutil.rmtree(PGC_DIR) + + # clone pgc repo + git_clone_wrapper(PGC_REPO, PGC_DIR) + + # copy pgc conf + 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}")