From 49b93db6df0aaf142da1da239bec799583f97355 Mon Sep 17 00:00:00 2001 From: Nino van Hooff Date: Tue, 24 Dec 2019 14:51:50 +0100 Subject: [PATCH 1/5] Use plugin Id instead of __file__ for USBPrinting CURA-6255 --- plugins/USBPrinting/USBPrinterOutputDevice.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugins/USBPrinting/USBPrinterOutputDevice.py b/plugins/USBPrinting/USBPrinterOutputDevice.py index c9758d88d4..060b795fde 100644 --- a/plugins/USBPrinting/USBPrinterOutputDevice.py +++ b/plugins/USBPrinting/USBPrinterOutputDevice.py @@ -88,7 +88,10 @@ class USBPrinterOutputDevice(PrinterOutputDevice): self._firmware_name_requested = False self._firmware_updater = AvrFirmwareUpdater(self) - self._monitor_view_qml_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "MonitorItem.qml") + from .USBPrinterOutputDeviceManager import USBPrinterOutputDeviceManager + plugin_id = USBPrinterOutputDeviceManager.getInstance().getPluginId() + plugin_path = PluginRegistry.getInstance().getPluginPath(plugin_id) + self._monitor_view_qml_path = os.path.join(plugin_path, "MonitorItem.qml") CuraApplication.getInstance().getOnExitCallbackManager().addCallback(self._checkActivePrintingUponAppExit) From 128bfa987e67bb521c6d039eca76feca211c6ab2 Mon Sep 17 00:00:00 2001 From: Nino van Hooff Date: Tue, 24 Dec 2019 15:42:01 +0100 Subject: [PATCH 2/5] Use plugin Id instead of __file__ for XmlMaterialProfile CURA-6255 --- plugins/XmlMaterialProfile/PluginInfo.py | 15 +++++++++++++++ plugins/XmlMaterialProfile/XmlMaterialProfile.py | 6 +++++- plugins/XmlMaterialProfile/__init__.py | 3 ++- 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 plugins/XmlMaterialProfile/PluginInfo.py diff --git a/plugins/XmlMaterialProfile/PluginInfo.py b/plugins/XmlMaterialProfile/PluginInfo.py new file mode 100644 index 0000000000..bb430b0af8 --- /dev/null +++ b/plugins/XmlMaterialProfile/PluginInfo.py @@ -0,0 +1,15 @@ +from UM.PluginObject import PluginObject + + +class PluginInfo(PluginObject): + __instance = None # type: PluginInfo + + def __init__(self, *args, **kwags): + super().__init__(*args, **kwags) + if PluginInfo.__instance is not None: + raise RuntimeError("Try to create singleton '%s' more than once" % self.__class__.__name__) + PluginInfo.__instance = self + + @classmethod + def getInstance(cls, *args, **kwargs) -> "USBPrinterOutputDeviceManager": + return cls.__instance \ No newline at end of file diff --git a/plugins/XmlMaterialProfile/XmlMaterialProfile.py b/plugins/XmlMaterialProfile/XmlMaterialProfile.py index bf1bf19e32..f4cef3a46d 100644 --- a/plugins/XmlMaterialProfile/XmlMaterialProfile.py +++ b/plugins/XmlMaterialProfile/XmlMaterialProfile.py @@ -9,6 +9,7 @@ import sys from typing import Any, Dict, List, Optional, Tuple, cast, Set, Union import xml.etree.ElementTree as ET +from UM.PluginRegistry import PluginRegistry from UM.Resources import Resources from UM.Logger import Logger import UM.Dictionary @@ -19,6 +20,7 @@ from UM.ConfigurationErrorMessage import ConfigurationErrorMessage from cura.CuraApplication import CuraApplication from cura.Machines.ContainerTree import ContainerTree from cura.Machines.VariantType import VariantType +from plugins.XmlMaterialProfile import PluginInfo try: from .XmlMaterialValidator import XmlMaterialValidator @@ -1068,7 +1070,9 @@ class XmlMaterialProfile(InstanceContainer): # This loads the mapping from a file. @classmethod def getProductIdMap(cls) -> Dict[str, List[str]]: - product_to_id_file = os.path.join(os.path.dirname(sys.modules[cls.__module__].__file__), "product_to_id.json") + plugin_id = PluginInfo.getInstance().getPluginId() + plugin_path = PluginRegistry.getInstance().getPluginPath(plugin_id) + product_to_id_file = os.path.join(plugin_path, "product_to_id.json") with open(product_to_id_file, encoding = "utf-8") as f: product_to_id_map = json.load(f) product_to_id_map = {key: [value] for key, value in product_to_id_map.items()} diff --git a/plugins/XmlMaterialProfile/__init__.py b/plugins/XmlMaterialProfile/__init__.py index b3810c97dc..aa1db90d6d 100644 --- a/plugins/XmlMaterialProfile/__init__.py +++ b/plugins/XmlMaterialProfile/__init__.py @@ -1,6 +1,6 @@ # Copyright (c) 2017 Ultimaker B.V. # Cura is released under the terms of the LGPLv3 or higher. - +from plugins.XmlMaterialProfile.PluginInfo import PluginInfo from . import XmlMaterialProfile from . import XmlMaterialUpgrader @@ -46,4 +46,5 @@ def register(app): return {"version_upgrade": upgrader, "settings_container": XmlMaterialProfile.XmlMaterialProfile("default_xml_material_profile"), + "plugin_info": PluginInfo() } From 8ae7459ea741da41fc4b03158268f1008aa603d1 Mon Sep 17 00:00:00 2001 From: Nino van Hooff Date: Tue, 24 Dec 2019 15:49:18 +0100 Subject: [PATCH 3/5] Use plugin Id instead of __file__ for XmlMaterialProfile CURA-6255 --- plugins/XmlMaterialProfile/PluginInfo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/XmlMaterialProfile/PluginInfo.py b/plugins/XmlMaterialProfile/PluginInfo.py index bb430b0af8..4b78a47a5e 100644 --- a/plugins/XmlMaterialProfile/PluginInfo.py +++ b/plugins/XmlMaterialProfile/PluginInfo.py @@ -11,5 +11,5 @@ class PluginInfo(PluginObject): PluginInfo.__instance = self @classmethod - def getInstance(cls, *args, **kwargs) -> "USBPrinterOutputDeviceManager": + def getInstance(cls, *args, **kwargs) -> "PluginInfo": return cls.__instance \ No newline at end of file From 9aa5c3cd24dd903a9954008ff40f8b78d43b63f3 Mon Sep 17 00:00:00 2001 From: Nino van Hooff Date: Mon, 30 Dec 2019 12:04:41 +0100 Subject: [PATCH 4/5] Hardcode plugin id for UsbPrinter and XmlMaterialProfile Previous solution was more defensive but also introduced dependencies and extra files for minor benefit CURA-6255 --- plugins/USBPrinting/USBPrinterOutputDevice.py | 4 +--- plugins/XmlMaterialProfile/XmlMaterialProfile.py | 4 +--- plugins/XmlMaterialProfile/__init__.py | 3 +-- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/plugins/USBPrinting/USBPrinterOutputDevice.py b/plugins/USBPrinting/USBPrinterOutputDevice.py index 060b795fde..6c92d5a93a 100644 --- a/plugins/USBPrinting/USBPrinterOutputDevice.py +++ b/plugins/USBPrinting/USBPrinterOutputDevice.py @@ -88,9 +88,7 @@ class USBPrinterOutputDevice(PrinterOutputDevice): self._firmware_name_requested = False self._firmware_updater = AvrFirmwareUpdater(self) - from .USBPrinterOutputDeviceManager import USBPrinterOutputDeviceManager - plugin_id = USBPrinterOutputDeviceManager.getInstance().getPluginId() - plugin_path = PluginRegistry.getInstance().getPluginPath(plugin_id) + plugin_path = PluginRegistry.getInstance().getPluginPath("USBPrinting") self._monitor_view_qml_path = os.path.join(plugin_path, "MonitorItem.qml") CuraApplication.getInstance().getOnExitCallbackManager().addCallback(self._checkActivePrintingUponAppExit) diff --git a/plugins/XmlMaterialProfile/XmlMaterialProfile.py b/plugins/XmlMaterialProfile/XmlMaterialProfile.py index f4cef3a46d..2228cd4993 100644 --- a/plugins/XmlMaterialProfile/XmlMaterialProfile.py +++ b/plugins/XmlMaterialProfile/XmlMaterialProfile.py @@ -20,7 +20,6 @@ from UM.ConfigurationErrorMessage import ConfigurationErrorMessage from cura.CuraApplication import CuraApplication from cura.Machines.ContainerTree import ContainerTree from cura.Machines.VariantType import VariantType -from plugins.XmlMaterialProfile import PluginInfo try: from .XmlMaterialValidator import XmlMaterialValidator @@ -1070,8 +1069,7 @@ class XmlMaterialProfile(InstanceContainer): # This loads the mapping from a file. @classmethod def getProductIdMap(cls) -> Dict[str, List[str]]: - plugin_id = PluginInfo.getInstance().getPluginId() - plugin_path = PluginRegistry.getInstance().getPluginPath(plugin_id) + plugin_path = PluginRegistry.getInstance().getPluginPath("XmlMaterialProfile") product_to_id_file = os.path.join(plugin_path, "product_to_id.json") with open(product_to_id_file, encoding = "utf-8") as f: product_to_id_map = json.load(f) diff --git a/plugins/XmlMaterialProfile/__init__.py b/plugins/XmlMaterialProfile/__init__.py index aa1db90d6d..b3810c97dc 100644 --- a/plugins/XmlMaterialProfile/__init__.py +++ b/plugins/XmlMaterialProfile/__init__.py @@ -1,6 +1,6 @@ # Copyright (c) 2017 Ultimaker B.V. # Cura is released under the terms of the LGPLv3 or higher. -from plugins.XmlMaterialProfile.PluginInfo import PluginInfo + from . import XmlMaterialProfile from . import XmlMaterialUpgrader @@ -46,5 +46,4 @@ def register(app): return {"version_upgrade": upgrader, "settings_container": XmlMaterialProfile.XmlMaterialProfile("default_xml_material_profile"), - "plugin_info": PluginInfo() } From 0eea73d2a9613debc3c1788cc8cddf271ece0c5e Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Mon, 30 Dec 2019 12:57:04 +0100 Subject: [PATCH 5/5] Fix typing CURA-6255 --- plugins/USBPrinting/USBPrinterOutputDevice.py | 2 +- plugins/XmlMaterialProfile/XmlMaterialProfile.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/USBPrinting/USBPrinterOutputDevice.py b/plugins/USBPrinting/USBPrinterOutputDevice.py index 6c92d5a93a..adb3b03700 100644 --- a/plugins/USBPrinting/USBPrinterOutputDevice.py +++ b/plugins/USBPrinting/USBPrinterOutputDevice.py @@ -88,7 +88,7 @@ class USBPrinterOutputDevice(PrinterOutputDevice): self._firmware_name_requested = False self._firmware_updater = AvrFirmwareUpdater(self) - plugin_path = PluginRegistry.getInstance().getPluginPath("USBPrinting") + plugin_path = cast(str, PluginRegistry.getInstance().getPluginPath("USBPrinting")) self._monitor_view_qml_path = os.path.join(plugin_path, "MonitorItem.qml") CuraApplication.getInstance().getOnExitCallbackManager().addCallback(self._checkActivePrintingUponAppExit) diff --git a/plugins/XmlMaterialProfile/XmlMaterialProfile.py b/plugins/XmlMaterialProfile/XmlMaterialProfile.py index 2228cd4993..30bbecc86e 100644 --- a/plugins/XmlMaterialProfile/XmlMaterialProfile.py +++ b/plugins/XmlMaterialProfile/XmlMaterialProfile.py @@ -1069,7 +1069,7 @@ class XmlMaterialProfile(InstanceContainer): # This loads the mapping from a file. @classmethod def getProductIdMap(cls) -> Dict[str, List[str]]: - plugin_path = PluginRegistry.getInstance().getPluginPath("XmlMaterialProfile") + plugin_path = cast(str, PluginRegistry.getInstance().getPluginPath("XmlMaterialProfile")) product_to_id_file = os.path.join(plugin_path, "product_to_id.json") with open(product_to_id_file, encoding = "utf-8") as f: product_to_id_map = json.load(f)