2018-05-25 18:33:01 +02:00
|
|
|
# Add ability to define custom g-code macros
|
|
|
|
#
|
2019-01-22 20:35:32 +01:00
|
|
|
# Copyright (C) 2018-2019 Kevin O'Connor <kevin@koconnor.net>
|
2018-05-25 18:33:01 +02:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2018-09-30 18:32:13 +02:00
|
|
|
import traceback, logging
|
2019-01-22 20:35:32 +01:00
|
|
|
import jinja2
|
2018-05-25 18:33:01 +02:00
|
|
|
|
2019-01-22 20:35:32 +01:00
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Template handling
|
|
|
|
######################################################################
|
|
|
|
|
2019-05-28 16:59:12 +02:00
|
|
|
# Wrapper for access to printer object get_status() methods
|
|
|
|
class GetStatusWrapper:
|
2019-01-22 20:35:32 +01:00
|
|
|
def __init__(self, printer, eventtime=None):
|
|
|
|
self.printer = printer
|
|
|
|
self.eventtime = eventtime
|
|
|
|
self.cache = {}
|
|
|
|
def __getitem__(self, val):
|
|
|
|
sval = str(val).strip()
|
|
|
|
if sval in self.cache:
|
|
|
|
return self.cache[sval]
|
|
|
|
po = self.printer.lookup_object(sval, None)
|
|
|
|
if po is None or not hasattr(po, 'get_status'):
|
|
|
|
raise KeyError(val)
|
|
|
|
if self.eventtime is None:
|
|
|
|
self.eventtime = self.printer.get_reactor().monotonic()
|
|
|
|
self.cache[sval] = res = dict(po.get_status(self.eventtime))
|
|
|
|
return res
|
2019-06-04 18:41:28 +02:00
|
|
|
def __contains__(self, val):
|
|
|
|
try:
|
|
|
|
self.__getitem__(val)
|
|
|
|
except KeyError as e:
|
|
|
|
return False
|
|
|
|
return True
|
2019-01-22 20:35:32 +01:00
|
|
|
|
|
|
|
# Wrapper around a Jinja2 template
|
|
|
|
class TemplateWrapper:
|
|
|
|
def __init__(self, printer, env, name, script):
|
|
|
|
self.printer = printer
|
|
|
|
self.name = name
|
|
|
|
self.gcode = self.printer.lookup_object('gcode')
|
|
|
|
try:
|
|
|
|
self.template = env.from_string(script)
|
|
|
|
except Exception as e:
|
|
|
|
msg = "Error loading template '%s': %s" % (
|
|
|
|
name, traceback.format_exception_only(type(e), e)[-1])
|
|
|
|
logging.exception(msg)
|
|
|
|
raise printer.config_error(msg)
|
|
|
|
def create_status_wrapper(self, eventtime=None):
|
2019-05-28 16:59:12 +02:00
|
|
|
return GetStatusWrapper(self.printer, eventtime)
|
2019-01-22 20:35:32 +01:00
|
|
|
def render(self, context=None):
|
|
|
|
if context is None:
|
2019-05-28 16:59:12 +02:00
|
|
|
context = {'printer': self.create_status_wrapper()}
|
2019-01-22 20:35:32 +01:00
|
|
|
try:
|
|
|
|
return str(self.template.render(context))
|
|
|
|
except Exception as e:
|
|
|
|
msg = "Error evaluating '%s': %s" % (
|
|
|
|
self.name, traceback.format_exception_only(type(e), e)[-1])
|
|
|
|
logging.exception(msg)
|
|
|
|
raise self.gcode.error(msg)
|
|
|
|
def run_gcode_from_command(self, context=None):
|
|
|
|
self.gcode.run_script_from_command(self.render(context))
|
|
|
|
|
|
|
|
# Main gcode macro template tracking
|
|
|
|
class PrinterGCodeMacro:
|
|
|
|
def __init__(self, config):
|
|
|
|
self.printer = config.get_printer()
|
|
|
|
self.env = jinja2.Environment('{%', '%}', '{', '}')
|
|
|
|
def load_template(self, config, option):
|
|
|
|
name = "%s:%s" % (config.get_name(), option)
|
|
|
|
script = config.get(option, '')
|
|
|
|
return TemplateWrapper(self.printer, self.env, name, script)
|
|
|
|
|
|
|
|
def load_config(config):
|
|
|
|
return PrinterGCodeMacro(config)
|
|
|
|
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# GCode macro
|
|
|
|
######################################################################
|
2018-09-27 18:08:01 +02:00
|
|
|
|
2018-05-25 18:33:01 +02:00
|
|
|
class GCodeMacro:
|
|
|
|
def __init__(self, config):
|
2019-05-28 18:17:08 +02:00
|
|
|
name = config.get_name().split()[1]
|
|
|
|
self.alias = name.upper()
|
2018-05-25 18:33:01 +02:00
|
|
|
printer = config.get_printer()
|
2019-01-22 20:35:32 +01:00
|
|
|
config.get('gcode')
|
|
|
|
gcode_macro = printer.try_load_module(config, 'gcode_macro')
|
|
|
|
self.template = gcode_macro.load_template(config, 'gcode')
|
2018-05-25 18:33:01 +02:00
|
|
|
self.gcode = printer.lookup_object('gcode')
|
2019-02-19 00:04:42 +01:00
|
|
|
self.gcode.register_command(self.alias, self.cmd, desc=self.cmd_desc)
|
2019-05-28 18:17:08 +02:00
|
|
|
self.gcode.register_mux_command("SET_GCODE_VARIABLE", "MACRO",
|
|
|
|
name, self.cmd_SET_GCODE_VARIABLE,
|
|
|
|
desc=self.cmd_SET_GCODE_VARIABLE_help)
|
2018-05-25 18:33:01 +02:00
|
|
|
self.in_script = False
|
2019-01-22 20:35:32 +01:00
|
|
|
prefix = 'default_parameter_'
|
|
|
|
self.kwparams = { o[len(prefix):].upper(): config.get(o)
|
|
|
|
for o in config.get_prefix_options(prefix) }
|
2019-05-28 18:17:08 +02:00
|
|
|
prefix = 'variable_'
|
|
|
|
self.variables = { o[len(prefix):]: config.get(o)
|
|
|
|
for o in config.get_prefix_options(prefix) }
|
|
|
|
def get_status(self, eventtime):
|
|
|
|
return dict(self.variables)
|
|
|
|
cmd_SET_GCODE_VARIABLE_help = "Set the value of a G-Code macro variable"
|
|
|
|
def cmd_SET_GCODE_VARIABLE(self, params):
|
|
|
|
variable = self.gcode.get_str('VARIABLE', params)
|
|
|
|
value = self.gcode.get_str('VALUE', params)
|
|
|
|
if variable not in self.variables:
|
|
|
|
if variable in self.kwparams:
|
|
|
|
self.kwparams[variable] = value
|
|
|
|
return
|
|
|
|
raise self.gcode.error("Unknown gcode_macro variable '%s'" % (
|
|
|
|
variable,))
|
|
|
|
self.variables[variable] = value
|
2018-05-25 18:33:01 +02:00
|
|
|
cmd_desc = "G-Code macro"
|
|
|
|
def cmd(self, params):
|
|
|
|
if self.in_script:
|
2018-09-26 07:23:45 +02:00
|
|
|
raise self.gcode.error(
|
|
|
|
"Macro %s called recursively" % (self.alias,))
|
2018-09-27 18:08:01 +02:00
|
|
|
kwparams = dict(self.kwparams)
|
|
|
|
kwparams.update(params)
|
2019-05-28 18:17:08 +02:00
|
|
|
kwparams.update(self.variables)
|
2019-05-28 16:59:12 +02:00
|
|
|
kwparams['printer'] = self.template.create_status_wrapper()
|
2019-01-22 20:35:32 +01:00
|
|
|
kwparams['params'] = params
|
2018-05-25 18:33:01 +02:00
|
|
|
self.in_script = True
|
|
|
|
try:
|
2019-01-22 20:35:32 +01:00
|
|
|
self.template.run_gcode_from_command(kwparams)
|
2018-05-25 18:33:01 +02:00
|
|
|
finally:
|
|
|
|
self.in_script = False
|
|
|
|
|
|
|
|
def load_config_prefix(config):
|
|
|
|
return GCodeMacro(config)
|