gcode: Add a get_str() method

Add a get_str() method that can extract a required parameter from a
g-code command.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2017-12-03 20:16:20 -05:00
parent 80f23441dd
commit 64a091fb98
1 changed files with 10 additions and 17 deletions

View File

@ -178,26 +178,21 @@ class GCodeParser:
self.respond_info("\n".join(lines[:-1])) self.respond_info("\n".join(lines[:-1]))
self.respond('!! %s' % (lines[-1].strip(),)) self.respond('!! %s' % (lines[-1].strip(),))
# Parameter parsing helpers # Parameter parsing helpers
def get_int(self, name, params, default=None): class sentinel: pass
def get_str(self, name, params, default=sentinel, parser=str):
if name in params: if name in params:
try: try:
return int(params[name]) return parser(params[name])
except ValueError: except:
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 default is not None: if default is not self.sentinel:
return default
raise error("Error on '%s': missing %s" % (params['#original'], name))
def get_float(self, name, params, default=None):
if name in params:
try:
return float(params[name])
except ValueError:
raise error("Error on '%s': unable to parse %s" % (
params['#original'], params[name]))
if default is not None:
return default return default
raise error("Error on '%s': missing %s" % (params['#original'], name)) raise error("Error on '%s': missing %s" % (params['#original'], name))
def get_int(self, name, params, default=sentinel):
return self.get_str(name, params, default, parser=int)
def get_float(self, name, params, default=sentinel):
return self.get_str(name, params, default, parser=float)
extended_r = re.compile( extended_r = re.compile(
r'^\s*(?:N[0-9]+\s*)?' r'^\s*(?:N[0-9]+\s*)?'
r'(?P<cmd>[a-zA-Z_][a-zA-Z_]+)(?:\s+|$)' r'(?P<cmd>[a-zA-Z_][a-zA-Z_]+)(?:\s+|$)'
@ -470,9 +465,7 @@ class GCodeParser:
cmd_SET_SERVO_help = "Set servo angle" cmd_SET_SERVO_help = "Set servo angle"
def cmd_SET_SERVO(self, params): def cmd_SET_SERVO(self, params):
params = self.get_extended_params(params) params = self.get_extended_params(params)
name = params.get('SERVO') name = self.get_str('SERVO', params)
if name is None:
raise error("Error on '%s': missing SERVO" % (params['#original'],))
s = chipmisc.get_printer_servo(self.printer, name) s = chipmisc.get_printer_servo(self.printer, name)
if s is None: if s is None:
raise error("Servo not configured") raise error("Servo not configured")