From b70ac0dfd7d23035480434984c7b01244f9d1ae3 Mon Sep 17 00:00:00 2001 From: dw-0 Date: Wed, 1 May 2024 21:12:37 +0200 Subject: [PATCH] refactor: move config related helper methods into own util module Signed-off-by: Dominik Willner --- .../client_config/client_config_remove.py | 3 +- .../client_config/client_config_setup.py | 7 +- .../components/webui_client/client_remove.py | 2 +- kiauh/components/webui_client/client_setup.py | 2 +- kiauh/utils/config_utils.py | 83 ++++++++++++++ kiauh/utils/filesystem_utils.py | 101 +----------------- 6 files changed, 90 insertions(+), 108 deletions(-) create mode 100644 kiauh/utils/config_utils.py diff --git a/kiauh/components/webui_client/client_config/client_config_remove.py b/kiauh/components/webui_client/client_config/client_config_remove.py index 6a5a014..3e1dffd 100644 --- a/kiauh/components/webui_client/client_config/client_config_remove.py +++ b/kiauh/components/webui_client/client_config/client_config_remove.py @@ -16,7 +16,8 @@ from components.klipper.klipper import Klipper from components.moonraker.moonraker import Moonraker from components.webui_client.base_data import BaseWebClientConfig from core.instance_manager.instance_manager import InstanceManager -from utils.filesystem_utils import remove_file, remove_config_section +from utils.config_utils import remove_config_section +from utils.filesystem_utils import remove_file from utils.logger import Logger diff --git a/kiauh/components/webui_client/client_config/client_config_setup.py b/kiauh/components/webui_client/client_config/client_config_setup.py index 06f1f05..8b01c48 100644 --- a/kiauh/components/webui_client/client_config/client_config_setup.py +++ b/kiauh/components/webui_client/client_config/client_config_setup.py @@ -26,11 +26,8 @@ from components.webui_client.client_utils import ( from core.instance_manager.instance_manager import InstanceManager from utils.common import backup_printer_config_dir -from utils.filesystem_utils import ( - create_symlink, - add_config_section, - add_config_section_at_top, -) +from utils.config_utils import add_config_section, add_config_section_at_top +from utils.filesystem_utils import create_symlink from utils.git_utils import git_clone_wrapper, git_pull_wrapper from utils.input_utils import get_confirm from utils.logger import Logger diff --git a/kiauh/components/webui_client/client_remove.py b/kiauh/components/webui_client/client_remove.py index 4315aa1..745954c 100644 --- a/kiauh/components/webui_client/client_remove.py +++ b/kiauh/components/webui_client/client_remove.py @@ -23,10 +23,10 @@ from components.webui_client.client_config.client_config_remove import ( from components.webui_client.client_utils import backup_mainsail_config_json from core.instance_manager.instance_manager import InstanceManager +from utils.config_utils import remove_config_section from utils.filesystem_utils import ( remove_nginx_config, remove_nginx_logs, - remove_config_section, ) from utils.logger import Logger diff --git a/kiauh/components/webui_client/client_setup.py b/kiauh/components/webui_client/client_setup.py index e7cef99..0afce0b 100644 --- a/kiauh/components/webui_client/client_setup.py +++ b/kiauh/components/webui_client/client_setup.py @@ -37,6 +37,7 @@ from core.instance_manager.instance_manager import InstanceManager from core.settings.kiauh_settings import KiauhSettings from utils import NGINX_SITES_AVAILABLE, NGINX_SITES_ENABLED from utils.common import check_install_dependencies +from utils.config_utils import add_config_section from utils.filesystem_utils import ( unzip, copy_upstream_nginx_cfg, @@ -44,7 +45,6 @@ from utils.filesystem_utils import ( create_nginx_cfg, create_symlink, remove_file, - add_config_section, read_ports_from_nginx_configs, is_valid_port, get_next_free_port, diff --git a/kiauh/utils/config_utils.py b/kiauh/utils/config_utils.py new file mode 100644 index 0000000..42a2992 --- /dev/null +++ b/kiauh/utils/config_utils.py @@ -0,0 +1,83 @@ +# ======================================================================= # +# 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 tempfile +from pathlib import Path +from typing import List, TypeVar, Tuple, Optional + +from components.klipper.klipper import Klipper +from components.moonraker.moonraker import Moonraker +from core.config_manager.config_manager import ConfigManager +from utils.logger import Logger + +B = TypeVar("B", Klipper, Moonraker) +ConfigOption = Tuple[str, str] + + +def add_config_section( + section: str, + instances: List[B], + options: Optional[List[ConfigOption]] = None, +) -> None: + for instance in instances: + cfg_file = instance.cfg_file + Logger.print_status(f"Add section '[{section}]' to '{cfg_file}' ...") + + if not Path(cfg_file).exists(): + Logger.print_warn(f"'{cfg_file}' not found!") + continue + + cm = ConfigManager(cfg_file) + if cm.config.has_section(section): + Logger.print_info("Section already exist. Skipped ...") + continue + + cm.config.add_section(section) + + if options is not None: + for option in options: + cm.config.set(section, option[0], option[1]) + + cm.write_config() + + +def add_config_section_at_top(section: str, instances: List[B]): + for instance in instances: + tmp_cfg = tempfile.NamedTemporaryFile(mode="w", delete=False) + tmp_cfg_path = Path(tmp_cfg.name) + cmt = ConfigManager(tmp_cfg_path) + cmt.config.add_section(section) + cmt.write_config() + tmp_cfg.close() + + cfg_file = instance.cfg_file + with open(cfg_file, "r") as org: + org_content = org.readlines() + with open(tmp_cfg_path, "a") as tmp: + tmp.writelines(org_content) + + cfg_file.unlink() + tmp_cfg_path.rename(cfg_file) + + +def remove_config_section(section: str, instances: List[B]) -> None: + for instance in instances: + cfg_file = instance.cfg_file + Logger.print_status(f"Remove section '[{section}]' from '{cfg_file}' ...") + + if not Path(cfg_file).exists(): + Logger.print_warn(f"'{cfg_file}' not found!") + continue + + cm = ConfigManager(cfg_file) + if not cm.config.has_section(section): + Logger.print_info("Section does not exist. Skipped ...") + continue + + cm.config.remove_section(section) + cm.write_config() diff --git a/kiauh/utils/filesystem_utils.py b/kiauh/utils/filesystem_utils.py index 367e4f7..dbf5b51 100644 --- a/kiauh/utils/filesystem_utils.py +++ b/kiauh/utils/filesystem_utils.py @@ -12,15 +12,12 @@ import re import shutil import subprocess -import tempfile from pathlib import Path from zipfile import ZipFile -from typing import List, TypeVar, Tuple, Optional +from typing import List from components.klipper.klipper import Klipper -from components.moonraker.moonraker import Moonraker -from core.config_manager.config_manager import ConfigManager from core.instance_manager.instance_manager import InstanceManager from utils import ( NGINX_SITES_AVAILABLE, @@ -31,10 +28,6 @@ from utils import ( from utils.logger import Logger -B = TypeVar("B", Klipper, Moonraker) -ConfigOption = Tuple[str, str] - - def check_file_exist(file_path: Path, sudo=False) -> bool: """ Helper function for checking the existence of a file | @@ -183,98 +176,6 @@ def get_next_free_port(ports_in_use: List[int]) -> int: return min(valid_ports - used_ports) -def add_config_section( - section: str, - instances: List[B], - options: Optional[List[ConfigOption]] = None, -) -> None: - for instance in instances: - cfg_file = instance.cfg_file - Logger.print_status(f"Add section '[{section}]' to '{cfg_file}' ...") - - if not Path(cfg_file).exists(): - Logger.print_warn(f"'{cfg_file}' not found!") - continue - - cm = ConfigManager(cfg_file) - if cm.config.has_section(section): - Logger.print_info("Section already exist. Skipped ...") - continue - - cm.config.add_section(section) - - if options is not None: - for option in options: - cm.config.set(section, option[0], option[1]) - - cm.write_config() - - -def add_config_section_at_top(section: str, instances: List[B]): - for instance in instances: - tmp_cfg = tempfile.NamedTemporaryFile(mode="w", delete=False) - tmp_cfg_path = Path(tmp_cfg.name) - cmt = ConfigManager(tmp_cfg_path) - cmt.config.add_section(section) - cmt.write_config() - tmp_cfg.close() - - cfg_file = instance.cfg_file - with open(cfg_file, "r") as org: - org_content = org.readlines() - with open(tmp_cfg_path, "a") as tmp: - tmp.writelines(org_content) - - cfg_file.unlink() - tmp_cfg_path.rename(cfg_file) - - -def remove_config_section(section: str, instances: List[B]) -> None: - for instance in instances: - cfg_file = instance.cfg_file - Logger.print_status(f"Remove section '[{section}]' from '{cfg_file}' ...") - - if not Path(cfg_file).exists(): - Logger.print_warn(f"'{cfg_file}' not found!") - continue - - cm = ConfigManager(cfg_file) - if not cm.config.has_section(section): - Logger.print_info("Section does not exist. Skipped ...") - continue - - cm.config.remove_section(section) - cm.write_config() - - -def patch_moonraker_conf( - moonraker_instances: List[Moonraker], - name: str, - section_name: str, - template_file: str, -) -> None: - for instance in moonraker_instances: - cfg_file = instance.cfg_file - Logger.print_status(f"Add {name} update section to '{cfg_file}' ...") - - if not Path(cfg_file).exists(): - Logger.print_warn(f"'{cfg_file}' not found!") - return - - cm = ConfigManager(cfg_file) - if cm.config.has_section(section_name): - Logger.print_info("Section already exist. Skipped ...") - return - - template = MODULE_PATH.joinpath("assets", template_file) - with open(template, "r") as t: - template_content = "\n" - template_content += t.read() - - with open(cfg_file, "a") as f: - f.write(template_content) - - def remove_nginx_config(name: str) -> None: Logger.print_status(f"Removing NGINX config for {name.capitalize()} ...") try: