mirror of
https://github.com/dw-0/kiauh.git
synced 2025-12-24 00:03:42 +05:00
feat: allow sections to be added to the top of a config file
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
@@ -1,5 +1,3 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
|
|
||||||
# ======================================================================= #
|
# ======================================================================= #
|
||||||
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
|
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
|
||||||
# #
|
# #
|
||||||
@@ -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_dialogs import print_client_already_installed_dialog
|
||||||
from components.webui_client.client_utils import (
|
from components.webui_client.client_utils import (
|
||||||
load_client_data,
|
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.config_manager.config_manager import ConfigManager
|
||||||
|
|
||||||
from core.instance_manager.instance_manager import InstanceManager
|
from core.instance_manager.instance_manager import InstanceManager
|
||||||
from core.repo_manager.repo_manager import RepoManager
|
from core.repo_manager.repo_manager import RepoManager
|
||||||
from utils.filesystem_utils import (
|
from utils.filesystem_utils import (
|
||||||
create_symlink,
|
create_symlink,
|
||||||
add_config_section,
|
add_config_section, add_config_section_at_top,
|
||||||
)
|
)
|
||||||
from utils.input_utils import get_confirm
|
from utils.input_utils import get_confirm
|
||||||
from utils.logger import Logger
|
from utils.logger import Logger
|
||||||
|
|
||||||
@@ -70,7 +69,9 @@ def install_client_config(client_name: ClientName) -> None:
|
|||||||
("managed_services", "klipper"),
|
("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()
|
kl_im.restart_all_instance()
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
import os
|
||||||
|
|
||||||
# ======================================================================= #
|
# ======================================================================= #
|
||||||
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
|
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
|
||||||
@@ -12,15 +13,15 @@
|
|||||||
import re
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import tempfile
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from zipfile import ZipFile
|
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.klipper.klipper import Klipper
|
||||||
from components.moonraker.moonraker import Moonraker
|
from components.moonraker.moonraker import Moonraker
|
||||||
from core.config_manager.config_manager import ConfigManager
|
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 core.instance_manager.instance_manager import InstanceManager
|
||||||
from utils import (
|
from utils import (
|
||||||
NGINX_SITES_AVAILABLE,
|
NGINX_SITES_AVAILABLE,
|
||||||
@@ -31,7 +32,7 @@ from utils import (
|
|||||||
from utils.logger import Logger
|
from utils.logger import Logger
|
||||||
|
|
||||||
|
|
||||||
B = TypeVar('B', bound='BaseInstance')
|
B = TypeVar("B", Klipper, Moonraker)
|
||||||
ConfigOption = Tuple[str, str]
|
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))
|
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:
|
for instance in instances:
|
||||||
cfg_file = instance.cfg_file
|
cfg_file = instance.cfg_file
|
||||||
Logger.print_status(f"Add section '[{section}]' to '{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()
|
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:
|
def remove_config_section(section: str, instances: List[B]) -> None:
|
||||||
for instance in instances:
|
for instance in instances:
|
||||||
|
|||||||
Reference in New Issue
Block a user