diff --git a/continuousprint/plugin.py b/continuousprint/plugin.py index a4a3fb40..3f2d9990 100644 --- a/continuousprint/plugin.py +++ b/continuousprint/plugin.py @@ -8,6 +8,7 @@ import traceback import random from pathlib import Path +from octoprint import __version__ as octoprint_version from octoprint.events import Events from octoprint.filemanager import NoSuchStorage from octoprint.filemanager.analysis import AnalysisQueue, QueueEntry @@ -113,6 +114,10 @@ def _get_key(self, k, default=None): v = self._settings.get([k.setting]) return v if v is not None else default + def _octoprint_version_exceeds(self, major: int, minor: int): + cur_major, cur_minor = [int(c) for c in octoprint_version.split(".")[:2]] + return cur_major > major or (cur_major == major and cur_minor > minor) + def _add_folder(self, path): return self._file_manager.add_folder( FileDestinations.LOCAL, self._path_in_storage(path) @@ -667,9 +672,24 @@ def on_event(self, event, payload): self._logger.debug(f"Enqueued newly added file {payload['path']}") return - if ( - event == Events.UPLOAD - ): # https://docs.octoprint.org/en/master/events/index.html#file-handling + # Events.UPLOAD only applies to REST file upload calls. + # This ignores other file addition events from plugins + # and the file watcher. + # https://github.com/OctoPrint/OctoPrint/pull/4687 adds the + # `operation`attribute to Events.FILE_ADDED and eliminates the + # need for Events.UPLOAD to discriminate adding/copying/moving files. + file_uploaded = False + version_after_1_8 = self._octoprint_version_exceeds(1, 8) + if not version_after_1_8 and event == Events.UPLOAD: + file_uploaded = True + elif ( + version_after_1_8 + and event == Events.FILE_ADDED + and payload["operation"] == "add" + ): + file_uploaded = True + + if file_uploaded: upload_action = self._get_key(Keys.UPLOAD_ACTION, "do_nothing") if upload_action != "do_nothing": if payload["path"].endswith(".gcode"): diff --git a/continuousprint/plugin_test.py b/continuousprint/plugin_test.py index 5db89cb6..d7f2c85f 100644 --- a/continuousprint/plugin_test.py +++ b/continuousprint/plugin_test.py @@ -250,6 +250,7 @@ def setUp(self): self.p.q = MagicMock() self.p._sync_state = MagicMock() self.p._plugin_manager.plugins.get.return_value = None + self.p._octoprint_version_exceeds = lambda a, b: False self.p._setup_thirdparty_plugin_integration() def testTick(self): @@ -427,6 +428,16 @@ def testFileAddedWithNoAnalysis(self): self.p.on_event(Events.FILE_ADDED, dict(path="a.gcode")) self.p._analysis_queue.enqueue.assert_called() + def testFileAddedWithOperationPrintable(self): + self.p._octoprint_version_exceeds = lambda a, b: True + self.p._set_key(Keys.UPLOAD_ACTION, "add_printable") + self.p._add_set = MagicMock() + self.p.on_event( + Events.FILE_ADDED, + dict(path="testpath.gcode", storage="local", operation="add"), + ) + self.p._add_set.assert_called_with(draft=False, sd=False, path="testpath.gcode") + class TestGetters(unittest.TestCase): def setUp(self):