Ensure that post processing scripts are correctly reloaded on project loading

Previously it would not re load post processing plugins if it was updating an existing machine

Fixes #6881
This commit is contained in:
Jaime van Kessel 2020-01-07 10:45:57 +01:00
parent 2a09404235
commit 523b4e96a8
No known key found for this signature in database
GPG key ID: 3710727397403C91
2 changed files with 39 additions and 17 deletions

View file

@ -1005,7 +1005,6 @@ class ThreeMFWorkspaceReader(WorkspaceReader):
# Set metadata fields that are missing from the global stack # Set metadata fields that are missing from the global stack
for key, value in self._machine_info.metadata_dict.items(): for key, value in self._machine_info.metadata_dict.items():
if key not in global_stack.getMetaData():
global_stack.setMetaDataEntry(key, value) global_stack.setMetaDataEntry(key, value)
def _updateActiveMachine(self, global_stack): def _updateActiveMachine(self, global_stack):

View file

@ -44,6 +44,9 @@ class PostProcessingPlugin(QObject, Extension):
# There can be duplicates, which will be executed in sequence. # There can be duplicates, which will be executed in sequence.
self._script_list = [] # type: List[Script] self._script_list = [] # type: List[Script]
self._selected_script_index = -1 self._selected_script_index = -1
self._global_container_stack = Application.getInstance().getGlobalContainerStack()
if self._global_container_stack:
self._global_container_stack.metaDataChanged.connect(self._restoreScriptInforFromMetadata)
Application.getInstance().getOutputDeviceManager().writeStarted.connect(self.execute) Application.getInstance().getOutputDeviceManager().writeStarted.connect(self.execute)
Application.getInstance().globalContainerStackChanged.connect(self._onGlobalContainerStackChanged) # When the current printer changes, update the list of scripts. Application.getInstance().globalContainerStackChanged.connect(self._onGlobalContainerStackChanged) # When the current printer changes, update the list of scripts.
@ -209,11 +212,9 @@ class PostProcessingPlugin(QObject, Extension):
self.scriptListChanged.emit() self.scriptListChanged.emit()
self._propertyChanged() self._propertyChanged()
## When the global container stack is changed, swap out the list of active def _restoreScriptInforFromMetadata(self):
# scripts.
def _onGlobalContainerStackChanged(self) -> None:
self.loadAllScripts() self.loadAllScripts()
new_stack = Application.getInstance().getGlobalContainerStack() new_stack = self._global_container_stack
if new_stack is None: if new_stack is None:
return return
self._script_list.clear() self._script_list.clear()
@ -224,7 +225,8 @@ class PostProcessingPlugin(QObject, Extension):
self._script_list.clear() self._script_list.clear()
scripts_list_strs = new_stack.getMetaDataEntry("post_processing_scripts") scripts_list_strs = new_stack.getMetaDataEntry("post_processing_scripts")
for script_str in scripts_list_strs.split("\n"): # Encoded config files should never contain three newlines in a row. At most 2, just before section headers. for script_str in scripts_list_strs.split(
"\n"): # Encoded config files should never contain three newlines in a row. At most 2, just before section headers.
if not script_str: # There were no scripts in this one (or a corrupt file caused more than 3 consecutive newlines here). if not script_str: # There were no scripts in this one (or a corrupt file caused more than 3 consecutive newlines here).
continue continue
script_str = script_str.replace(r"\\\n", "\n").replace(r"\\\\", "\\\\") # Unescape escape sequences. script_str = script_str.replace(r"\\\n", "\n").replace(r"\\\\", "\\\\") # Unescape escape sequences.
@ -235,7 +237,9 @@ class PostProcessingPlugin(QObject, Extension):
if script_name == "DEFAULT": # ConfigParser always has a DEFAULT section, but we don't fill it. Ignore this one. if script_name == "DEFAULT": # ConfigParser always has a DEFAULT section, but we don't fill it. Ignore this one.
continue continue
if script_name not in self._loaded_scripts: # Don't know this post-processing plug-in. if script_name not in self._loaded_scripts: # Don't know this post-processing plug-in.
Logger.log("e", "Unknown post-processing script {script_name} was encountered in this global stack.".format(script_name = script_name)) Logger.log("e",
"Unknown post-processing script {script_name} was encountered in this global stack.".format(
script_name=script_name))
continue continue
new_script = self._loaded_scripts[script_name]() new_script = self._loaded_scripts[script_name]()
new_script.initialize() new_script.initialize()
@ -245,7 +249,22 @@ class PostProcessingPlugin(QObject, Extension):
self._script_list.append(new_script) self._script_list.append(new_script)
self.setSelectedScriptIndex(0) self.setSelectedScriptIndex(0)
# Ensure that we always force an update (otherwise the fields don't update correctly!)
self.selectedIndexChanged.emit()
self.scriptListChanged.emit() self.scriptListChanged.emit()
self._propertyChanged()
## When the global container stack is changed, swap out the list of active
# scripts.
def _onGlobalContainerStackChanged(self) -> None:
if self._global_container_stack:
self._global_container_stack.metaDataChanged.disconnect(self._restoreScriptInforFromMetadata)
self._global_container_stack = Application.getInstance().getGlobalContainerStack()
if self._global_container_stack:
self._global_container_stack.metaDataChanged.connect(self._restoreScriptInforFromMetadata)
self._restoreScriptInforFromMetadata()
@pyqtSlot() @pyqtSlot()
def writeScriptsToStack(self) -> None: def writeScriptsToStack(self) -> None:
@ -267,14 +286,18 @@ class PostProcessingPlugin(QObject, Extension):
script_list_string = "\n".join(script_list_strs) # ConfigParser should never output three newlines in a row when serialised, so it's a safe delimiter. script_list_string = "\n".join(script_list_strs) # ConfigParser should never output three newlines in a row when serialised, so it's a safe delimiter.
global_stack = Application.getInstance().getGlobalContainerStack() if self._global_container_stack is None:
if global_stack is None:
return return
if "post_processing_scripts" not in global_stack.getMetaData(): # Ensure we don't get triggered by our own write.
global_stack.setMetaDataEntry("post_processing_scripts", "") self._global_container_stack.metaDataChanged.disconnect(self._restoreScriptInforFromMetadata)
global_stack.setMetaDataEntry("post_processing_scripts", script_list_string) if "post_processing_scripts" not in self._global_container_stack.getMetaData():
self._global_container_stack.setMetaDataEntry("post_processing_scripts", "")
self._global_container_stack.setMetaDataEntry("post_processing_scripts", script_list_string)
# We do want to listen to other events.
self._global_container_stack.metaDataChanged.connect(self._restoreScriptInforFromMetadata)
## Creates the view used by show popup. The view is saved because of the fairly aggressive garbage collection. ## Creates the view used by show popup. The view is saved because of the fairly aggressive garbage collection.
def _createView(self) -> None: def _createView(self) -> None: