From 11f04ba1bae774d32fd43bed4a0772c58d8df199 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 17 Jun 2024 13:49:05 -0400 Subject: [PATCH] configfile: Allow getchoice() to take a list If a list is passed to getchoice(), seamlessly convert it to a dict. Signed-off-by: Kevin O'Connor --- klippy/configfile.py | 2 ++ klippy/extras/bltouch.py | 2 +- klippy/extras/display/hd44780.py | 2 +- klippy/extras/display/hd44780_spi.py | 2 +- klippy/extras/display/menu_keys.py | 2 +- klippy/extras/probe.py | 2 +- klippy/extras/replicape.py | 2 +- klippy/kinematics/cartesian.py | 2 +- klippy/kinematics/hybrid_corexy.py | 2 +- klippy/kinematics/hybrid_corexz.py | 2 +- klippy/mcu.py | 3 +-- 11 files changed, 12 insertions(+), 11 deletions(-) diff --git a/klippy/configfile.py b/klippy/configfile.py index 91b555cdef75..a8a4a4ff76f3 100644 --- a/klippy/configfile.py +++ b/klippy/configfile.py @@ -69,6 +69,8 @@ def getboolean(self, option, default=sentinel, note_valid=True): return self._get_wrapper(self.fileconfig.getboolean, option, default, note_valid=note_valid) def getchoice(self, option, choices, default=sentinel, note_valid=True): + if type(choices) == type([]): + choices = {i: i for i in choices} if choices and type(list(choices.keys())[0]) == int: c = self.getint(option, default, note_valid=note_valid) else: diff --git a/klippy/extras/bltouch.py b/klippy/extras/bltouch.py index 58f66819732d..ae461f4b83be 100644 --- a/klippy/extras/bltouch.py +++ b/klippy/extras/bltouch.py @@ -42,7 +42,7 @@ def __init__(self, config): # Create an "endstop" object to handle the sensor pin self.mcu_endstop = ppins.setup_pin('endstop', config.get('sensor_pin')) # 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. diff --git a/klippy/extras/display/hd44780.py b/klippy/extras/display/hd44780.py index 9adfa20f7992..2da49c51ed16 100644 --- a/klippy/extras/display/hd44780.py +++ b/klippy/extras/display/hd44780.py @@ -8,7 +8,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' } diff --git a/klippy/extras/display/hd44780_spi.py b/klippy/extras/display/hd44780_spi.py index cd1d9e3ea773..f21accbb4291 100644 --- a/klippy/extras/display/hd44780_spi.py +++ b/klippy/extras/display/hd44780_spi.py @@ -9,7 +9,7 @@ from .. import bus LINE_LENGTH_DEFAULT=20 -LINE_LENGTH_OPTIONS={16:16, 20:20} +LINE_LENGTH_OPTIONS=[16, 20] TextGlyphs = { 'right_arrow': b'\x7e' } diff --git a/klippy/extras/display/menu_keys.py b/klippy/extras/display/menu_keys.py index 91a96e19fbaa..8094c9964ae9 100644 --- a/klippy/extras/display/menu_keys.py +++ b/klippy/extras/display/menu_keys.py @@ -18,7 +18,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) + [2, 4], 4) if encoder_pins is not None: try: pin1, pin2 = encoder_pins.split(',') diff --git a/klippy/extras/probe.py b/klippy/extras/probe.py index 88aed25fe1a4..c467e181e4d5 100644 --- a/klippy/extras/probe.py +++ b/klippy/extras/probe.py @@ -252,7 +252,7 @@ def __init__(self, config, mcu_probe): self.sample_count = config.getint('samples', 1, minval=1) self.sample_retract_dist = config.getfloat('sample_retract_dist', 2., above=0.) - atypes = {'median': 'median', 'average': 'average'} + atypes = ['median', 'average'] self.samples_result = config.getchoice('samples_result', atypes, 'average') self.samples_tolerance = config.getfloat('samples_tolerance', 0.100, diff --git a/klippy/extras/replicape.py b/klippy/extras/replicape.py index ab501cafc427..f7f7bb64bd22 100644 --- a/klippy/extras/replicape.py +++ b/klippy/extras/replicape.py @@ -160,7 +160,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 diff --git a/klippy/kinematics/cartesian.py b/klippy/kinematics/cartesian.py index 9774672e25cc..0c4bb9255853 100644 --- a/klippy/kinematics/cartesian.py +++ b/klippy/kinematics/cartesian.py @@ -23,7 +23,7 @@ def __init__(self, toolhead, config): self.dc_module = None 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)) diff --git a/klippy/kinematics/hybrid_corexy.py b/klippy/kinematics/hybrid_corexy.py index e852826afba6..265a0e6da33d 100644 --- a/klippy/kinematics/hybrid_corexy.py +++ b/klippy/kinematics/hybrid_corexy.py @@ -27,7 +27,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( diff --git a/klippy/kinematics/hybrid_corexz.py b/klippy/kinematics/hybrid_corexz.py index 58e6b0d390c9..2d89e3f7bd03 100644 --- a/klippy/kinematics/hybrid_corexz.py +++ b/klippy/kinematics/hybrid_corexz.py @@ -27,7 +27,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( diff --git a/klippy/mcu.py b/klippy/mcu.py index 6b106245bdcd..23ba07173257 100644 --- a/klippy/mcu.py +++ b/klippy/mcu.py @@ -574,9 +574,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_methods, None) self._reset_cmd = self._config_reset_cmd = None self._is_mcu_bridge = False self._emergency_stop_cmd = None