diff --git a/kiauh/core/menus/main_menu.py b/kiauh/core/menus/main_menu.py index 66324bc..68e964e 100644 --- a/kiauh/core/menus/main_menu.py +++ b/kiauh/core/menus/main_menu.py @@ -19,6 +19,7 @@ from kiauh.core.menus.remove_menu import RemoveMenu from kiauh.core.menus.settings_menu import SettingsMenu from kiauh.core.menus.update_menu import UpdateMenu from kiauh.modules.klipper.klipper_utils import get_klipper_status +from kiauh.modules.log_uploads.menus.log_upload_menu import LogUploadMenu from kiauh.modules.mainsail.mainsail_utils import get_mainsail_status from kiauh.modules.moonraker.moonraker_utils import get_moonraker_status from kiauh.utils.constants import ( @@ -36,7 +37,7 @@ class MainMenu(BaseMenu): super().__init__( header=True, options={ - 0: None, + 0: LogUploadMenu, 1: InstallMenu, 2: UpdateMenu, 3: RemoveMenu, diff --git a/kiauh/modules/log_uploads/__init__.py b/kiauh/modules/log_uploads/__init__.py new file mode 100644 index 0000000..7672138 --- /dev/null +++ b/kiauh/modules/log_uploads/__init__.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 + +# ======================================================================= # +# 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 # +# ======================================================================= # + +from pathlib import Path +from typing import Dict, Union, Literal + +FileKey = Literal["filepath", "display_name"] +LogFile = Dict[FileKey, Union[str, Path]] diff --git a/kiauh/modules/log_uploads/log_upload_utils.py b/kiauh/modules/log_uploads/log_upload_utils.py new file mode 100644 index 0000000..c61dbc6 --- /dev/null +++ b/kiauh/modules/log_uploads/log_upload_utils.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 + +# ======================================================================= # +# 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 # +# ======================================================================= # + +from typing import List +from pathlib import Path + +import urllib.request + +from kiauh.core.instance_manager.instance_manager import InstanceManager +from kiauh.modules.klipper.klipper import Klipper +from kiauh.modules.log_uploads import LogFile +from kiauh.utils.logger import Logger + + +def get_logfile_list() -> List[LogFile]: + cm = InstanceManager(Klipper) + log_dirs: List[Path] = [instance.log_dir for instance in cm.instances] + + logfiles: List[LogFile] = [] + for _dir in log_dirs: + for f in _dir.iterdir(): + logfiles.append({"filepath": f, "display_name": get_display_name(f)}) + + return logfiles + + +def get_display_name(filepath: Path) -> str: + printer = " ".join(filepath.parts[-3].split("_")[:-1]) + name = filepath.name + + return f"{printer}: {name}" + + +def upload_logfile(logfile: LogFile) -> None: + file = logfile.get("filepath") + name = logfile.get("display_name") + Logger.print_status(f"Uploading the following logfile from {name} ...") + + with open(file, "rb") as f: + headers = {"x-random": ""} + req = urllib.request.Request("http://paste.c-net.org/", headers=headers, data=f) + try: + response = urllib.request.urlopen(req) + link = response.read().decode("utf-8") + Logger.print_ok("Upload successfull! Access it via the following link:") + Logger.print_ok(f">>>> {link}", False) + except Exception as e: + Logger.print_error(f"Uploading logfile failed!") + Logger.print_error(str(e)) diff --git a/kiauh/modules/log_uploads/menus/log_upload_menu.py b/kiauh/modules/log_uploads/menus/log_upload_menu.py new file mode 100644 index 0000000..9c96c5c --- /dev/null +++ b/kiauh/modules/log_uploads/menus/log_upload_menu.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 + +# ======================================================================= # +# 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 textwrap + +from kiauh.core.menus import BACK_FOOTER +from kiauh.core.menus.base_menu import BaseMenu +from kiauh.modules.log_uploads.log_upload_utils import upload_logfile +from kiauh.modules.log_uploads.log_upload_utils import get_logfile_list +from kiauh.utils.constants import RESET_FORMAT, COLOR_YELLOW + + +# noinspection PyMethodMayBeStatic +class LogUploadMenu(BaseMenu): + def __init__(self): + self.logfile_list = get_logfile_list() + options = {index: self.upload for index in range(len(self.logfile_list))} + super().__init__( + header=True, + options=options, + footer_type=BACK_FOOTER, + ) + + def print_menu(self): + header = " [ Log Upload ] " + color = COLOR_YELLOW + count = 62 - len(color) - len(RESET_FORMAT) + menu = textwrap.dedent( + f""" + /=======================================================\\ + | {color}{header:~^{count}}{RESET_FORMAT} | + |-------------------------------------------------------| + | You can select the following logfiles for uploading: | + | | + """ + )[1:] + + logfile_list = get_logfile_list() + for logfile in enumerate(logfile_list): + line = f"{logfile[0]}) {logfile[1].get('display_name')}" + menu += f"| {line:<54}|\n" + + print(menu, end="") + + def upload(self, **kwargs): + upload_logfile(self.logfile_list[kwargs.get("opt_index")])