From 6545f9a81668cf5e6a19faf5ee4e41a16aa64944 Mon Sep 17 00:00:00 2001 From: Shawn Bruce Date: Thu, 4 Jan 2018 14:42:01 -0500 Subject: [PATCH] Allow plugins to define gcode script variables using the octoprint.comm.protocol.scripts hook --- docs/features/gcode_scripts.rst | 1 + docs/plugins/hooks.rst | 23 ++++++++++++++++------- src/octoprint/util/comm.py | 33 +++++++++++++++++++++------------ 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/docs/features/gcode_scripts.rst b/docs/features/gcode_scripts.rst index 4be810ea1..2533bdada 100644 --- a/docs/features/gcode_scripts.rst +++ b/docs/features/gcode_scripts.rst @@ -80,6 +80,7 @@ All GCODE scripts have access to the following template variables through the te note that ``actual`` and ``target`` might be ``None``. * ``script``: An object wrapping the script's type (``gcode``) and name (e.g. ``afterPrintCancelled``) as ``script.type`` and ``script.name`` respectively. + * ``plugins``: An object containing variables provided by plugins (e.g ``plugins.myplugin.myvariable``) There are a few additional template variables available for the following specific scripts: diff --git a/docs/plugins/hooks.rst b/docs/plugins/hooks.rst index 75c255da6..1a320573d 100644 --- a/docs/plugins/hooks.rst +++ b/docs/plugins/hooks.rst @@ -559,19 +559,19 @@ octoprint.comm.protocol.scripts .. py:function:: protocol_scripts_hook(comm_instance, script_type, script_name, *args, **kwargs) - Return a prefix to prepend and a postfix to append to the script ``script_name`` of type ``type``. Handlers should + Return a prefix to prepend, postfix to append, and optionally a dictionary of variables to provide to the script ``script_name`` of type ``type``. Handlers should make sure to only proceed with returning additional scripts if the ``script_type`` and ``script_name`` match handled scripts. If not, None should be returned directly. - If the hook handler has something to add to the specified script, it may return a 2-tuple, with the first entry - defining the prefix (what to *prepend* to the script in question) and the last entry defining the postfix (what to - *append* to the script in question). Both prefix and postfix can be None to signify that nothing should be prepended + If the hook handler has something to add to the specified script, it may return a 2-tuple or a 3-tuple with the first entry + defining the prefix (what to *prepend* to the script in question), the second entry defining the postfix (what to + *append* to the script in question), and finally if desired a dictionary of variables to be made available to the script. Both prefix and postfix can be None to signify that nothing should be prepended respectively appended. - The returned entries may be either iterables of script lines or a string including newlines of the script lines (which + The returned prefix and postfix entries may be either iterables of script lines or a string including newlines of the script lines (which will be split by the caller if necessary). - **Example:** + **Example 1:** Appends an ``M117 OctoPrint connected`` to the configured ``afterPrinterConnected`` GCODE script. @@ -580,10 +580,19 @@ octoprint.comm.protocol.scripts :tab-width: 4 :caption: `message_on_connect.py `_ + **Example 2:** + + Provides the variable ``myvariable`` to the configured ``beforePrintStarted`` GCODE script. + + .. onlineinclude:: https://raw.githubusercontent.com/OctoPrint/Plugin-Examples/master/gcode_script_variables.py + :linenos: + :tab-width: 4 + :caption: `gcode_script_variables.py `_ + :param MachineCom comm_instance: The :class:`~octoprint.util.comm.MachineCom` instance which triggered the hook. :param str script_type: The type of the script for which the hook was called, currently only "gcode" is supported here. :param str script_name: The name of the script for which the hook was called. - :return: A 2-tuple in the form ``(prefix, postfix)`` or None + :return: A 2-tuple in the form ``(prefix, postfix)``, 3-tuple in the form ``(prefix, postfix, variables)``, or None :rtype: tuple or None .. _sec-plugins-hook-comm-protocol-temperatures-received: diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 09a3834fa..6bebec3d5 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -797,21 +797,18 @@ class MachineCom(object): context.update(dict(cancel_position=self.cancel_position, cancel_temperature=self.cancel_temperature.as_script_dict())) - template = settings().loadScript("gcode", scriptName, context=context) - if template is None: - scriptLines = [] - else: - scriptLines = template.split("\n") + scriptLinesPrefix = [] + scriptLinesSuffix = [] - for hook in self._gcodescript_hooks: + for name, hook in self._gcodescript_hooks.items(): try: - retval = self._gcodescript_hooks[hook](self, "gcode", scriptName) + retval = hook(self, "gcode", scriptName) except: - self._logger.exception("Error while processing gcodescript hook %s" % hook) + self._logger.exception("Error while processing hook {name}.".format(**locals())) else: if retval is None: continue - if not isinstance(retval, (list, tuple)) or not len(retval) == 2: + if not isinstance(retval, (list, tuple)) or not len(retval) in [2, 3]: continue def to_list(data): @@ -825,11 +822,23 @@ class MachineCom(object): else: return None - prefix, suffix = map(to_list, retval) + prefix, suffix = map(to_list, retval[0:2]) if prefix: - scriptLines = list(prefix) + scriptLines + scriptLinesPrefix = list(prefix) + scriptLinesPrefix if suffix: - scriptLines += list(suffix) + scriptLinesSuffix += list(suffix) + + if len(retval) == 3: + variables = retval[2] + context.update(dict(plugins={name:variables})) + + template = settings().loadScript("gcode", scriptName, context=context) + if template is None: + scriptLines = [] + else: + scriptLines = template.split("\n") + + scriptLines = scriptLinesPrefix + scriptLines + scriptLinesSuffix return filter(lambda x: x is not None and x.strip() != "", map(lambda x: process_gcode_line(x, offsets=self._tempOffsets, current_tool=self._currentTool),