From 57244de37d6b4e2247c09803d06728daacfdaa48 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Wed, 30 Nov 2016 15:39:36 -0500 Subject: [PATCH] klippy: Throw an exception if any required config parameter is missing Signed-off-by: Kevin O'Connor --- klippy/klippy.py | 37 ++++++++++++++++++++++--------------- klippy/mcu.py | 2 +- klippy/stepper.py | 18 ++++++++++-------- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/klippy/klippy.py b/klippy/klippy.py index 849c5b8b..bf63d01d 100644 --- a/klippy/klippy.py +++ b/klippy/klippy.py @@ -35,25 +35,32 @@ Printer is shutdown class ConfigWrapper: error = ConfigParser.Error + class sentinel: + pass def __init__(self, printer, section): self.printer = printer self.section = section - def get(self, option, default=None): - if not self.printer.fileconfig.has_option(self.section, option): + def get_wrapper(self, parser, option, default): + if (default is not self.sentinel + and not self.printer.fileconfig.has_option(self.section, option)): return default - return self.printer.fileconfig.get(self.section, option) - def getint(self, option, default=None): - if not self.printer.fileconfig.has_option(self.section, option): - return default - return self.printer.fileconfig.getint(self.section, option) - def getfloat(self, option, default=None): - if not self.printer.fileconfig.has_option(self.section, option): - return default - return self.printer.fileconfig.getfloat(self.section, option) - def getboolean(self, option, default=None): - if not self.printer.fileconfig.has_option(self.section, option): - return default - return self.printer.fileconfig.getboolean(self.section, option) + try: + return parser(self.section, option) + except self.error, e: + raise + except: + raise self.error("Unable to parse option '%s' in section '%s'" % ( + option, self.section)) + def get(self, option, default=sentinel): + return self.get_wrapper(self.printer.fileconfig.get, option, default) + def getint(self, option, default=sentinel): + return self.get_wrapper(self.printer.fileconfig.getint, option, default) + def getfloat(self, option, default=sentinel): + return self.get_wrapper( + self.printer.fileconfig.getfloat, option, default) + def getboolean(self, option, default=sentinel): + return self.get_wrapper( + self.printer.fileconfig.getboolean, option, default) def getsection(self, section): return ConfigWrapper(self.printer, section) diff --git a/klippy/mcu.py b/klippy/mcu.py index 09942df8..5d61d5b1 100644 --- a/klippy/mcu.py +++ b/klippy/mcu.py @@ -394,7 +394,7 @@ class MCU: # Resolve pin names mcu = self.serial.msgparser.config['MCU'] - pin_map = self._config.get('pin_map') + pin_map = self._config.get('pin_map', None) if pin_map is None: pnames = pins.mcu_to_pins(mcu) else: diff --git a/klippy/stepper.py b/klippy/stepper.py index 2def4368..f770afdb 100644 --- a/klippy/stepper.py +++ b/klippy/stepper.py @@ -23,9 +23,9 @@ class PrinterStepper: self.homing_positive_dir = config.getboolean( 'homing_positive_dir', False) self.homing_retract_dist = config.getfloat('homing_retract_dist', 5.) - self.homing_stepper_phases = config.getint('homing_stepper_phases') - self.homing_endstop_phase = config.getint('homing_endstop_phase') - endstop_accuracy = config.getfloat('homing_endstop_accuracy') + self.homing_stepper_phases = config.getint('homing_stepper_phases', None) + self.homing_endstop_phase = config.getint('homing_endstop_phase', None) + endstop_accuracy = config.getfloat('homing_endstop_accuracy', None) self.homing_endstop_accuracy = None if self.homing_stepper_phases: if endstop_accuracy is None: @@ -40,9 +40,11 @@ class PrinterStepper: logging.info("Endstop for %s is not accurate enough for stepper" " phase adjustment" % (self.config.section,)) self.homing_stepper_phases = None - self.position_min = config.getfloat('position_min', 0.) - self.position_endstop = config.getfloat('position_endstop') - self.position_max = config.getfloat('position_max') + self.position_min = self.position_endstop = self.position_max = None + if config.get('endstop_pin', None) is not None: + self.position_min = config.getfloat('position_min', 0.) + self.position_endstop = config.getfloat('position_endstop') + self.position_max = config.getfloat('position_max', 0.) self.need_motor_enable = True def set_max_jerk(self, max_jerk): @@ -59,10 +61,10 @@ class PrinterStepper: mcu = self.printer.mcu self.mcu_stepper = mcu.create_stepper( step_pin, dir_pin, min_stop_interval, max_error) - enable_pin = self.config.get('enable_pin') + enable_pin = self.config.get('enable_pin', None) if enable_pin is not None: self.mcu_enable = mcu.create_digital_out(enable_pin, 0) - endstop_pin = self.config.get('endstop_pin') + endstop_pin = self.config.get('endstop_pin', None) if endstop_pin is not None: self.mcu_endstop = mcu.create_endstop(endstop_pin, self.mcu_stepper) def motor_enable(self, move_time, enable=0):