From 9aeeb5fecc565678d37a12ed5c33c7d15c0ac5d8 Mon Sep 17 00:00:00 2001 From: Ryan Finnie Date: Wed, 24 Apr 2019 09:11:42 -0700 Subject: [PATCH] Allow subscriptions of multiple events events > subscriptions[] > event may be either a string (existing functionality) or now a list of strings. If a list, the subscriptions are treated the same for each event in the list. To make it possible for command subscriptions to determine which event was triggered, a {__eventname} substitution has been added. --- AUTHORS.md | 1 + docs/events/index.rst | 8 ++++++++ src/octoprint/events.py | 27 ++++++++++++++++++--------- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/AUTHORS.md b/AUTHORS.md index ae9b33cf7..0f846d8f2 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -99,6 +99,7 @@ date of first contribution): * [Daniel Joyce](https://github.com/DanielJoyce) * [Andy Qua](https://github.com/AndyQ) * [Fabio Santos](https://github.com/Fabi0San) + * [Ryan Finnie](https://github.com/rfinnie) OctoPrint started off as a fork of [Cura](https://github.com/daid/Cura) by [Daid Braam](https://github.com/daid). Parts of its communication layer and diff --git a/docs/events/index.rst b/docs/events/index.rst index 108cfc20b..eeaac5869 100644 --- a/docs/events/index.rst +++ b/docs/events/index.rst @@ -38,6 +38,13 @@ Example - event: PrintDone command: python ~/growl.py -t mygrowlserver -d "Completed {file}" -a OctoPrint -i http://raspi/Octoprint_logo.png type: system + - event: + - PrintStarted + - PrintFailed + - PrintDone + - PrintCancelled + command: python ~/growl.py -t mygrowlserver -d "Event {__eventname} ({name})" -a OctoPrint -i http://raspi/Octoprint_logo.png + type: system - event: Connected command: - M115 @@ -53,6 +60,7 @@ Placeholders You can use the following generic placeholders in your event hooks: * ``{__currentZ}``: the current Z position of the head if known, -1 if not available + * ``{__eventname}`` : the name of the event hook being triggered * ``{__filename}`` : name of currently selected file, or ``NO FILE`` if no file is selected * ``{__filepath}`` : path in origin location of currently selected file, or ``NO FILE`` if no file is selected * ``{__fileorigin}`` : origin of currently selected file, or ``NO FILE`` if no file is selected diff --git a/src/octoprint/events.py b/src/octoprint/events.py index 74374f7ec..dc5478cd6 100644 --- a/src/octoprint/events.py +++ b/src/octoprint/events.py @@ -343,17 +343,24 @@ class CommandTrigger(GenericEventListener): self._logger.info("Disabled command trigger: {!r}".format(subscription)) continue - event = subscription["event"] + events = subscription["event"] command = subscription["command"] commandType = subscription["type"] debug = subscription["debug"] if "debug" in subscription else False - if not event in self._subscriptions.keys(): - self._subscriptions[event] = [] - self._subscriptions[event].append((command, commandType, debug)) + # "event" in the configuration can be a string, or + # a list of strings. If it's the former, convert it + # into the latter. + if not isinstance(events, (tuple, list, set)): + events = [events] - if not event in eventsToSubscribe: - eventsToSubscribe.append(event) + for event in events: + if not event in self._subscriptions.keys(): + self._subscriptions[event] = [] + self._subscriptions[event].append((command, commandType, debug)) + + if not event in eventsToSubscribe: + eventsToSubscribe.append(event) self.subscribe(eventsToSubscribe) @@ -373,9 +380,9 @@ class CommandTrigger(GenericEventListener): if isinstance(command, (tuple, list, set)): processedCommand = [] for c in command: - processedCommand.append(self._processCommand(c, payload)) + processedCommand.append(self._processCommand(c, event, payload)) else: - processedCommand = self._processCommand(command, payload) + processedCommand = self._processCommand(command, event, payload) self.executeCommand(processedCommand, commandType, debug=debug) except KeyError as e: self._logger.warn("There was an error processing one or more placeholders in the following command: %s" % command) @@ -424,13 +431,14 @@ class CommandTrigger(GenericEventListener): self._logger.info("Executing GCode commands: %r" % command) self._printer.commands(commands) - def _processCommand(self, command, payload): + def _processCommand(self, command, event, payload): """ Performs string substitutions in the command string based on a few current parameters. The following substitutions are currently supported: - {__currentZ} : current Z position of the print head, or -1 if not available + - {__eventname} : the name of the event hook being triggered - {__filename} : name of currently selected file, or "NO FILE" if no file is selected - {__filepath} : path in origin location of currently selected file, or "NO FILE" if no file is selected - {__fileorigin} : origin of currently selected file, or "NO FILE" if no file is selected @@ -452,6 +460,7 @@ class CommandTrigger(GenericEventListener): params = { "__currentZ": "-1", + "__eventname": event, "__filename": "NO FILE", "__filepath": "NO PATH", "__progress": "0",