From e1ac1fa9834e800a53918e875593103115bdbfe0 Mon Sep 17 00:00:00 2001 From: Levi Blaney Date: Thu, 24 Dec 2020 05:36:58 -0500 Subject: [PATCH 1/6] send events for gcode scripts --- src/octoprint/printer/standard.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/octoprint/printer/standard.py b/src/octoprint/printer/standard.py index a396362bc..2adb0c0cb 100644 --- a/src/octoprint/printer/standard.py +++ b/src/octoprint/printer/standard.py @@ -444,6 +444,12 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): if name is None or not name: raise ValueError("name must be set") + event_name = name[0].upper() + name[1:] + event_start = "{}{}".format(event_name, "GcodeRunning") + payload = context.get("event", None) if isinstance(context, dict) else None + + eventManager().fire(event_start, payload) + result = self._comm.sendGcodeScript( name, part_of_job=part_of_job, @@ -453,6 +459,9 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): if not result and must_be_set: raise UnknownScript(name) + event_end = "{}{}".format(event_name, "GcodeFinished") + eventManager().fire(event_end, payload) + def jog(self, axes, relative=True, speed=None, *args, **kwargs): if isinstance(axes, basestring): # legacy parameter format, there should be an amount as first anonymous positional arguments too From cd9aab7eef69a0abad711239a9d99fa4d19329db Mon Sep 17 00:00:00 2001 From: Levi Blaney Date: Tue, 5 Jan 2021 09:13:59 -0500 Subject: [PATCH 2/6] update docs with dynamic gcode events --- docs/events/index.rst | 28 ++++++++++++++++++++++++++++ docs/features/gcode_scripts.rst | 9 +++++++++ 2 files changed, 37 insertions(+) diff --git a/docs/events/index.rst b/docs/events/index.rst index 7fa5e733e..5407a4935 100644 --- a/docs/events/index.rst +++ b/docs/events/index.rst @@ -539,6 +539,34 @@ PrintResumed .. versionchanged:: 1.4.0 +${ScriptName}Running + A custom :ref:`GCODE script ` has started running. + + Payload: + + * ``name``: the file's name + * ``path``: the file's path within its storage location + * ``origin``: the origin storage location of the file, either ``local`` or ``sdcard`` + * ``size``: the file's size in bytes (if available) + * ``owner``: the user who started the print job (if available) + * ``time``: the time needed for the print, in seconds (float) + + .. versionadded:: X.X.X + +${ScriptName}Finished + A custom :ref:`GCODE script ` has finished running. + + Payload: + + * ``name``: the file's name + * ``path``: the file's path within its storage location + * ``origin``: the origin storage location of the file, either ``local`` or ``sdcard`` + * ``size``: the file's size in bytes (if available) + * ``owner``: the user who started the print job (if available) + * ``time``: the time needed for the print, in seconds (float) + + .. versionadded:: X.X.X + .. _sec-events-available_events-gcode_processing: GCODE processing diff --git a/docs/features/gcode_scripts.rst b/docs/features/gcode_scripts.rst index dbd84ef0d..a1c0d6167 100644 --- a/docs/features/gcode_scripts.rst +++ b/docs/features/gcode_scripts.rst @@ -43,6 +43,15 @@ The following GCODE scripts are sent by OctoPrint automatically: Plugins may extend these scripts through :ref:`a hook `. +.. _sec-features-gcode_scripts-events: + +Events +------ + +Every GCODE script that is executed will emit two events. The events will start with the capitalized name of the script. +When ``afterPrintDone`` has started the event will be ``AfterPrintDoneRunning`` and once it has completed the last event +will be ``AfterPrintDoneFinished``. You can find more details in the :ref:`Events ` documentation. + .. _sec-features-gcode_scripts-snippets: Snippets From 2474653a7f61c2e30d040894380bdafeb4492f30 Mon Sep 17 00:00:00 2001 From: Levi Blaney Date: Mon, 18 Jan 2021 09:16:24 -0500 Subject: [PATCH 3/6] fix name scheme for gcode script events --- docs/events/index.rst | 8 ++++---- docs/features/gcode_scripts.rst | 6 +++--- src/octoprint/printer/standard.py | 7 +++++-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/docs/events/index.rst b/docs/events/index.rst index 5407a4935..71a55d68e 100644 --- a/docs/events/index.rst +++ b/docs/events/index.rst @@ -539,7 +539,7 @@ PrintResumed .. versionchanged:: 1.4.0 -${ScriptName}Running +GcodeScript${ScriptName}Running A custom :ref:`GCODE script ` has started running. Payload: @@ -551,9 +551,9 @@ ${ScriptName}Running * ``owner``: the user who started the print job (if available) * ``time``: the time needed for the print, in seconds (float) - .. versionadded:: X.X.X + .. versionadded:: 1.6.0 -${ScriptName}Finished +GcodeScript${ScriptName}Finished A custom :ref:`GCODE script ` has finished running. Payload: @@ -565,7 +565,7 @@ ${ScriptName}Finished * ``owner``: the user who started the print job (if available) * ``time``: the time needed for the print, in seconds (float) - .. versionadded:: X.X.X + .. versionadded:: 1.6.0 .. _sec-events-available_events-gcode_processing: diff --git a/docs/features/gcode_scripts.rst b/docs/features/gcode_scripts.rst index a1c0d6167..d836d3fad 100644 --- a/docs/features/gcode_scripts.rst +++ b/docs/features/gcode_scripts.rst @@ -48,9 +48,9 @@ The following GCODE scripts are sent by OctoPrint automatically: Events ------ -Every GCODE script that is executed will emit two events. The events will start with the capitalized name of the script. -When ``afterPrintDone`` has started the event will be ``AfterPrintDoneRunning`` and once it has completed the last event -will be ``AfterPrintDoneFinished``. You can find more details in the :ref:`Events ` documentation. +Every GCODE script that is executed will emit two events. The event name will start with 'GcodeScript' followed by the capitalized name +of the script. When ``afterPrintDone`` has started the event will be ``GcodeScriptAfterPrintDoneRunning`` and once it has completed the last event +will be ``GcodeScriptAfterPrintDoneFinished``. You can find more details in the :ref:`Events ` documentation. .. _sec-features-gcode_scripts-snippets: diff --git a/src/octoprint/printer/standard.py b/src/octoprint/printer/standard.py index 2adb0c0cb..d971c0128 100644 --- a/src/octoprint/printer/standard.py +++ b/src/octoprint/printer/standard.py @@ -444,8 +444,11 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): if name is None or not name: raise ValueError("name must be set") + # .capitalize() will lowercase all letters but the first + # this code preserves existing CamelCase event_name = name[0].upper() + name[1:] - event_start = "{}{}".format(event_name, "GcodeRunning") + + event_start = "GcodeScript{}Running".format(event_name) payload = context.get("event", None) if isinstance(context, dict) else None eventManager().fire(event_start, payload) @@ -459,7 +462,7 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): if not result and must_be_set: raise UnknownScript(name) - event_end = "{}{}".format(event_name, "GcodeFinished") + event_end = "GcodeScript{}Finished".format(event_name) eventManager().fire(event_end, payload) def jog(self, axes, relative=True, speed=None, *args, **kwargs): From 850ec59694d22775e9104f2c93f37caf17e599d0 Mon Sep 17 00:00:00 2001 From: Levi Blaney Date: Mon, 18 Jan 2021 09:20:44 -0500 Subject: [PATCH 4/6] update authors list --- AUTHORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.md b/AUTHORS.md index f7477841b..b30ca9644 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -133,6 +133,7 @@ date of first contribution): * ["coliss86"](https://github.com/coliss86) * ["MichaIng"](https://github.com/MichaIng) * ["jasonbcox"](https://github.com/jasonbcox) + * ["shadycuz"](https://github.com/shadycuz) 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 From 1be8c23069bf5d72c947d279a37b182d72314a64 Mon Sep 17 00:00:00 2001 From: Levi Blaney Date: Mon, 18 Jan 2021 10:21:57 -0500 Subject: [PATCH 5/6] add tests for gcode script events --- tests/printer/test_script.py | 77 ++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 tests/printer/test_script.py diff --git a/tests/printer/test_script.py b/tests/printer/test_script.py new file mode 100644 index 000000000..58d250bf3 --- /dev/null +++ b/tests/printer/test_script.py @@ -0,0 +1,77 @@ +__author__ = "Gina Häußge " +__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html" +__copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms of the AGPLv3 License" + +import unittest +from unittest.mock import ANY, MagicMock, call, patch + +from octoprint.printer.standard import Printer + + +class ScriptsTestCase(unittest.TestCase): + def setUp(self): + # mock comm + self.comm = MagicMock() + + # mock event manager + self.event_manager = MagicMock() + + self.event_manager_patcher = patch("octoprint.printer.standard.eventManager") + self.event_manager_getter = self.event_manager_patcher.start() + self.event_manager_getter.return_value = self.event_manager + + # mock plugin manager + self.plugin_manager = MagicMock() + self.plugin_manager.get_hooks.return_value = {} + + self.plugin_manager_patcher = patch("octoprint.printer.standard.plugin_manager") + self.plugin_manager_getter = self.plugin_manager_patcher.start() + self.plugin_manager_getter.return_value = self.plugin_manager + + # mock settings + self.settings = MagicMock() + self.settings.getInt.return_value = 1 + self.settings.getBoolean.return_value = False + + self.settings_patcher = patch("octoprint.printer.standard.settings") + self.settings_getter = self.settings_patcher.start() + self.settings_getter.return_value = self.settings + + self.printer = Printer(MagicMock(), MagicMock(), MagicMock()) + self.printer._comm = self.comm + + def tearDown(self): + self.settings_patcher.stop() + self.plugin_manager_patcher.stop() + self.event_manager_patcher.stop() + + def test_event_name(self): + self.printer.script("testEvent") + + self.event_manager.fire.assert_any_call("GcodeScriptTestEventRunning", ANY) + + def test_event_order(self): + self.printer.script("EventOrder") + + expected_order = [ + call("GcodeScriptEventOrderRunning", ANY), + call("GcodeScriptEventOrderFinished", ANY), + ] + + self.event_manager.fire.assert_has_calls(expected_order) + + def test_payload_handling(self): + expected_paylod = {"payloadKey": "payloadValue"} + context = {"event": expected_paylod} + + self.printer.script("GetPayload", context) + + self.event_manager.fire.assert_called_with(ANY, expected_paylod) + + self.printer.script("WrongPayload", []) + + self.event_manager.fire.assert_called_with(ANY, None) + + self.printer.script("WrongContext", {"noEvent": {}}) + + self.event_manager.fire.assert_called_with(ANY, None) From 20c0833cbc4b0190cf608e55c21b264d42d5a23e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 18 Jan 2021 16:52:33 +0100 Subject: [PATCH 6/6] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20Fix=20file=20header?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/printer/test_script.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/printer/test_script.py b/tests/printer/test_script.py index 58d250bf3..57a0fefb4 100644 --- a/tests/printer/test_script.py +++ b/tests/printer/test_script.py @@ -1,6 +1,5 @@ -__author__ = "Gina Häußge " __license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html" -__copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms of the AGPLv3 License" +__copyright__ = "Copyright (C) 2021 The OctoPrint Project - Released under terms of the AGPLv3 License" import unittest from unittest.mock import ANY, MagicMock, call, patch