gcode: Raise a config error on invalid register_command()

Raise a printer.config_error() on an invalid register_command() call.
This error is easier to handle for the vast majority of callers.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-02-18 18:04:42 -05:00
parent 276d5a1436
commit b260eb9168
5 changed files with 26 additions and 25 deletions

View File

@ -24,17 +24,20 @@ class PrinterLCD:
# menu # menu
self.menu = menu.MenuManager(config, self.lcd_chip) self.menu = menu.MenuManager(config, self.lcd_chip)
# printer objects # printer objects
self.gcode = self.toolhead = self.sdcard = None self.toolhead = self.sdcard = None
self.fan = self.extruder0 = self.extruder1 = self.heater_bed = None self.fan = self.extruder0 = self.extruder1 = self.heater_bed = None
self.printer.register_event_handler("klippy:ready", self.handle_ready) self.printer.register_event_handler("klippy:ready", self.handle_ready)
# screen updating # screen updating
self.screen_update_timer = self.reactor.register_timer( self.screen_update_timer = self.reactor.register_timer(
self.screen_update_event) self.screen_update_event)
# Register commands
self.gcode = self.printer.lookup_object('gcode')
self.gcode.register_command('M73', self.cmd_M73)
self.gcode.register_command('M117', self.cmd_M117)
# Initialization # Initialization
def handle_ready(self): def handle_ready(self):
self.lcd_chip.init() self.lcd_chip.init()
# Load printer objects # Load printer objects
self.gcode = self.printer.lookup_object('gcode')
self.toolhead = self.printer.lookup_object('toolhead') self.toolhead = self.printer.lookup_object('toolhead')
self.sdcard = self.printer.lookup_object('virtual_sdcard', None) self.sdcard = self.printer.lookup_object('virtual_sdcard', None)
self.fan = self.printer.lookup_object('fan', None) self.fan = self.printer.lookup_object('fan', None)
@ -45,8 +48,6 @@ class PrinterLCD:
self.progress = None self.progress = None
self.msg_time = None self.msg_time = None
self.message = None self.message = None
self.gcode.register_command('M73', self.cmd_M73)
self.gcode.register_command('M117', self.cmd_M117)
# Start screen update timer # Start screen update timer
self.reactor.update_timer(self.screen_update_timer, self.reactor.NOW) self.reactor.update_timer(self.screen_update_timer, self.reactor.NOW)
# Get menu instance # Get menu instance

View File

@ -13,11 +13,7 @@ class GCodeMacro:
self.script = config.get('gcode') self.script = config.get('gcode')
printer = config.get_printer() printer = config.get_printer()
self.gcode = printer.lookup_object('gcode') self.gcode = printer.lookup_object('gcode')
try: self.gcode.register_command(self.alias, self.cmd, desc=self.cmd_desc)
self.gcode.register_command(
self.alias, self.cmd, desc=self.cmd_desc)
except self.gcode.error as e:
raise config.error(str(e))
self.in_script = False self.in_script = False
self.kwparams = { o[len(DEFAULT_PREFIX):].upper(): config.get(o) self.kwparams = { o[len(DEFAULT_PREFIX):].upper(): config.get(o)
for o in config.get_prefix_options(DEFAULT_PREFIX) } for o in config.get_prefix_options(DEFAULT_PREFIX) }

View File

@ -58,7 +58,7 @@ class ManualProbeHelper:
try: try:
self.gcode.register_command('ACCEPT', self.cmd_ACCEPT, self.gcode.register_command('ACCEPT', self.cmd_ACCEPT,
desc=self.cmd_ACCEPT_help) desc=self.cmd_ACCEPT_help)
except self.gcode.error as e: except self.printer.config_error as e:
self.gcode.respond_error( self.gcode.respond_error(
"Already in a manual Z probe. Use ABORT to abort it.") "Already in a manual Z probe. Use ABORT to abort it.")
self.finalize_callback(None) self.finalize_callback(None)

View File

