Compare commits

..

5 Commits

Author SHA1 Message Date
dw-0
3fc190ff25 fix: actually raise exception on empty config value
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
2024-11-10 21:15:08 +01:00
dw-0
6ff45aab41 refactor: implement basic input validation for repo switch feature
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
2024-11-10 20:58:37 +01:00
dw-0
b9c9feef3c refactor: clone repo in repo switch routine only if there is already a repo present
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
2024-11-10 20:14:47 +01:00
dw-0
d37d047aaa refactor: fallback to config settings for repos in settings menu
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
2024-11-10 19:56:47 +01:00
dw-0
a3fb57aee3 refactor: return - if branch cannot be read
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
2024-11-10 19:54:26 +01:00
7 changed files with 68 additions and 30 deletions

View File

@@ -13,6 +13,8 @@ from core.backup_manager import BACKUP_ROOT_DIR
MODULE_PATH = Path(__file__).resolve().parent
KLIPPER_REPO_URL = "https://github.com/Klipper3d/klipper.git"
# names
KLIPPER_LOG_NAME = "klippy.log"
KLIPPER_CFG_NAME = "printer.cfg"

View File

@@ -13,6 +13,8 @@ from core.backup_manager import BACKUP_ROOT_DIR
MODULE_PATH = Path(__file__).resolve().parent
MOONRAKER_REPO_URL = "https://github.com/Arksine/moonraker.git"
# names
MOONRAKER_CFG_NAME = "moonraker.conf"
MOONRAKER_LOG_NAME = "moonraker.log"

View File

