Skip to content

Commit

Permalink
configfile: Allow getchoice() to take a list
Browse files Browse the repository at this point in the history
If a list is passed to getchoice(), seamlessly convert it to a dict.

Signed-off-by: Kevin O'Connor <[email protected]>
  • Loading branch information
KevinOConnor authored and rogerlz committed Jul 18, 2024
1 parent fdb52d4 commit c61695c
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 11 deletions.
2 changes: 2 additions & 0 deletions klippy/configfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ def getboolean(self, option, default=sentinel, note_valid=True):
)

def getchoice(self, option, choices, default=sentinel, note_valid=True):
if isinstance(choices, list):
choices = {i: i for i in choices}
if choices and isinstance(list(choices.keys())[0], int):
c = self.getint(option, default, note_valid=note_valid)
else:
Expand Down
2 changes: 1 addition & 1 deletion klippy/extras/bltouch.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(self, config):
mcu = pin_params["chip"]
self.mcu_endstop = mcu.setup_pin("endstop", pin_params)
# output mode
omodes = {"5V": "5V", "OD": "OD", None: None}
omodes = ["5V", "OD", None]
self.output_mode = config.getchoice("set_output_mode", omodes, None)
# Setup for sensor test
self.next_test_time = 0.0
Expand Down
2 changes: 1 addition & 1 deletion klippy/extras/display/hd44780.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

BACKGROUND_PRIORITY_CLOCK = 0x7FFFFFFF00000000
LINE_LENGTH_DEFAULT = 20
LINE_LENGTH_OPTIONS = {16: 16, 20: 20}
LINE_LENGTH_OPTIONS = [16, 20]

TextGlyphs = {"right_arrow": b"\x7e"}

Expand Down
4 changes: 3 additions & 1 deletion klippy/extras/display/hd44780_spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
from .. import bus

LINE_LENGTH_DEFAULT = 20
LINE_LENGTH_OPTIONS = {16: 16, 20: 20}
LINE_LENGTH_OPTIONS = [16, 20]

TextGlyphs = {"right_arrow": b"\x7e"}

TextGlyphs = {"right_arrow": b"\x7e"}

Expand Down
2 changes: 1 addition & 1 deletion klippy/extras/display/menu_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self, config, callback):
# Register rotary encoder
encoder_pins = config.get("encoder_pins", None)
encoder_steps_per_detent = config.getchoice(
"encoder_steps_per_detent", {2: 2, 4: 4}, 4
"encoder_steps_per_detent", [2, 4], 4
)
if encoder_pins is not None:
try:
Expand Down
2 changes: 1 addition & 1 deletion klippy/extras/probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(self, config, mcu_probe):
self.sample_retract_dist = config.getfloat(
"sample_retract_dist", 2.0, above=0.0
)
atypes = {"median": "median", "average": "average"}
atypes = ["median", "average"]
self.samples_result = config.getchoice(
"samples_result", atypes, "average"
)
Expand Down
2 changes: 1 addition & 1 deletion klippy/extras/replicape.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def __init__(self, config):
printer = config.get_printer()
ppins = printer.lookup_object("pins")
ppins.register_chip("replicape", self)
revisions = {"B3": "B3"}
revisions = ["B3"]
config.getchoice("revision", revisions)
self.host_mcu = mcu.get_printer_mcu(printer, config.get("host_mcu"))
# Setup enable pin
Expand Down
2 changes: 1 addition & 1 deletion klippy/kinematics/cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self, toolhead, config):
self.supports_dual_carriage = True
if config.has_section("dual_carriage"):
dc_config = config.getsection("dual_carriage")
dc_axis = dc_config.getchoice("axis", {"x": "x", "y": "y"})
dc_axis = dc_config.getchoice("axis", ["x", "y"])
self.dual_carriage_axis = {"x": 0, "y": 1}[dc_axis]
# setup second dual carriage rail
self.rails.append(stepper.LookupMultiRail(dc_config))
Expand Down
2 changes: 1 addition & 1 deletion klippy/kinematics/hybrid_corexy.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, toolhead, config):
if config.has_section("dual_carriage"):
dc_config = config.getsection("dual_carriage")
# dummy for cartesian config users
dc_config.getchoice("axis", {"x": "x"}, default="x")
dc_config.getchoice("axis", ["x"], default="x")
# setup second dual carriage rail
self.rails.append(stepper.PrinterRail(dc_config))
self.rails[1].get_endstops()[0][0].add_stepper(
Expand Down
2 changes: 1 addition & 1 deletion klippy/kinematics/hybrid_corexz.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, toolhead, config):
if config.has_section("dual_carriage"):
dc_config = config.getsection("dual_carriage")
# dummy for cartesian config users
dc_config.getchoice("axis", {"x": "x"}, default="x")
dc_config.getchoice("axis", ["x"], default="x")
# setup second dual carriage rail
self.rails.append(stepper.PrinterRail(dc_config))
self.rails[2].get_endstops()[0][0].add_stepper(
Expand Down
3 changes: 1 addition & 2 deletions klippy/mcu.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,9 +764,8 @@ def __init__(self, config, clocksync):
restart_methods = [None, "arduino", "cheetah", "command", "rpi_usb"]
self._restart_method = "command"
if self._baud:
rmethods = {m: m for m in restart_methods}
self._restart_method = config.getchoice(
"restart_method", rmethods, None
"restart_method", restart_methods, None
)
self._reset_cmd = self._config_reset_cmd = None
self._is_mcu_bridge = False
Expand Down

0 comments on commit c61695c

Please sign in to comment.