gcode: Remove "action_" commands from get_status() calls

Rename printer.gcode.action_emergency_stop() to
action_emergency_stop(), printer.gcode.action_respond_info() to
action_respond_info(), and printer.gcode.action_respond_error() to
action_raise_error() in command templates.

This simplifies the get_status() interface, as returning callable
functions from that interface was confusing.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2020-08-16 15:39:30 -04:00
parent 2caaaea9a4
commit b2c78d71b0
10 changed files with 67 additions and 61 deletions

View File

@ -124,25 +124,8 @@ If the config section contains spaces in it, then one can access it
via the `[ ]` accessor - for example:
`printer["generic_heater my_chamber_heater"].temperature`.
Some printer objects allow one to alter the state of the printer. By
convention, these objects use an `action_` prefix. For example,
`printer.gcode.action_emergency_stop()` would cause the printer to go
into a shutdown state. These actions are taken at the time that the
macro is evaluated, which may be a significant amount of time before
the generated commands are executed.
The following are common printer attributes:
- `printer.fan.speed`: The fan speed as a float between 0.0 and 1.0.
- `printer.gcode.action_respond_info(msg)`: Write the given `msg` to
the /tmp/printer pseudo-terminal. Each line of `msg` will be sent
with a "// " prefix.
- `printer.gcode.action_respond_error(msg)`: Write the given `msg` to
the /tmp/printer pseudo-terminal. The first line of `msg` will be
sent with a "!! " prefix and subsequent lines will have a "// "
prefix.
- `printer.gcode.action_emergency_stop(msg)`: Transition the printer
to a shutdown state. The `msg` parameter is optional, it may be
useful to describe the reason for the shutdown.
- `printer.gcode.gcode_position`: The current position of the toolhead
relative to the current G-Code origin. It is possible to access the
x, y, z, and e components of this position (eg,
@ -194,6 +177,26 @@ attributes may be available (via `get_status()` methods defined in the
software). However, undocumented attributes may change without notice
in future Klipper releases.
### Actions
There are some commands available that can alter the state of the
printer. For example, `{ action_emergency_stop() }` would cause the
printer to go into a shutdown state. Note that these actions are taken
at the time that the macro is evaluated, which may be a significant
amount of time before the generated g-code commands are executed.
Available "action" commands:
- `action_respond_info(msg)`: Write the given `msg` to the
/tmp/printer pseudo-terminal. Each line of `msg` will be sent with a
"// " prefix.
- `action_raise_error(msg)`: Abort the current macro (and any calling
macros) and write the given `msg` to the /tmp/printer
pseudo-terminal. The first line of `msg` will be sent with a "!! "
prefix and subsequent lines will have a "// " prefix.
- `action_emergency_stop(msg)`: Transition the printer to a shutdown
state. The `msg` parameter is optional, it may be useful to describe
the reason for the shutdown.
### Variables
The SET_GCODE_VARIABLE command may be useful for saving state between
@ -267,11 +270,8 @@ the gcode option:
[delayed_gcode report_temp]
initial_duration: 2.
gcode:
{printer.gcode.action_respond_info(
"Extruder Temp: %.1f" %
(printer.extruder0.temperature))}
{action_respond_info("Extruder Temp: %.1f" % (printer.extruder0.temperature))}
UPDATE_DELAYED_GCODE ID=report_temp DURATION=2
```
The above delayed_gcode will send "// Extruder Temp: [ex0_temp]" to

View File

@ -6,6 +6,12 @@ All dates in this document are approximate.
# Changes
20200816: The gcode macro "action_" system has changed. Replace any
calls to `printer.gcode.action_emergency_stop()` with
`action_emergency_stop()`, `printer.gcode.action_respond_info()` with
`action_respond_info()`, and `printer.gcode.action_respond_error()`
with `action_raise_error()`.
20200809: The menu system has been rewritten. If the menu has been
customized then it will be necessary to update to the new
configuration. See config/example-menu.cfg for configuration details

View File

@ -66,9 +66,8 @@ class DisplayGroup:
template = gcode_macro.load_template(c, 'text')
self.data_items.append((row, col, template))
def show(self, display, templates, eventtime):
swrap = self.data_items[0][2].create_status_wrapper(eventtime)
context = { 'printer': swrap,
'draw_progress_bar': display.draw_progress_bar }
context = self.data_items[0][2].create_template_context(eventtime)
context['draw_progress_bar'] = display.draw_progress_bar
def render(name, **kwargs):
return templates[name].render(context, **kwargs)
context['render'] = render

View File

@ -137,21 +137,21 @@ type: command
enable: {printer.idle_timeout.state == "Printing"}
name: Pause printing
gcode:
{printer.gcode.action_respond_info('action:pause')}
{action_respond_info('action:pause')}
[menu __main __octoprint __resume]
type: command
enable: {not printer.idle_timeout.state == "Printing"}
name: Resume printing
gcode:
{printer.gcode.action_respond_info('action:resume')}
{action_respond_info('action:resume')}
[menu __main __octoprint __abort]
type: command
enable: {printer.idle_timeout.state == "Printing"}
name: Abort printing
gcode:
{printer.gcode.action_respond_info('action:cancel')}
{action_respond_info('action:cancel')}
### menu virtual sdcard ###
[menu __main __sdcard]
@ -720,8 +720,9 @@ name: Dump parameters
gcode:
{% for name1 in printer %}
{% for name2 in printer[name1] %}
{ printer.gcode.action_respond_info("printer['%s'].%s = %s" % (name1, name2, printer[name1][name2])) }
{ action_respond_info("printer['%s'].%s = %s"
% (name1, name2, printer[name1][name2])) }
{% else %}
{ printer.gcode.action_respond_info("printer['%s'] = %s" % (name1, printer[name1])) }
{ action_respond_info("printer['%s'] = %s" % (name1, printer[name1])) }
{% endfor %}
{% endfor %}

View File

@ -7,7 +7,6 @@
import os, logging
from string import Template
from . import menu_keys
from .. import gcode_macro
class sentinel:
@ -722,13 +721,11 @@ class MenuManager:
def update_context(self, eventtime):
# menu default jinja2 context
self.context = {
'printer': gcode_macro.GetStatusWrapper(self.printer, eventtime),
'menu': {
'eventtime': eventtime,
'back': self._action_back,
'exit': self._action_exit
}
self.context = self.gcode_macro.create_template_context(eventtime)
self.context['menu'] = {
'eventtime': eventtime,
'back': self._action_back,
'exit': self._action_exit
}
def stack_push(self, container):

View File

@ -45,6 +45,8 @@ class TemplateWrapper:
self.printer = printer
self.name = name
self.gcode = self.printer.lookup_object('gcode')
gcode_macro = self.printer.lookup_object('gcode_macro')
self.create_template_context = gcode_macro.create_template_context
try:
self.template = env.from_string(script)
except Exception as e:
@ -52,11 +54,9 @@ class TemplateWrapper:
name, traceback.format_exception_only(type(e), e)[-1])
logging.exception(msg)
raise printer.config_error(msg)
def create_status_wrapper(self, eventtime=None):
return GetStatusWrapper(self.printer, eventtime)
def render(self, context=None):
if context is None:
context = {'printer': self.create_status_wrapper()}
context = self.create_template_context()
try:
return str(self.template.render(context))
except Exception as e:
@ -79,6 +79,21 @@ class PrinterGCodeMacro:
else:
script = config.get(option, default)
return TemplateWrapper(self.printer, self.env, name, script)
def _action_emergency_stop(self, msg="action_emergency_stop"):
self.printer.invoke_shutdown("Shutdown due to %s" % (msg,))
return ""
def _action_respond_info(self, msg):
self.printer.lookup_object('gcode').respond_info(msg)
return ""
def _action_raise_error(self, msg):
raise self.printer.command_error(msg)
def create_template_context(self, eventtime=None):
return {
'printer': GetStatusWrapper(self.printer, eventtime),
'action_emergency_stop': self._action_emergency_stop,
'action_respond_info': self._action_respond_info,
'action_raise_error': self._action_raise_error,
}
def load_config(config):
return PrinterGCodeMacro(config)
@ -159,7 +174,7 @@ class GCodeMacro:
kwparams = dict(self.kwparams)
kwparams.update(params)
kwparams.update(self.variables)
kwparams['printer'] = self.template.create_status_wrapper()
kwparams.update(self.template.create_template_context())
kwparams['params'] = params
self.in_script = True
try:

View File

@ -52,11 +52,11 @@ class HomingOverride:
homing_axes.append(axis)
toolhead.set_position(pos, homing_axes=homing_axes)
# Perform homing
kwparams = { 'printer': self.template.create_status_wrapper() }
kwparams['params'] = gcmd.get_command_parameters()
context = self.template.create_template_context()
context['params'] = gcmd.get_command_parameters()
try:
self.in_script = True
self.template.run_gcode_from_command(kwparams)
self.template.run_gcode_from_command(context)
finally:
self.in_script = False

View File

@ -167,15 +167,6 @@ class GCodeParser:
self.move_with_transform = transform.move
self.position_with_transform = transform.get_position
return old_transform
def _action_emergency_stop(self, msg="action_emergency_stop"):
self.printer.invoke_shutdown("Shutdown due to %s" % (msg,))
return ""
def _action_respond_info(self, msg):
self.respond_info(msg)
return ""
def _action_respond_error(self, msg):
self._respond_error(msg)
return ""
def _get_gcode_position(self):
p = [lp - bp for lp, bp in zip(self.last_position, self.base_position)]
p[3] /= self.extrude_factor
@ -208,9 +199,6 @@ class GCodeParser:
'homing_ypos': self.homing_position[1],
'homing_zpos': self.homing_position[2],
'gcode_position': homing.Coord(*move_position),
'action_respond_info': self._action_respond_info,
'action_respond_error': self._action_respond_error,
'action_emergency_stop': self._action_emergency_stop,
}
def dump_state(self):
return ("gcode state: absolute_coord=%s absolute_extrude=%s"

View File

@ -393,7 +393,7 @@ class QueryStatusHelper:
cres = {}
for ri in req_items:
rd = res.get(ri, None)
if not callable(rd) and (is_query or rd != lres.get(ri)):
if is_query or rd != lres.get(ri):
cres[ri] = rd
if cres or is_query:
cquery[obj_name] = cres

View File

@ -76,13 +76,13 @@ gcode:
{% if printer.gcode.gcode_position.x != 0.0 %}
M112
{% else %}
{ printer.gcode.action_respond_info("TEST_expression") }
{ action_respond_info("TEST_expression") }
{% endif %}
[gcode_macro TEST_variable]
variable_t: 12.0
gcode:
{ printer.gcode.action_respond_info("TEST_variable") }
{ action_respond_info("TEST_variable") }
{% if t - 12.0 != printer.toolhead.position.y %}
M112
{% endif %}
@ -94,14 +94,14 @@ gcode:
[gcode_macro TEST_variable_part2]
gcode:
{ printer.gcode.action_respond_info("TEST_variable_part2") }
{ action_respond_info("TEST_variable_part2") }
{% if printer["gcode_macro TEST_variable"].t != 17.0 %}
M112
{% endif %}
[gcode_macro TEST_param]
gcode:
{ printer.gcode.action_respond_info("TEST_param") }
{ action_respond_info("TEST_param") }
{% if params.T != "123" %}
M112
{% endif %}