@@ -9,16 +9,21 @@
from __future__ import annotations
import textwrap
from pathlib import Path
from typing import Literal, Tuple, Type
from components.klipper import KLIPPER_DIR, KLIPPER_REPO_URL
from components.klipper.klipper_utils import get_klipper_status
from components.moonraker import MOONRAKER_DIR, MOONRAKER_REPO_URL
from components.moonraker.moonraker_utils import get_moonraker_status
from core.logger import DialogType, Logger
from core.menus import Option
from core.menus.base_menu import BaseMenu
from core.settings.kiauh_settings import KiauhSettings, RepoSettings
from core.types.color import Color
from core.types.component_status import ComponentStatus
from procedures.switch_repo import run_switch_repo_routine
from utils.git_utils import get_repo_name
from utils.input_utils import get_confirm, get_string_input
@@ -30,12 +35,13 @@ class SettingsMenu(BaseMenu):
self.title = "Settings Menu"
self.title_color = Color.CYAN
self.previous_menu: Type[BaseMenu] | None = previous_menu
self.klipper_status = get_klipper_status()
self.moonraker_status = get_moonraker_status()
self.mainsail_unstable: bool | None = None
self.fluidd_unstable: bool | None = None
self.auto_backups_enabled: bool | None = None
self._load_settings()
print(self.klipper_status)
def set_previous_menu(self, previous_menu: Type[BaseMenu] | None) -> None:
from core.menus.main_menu import MainMenu
@@ -103,32 +109,51 @@ class SettingsMenu(BaseMenu):
self.mainsail_unstable = self.settings.mainsail.unstable_releases
self.fluidd_unstable = self.settings.fluidd.unstable_releases
def _gather_input(self) -> Tuple[str, str]:
Logger.print_dialog(
DialogType.ATTENTION,
[
"There is no input validation in place! Make sure your the input is "
"valid and has no typos or invalid characters! For the change to take "
"effect, the new repository will be cloned. A backup of the old "
"repository will be created.",
# by default, we show the status of the installed repositories
self.klipper_status = get_klipper_status()
self.moonraker_status = get_moonraker_status()
# if the repository is not installed, we show the status of the settings from the config file
if self.klipper_status.repo == "-":
url_parts = self.settings.klipper.repo_url.split("/")
self.klipper_status.repo = url_parts[-1]
self.klipper_status.owner = url_parts[-2]
self.klipper_status.branch = self.settings.klipper.branch
if self.moonraker_status.repo == "-":
url_parts = self.settings.moonraker.repo_url.split("/")
self.moonraker_status.repo = url_parts[-1]
self.moonraker_status.owner = url_parts[-2]
self.moonraker_status.branch = self.settings.moonraker.branch
def _gather_input(self, repo_name: Literal["klipper", "moonraker"], repo_dir: Path) -> Tuple[str, str]:
warn_msg = [
"There is only basic input validation in place! "
"Make sure your the input is valid and has no typos or invalid characters!"]
if repo_dir.exists():
warn_msg.extend([
"For the change to take effect, the new repository will be cloned. "
"A backup of the old repository will be created.",
"\n\n",
"Make sure you don't have any ongoing prints running, as the services "
"will be restarted during this process! You will loose any ongoing print!",
],
)
"will be restarted during this process! You will loose any ongoing print!"])
Logger.print_dialog(DialogType.ATTENTION, warn_msg)
repo = get_string_input(
"Enter new repository URL",
allow_special_chars=True,
regex="^[\w/.:-]+$",
default=KLIPPER_REPO_URL if repo_name == "klipper" else MOONRAKER_REPO_URL,
)
branch = get_string_input(
"Enter new branch name",
allow_special_chars=True,
regex="^.+$",
default="master"
)
return repo, branch
def _set_repo(self, repo_name: Literal["klipper", "moonraker"]) -> None:
repo_url, branch = self._gather_input()
def _set_repo(self, repo_name: Literal["klipper", "moonraker"], repo_dir: Path) -> None:
repo_url, branch = self._gather_input(repo_name, repo_dir)
display_name = repo_name.capitalize()
Logger.print_dialog(
DialogType.CUSTOM,
@@ -151,22 +176,26 @@ class SettingsMenu(BaseMenu):
Logger.print_ok("Changes saved!")
else:
Logger.print_info(
f"Skipping change of {display_name} source repository ..."
f"Changing of {display_name} source repository canceled ..."
)
return
Logger.print_status(f"Switching to {display_name}'s new source repository ...")
self._switch_repo(repo_name)
self._switch_repo(repo_name, repo_dir)
def _switch_repo(self, name: Literal["klipper", "moonraker"], repo_dir: Path ) -> None:
if not repo_dir.exists():
return
Logger.print_status(f"Switching to {name.capitalize()}'s new source repository ...")
def _switch_repo(self, name: Literal["klipper", "moonraker"]) -> None:
repo: RepoSettings = self.settings[name]
run_switch_repo_routine(name, repo)
def set_klipper_repo(self, **kwargs) -> None:
self._set_repo("klipper")
self._set_repo("klipper", KLIPPER_DIR)
def set_moonraker_repo(self, **kwargs) -> None:
self._set_repo("moonraker")
self._set_repo("moonraker", MOONRAKER_DIR)
def toggle_mainsail_release(self, **kwargs) -> None:
self.mainsail_unstable = not self.mainsail_unstable

View File

@@ -145,7 +145,8 @@ class KiauhSettings:
def _validate_str(self, section: str, option: str) -> None:
self._v_section, self._v_option = (section, option)
v = self.config.getval(section, option)
if v.isdigit() or v.lower() == "true" or v.lower() == "false":
if not v:
raise ValueError
def _apply_settings_from_file(self) -> None:

View File

@@ -64,7 +64,7 @@ def run_switch_repo_routine(
try:
# step 2: backup old repo and env
org, repo = get_repo_name(repo_dir)
org, _ = get_repo_name(repo_dir)
backup_dir = backup_dir.joinpath(org)
bm = BackupManager()
repo_dir_backup_path = bm.backup_directory(

View File

@@ -104,10 +104,10 @@ def get_current_branch(repo: Path) -> str:
result: str = check_output(cmd, stderr=DEVNULL, cwd=repo).decode(
encoding="utf-8"
)
return result.strip()
return result.strip() if result else "-"
except CalledProcessError:
return ""
return "-"
def get_local_tags(repo_path: Path, _filter: str | None = None) -> List[str]:

View File

@@ -86,6 +86,7 @@ def get_string_input(
question: str,
regex: str | None = None,
exclude: List[str] | None = None,
allow_empty: bool = False,
allow_special_chars: bool = False,
default: str | None = None,
) -> str:
@@ -94,6 +95,7 @@ def get_string_input(
:param question: The question to display
:param regex: An optional regex pattern to validate the input against
:param exclude: List of strings which are not allowed
:param allow_empty: Whether to allow empty input
:param allow_special_chars: Wheter to allow special characters in the input
:param default: Optional default value
:return: The validated string value
@@ -104,12 +106,14 @@ def get_string_input(
while True:
_input = input(_question)
if _input.lower() in _exclude:
Logger.print_error("This value is already in use/reserved.")
elif default is not None and _input == "":
if default is not None and _input == "":
return default
elif _input == "" and not allow_empty:
Logger.print_error("Input must not be empty!")
elif _pattern is not None and _pattern.match(_input):
return _input
elif _input.lower() in _exclude:
Logger.print_error("This value is already in use/reserved.")
elif allow_special_chars:
return _input
elif not allow_special_chars and _input.isalnum():