Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions continuousprint/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"):
Expand Down
11 changes: 11 additions & 0 deletions continuousprint/plugin_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down