From 00665109c2f4b808dbc52e704a875f0e28ab6935 Mon Sep 17 00:00:00 2001 From: dw-0 Date: Fri, 22 Mar 2024 22:11:56 +0100 Subject: [PATCH] feat: allow sections to be added to the top of a config file Signed-off-by: Dominik Willner --- .../client_config/client_config_setup.py | 15 +++++---- kiauh/utils/filesystem_utils.py | 33 ++++++++++++++++--- 2 files changed, 37 insertions(+), 11 deletions(-) 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 13f1062..01ca5cc 100644 --- a/kiauh/components/webui_client/client_config/client_config_setup.py +++ b/kiauh/components/webui_client/client_config/client_config_setup.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python3 - # ======================================================================= # # Copyright (C) 2020 - 2024 Dominik Willner # # # @@ -21,16 +19,17 @@ from components.webui_client import ClientConfigData, ClientName, ClientData from components.webui_client.client_dialogs import print_client_already_installed_dialog from components.webui_client.client_utils import ( load_client_data, - backup_client_config_data, config_for_other_client_exist, - ) + backup_client_config_data, + config_for_other_client_exist, +) from core.config_manager.config_manager import ConfigManager from core.instance_manager.instance_manager import InstanceManager from core.repo_manager.repo_manager import RepoManager from utils.filesystem_utils import ( create_symlink, - add_config_section, -) + add_config_section, add_config_section_at_top, + ) from utils.input_utils import get_confirm from utils.logger import Logger @@ -70,7 +69,9 @@ def install_client_config(client_name: ClientName) -> None: ("managed_services", "klipper"), ], ) - add_config_section(client_config.get("printer_cfg_section"), kl_instances) + add_config_section_at_top( + client_config.get("printer_cfg_section"), kl_instances + ) kl_im.restart_all_instance() except Exception as e: diff --git a/kiauh/utils/filesystem_utils.py b/kiauh/utils/filesystem_utils.py index b7fed2f..0406bc2 100644 --- a/kiauh/utils/filesystem_utils.py +++ b/kiauh/utils/filesystem_utils.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import os # ======================================================================= # # Copyright (C) 2020 - 2024 Dominik Willner # @@ -12,15 +13,15 @@ import re import shutil import subprocess +import tempfile from pathlib import Path from zipfile import ZipFile -from typing import List, Type, TypeVar, Union, Tuple +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 core.instance_manager.base_instance import BaseInstance from core.instance_manager.instance_manager import InstanceManager from utils import ( NGINX_SITES_AVAILABLE, @@ -31,7 +32,7 @@ from utils import ( from utils.logger import Logger -B = TypeVar('B', bound='BaseInstance') +B = TypeVar("B", Klipper, Moonraker) ConfigOption = Tuple[str, str] @@ -182,7 +183,11 @@ def get_next_free_port(ports_in_use: List[str]) -> str: return str(min(valid_ports - used_ports)) -def add_config_section(section: str, instances: List[B], options: List[ConfigOption] = None) -> None: +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}' ...") @@ -204,6 +209,26 @@ def add_config_section(section: str, instances: List[B], options: List[ConfigOpt 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: