feat(ConfigManager): implement ConfigManager
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -3,6 +3,4 @@
|
|||||||
.pytest_cache
|
.pytest_cache
|
||||||
.kiauh-env
|
.kiauh-env
|
||||||
*.code-workspace
|
*.code-workspace
|
||||||
klipper_repos.txt
|
kiauh.cfg
|
||||||
klipper_repos.json
|
|
||||||
|
|
||||||
|
|||||||
12
kiauh.cfg.example
Normal file
12
kiauh.cfg.example
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[kiauh]
|
||||||
|
backup_before_update: False
|
||||||
|
|
||||||
|
[klipper]
|
||||||
|
repository_url: https://github.com/Klipper3d/klipper
|
||||||
|
branch: master
|
||||||
|
method: https
|
||||||
|
|
||||||
|
[moonraker]
|
||||||
|
repository_url: https://github.com/Klipper3d/klipper
|
||||||
|
branch: master
|
||||||
|
method: https
|
||||||
0
kiauh/config_manager/__init__.py
Normal file
0
kiauh/config_manager/__init__.py
Normal file
61
kiauh/config_manager/config_manager.py
Normal file
61
kiauh/config_manager/config_manager.py
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# ======================================================================= #
|
||||||
|
# Copyright (C) 2020 - 2023 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 #
|
||||||
|
# ======================================================================= #
|
||||||
|
|
||||||
|
import os
|
||||||
|
import configparser
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
from kiauh.utils.logger import Logger
|
||||||
|
|
||||||
|
|
||||||
|
# noinspection PyMethodMayBeStatic
|
||||||
|
class ConfigManager:
|
||||||
|
def __init__(self):
|
||||||
|
self.config_file = self._get_cfg_location()
|
||||||
|
self.config = configparser.ConfigParser()
|
||||||
|
|
||||||
|
def read_config(self) -> None:
|
||||||
|
if not self.config_file:
|
||||||
|
Logger.print_error("Unable to read config file. File not found.")
|
||||||
|
return
|
||||||
|
|
||||||
|
self.config.read_file(open(self.config_file, "r"))
|
||||||
|
|
||||||
|
def write_config(self) -> None:
|
||||||
|
with open(self.config_file, "w") as cfg:
|
||||||
|
self.config.write(cfg)
|
||||||
|
|
||||||
|
def get_value(self, section: str, key: str) -> Union[str, None]:
|
||||||
|
if not self.config.has_section(section):
|
||||||
|
log = f"Section not defined. Unable to read section: [{section}]."
|
||||||
|
Logger.print_error(log)
|
||||||
|
return None
|
||||||
|
|
||||||
|
if not self.config.has_option(section, key):
|
||||||
|
log = f"Option not defined in section [{section}]. Unable to read option: '{key}'."
|
||||||
|
Logger.print_error(log)
|
||||||
|
return None
|
||||||
|
|
||||||
|
return self.config.get(section, key)
|
||||||
|
|
||||||
|
def set_value(self, section: str, key: str, value: str):
|
||||||
|
self.config.set(section, key, value)
|
||||||
|
|
||||||
|
def check_config_exists(self) -> bool:
|
||||||
|
return True if self._get_cfg_location() else False
|
||||||
|
|
||||||
|
def _get_cfg_location(self) -> str:
|
||||||
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
project_dir = os.path.dirname(os.path.dirname(current_dir))
|
||||||
|
cfg_path = os.path.join(project_dir, "kiauh.cfg")
|
||||||
|
|
||||||
|
return cfg_path if Path(cfg_path).exists() else None
|
||||||
@@ -17,6 +17,7 @@ import textwrap
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional, List, Union
|
from typing import Optional, List, Union
|
||||||
|
|
||||||
|
from kiauh.config_manager.config_manager import ConfigManager
|
||||||
from kiauh.instance_manager.instance_manager import InstanceManager
|
from kiauh.instance_manager.instance_manager import InstanceManager
|
||||||
from kiauh.modules.klipper.klipper import Klipper
|
from kiauh.modules.klipper.klipper import Klipper
|
||||||
from kiauh.modules.klipper.klipper_utils import (
|
from kiauh.modules.klipper.klipper_utils import (
|
||||||
@@ -118,10 +119,18 @@ def install_klipper(instance_manager: InstanceManager) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def setup_klipper_prerequesites() -> None:
|
def setup_klipper_prerequesites() -> None:
|
||||||
# clone klipper TODO: read branch and url from json to allow forks
|
cm = ConfigManager()
|
||||||
|
cm.read_config()
|
||||||
|
|
||||||
|
repo = (
|
||||||
|
cm.get_value("klipper", "repository_url")
|
||||||
|
or "https://github.com/Klipper3D/klipper"
|
||||||
|
)
|
||||||
|
branch = cm.get_value("klipper", "branch") or "master"
|
||||||
|
|
||||||
repo_manager = RepoManager(
|
repo_manager = RepoManager(
|
||||||
repo="https://github.com/Klipper3D/klipper",
|
repo=repo,
|
||||||
branch="master",
|
branch=branch,
|
||||||
target_dir=KLIPPER_DIR,
|
target_dir=KLIPPER_DIR,
|
||||||
)
|
)
|
||||||
repo_manager.clone_repo()
|
repo_manager.clone_repo()
|
||||||
|
|||||||
@@ -9,10 +9,9 @@
|
|||||||
# This file may be distributed under the terms of the GNU GPLv3 license #
|
# This file may be distributed under the terms of the GNU GPLv3 license #
|
||||||
# ======================================================================= #
|
# ======================================================================= #
|
||||||
|
|
||||||
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
from pathlib import Path
|
|
||||||
from typing import Union
|
|
||||||
|
|
||||||
from kiauh.utils.input_utils import get_confirm
|
from kiauh.utils.input_utils import get_confirm
|
||||||
from kiauh.utils.logger import Logger
|
from kiauh.utils.logger import Logger
|
||||||
@@ -62,12 +61,11 @@ class RepoManager:
|
|||||||
log = f"Cloning repository from '{self.repo}' with method '{self.method}'"
|
log = f"Cloning repository from '{self.repo}' with method '{self.method}'"
|
||||||
Logger.print_info(log)
|
Logger.print_info(log)
|
||||||
try:
|
try:
|
||||||
question = "Target directory already exists. Overwrite?"
|
if os.path.exists(self.target_dir):
|
||||||
if Path(self.target_dir).exists() and get_confirm(question):
|
if not get_confirm("Target directory already exists. Overwrite?"):
|
||||||
|
Logger.print_info("Skipping re-clone of repository ...")
|
||||||
|
return
|
||||||
shutil.rmtree(self.target_dir)
|
shutil.rmtree(self.target_dir)
|
||||||
else:
|
|
||||||
Logger.print_info("Skipping re-clone of repository ...")
|
|
||||||
return
|
|
||||||
|
|
||||||
self._clone()
|
self._clone()
|
||||||
self._checkout()
|
self._checkout()
|
||||||
@@ -81,7 +79,7 @@ class RepoManager:
|
|||||||
|
|
||||||
def _clone(self):
|
def _clone(self):
|
||||||
try:
|
try:
|
||||||
command = ["git", "clone", f"{self.repo}"]
|
command = ["git", "clone", self.repo, self.target_dir]
|
||||||
subprocess.run(command, check=True)
|
subprocess.run(command, check=True)
|
||||||
|
|
||||||
Logger.print_ok("Clone successfull!")
|
Logger.print_ok("Clone successfull!")
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
# This file acts as an example file.
|
|
||||||
#
|
|
||||||
# 1) Make a copy of this file and rename it to 'klipper_repos.txt'
|
|
||||||
# 2) Add your custom Klipper repository to the bottom of that copy
|
|
||||||
# 3) Save the file
|
|
||||||
#
|
|
||||||
# Back in KIAUH you can now go into -> [Settings] and use action '2' to set a different Klipper repository
|
|
||||||
#
|
|
||||||
# Make sure to always separate the repository and the branch with a ','.
|
|
||||||
# <repository>,<branch> -> https://github.com/Klipper3d/klipper,master
|
|
||||||
# If you omit a branch, it will always default to 'master'
|
|
||||||
#
|
|
||||||
# You are allowed to omit the 'https://github.com/' part of the repository URL
|
|
||||||
# Down below are now a few examples of what is considered as valid:
|
|
||||||
https://github.com/Klipper3d/klipper,master
|
|
||||||
https://github.com/Klipper3d/klipper
|
|
||||||
Klipper3d/klipper,master
|
|
||||||
Klipper3d/klipper
|
|
||||||
Reference in New Issue
Block a user