mirror of
https://github.com/dw-0/kiauh.git
synced 2025-12-13 10:34:28 +05:00
* Squashed 'kiauh/core/submodules/simple_config_parser/' changes from f5eee99..5bc9e0a 5bc9e0a docs: update README 394dd7b refactor!: improve parsing and writing for config (#5) git-subtree-dir: kiauh/core/submodules/simple_config_parser git-subtree-split: 5bc9e0a50947f1be2f4877a10ab3a632774f82ea * fix(logging): change warning to error message for config creation failure * fix(config): improve readability by using descriptive variable names for options
108 lines
3.4 KiB
Python
108 lines
3.4 KiB
Python
# ======================================================================= #
|
|
# Copyright (C) 2020 - 2025 Dominik Willner <th33xitus@gmail.com> #
|
|
# #
|
|
# 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 #
|
|
# ======================================================================= #
|
|
from __future__ import annotations
|
|
|
|
import shutil
|
|
import tempfile
|
|
from pathlib import Path
|
|
from typing import List, Tuple, Union
|
|
|
|
from core.logger import Logger
|
|
from core.submodules.simple_config_parser.src.simple_config_parser.simple_config_parser import (
|
|
SimpleConfigParser,
|
|
)
|
|
from utils.instance_type import InstanceType
|
|
|
|
ConfigOption = Tuple[str, Union[str, List[str]]]
|
|
|
|
|
|
def add_config_section(
|
|
section: str,
|
|
instances: List[InstanceType],
|
|
options: List[ConfigOption] | None = None,
|
|
) -> None:
|
|
if not instances:
|
|
return
|
|
|
|
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
|
|
|
|
scp = SimpleConfigParser()
|
|
scp.read_file(cfg_file)
|
|
if scp.has_section(section):
|
|
Logger.print_info("Section already exist. Skipped ...")
|
|
continue
|
|
|
|
scp.add_section(section)
|
|
|
|
if options is not None:
|
|
for option in reversed(options):
|
|
opt_name = option[0]
|
|
opt_value = option[1]
|
|
scp.set_option(section, opt_name, opt_value)
|
|
|
|
scp.write_file(cfg_file)
|
|
|
|
Logger.print_ok("OK!")
|
|
|
|
|
|
def add_config_section_at_top(section: str, instances: List[InstanceType]) -> None:
|
|
# TODO: this could be implemented natively in SimpleConfigParser
|
|
for instance in instances:
|
|
tmp_cfg = tempfile.NamedTemporaryFile(mode="w", delete=False)
|
|
tmp_cfg_path = Path(tmp_cfg.name)
|
|
scp = SimpleConfigParser()
|
|
scp.read_file(tmp_cfg_path)
|
|
scp.add_section(section)
|
|
scp.write_file(tmp_cfg_path)
|
|
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()
|
|
shutil.move(tmp_cfg_path.as_posix(), cfg_file)
|
|
|
|
Logger.print_ok("OK!")
|
|
|
|
|
|
def remove_config_section(
|
|
section: str, instances: List[InstanceType]
|
|
) -> List[InstanceType]:
|
|
removed_from: List[InstanceType] = []
|
|
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
|
|
|
|
scp = SimpleConfigParser()
|
|
scp.read_file(cfg_file)
|
|
if not scp.has_section(section):
|
|
Logger.print_info("Section does not exist. Skipped ...")
|
|
continue
|
|
|
|
scp.remove_section(section)
|
|
scp.write_file(cfg_file)
|
|
|
|
removed_from.append(instance)
|
|
Logger.print_ok("OK!")
|
|
|
|
return removed_from
|