From 414b696b32cb9e3f5e5a326bdb33b7f7ac573dd8 Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Tue, 14 Jan 2020 11:04:42 +0100 Subject: [PATCH 01/10] Use the resetWorkspace for new project This makes it a bit cleaner as we don't have to copy functionality CURA-6627 --- resources/qml/MainWindow/ApplicationMenu.qml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/qml/MainWindow/ApplicationMenu.qml b/resources/qml/MainWindow/ApplicationMenu.qml index 30e44d7d3b..72a371e2f5 100644 --- a/resources/qml/MainWindow/ApplicationMenu.qml +++ b/resources/qml/MainWindow/ApplicationMenu.qml @@ -127,8 +127,8 @@ Item icon: StandardIcon.Question onYes: { - CuraApplication.deleteAll(); - Cura.Actions.resetProfile.trigger(); + CuraApplication.resetWorkspace() + Cura.Actions.resetProfile.trigger() UM.Controller.setActiveStage("PrepareStage") } } From b6d1429eb779b4724ea186d7d9eaee75e99054c3 Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Tue, 14 Jan 2020 15:27:30 +0100 Subject: [PATCH 02/10] Ensure that 3mf workspace reader loads aditional metadata CURA-6627 --- plugins/3MFReader/ThreeMFWorkspaceReader.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/plugins/3MFReader/ThreeMFWorkspaceReader.py b/plugins/3MFReader/ThreeMFWorkspaceReader.py index 2e395c9efa..953980d0b7 100755 --- a/plugins/3MFReader/ThreeMFWorkspaceReader.py +++ b/plugins/3MFReader/ThreeMFWorkspaceReader.py @@ -4,7 +4,7 @@ from configparser import ConfigParser import zipfile import os -from typing import cast, Dict, List, Optional, Tuple +from typing import cast, Dict, List, Optional, Tuple, Any import xml.etree.ElementTree as ET @@ -732,7 +732,17 @@ class ThreeMFWorkspaceReader(WorkspaceReader): base_file_name = os.path.basename(file_name) self.setWorkspaceName(base_file_name) - return nodes + + return nodes, self._loadMetadata(file_name) + + def _loadMetadata(self, file_name) -> Dict[str, Dict[str, Any]]: + archive = zipfile.ZipFile(file_name, "r") + import json + try: + return json.loads(archive.open("Cura/plugin_metadata.json").read().decode("utf-8")) + except KeyError: + # The plugin_metadata.json file doesn't exist + return dict() def _processQualityChanges(self, global_stack): if self._machine_info.quality_changes_info is None: From 6ae3172287a20cd2e54b22a95997aa10c7bfe29a Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Tue, 14 Jan 2020 15:40:38 +0100 Subject: [PATCH 03/10] Store metadata when writing the workspace --- plugins/3MFWriter/ThreeMFWorkspaceWriter.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/plugins/3MFWriter/ThreeMFWorkspaceWriter.py b/plugins/3MFWriter/ThreeMFWorkspaceWriter.py index 33df0bfe90..953b0a57e6 100644 --- a/plugins/3MFWriter/ThreeMFWorkspaceWriter.py +++ b/plugins/3MFWriter/ThreeMFWorkspaceWriter.py @@ -73,11 +73,22 @@ class ThreeMFWorkspaceWriter(WorkspaceWriter): version_config_parser.write(version_file_string) archive.writestr(version_file, version_file_string.getvalue()) + self._writePluginMetadataToArchive(archive) + # Close the archive & reset states. archive.close() mesh_writer.setStoreArchive(False) return True + def _writePluginMetadataToArchive(self, archive): + file_name = "Cura/plugin_metadata.json" + + file_in_archive = zipfile.ZipInfo(file_name) + # For some reason we have to set the compress type of each file as well (it doesn't keep the type of the entire archive) + file_in_archive.compress_type = zipfile.ZIP_DEFLATED + import json + archive.writestr(file_in_archive, json.dumps(Application.getInstance()._workspace_metadata_storage.getAllData())) + ## Helper function that writes ContainerStacks, InstanceContainers and DefinitionContainers to the archive. # \param container That follows the \type{ContainerInterface} to archive. # \param archive The archive to write to. From b8dbc1d1600c72f49ac7e9f781b0f5236afee201 Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Thu, 16 Jan 2020 09:50:45 +0100 Subject: [PATCH 04/10] Store the data from each plugin in it's own folder CURA-6627 --- plugins/3MFWriter/ThreeMFWorkspaceWriter.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/plugins/3MFWriter/ThreeMFWorkspaceWriter.py b/plugins/3MFWriter/ThreeMFWorkspaceWriter.py index 953b0a57e6..cd56e77ebd 100644 --- a/plugins/3MFWriter/ThreeMFWorkspaceWriter.py +++ b/plugins/3MFWriter/ThreeMFWorkspaceWriter.py @@ -81,13 +81,15 @@ class ThreeMFWorkspaceWriter(WorkspaceWriter): return True def _writePluginMetadataToArchive(self, archive): - file_name = "Cura/plugin_metadata.json" + file_name_template = "%s/plugin_metadata.json" - file_in_archive = zipfile.ZipInfo(file_name) - # For some reason we have to set the compress type of each file as well (it doesn't keep the type of the entire archive) - file_in_archive.compress_type = zipfile.ZIP_DEFLATED - import json - archive.writestr(file_in_archive, json.dumps(Application.getInstance()._workspace_metadata_storage.getAllData())) + for plugin_id, metadata in Application.getInstance()._workspace_metadata_storage.getAllData().items(): + file_name = file_name_template % plugin_id + file_in_archive = zipfile.ZipInfo(file_name) + # We have to set the compress type of each file as well (it doesn't keep the type of the entire archive) + file_in_archive.compress_type = zipfile.ZIP_DEFLATED + import json + archive.writestr(file_in_archive, json.dumps(metadata, separators = (", ", ": "), indent = 4)) ## Helper function that writes ContainerStacks, InstanceContainers and DefinitionContainers to the archive. # \param container That follows the \type{ContainerInterface} to archive. From 4e0e0c033953e97325e37495cbe717866cfb6f95 Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Thu, 16 Jan 2020 11:19:29 +0100 Subject: [PATCH 05/10] Add skipkeys flag to writing plugin metadata to workspace CURA-6627 --- plugins/3MFWriter/ThreeMFWorkspaceWriter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/3MFWriter/ThreeMFWorkspaceWriter.py b/plugins/3MFWriter/ThreeMFWorkspaceWriter.py index cd56e77ebd..45d9db77f6 100644 --- a/plugins/3MFWriter/ThreeMFWorkspaceWriter.py +++ b/plugins/3MFWriter/ThreeMFWorkspaceWriter.py @@ -89,7 +89,7 @@ class ThreeMFWorkspaceWriter(WorkspaceWriter): # We have to set the compress type of each file as well (it doesn't keep the type of the entire archive) file_in_archive.compress_type = zipfile.ZIP_DEFLATED import json - archive.writestr(file_in_archive, json.dumps(metadata, separators = (", ", ": "), indent = 4)) + archive.writestr(file_in_archive, json.dumps(metadata, separators = (", ", ": "), indent = 4, skipkeys = True)) ## Helper function that writes ContainerStacks, InstanceContainers and DefinitionContainers to the archive. # \param container That follows the \type{ContainerInterface} to archive. From 46050f9618afffce36701530010cd663c03ce41b Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Thu, 16 Jan 2020 11:25:30 +0100 Subject: [PATCH 06/10] Use getter instead of protected attribute CURA-6627 --- plugins/3MFWriter/ThreeMFWorkspaceWriter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/3MFWriter/ThreeMFWorkspaceWriter.py b/plugins/3MFWriter/ThreeMFWorkspaceWriter.py index 45d9db77f6..08918e1c12 100644 --- a/plugins/3MFWriter/ThreeMFWorkspaceWriter.py +++ b/plugins/3MFWriter/ThreeMFWorkspaceWriter.py @@ -83,7 +83,7 @@ class ThreeMFWorkspaceWriter(WorkspaceWriter): def _writePluginMetadataToArchive(self, archive): file_name_template = "%s/plugin_metadata.json" - for plugin_id, metadata in Application.getInstance()._workspace_metadata_storage.getAllData().items(): + for plugin_id, metadata in Application.getInstance().getWorkspaceMetadataStorage().getAllData().items(): file_name = file_name_template % plugin_id file_in_archive = zipfile.ZipInfo(file_name) # We have to set the compress type of each file as well (it doesn't keep the type of the entire archive) From 712cebcdd2d11310199b62e5e2aa0a2ded712515 Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Thu, 16 Jan 2020 11:43:07 +0100 Subject: [PATCH 07/10] Let 3mf workspace reader read from files per plugin CURA-6627 --- plugins/3MFReader/ThreeMFWorkspaceReader.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/plugins/3MFReader/ThreeMFWorkspaceReader.py b/plugins/3MFReader/ThreeMFWorkspaceReader.py index 953980d0b7..7d4efa6e74 100755 --- a/plugins/3MFReader/ThreeMFWorkspaceReader.py +++ b/plugins/3MFReader/ThreeMFWorkspaceReader.py @@ -737,12 +737,20 @@ class ThreeMFWorkspaceReader(WorkspaceReader): def _loadMetadata(self, file_name) -> Dict[str, Dict[str, Any]]: archive = zipfile.ZipFile(file_name, "r") + + metadata_files = [name for name in archive.namelist() if name.endswith("plugin_metadata.json")] import json - try: - return json.loads(archive.open("Cura/plugin_metadata.json").read().decode("utf-8")) - except KeyError: - # The plugin_metadata.json file doesn't exist - return dict() + + result = dict() + + for metadata_file in metadata_files: + try: + plugin_id = metadata_file.split("/")[0] + result[plugin_id] = json.loads(archive.open("Cura/plugin_metadata.json").read().decode("utf-8")) + except Exception: + Logger.logException("w", "Unable to retrieve metadata for %s", metadata_file) + + return result def _processQualityChanges(self, global_stack): if self._machine_info.quality_changes_info is None: From bee641da5ac08477e0956f181dd80b01a0a1be68 Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Mon, 20 Jan 2020 15:30:23 +0100 Subject: [PATCH 08/10] Remove local import of json CURA-7005 --- plugins/3MFReader/ThreeMFWorkspaceReader.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/3MFReader/ThreeMFWorkspaceReader.py b/plugins/3MFReader/ThreeMFWorkspaceReader.py index 7d4efa6e74..e92b60976b 100755 --- a/plugins/3MFReader/ThreeMFWorkspaceReader.py +++ b/plugins/3MFReader/ThreeMFWorkspaceReader.py @@ -4,6 +4,7 @@ from configparser import ConfigParser import zipfile import os +import json from typing import cast, Dict, List, Optional, Tuple, Any import xml.etree.ElementTree as ET @@ -739,7 +740,6 @@ class ThreeMFWorkspaceReader(WorkspaceReader): archive = zipfile.ZipFile(file_name, "r") metadata_files = [name for name in archive.namelist() if name.endswith("plugin_metadata.json")] - import json result = dict() From 52ce10639932cc9da4c471864bdb315e6f33a295 Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Mon, 20 Jan 2020 15:37:57 +0100 Subject: [PATCH 09/10] Change _loadMetadata to be static CURA-6627 --- plugins/3MFReader/ThreeMFWorkspaceReader.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/3MFReader/ThreeMFWorkspaceReader.py b/plugins/3MFReader/ThreeMFWorkspaceReader.py index e92b60976b..ba169f3dab 100755 --- a/plugins/3MFReader/ThreeMFWorkspaceReader.py +++ b/plugins/3MFReader/ThreeMFWorkspaceReader.py @@ -736,7 +736,8 @@ class ThreeMFWorkspaceReader(WorkspaceReader): return nodes, self._loadMetadata(file_name) - def _loadMetadata(self, file_name) -> Dict[str, Dict[str, Any]]: + @staticmethod + def _loadMetadata(file_name: str) -> Dict[str, Dict[str, Any]]: archive = zipfile.ZipFile(file_name, "r") metadata_files = [name for name in archive.namelist() if name.endswith("plugin_metadata.json")] From 06ccd882e1395909cfa2d66be6310270bcfc94bb Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Mon, 20 Jan 2020 16:03:56 +0100 Subject: [PATCH 10/10] Add missing typing CURA-6627 --- plugins/3MFWriter/ThreeMFWorkspaceWriter.py | 3 ++- plugins/3MFWriter/ThreeMFWriter.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/3MFWriter/ThreeMFWorkspaceWriter.py b/plugins/3MFWriter/ThreeMFWorkspaceWriter.py index 08918e1c12..10a21f445b 100644 --- a/plugins/3MFWriter/ThreeMFWorkspaceWriter.py +++ b/plugins/3MFWriter/ThreeMFWorkspaceWriter.py @@ -80,7 +80,8 @@ class ThreeMFWorkspaceWriter(WorkspaceWriter): mesh_writer.setStoreArchive(False) return True - def _writePluginMetadataToArchive(self, archive): + @staticmethod + def _writePluginMetadataToArchive(archive: zipfile.ZipFile) -> None: file_name_template = "%s/plugin_metadata.json" for plugin_id, metadata in Application.getInstance().getWorkspaceMetadataStorage().getAllData().items(): diff --git a/plugins/3MFWriter/ThreeMFWriter.py b/plugins/3MFWriter/ThreeMFWriter.py index 640aabd30c..9860804542 100644 --- a/plugins/3MFWriter/ThreeMFWriter.py +++ b/plugins/3MFWriter/ThreeMFWriter.py @@ -1,5 +1,6 @@ # Copyright (c) 2015 Ultimaker B.V. # Uranium is released under the terms of the LGPLv3 or higher. +from typing import Optional from UM.Mesh.MeshWriter import MeshWriter from UM.Math.Vector import Vector @@ -40,7 +41,7 @@ class ThreeMFWriter(MeshWriter): } self._unit_matrix_string = self._convertMatrixToString(Matrix()) - self._archive = None + self._archive = None # type: Optional[zipfile.ZipFile] self._store_archive = False def _convertMatrixToString(self, matrix):