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 diff --git a/docs/events/index.rst b/docs/events/index.rst index 7fa5e733e..71a55d68e 100644 --- a/docs/events/index.rst +++ b/docs/events/index.rst @@ -539,6 +539,34 @@ PrintResumed .. versionchanged:: 1.4.0 +GcodeScript${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:: 1.6.0 + +GcodeScript${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:: 1.6.0 + .. _sec-events-available_events-gcode_processing: GCODE processing diff --git a/docs/features/gcode_scripts.rst b/docs/features/gcode_scripts.rst index dbd84ef0d..d836d3fad 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 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: Snippets diff --git a/src/octoprint/printer/standard.py b/src/octoprint/printer/standard.py index a396362bc..d971c0128 100644 --- a/src/octoprint/printer/standard.py +++ b/src/octoprint/printer/standard.py @@ -444,6 +444,15 @@ 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 = "GcodeScript{}Running".format(event_name) + 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 +462,9 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): if not result and must_be_set: raise UnknownScript(name) + event_end = "GcodeScript{}Finished".format(event_name) + 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 diff --git a/tests/printer/test_script.py b/tests/printer/test_script.py new file mode 100644 index 000000000..57a0fefb4 --- /dev/null +++ b/tests/printer/test_script.py @@ -0,0 +1,76 @@ +__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html" +__copyright__ = "Copyright (C) 2021 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)