diff --git a/src/octoprint/filemanager/storage.py b/src/octoprint/filemanager/storage.py index 54d13a434..b2cf61425 100644 --- a/src/octoprint/filemanager/storage.py +++ b/src/octoprint/filemanager/storage.py @@ -9,9 +9,12 @@ __copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms import logging import os import pylru +import tempfile import octoprint.filemanager +from octoprint.util import safeRename + class StorageInterface(object): @@ -867,12 +870,16 @@ class LocalFileStorage(StorageInterface): def _save_metadata(self, path, metadata): metadata_path = os.path.join(path, ".metadata.yaml") + fh, metadata_temporary_path = tempfile.mkstemp() + os.close(fh) + with self._metadata_lock: - with open(metadata_path, "w") as f: - try: + try: + with open(metadata_temporary_path, "w") as f: import yaml yaml.safe_dump(metadata, stream=f, default_flow_style=False, indent=" ", allow_unicode=True) - except: - self._logger.exception("Error while writing .metadata.yaml to {path}".format(**locals())) - else: - self._metadata_cache[path] = metadata + safeRename(metadata_temporary_path, metadata_path, throw_error=True) + except: + self._logger.exception("Error while writing .metadata.yaml to {path}".format(**locals())) + else: + self._metadata_cache[path] = metadata diff --git a/src/octoprint/printer/__init__.py b/src/octoprint/printer/__init__.py index 8c3403717..929c5dedb 100644 --- a/src/octoprint/printer/__init__.py +++ b/src/octoprint/printer/__init__.py @@ -705,6 +705,9 @@ class Printer(): def mcReceivedRegisteredMessage(self, command, output): self._sendFeedbackCommandOutput(command, output) + def mcForceDisconnect(self): + self.disconnect() + #~~ sd file handling def getSdFiles(self): diff --git a/src/octoprint/util/__init__.py b/src/octoprint/util/__init__.py index 59adf6076..3972f5fcd 100644 --- a/src/octoprint/util/__init__.py +++ b/src/octoprint/util/__init__.py @@ -148,7 +148,7 @@ def findCollisionfreeName(input, extension, existingFilenames): raise ValueError("Can't create a collision free filename") -def safeRename(old, new): +def safeRename(old, new, throw_error=False): """ Safely renames a file. @@ -173,12 +173,14 @@ def safeRename(old, new): os.rename(new, backup) os.rename(old, new) os.remove(backup) - except OSError: + except OSError as e: # if anything went wrong, try to rename the backup file to its original name logger.error("Could not perform safe rename, trying to revert") if os.path.exists(backup): - os.remove(new) - os.rename(backup, new) + silentRemove(new) + os.rename(backup, new) + if throw_error: + raise e else: # on anything else than windows it's ooooh so much easier... os.rename(old, new) diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 2bbfd2084..bcdcd0172 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -161,6 +161,7 @@ class MachineCom(object): # hooks self._pluginManager = octoprint.plugin.plugin_manager() self._gcode_hooks = self._pluginManager.get_hooks("octoprint.comm.protocol.gcode") + self._printer_action_hooks = self._pluginManager.get_hooks("octoprint.comm.protocol.action") # SD status data self._sdAvailable = False @@ -655,6 +656,27 @@ class MachineCom(object): if line.strip() is not "": self._timeout = getNewTimeout("communication") + ##~~ debugging output handling + if line.startswith("//"): + debugging_output = line[2:].strip() + if debugging_output.startswith("action:"): + action_command = debugging_output[len("action:"):].strip() + + if action_command == "pause": + self._log("Pausing on request of the printer...") + self.setPause(True) + elif action_command == "resume": + self._log("Resuming on request of the printer...") + self.setPause(False) + elif action_command == "disconnect": + self._log("Disconnecting on request of the printer...") + self._callback.mcForceDisconnect() + else: + for hook in self._printer_action_hooks: + self._printer_action_hooks[hook](self, line, action_command) + else: + continue + ##~~ Error handling line = self._handleErrors(line) @@ -1296,6 +1318,9 @@ class MachineComPrintCallback(object): def mcReceivedRegisteredMessage(self, command, message): pass + def mcForceDisconnect(self): + pass + ### Printing file information classes ################################################################################## class PrintingFileInformation(object): diff --git a/src/octoprint/util/virtual.py b/src/octoprint/util/virtual.py index 2983d5a78..6c1b6978d 100644 --- a/src/octoprint/util/virtual.py +++ b/src/octoprint/util/virtual.py @@ -227,6 +227,8 @@ class VirtualPrinter(): self._relative = True elif "G92" in data: self._setPosition(data) + elif data.startswith("!!DEBUG:"): + self._debugTrigger(data[len("!!DEBUG:"):].strip()) elif data.startswith("G0") or data.startswith("G1") or data.startswith("G2") or data.startswith("G3") \ or data.startswith("G28") or data.startswith("G29") or data.startswith("G30") \ @@ -237,6 +239,14 @@ class VirtualPrinter(): if len(data.strip()) > 0: self._sendOk() + def _debugTrigger(self, data): + if data == "action_pause": + self.outgoing.put("// action:pause") + elif data == "action_resume": + self.outgoing.put("// action:resume") + elif data == "action_disconnect": + self.outgoing.put("// action:disconnect") + def _listSd(self): self.outgoing.put("Begin file list") if settings().getBoolean(["devel", "virtualPrinter", "extendedSdFileList"]):