From 975629f09735365dfe78012e4f459344c7f3d72f Mon Sep 17 00:00:00 2001 From: dw-0 Date: Sat, 21 Sep 2024 18:42:19 +0200 Subject: [PATCH] refactor: rework client config conflict detection (#537) Signed-off-by: Dominik Willner --- kiauh/components/webui_client/client_utils.py | 48 +++++++++++++++---- kiauh/core/menus/main_menu.py | 10 ++-- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/kiauh/components/webui_client/client_utils.py b/kiauh/components/webui_client/client_utils.py index db7f057..ca9727f 100644 --- a/kiauh/components/webui_client/client_utils.py +++ b/kiauh/components/webui_client/client_utils.py @@ -34,6 +34,9 @@ from core.constants import ( ) from core.logger import Logger from core.settings.kiauh_settings import KiauhSettings +from core.submodules.simple_config_parser.src.simple_config_parser.simple_config_parser import ( + SimpleConfigParser, +) from core.types import ComponentStatus from utils.common import get_install_status from utils.fs_utils import create_symlink, remove_file @@ -41,6 +44,7 @@ from utils.git_utils import ( get_latest_remote_tag, get_latest_unstable_tag, ) +from utils.instance_utils import get_instances def get_client_status( @@ -67,20 +71,46 @@ def get_client_config_status(client: BaseWebClient) -> ComponentStatus: return get_install_status(client.client_config.config_dir) -def get_current_client_config(clients: List[BaseWebClient]) -> str: - installed = [] - for client in clients: - client_config = client.client_config - if client_config.config_dir.exists(): - installed.append(client) +def get_current_client_config() -> str: + mainsail, fluidd = MainsailData(), FluiddData() + clients: List[BaseWebClient] = [mainsail, fluidd] + installed = [c for c in clients if c.client_config.config_dir.exists()] - if len(installed) > 1: - return f"{COLOR_YELLOW}Conflict!{RESET_FORMAT}" + if not installed: + return f"{COLOR_CYAN}-{RESET_FORMAT}" elif len(installed) == 1: cfg = installed[0].client_config return f"{COLOR_CYAN}{cfg.display_name}{RESET_FORMAT}" - return f"{COLOR_CYAN}-{RESET_FORMAT}" + # at this point, both client config folders exists, so we need to check + # which are actually included in the printer.cfg of all klipper instances + mainsail_includes, fluidd_includes = [], [] + klipper_instances: List[Klipper] = get_instances(Klipper) + for instance in klipper_instances: + scp = SimpleConfigParser() + scp.read_file(instance.cfg_file) + includes_mainsail = scp.has_section(mainsail.client_config.config_section) + includes_fluidd = scp.has_section(fluidd.client_config.config_section) + + if includes_mainsail: + mainsail_includes.append(instance) + if includes_fluidd: + fluidd_includes.append(instance) + + # if both are included in the same file, we have a potential conflict + if includes_mainsail and includes_fluidd: + return f"{COLOR_YELLOW}Conflict!{RESET_FORMAT}" + + if not mainsail_includes and not fluidd_includes: + # there are no includes at all, even though the client config folders exist + return f"{COLOR_CYAN}-{RESET_FORMAT}" + elif len(fluidd_includes) > len(mainsail_includes): + # there are more instances that include fluidd than mainsail + return f"{COLOR_CYAN}{fluidd.client_config.display_name}{RESET_FORMAT}" + else: + # there are the same amount of non-conflicting includes for each config + # or more instances include mainsail than fluidd + return f"{COLOR_CYAN}{mainsail.client_config.display_name}{RESET_FORMAT}" def enable_mainsail_remotemode() -> None: diff --git a/kiauh/core/menus/main_menu.py b/kiauh/core/menus/main_menu.py index 2e691b8..d29f208 100644 --- a/kiauh/core/menus/main_menu.py +++ b/kiauh/core/menus/main_menu.py @@ -55,10 +55,10 @@ class MainMenu(BaseMenu): self.footer_type: FooterType = FooterType.QUIT self.version = "" - self.kl_status = self.kl_owner = self.kl_repo = "" - self.mr_status = self.mr_owner = self.mr_repo = "" - self.ms_status = self.fl_status = self.ks_status = "" - self.cn_status = self.cc_status = "" + self.kl_status, self.kl_owner, self.kl_repo = "", "", "" + self.mr_status, self.mr_owner, self.mr_repo = "", "", "" + self.ms_status, self.fl_status, self.ks_status = "", "", "" + self.cn_status, self.cc_status = "", "" self._init_status() def set_previous_menu(self, previous_menu: Type[BaseMenu] | None) -> None: @@ -92,9 +92,9 @@ class MainMenu(BaseMenu): self._get_component_status("mr", get_moonraker_status) self._get_component_status("ms", get_client_status, MainsailData()) self._get_component_status("fl", get_client_status, FluiddData()) - self.cc_status = get_current_client_config([MainsailData(), FluiddData()]) self._get_component_status("ks", get_klipperscreen_status) self._get_component_status("cn", get_crowsnest_status) + self.cc_status = get_current_client_config() def _get_component_status(self, name: str, status_fn: Callable, *args) -> None: status_data: ComponentStatus = status_fn(*args)