@ -24,7 +24,7 @@ class FilamentWidthSensor:
self.filament_array = [] self.filament_array = []
self.lastFilamentWidthReading = 0 self.lastFilamentWidthReading = 0
# printer objects # printer objects
self.gcode = self.toolhead = self.ppins = self.mcu_adc = None self.toolhead = self.ppins = self.mcu_adc = None
self.printer.register_event_handler("klippy:ready", self.handle_ready) self.printer.register_event_handler("klippy:ready", self.handle_ready)
# Start adc # Start adc
self.ppins = self.printer.lookup_object('pins') self.ppins = self.printer.lookup_object('pins')
@ -34,17 +34,18 @@ class FilamentWidthSensor:
# extrude factor updating # extrude factor updating
self.extrude_factor_update_timer = self.reactor.register_timer( self.extrude_factor_update_timer = self.reactor.register_timer(
self.extrude_factor_update_event) self.extrude_factor_update_event)
# Register commands
# Initialization
def handle_ready(self):
# Load printer objects
self.gcode = self.printer.lookup_object('gcode') self.gcode = self.printer.lookup_object('gcode')
self.toolhead = self.printer.lookup_object('toolhead')
self.gcode.register_command('QUERY_FILAMENT_WIDTH', self.cmd_M407) self.gcode.register_command('QUERY_FILAMENT_WIDTH', self.cmd_M407)
self.gcode.register_command('RESET_FILAMENT_WIDTH_SENSOR', self.cmd_ClearFilamentArray) self.gcode.register_command('RESET_FILAMENT_WIDTH_SENSOR', self.cmd_ClearFilamentArray)
self.gcode.register_command('DISABLE_FILAMENT_WIDTH_SENSOR', self.cmd_M406) self.gcode.register_command('DISABLE_FILAMENT_WIDTH_SENSOR', self.cmd_M406)
self.gcode.register_command('ENABLE_FILAMENT_WIDTH_SENSOR', self.cmd_M405) self.gcode.register_command('ENABLE_FILAMENT_WIDTH_SENSOR', self.cmd_M405)
# Initialization
def handle_ready(self):
# Load printer objects
self.toolhead = self.printer.lookup_object('toolhead')
# Start extrude factor update timer # Start extrude factor update timer
self.reactor.update_timer(self.extrude_factor_update_timer, self.reactor.NOW) self.reactor.update_timer(self.extrude_factor_update_timer, self.reactor.NOW)

View File

@ -67,7 +67,8 @@ class GCodeParser:
del self.base_gcode_handlers[cmd] del self.base_gcode_handlers[cmd]
return return
if cmd in self.ready_gcode_handlers: if cmd in self.ready_gcode_handlers:
raise error("gcode command %s already registered" % (cmd,)) raise self.printer.config_error(
"gcode command %s already registered" % (cmd,))
if not (len(cmd) >= 2 and not cmd[0].isupper() and cmd[1].isdigit()): if not (len(cmd) >= 2 and not cmd[0].isupper() and cmd[1].isdigit()):
origfunc = func origfunc = func
func = lambda params: origfunc(self.get_extended_params(params)) func = lambda params: origfunc(self.get_extended_params(params))
@ -83,11 +84,13 @@ class GCodeParser:
self.mux_commands[cmd] = prev = (key, {}) self.mux_commands[cmd] = prev = (key, {})
prev_key, prev_values = prev prev_key, prev_values = prev
if prev_key != key: if prev_key != key:
raise error("mux command %s %s %s may have only one key (%s)" % ( raise self.printer.config_error(
cmd, key, value, prev_key)) "mux command %s %s %s may have only one key (%s)" % (
cmd, key, value, prev_key))
if value in prev_values: if value in prev_values:
raise error("mux command %s %s %s already registered (%s)" % ( raise self.printer.config_error(
cmd, key, value, prev_values)) "mux command %s %s %s already registered (%s)" % (
cmd, key, value, prev_values))
prev_values[value] = func prev_values[value] = func
def set_move_transform(self, transform): def set_move_transform(self, transform):
if self.move_transform is not None: if self.move_transform is not None:
@ -328,16 +331,16 @@ class GCodeParser:
raise error("Error on '%s': unable to parse %s" % ( raise error("Error on '%s': unable to parse %s" % (
params['#original'], params[name])) params['#original'], params[name]))
if minval is not None and value < minval: if minval is not None and value < minval:
raise self.error("Error on '%s': %s must have minimum of %s" % ( raise error("Error on '%s': %s must have minimum of %s" % (
params['#original'], name, minval)) params['#original'], name, minval))
if maxval is not None and value > maxval: if maxval is not None and value > maxval:
raise self.error("Error on '%s': %s must have maximum of %s" % ( raise error("Error on '%s': %s must have maximum of %s" % (
params['#original'], name, maxval)) params['#original'], name, maxval))
if above is not None and value <= above: if above is not None and value <= above:
raise self.error("Error on '%s': %s must be above %s" % ( raise error("Error on '%s': %s must be above %s" % (
params['#original'], name, above)) params['#original'], name, above))
if below is not None and value >= below: if below is not None and value >= below:
raise self.error("Error on '%s': %s must be below %s" % ( raise error("Error on '%s': %s must be below %s" % (
params['#original'], name, below)) params['#original'], name, below))
return value return value
def get_int(self, name, params, default=sentinel, minval=None, maxval=None): def get_int(self, name, params, default=sentinel, minval=None, maxval=None):