refactor: start at index 1 in moonraker setup dialog if multi instance

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2024-07-27 22:23:06 +02:00
parent 871bedb76b
commit 32742943a0
7 changed files with 47 additions and 40 deletions

View File

@@ -52,7 +52,7 @@ def select_instances_to_remove(
) -> Union[List[Klipper], None]: ) -> Union[List[Klipper], None]:
start_index = 1 start_index = 1
options = [str(i + start_index) for i in range(len(instances))] options = [str(i + start_index) for i in range(len(instances))]
options.extend(["a", "A", "b", "B"]) options.extend(["a", "b"])
instance_map = {options[i]: instances[i] for i in range(len(instances))} instance_map = {options[i]: instances[i] for i in range(len(instances))}
print_instance_overview( print_instance_overview(
@@ -64,9 +64,9 @@ def select_instances_to_remove(
selection = get_selection_input("Select Klipper instance to remove", options) selection = get_selection_input("Select Klipper instance to remove", options)
instances_to_remove = [] instances_to_remove = []
if selection == "b".lower(): if selection == "b":
return None return None
elif selection == "a".lower(): elif selection == "a":
instances_to_remove.extend(instances) instances_to_remove.extend(instances)
else: else:
instances_to_remove.append(instance_map[selection]) instances_to_remove.append(instance_map[selection])

View File

@@ -48,7 +48,7 @@ def print_moonraker_overview(
for i, k in enumerate(instance_map): for i, k in enumerate(instance_map):
mr_name = instance_map.get(k) mr_name = instance_map.get(k)
m = f"<-> {mr_name}" if mr_name != "" else "" m = f"<-> {mr_name}" if mr_name != "" else ""
line = f"{COLOR_CYAN}{f'{i})' if show_index else ''} {k} {m} {RESET_FORMAT}" line = f"{COLOR_CYAN}{f'{i+1})' if show_index else ''} {k} {m} {RESET_FORMAT}"
dialog += f"{line:<63}\n" dialog += f"{line:<63}\n"
warn_l1 = f"{COLOR_YELLOW}PLEASE NOTE: {RESET_FORMAT}" warn_l1 = f"{COLOR_YELLOW}PLEASE NOTE: {RESET_FORMAT}"

View File

@@ -60,7 +60,7 @@ def select_instances_to_remove(
) -> Union[List[Moonraker], None]: ) -> Union[List[Moonraker], None]:
start_index = 1 start_index = 1
options = [str(i + start_index) for i in range(len(instances))] options = [str(i + start_index) for i in range(len(instances))]
options.extend(["a", "A", "b", "B"]) options.extend(["a", "b"])
instance_map = {options[i]: instances[i] for i in range(len(instances))} instance_map = {options[i]: instances[i] for i in range(len(instances))}
print_instance_overview( print_instance_overview(
@@ -72,9 +72,9 @@ def select_instances_to_remove(
selection = get_selection_input("Select Moonraker instance to remove", options) selection = get_selection_input("Select Moonraker instance to remove", options)
instances_to_remove = [] instances_to_remove = []
if selection == "b".lower(): if selection == "b":
return None return None
elif selection == "a".lower(): elif selection == "a":
instances_to_remove.extend(instances) instances_to_remove.extend(instances)
else: else:
instances_to_remove.append(instance_map[selection]) instances_to_remove.append(instance_map[selection])

View File

@@ -6,8 +6,11 @@
# # # #
# 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 #
# ======================================================================= # # ======================================================================= #
from __future__ import annotations
import json import json
import subprocess import subprocess
from typing import List
from components.klipper.klipper import Klipper from components.klipper.klipper import Klipper
from components.moonraker import ( from components.moonraker import (
@@ -57,36 +60,36 @@ def install_moonraker() -> None:
if not check_moonraker_install_requirements(): if not check_moonraker_install_requirements():
return return
kl_im = InstanceManager(Klipper) klipper_list: List[Klipper] = InstanceManager(Klipper).instances
klipper_instances = kl_im.instances
mr_im = InstanceManager(Moonraker) mr_im = InstanceManager(Moonraker)
moonraker_instances = mr_im.instances moonraker_list: List[Moonraker] = mr_im.instances
selected_klipper_instance = 0 instance_names = []
if len(klipper_instances) > 1: selected_option: str | Klipper
if len(klipper_list) == 0:
instance_names.append(klipper_list[0].suffix)
else:
print_moonraker_overview( print_moonraker_overview(
klipper_instances, klipper_list,
moonraker_instances, moonraker_list,
show_index=True, show_index=True,
show_select_all=True, show_select_all=True,
) )
options = [str(i) for i in range(len(klipper_instances))] options = {str(i + 1): k for i, k in enumerate(klipper_list)}
options.extend(["a", "A", "b", "B"]) additional_options = {"a": None, "b": None}
options = {**options, **additional_options}
question = "Select Klipper instance to setup Moonraker for" question = "Select Klipper instance to setup Moonraker for"
selected_klipper_instance = get_selection_input(question, options).lower() selected_option = get_selection_input(question, options)
instance_names = [] if selected_option == "b":
if selected_klipper_instance == "b":
Logger.print_status(EXIT_MOONRAKER_SETUP) Logger.print_status(EXIT_MOONRAKER_SETUP)
return return
elif selected_klipper_instance == "a": if selected_option == "a":
for instance in klipper_instances: instance_names.extend([k.suffix for k in klipper_list])
instance_names.append(instance.suffix)
else: else:
index = int(selected_klipper_instance) instance_names.append(options.get(selected_option).suffix)
instance_names.append(klipper_instances[index].suffix)
create_example_cfg = get_confirm("Create example moonraker.conf?") create_example_cfg = get_confirm("Create example moonraker.conf?")
@@ -95,9 +98,7 @@ def install_moonraker() -> None:
setup_moonraker_prerequesites() setup_moonraker_prerequesites()
install_moonraker_polkit() install_moonraker_polkit()
used_ports_map = { used_ports_map = {m.suffix: m.port for m in moonraker_list}
instance.suffix: instance.port for instance in moonraker_instances
}
for name in instance_names: for name in instance_names:
current_instance = Moonraker(suffix=name) current_instance = Moonraker(suffix=name)

View File

@@ -157,7 +157,7 @@ def get_printer_selection(
instances: List[BaseInstance], is_install: bool instances: List[BaseInstance], is_install: bool
) -> Union[List[BaseInstance], None]: ) -> Union[List[BaseInstance], None]:
options = [str(i) for i in range(len(instances))] options = [str(i) for i in range(len(instances))]
options.extend(["a", "A", "b", "B"]) options.extend(["a", "b"])
if is_install: if is_install:
q = "Select the printer to install the theme for" q = "Select the printer to install the theme for"
@@ -166,9 +166,9 @@ def get_printer_selection(
selection = get_selection_input(q, options) selection = get_selection_input(q, options)
install_for = [] install_for = []
if selection == "b".lower(): if selection == "b":
return None return None
elif selection == "a".lower(): elif selection == "a":
install_for.extend(instances) install_for.extend(instances)
else: else:
instance = instances[int(selection)] instance = instances[int(selection)]

View File

@@ -62,7 +62,7 @@ class ObicoExtension(BaseExtension):
obico_instances: List[MoonrakerObico] = obico_im.instances obico_instances: List[MoonrakerObico] = obico_im.instances
if obico_instances: if obico_instances:
self._print_is_already_installed() self._print_is_already_installed()
options = ["l", "L", "r", "R", "b", "B"] options = ["l", "r", "b"]
action = get_selection_input("Perform action", option_list=options) action = get_selection_input("Perform action", option_list=options)
if action.lower() == "b": if action.lower() == "b":
Logger.print_info("Exiting Obico for Klipper installation ...") Logger.print_info("Exiting Obico for Klipper installation ...")

View File

@@ -9,7 +9,7 @@
from __future__ import annotations from __future__ import annotations
import re import re
from typing import List, Union from typing import Dict, List, Union
from utils import INVALID_CHOICE from utils import INVALID_CHOICE
from utils.constants import COLOR_CYAN, RESET_FORMAT from utils.constants import COLOR_CYAN, RESET_FORMAT
@@ -120,7 +120,7 @@ def get_string_input(
Logger.print_error(INVALID_CHOICE) Logger.print_error(INVALID_CHOICE)
def get_selection_input(question: str, option_list: List, default=None) -> str: def get_selection_input(question: str, option_list: List | Dict, default=None) -> str:
""" """
Helper method to get a selection from a list of options from the user Helper method to get a selection from a list of options from the user
:param question: The question to display :param question: The question to display
@@ -129,10 +129,16 @@ def get_selection_input(question: str, option_list: List, default=None) -> str:
:return: The option that was selected by the user :return: The option that was selected by the user
""" """
while True: while True:
_input = input(format_question(question, default)).strip() _input = input(format_question(question, default)).strip().lower()
if isinstance(option_list, list):
if _input in option_list: if _input in option_list:
return _input return _input
elif isinstance(option_list, dict):
if _input in option_list.keys():
return _input
else:
raise ValueError("Invalid option_list type")
Logger.print_error(INVALID_CHOICE) Logger.print_error(INVALID_CHOICE)