gcode: Add '_' prefix to internal methods

Mark methods internal to gcode.py with an underscore so that it is
easier to see which functions are purely helper functions.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-05-24 18:45:18 -04:00
parent 61530138a0
commit b8a4cf7701
1 changed files with 39 additions and 39 deletions

View File

@ -16,10 +16,10 @@ class GCodeParser:
def __init__(self, printer, fd): def __init__(self, printer, fd):
self.printer = printer self.printer = printer
self.fd = fd self.fd = fd
printer.register_event_handler("klippy:ready", self.handle_ready) printer.register_event_handler("klippy:ready", self._handle_ready)
printer.register_event_handler("klippy:shutdown", self.handle_shutdown) printer.register_event_handler("klippy:shutdown", self._handle_shutdown)
printer.register_event_handler("klippy:disconnect", printer.register_event_handler("klippy:disconnect",
self.handle_disconnect) self._handle_disconnect)
# Input handling # Input handling
self.reactor = printer.get_reactor() self.reactor = printer.get_reactor()
self.is_processing_data = False self.is_processing_data = False
@ -27,7 +27,7 @@ class GCodeParser:
self.fd_handle = None self.fd_handle = None
if not self.is_fileinput: if not self.is_fileinput:
self.fd_handle = self.reactor.register_fd(self.fd, self.fd_handle = self.reactor.register_fd(self.fd,
self.process_data) self._process_data)
self.partial_input = "" self.partial_input = ""
self.pending_commands = [] self.pending_commands = []
self.bytes_read = 0 self.bytes_read = 0
@ -72,7 +72,7 @@ class GCodeParser:
"gcode command %s already registered" % (cmd,)) "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))
self.ready_gcode_handlers[cmd] = func self.ready_gcode_handlers[cmd] = func
if when_not_ready: if when_not_ready:
self.base_gcode_handlers[cmd] = func self.base_gcode_handlers[cmd] = func
@ -81,7 +81,7 @@ class GCodeParser:
def register_mux_command(self, cmd, key, value, func, desc=None): def register_mux_command(self, cmd, key, value, func, desc=None):
prev = self.mux_commands.get(cmd) prev = self.mux_commands.get(cmd)
if prev is None: if prev is None:
self.register_command(cmd, self.cmd_mux, desc=desc) self.register_command(cmd, self._cmd_mux, desc=desc)
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:
@ -102,12 +102,12 @@ class GCodeParser:
self.position_with_transform = transform.get_position self.position_with_transform = transform.get_position
def stats(self, eventtime): def stats(self, eventtime):
return False, "gcodein=%d" % (self.bytes_read,) return False, "gcodein=%d" % (self.bytes_read,)
def get_current_position(self): def _get_current_position(self):
p = [lp - bp for lp, bp in zip(self.last_position, self.base_position)] p = [lp - bp for lp, bp in zip(self.last_position, self.base_position)]
p[3] /= self.extrude_factor p[3] /= self.extrude_factor
return p return p
def get_status(self, eventtime): def get_status(self, eventtime):
move_position = self.get_current_position() move_position = self._get_current_position()
busy = self.is_processing_data busy = self.is_processing_data
return { return {
'speed_factor': self.speed_factor * 60., 'speed_factor': self.speed_factor * 60.,
@ -131,18 +131,18 @@ class GCodeParser:
'homing_ypos': self.homing_position[1], 'homing_ypos': self.homing_position[1],
'homing_zpos': self.homing_position[2] 'homing_zpos': self.homing_position[2]
} }
def handle_shutdown(self): def _handle_shutdown(self):
if not self.is_printer_ready: if not self.is_printer_ready:
return return
self.is_printer_ready = False self.is_printer_ready = False
self.gcode_handlers = self.base_gcode_handlers self.gcode_handlers = self.base_gcode_handlers
self.dump_debug() self._dump_debug()
if self.is_fileinput: if self.is_fileinput:
self.printer.request_exit('error_exit') self.printer.request_exit('error_exit')
self._respond_state("Shutdown") self._respond_state("Shutdown")
def handle_disconnect(self): def _handle_disconnect(self):
self._respond_state("Disconnect") self._respond_state("Disconnect")
def handle_ready(self): def _handle_ready(self):
self.is_printer_ready = True self.is_printer_ready = True
self.gcode_handlers = self.ready_gcode_handlers self.gcode_handlers = self.ready_gcode_handlers
# Lookup printer components # Lookup printer components
@ -158,11 +158,11 @@ class GCodeParser:
self.fan = self.printer.lookup_object('fan', None) self.fan = self.printer.lookup_object('fan', None)
if self.is_fileinput and self.fd_handle is None: if self.is_fileinput and self.fd_handle is None:
self.fd_handle = self.reactor.register_fd(self.fd, self.fd_handle = self.reactor.register_fd(self.fd,
self.process_data) self._process_data)
self._respond_state("Ready") self._respond_state("Ready")
def reset_last_position(self): def reset_last_position(self):
self.last_position = self.position_with_transform() self.last_position = self.position_with_transform()
def dump_debug(self): def _dump_debug(self):
out = [] out = []
out.append("Dumping gcode input %d blocks" % ( out.append("Dumping gcode input %d blocks" % (
len(self.input_log),)) len(self.input_log),))
@ -178,7 +178,7 @@ class GCodeParser:
logging.info("\n".join(out)) logging.info("\n".join(out))
# Parse input into commands # Parse input into commands
args_r = re.compile('([A-Z_]+|[A-Z*/])') args_r = re.compile('([A-Z_]+|[A-Z*/])')
def process_commands(self, commands, need_ack=True): def _process_commands(self, commands, need_ack=True):
for line in commands: for line in commands:
# Ignore comments and leading/trailing spaces # Ignore comments and leading/trailing spaces
line = origline = line.strip() line = origline = line.strip()
@ -216,7 +216,7 @@ class GCodeParser:
raise raise
self.ack() self.ack()
m112_r = re.compile('^(?:[nN][0-9]+)?\s*[mM]112(?:\s|$)') m112_r = re.compile('^(?:[nN][0-9]+)?\s*[mM]112(?:\s|$)')
def process_data(self, eventtime): def _process_data(self, eventtime):
# Read input, separate by newline, and add to pending_commands # Read input, separate by newline, and add to pending_commands
try: try:
data = os.read(self.fd, 4096) data = os.read(self.fd, 4096)
@ -253,38 +253,38 @@ class GCodeParser:
# Process commands # Process commands
self.is_processing_data = True self.is_processing_data = True
self.pending_commands = [] self.pending_commands = []
self.process_commands(pending_commands) self._process_commands(pending_commands)
if self.pending_commands: if self.pending_commands:
self.process_pending() self._process_pending()
self.is_processing_data = False self.is_processing_data = False
def process_pending(self): def _process_pending(self):
pending_commands = self.pending_commands pending_commands = self.pending_commands
while pending_commands: while pending_commands:
self.pending_commands = [] self.pending_commands = []
self.process_commands(pending_commands) self._process_commands(pending_commands)
pending_commands = self.pending_commands pending_commands = self.pending_commands
if self.fd_handle is None: if self.fd_handle is None:
self.fd_handle = self.reactor.register_fd(self.fd, self.fd_handle = self.reactor.register_fd(self.fd,
self.process_data) self._process_data)
def process_batch(self, commands): def process_batch(self, commands):
if self.is_processing_data: if self.is_processing_data:
return False return False
self.is_processing_data = True self.is_processing_data = True
try: try:
self.process_commands(commands, need_ack=False) self._process_commands(commands, need_ack=False)
except error as e: except error as e:
if self.pending_commands: if self.pending_commands:
self.process_pending() self._process_pending()
self.is_processing_data = False self.is_processing_data = False
raise raise
if self.pending_commands: if self.pending_commands:
self.process_pending() self._process_pending()
self.is_processing_data = False self.is_processing_data = False
return True return True
def run_script_from_command(self, script): def run_script_from_command(self, script):
prev_need_ack = self.need_ack prev_need_ack = self.need_ack
try: try:
self.process_commands(script.split('\n'), need_ack=False) self._process_commands(script.split('\n'), need_ack=False)
finally: finally:
self.need_ack = prev_need_ack self.need_ack = prev_need_ack
def run_script(self, script): def run_script(self, script):
@ -370,7 +370,7 @@ class GCodeParser:
r'(?P<cmd>[a-zA-Z_][a-zA-Z_]+)(?:\s+|$)' r'(?P<cmd>[a-zA-Z_][a-zA-Z_]+)(?:\s+|$)'
r'(?P<args>[^#*;]*?)' r'(?P<args>[^#*;]*?)'
r'\s*(?:[#*;].*)?$') r'\s*(?:[#*;].*)?$')
def get_extended_params(self, params): def _get_extended_params(self, params):
m = self.extended_r.match(params['#original']) m = self.extended_r.match(params['#original'])
if m is None: if m is None:
# Not an "extended" command # Not an "extended" command
@ -384,7 +384,7 @@ class GCodeParser:
except ValueError as e: except ValueError as e:
raise error("Malformed command '%s'" % (params['#original'],)) raise error("Malformed command '%s'" % (params['#original'],))
# Temperature wrappers # Temperature wrappers
def get_temp(self, eventtime): def _get_temp(self, eventtime):
# Tn:XXX /YYY B:XXX /YYY # Tn:XXX /YYY B:XXX /YYY
out = [] out = []
if self.heaters is not None: if self.heaters is not None:
@ -400,9 +400,9 @@ class GCodeParser:
eventtime = self.reactor.monotonic() eventtime = self.reactor.monotonic()
while self.is_printer_ready and heater.check_busy(eventtime): while self.is_printer_ready and heater.check_busy(eventtime):
print_time = self.toolhead.get_last_move_time() print_time = self.toolhead.get_last_move_time()
self.respond(self.get_temp(eventtime)) self.respond(self._get_temp(eventtime))
eventtime = self.reactor.pause(eventtime + 1.) eventtime = self.reactor.pause(eventtime + 1.)
def set_temp(self, params, is_bed=False, wait=False): def _set_temp(self, params, is_bed=False, wait=False):
temp = self.get_float('S', params, 0.) temp = self.get_float('S', params, 0.)
heater = None heater = None
if is_bed: if is_bed:
@ -425,7 +425,7 @@ class GCodeParser:
raise error(str(e)) raise error(str(e))
if wait and temp: if wait and temp:
self.bg_temp(heater) self.bg_temp(heater)
def set_fan_speed(self, speed): def _set_fan_speed(self, speed):
if self.fan is None: if self.fan is None:
if speed and not self.is_fileinput: if speed and not self.is_fileinput:
self.respond_info("Fan not configured") self.respond_info("Fan not configured")
@ -469,7 +469,7 @@ class GCodeParser:
self.extrude_factor = 1. self.extrude_factor = 1.
self.base_position[3] = self.last_position[3] self.base_position[3] = self.last_position[3]
self.run_script_from_command(self.extruder.get_activate_gcode(True)) self.run_script_from_command(self.extruder.get_activate_gcode(True))
def cmd_mux(self, params): def _cmd_mux(self, params):
key, values = self.mux_commands[params['#command']] key, values = self.mux_commands[params['#command']]
if None in values: if None in values:
key_param = self.get_str(key, params, None) key_param = self.get_str(key, params, None)
@ -582,7 +582,7 @@ class GCodeParser:
cmd_M114_when_not_ready = True cmd_M114_when_not_ready = True
def cmd_M114(self, params): def cmd_M114(self, params):
# Get Current Position # Get Current Position
p = self.get_current_position() p = self._get_current_position()
self.respond("X:%.3f Y:%.3f Z:%.3f E:%.3f" % tuple(p)) self.respond("X:%.3f Y:%.3f Z:%.3f E:%.3f" % tuple(p))
def cmd_M220(self, params): def cmd_M220(self, params):
# Set speed factor override percentage # Set speed factor override percentage
@ -620,25 +620,25 @@ class GCodeParser:
cmd_M105_when_not_ready = True cmd_M105_when_not_ready = True
def cmd_M105(self, params): def cmd_M105(self, params):
# Get Extruder Temperature # Get Extruder Temperature
self.ack(self.get_temp(self.reactor.monotonic())) self.ack(self._get_temp(self.reactor.monotonic()))
def cmd_M104(self, params): def cmd_M104(self, params):
# Set Extruder Temperature # Set Extruder Temperature
self.set_temp(params) self._set_temp(params)
def cmd_M109(self, params): def cmd_M109(self, params):
# Set Extruder Temperature and Wait # Set Extruder Temperature and Wait
self.set_temp(params, wait=True) self._set_temp(params, wait=True)
def cmd_M140(self, params): def cmd_M140(self, params):
# Set Bed Temperature # Set Bed Temperature
self.set_temp(params, is_bed=True) self._set_temp(params, is_bed=True)
def cmd_M190(self, params): def cmd_M190(self, params):
# Set Bed Temperature and Wait # Set Bed Temperature and Wait
self.set_temp(params, is_bed=True, wait=True) self._set_temp(params, is_bed=True, wait=True)
def cmd_M106(self, params): def cmd_M106(self, params):
# Set fan speed # Set fan speed
self.set_fan_speed(self.get_float('S', params, 255., minval=0.) / 255.) self._set_fan_speed(self.get_float('S', params, 255., minval=0.) / 255.)
def cmd_M107(self, params): def cmd_M107(self, params):
# Turn fan off # Turn fan off
self.set_fan_speed(0.) self._set_fan_speed(0.)
# G-Code miscellaneous commands # G-Code miscellaneous commands
cmd_M112_when_not_ready = True cmd_M112_when_not_ready = True
def cmd_M112(self, params): def cmd_M112(self, params):