refactor(KIAUH): move util functions into util modules

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2024-03-03 11:42:27 +01:00
parent 14aafd558a
commit 655b781aef
5 changed files with 47 additions and 51 deletions

View File

@@ -11,9 +11,7 @@
from pathlib import Path
from components.webui_client.client_config.client_config_setup import (
get_existing_client_config,
)
from components.webui_client.client_utils import get_existing_client_config
from kiauh import KIAUH_CFG
from components.klipper import (
EXIT_KLIPPER_SETUP,

View File

@@ -15,8 +15,7 @@ from pathlib import Path
from typing import List
from components.webui_client import MAINSAIL_DIR
from components.webui_client.client_config.client_config_setup import get_existing_client_config, get_existing_clients
from components.webui_client.client_utils import enable_mainsail_remotemode
from components.webui_client.client_utils import enable_mainsail_remotemode, get_existing_clients
from kiauh import KIAUH_CFG
from components.klipper.klipper import Klipper
from components.klipper.klipper_dialogs import print_instance_overview

View File

@@ -12,7 +12,7 @@
import shutil
import subprocess
from pathlib import Path
from typing import List, get_args, Union, Set
from typing import List
from kiauh import KIAUH_CFG
from components.klipper.klipper import Klipper
@@ -21,7 +21,7 @@ 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,
backup_client_config_data, config_for_other_client_exist,
)
from core.config_manager.config_manager import ConfigManager
@@ -80,44 +80,6 @@ def install_client_config(client_name: ClientName) -> None:
Logger.print_ok(f"{d_name} installation complete!", start="\n")
def config_for_other_client_exist(client_to_ignore: ClientName) -> bool:
"""
Check if any other client configs are present on the system.
It is usually not harmful, but chances are they can conflict each other.
Multiple client configs are, at least, redundant to have them installed
:param client_to_ignore: The client name to ignore for the check
:return: True, if other client configs were found, else False
"""
clients = set([c["name"] for c in get_existing_client_config()])
clients = clients - {client_to_ignore}
return True if len(clients) > 0 else False
def get_existing_clients() -> List[ClientData]:
clients = list(get_args(ClientName))
installed_clients: List[ClientData] = []
for c in clients:
c_data: ClientData = load_client_data(c)
if c_data.get("dir").exists():
installed_clients.append(c_data)
return installed_clients
def get_existing_client_config() -> List[ClientData]:
clients = list(get_args(ClientName))
installed_client_configs: List[ClientData] = []
for c in clients:
c_data: ClientData = load_client_data(c)
c_config_data: ClientConfigData = c_data.get("client_config")
if c_config_data.get("dir").exists():
installed_client_configs.append(c_data)
return installed_client_configs
def download_client_config(client_config: ClientConfigData) -> None:
try:
Logger.print_status(f"Downloading {client_config.get('display_name')} ...")

View File

@@ -21,7 +21,6 @@ from components.webui_client import (
from components.moonraker.moonraker import Moonraker
from components.webui_client.client_config.client_config_setup import (
install_client_config,
config_for_other_client_exist,
)
from components.webui_client.client_dialogs import (
print_moonraker_not_found_dialog,
@@ -33,7 +32,7 @@ from components.webui_client.client_utils import (
restore_mainsail_config_json,
enable_mainsail_remotemode,
symlink_webui_nginx_log,
load_client_data,
load_client_data, config_for_other_client_exist,
)
from core.config_manager.config_manager import ConfigManager
from core.instance_manager.instance_manager import InstanceManager

View File

@@ -13,7 +13,7 @@ import json
import shutil
from json import JSONDecodeError
from pathlib import Path
from typing import List, Optional, Dict, Literal, Union
from typing import List, Optional, Dict, Literal, Union, get_args
import urllib.request
@@ -43,7 +43,7 @@ from components.webui_client import (
from core.backup_manager.backup_manager import BackupManager
from core.repo_manager.repo_manager import RepoManager
from utils import NGINX_SITES_AVAILABLE, NGINX_CONFD
from utils.common import get_install_status_webui, get_install_status_common
from utils.common import get_install_status_webui
from utils.constants import COLOR_CYAN, RESET_FORMAT, COLOR_YELLOW
from utils.logger import Logger
@@ -235,3 +235,41 @@ def backup_client_config_data(client: ClientData) -> None:
target = client_config.get("backup_dir")
bm = BackupManager()
bm.backup_directory(name, source, target)
def get_existing_clients() -> List[ClientData]:
clients = list(get_args(ClientName))
installed_clients: List[ClientData] = []
for c in clients:
c_data: ClientData = load_client_data(c)
if c_data.get("dir").exists():
installed_clients.append(c_data)
return installed_clients
def get_existing_client_config() -> List[ClientData]:
clients = list(get_args(ClientName))
installed_client_configs: List[ClientData] = []
for c in clients:
c_data: ClientData = load_client_data(c)
c_config_data: ClientConfigData = c_data.get("client_config")
if c_config_data.get("dir").exists():
installed_client_configs.append(c_data)
return installed_client_configs
def config_for_other_client_exist(client_to_ignore: ClientName) -> bool:
"""
Check if any other client configs are present on the system.
It is usually not harmful, but chances are they can conflict each other.
Multiple client configs are, at least, redundant to have them installed
:param client_to_ignore: The client name to ignore for the check
:return: True, if other client configs were found, else False
"""
clients = set([c["name"] for c in get_existing_client_config()])
clients = clients - {client_to_ignore}
return True if len(clients) > 0 else False