From ddf4f4c6fef74751d43db55cb8410583e5ee4c5e Mon Sep 17 00:00:00 2001 From: Remco Burema Date: Thu, 1 Aug 2024 14:33:24 +0200 Subject: [PATCH] Also move printer name mapping to new FormatMaps utility. done as part of CURA-12005 --- cura/PrinterOutput/FormatMaps.py | 27 +++++++++++++++++ .../XmlMaterialProfile/XmlMaterialProfile.py | 30 +++---------------- .../texts}/product_to_id.json | 0 3 files changed, 31 insertions(+), 26 deletions(-) rename {plugins/XmlMaterialProfile => resources/texts}/product_to_id.json (100%) diff --git a/cura/PrinterOutput/FormatMaps.py b/cura/PrinterOutput/FormatMaps.py index 2aa9ccc86e..8c785eb2ff 100644 --- a/cura/PrinterOutput/FormatMaps.py +++ b/cura/PrinterOutput/FormatMaps.py @@ -1,6 +1,11 @@ # Copyright (c) 2024 UltiMaker # Cura is released under the terms of the LGPLv3 or higher. +from UM.Resources import Resources + +import json +from typing import Dict, List, Optional + class FormatMaps: PRINTER_TYPE_NAME = { @@ -38,3 +43,25 @@ class FormatMaps: "tpu": {"name": "TPU 95A", "guid": "19baa6a9-94ff-478b-b4a1-8157b74358d2"}, "im-pla": {"name": "Tough", "guid": "de031137-a8ca-4a72-bd1b-17bb964033ad"} } + + __product_to_id_map: Optional[Dict[str, List[str]]] = None + + @classmethod + def getProductIdMap(cls) -> Dict[str, List[str]]: + """Gets a mapping from product names in the XML files to their definition IDs. + + This loads the mapping from a file. + """ + if cls.__product_to_id_map is not None: + return cls.__product_to_id_map + + product_to_id_file = Resources.getPath(Resources.Texts, "product_to_id.json") + with open(product_to_id_file, encoding = "utf-8") as f: + contents = "" + for line in f: + contents += line if "#" not in line else "".join([line.replace("#", str(n)) for n in range(1, 12)]) + cls.__product_to_id_map = json.loads(contents) + cls.__product_to_id_map = {key: [value] for key, value in cls.__product_to_id_map.items()} + #This also loads "Ultimaker S5" -> "ultimaker_s5" even though that is not strictly necessary with the default to change spaces into underscores. + #However it is not always loaded with that default; this mapping is also used in serialize() without that default. + return cls.__product_to_id_map diff --git a/plugins/XmlMaterialProfile/XmlMaterialProfile.py b/plugins/XmlMaterialProfile/XmlMaterialProfile.py index 980288e3c9..49b5e7661d 100644 --- a/plugins/XmlMaterialProfile/XmlMaterialProfile.py +++ b/plugins/XmlMaterialProfile/XmlMaterialProfile.py @@ -17,6 +17,7 @@ from UM.Settings.ContainerRegistry import ContainerRegistry from UM.ConfigurationErrorMessage import ConfigurationErrorMessage from cura.CuraApplication import CuraApplication +from cura.PrinterOutput.FormatMaps import FormatMaps from cura.Machines.VariantType import VariantType try: @@ -249,7 +250,7 @@ class XmlMaterialProfile(InstanceContainer): machine_variant_map[definition_id][variant_name] = variant_dict # Map machine human-readable names to IDs - product_id_map = self.getProductIdMap() + product_id_map = FormatMaps.getProductIdMap() for definition_id, container in machine_container_map.items(): definition_id = container.getMetaDataEntry("definition") @@ -647,7 +648,7 @@ class XmlMaterialProfile(InstanceContainer): self._dirty = False # Map machine human-readable names to IDs - product_id_map = self.getProductIdMap() + product_id_map = FormatMaps.getProductIdMap() machines = data.iterfind("./um:settings/um:machine", self.__namespaces) for machine in machines: @@ -923,7 +924,7 @@ class XmlMaterialProfile(InstanceContainer): result_metadata.append(base_metadata) # Map machine human-readable names to IDs - product_id_map = cls.getProductIdMap() + product_id_map = FormatMaps.getProductIdMap() for machine in data.iterfind("./um:settings/um:machine", cls.__namespaces): machine_compatibility = common_compatibility @@ -1128,29 +1129,6 @@ class XmlMaterialProfile(InstanceContainer): id_list = list(id_list) return id_list - __product_to_id_map: Optional[Dict[str, List[str]]] = None - - @classmethod - def getProductIdMap(cls) -> Dict[str, List[str]]: - """Gets a mapping from product names in the XML files to their definition IDs. - - This loads the mapping from a file. - """ - if cls.__product_to_id_map is not None: - return cls.__product_to_id_map - - 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: - contents = "" - for line in f: - contents += line if "#" not in line else "".join([line.replace("#", str(n)) for n in range(1, 12)]) - cls.__product_to_id_map = json.loads(contents) - cls.__product_to_id_map = {key: [value] for key, value in cls.__product_to_id_map.items()} - #This also loads "Ultimaker S5" -> "ultimaker_s5" even though that is not strictly necessary with the default to change spaces into underscores. - #However it is not always loaded with that default; this mapping is also used in serialize() without that default. - return cls.__product_to_id_map - @staticmethod def _parseCompatibleValue(value: str): """Parse the value of the "material compatible" property.""" diff --git a/plugins/XmlMaterialProfile/product_to_id.json b/resources/texts/product_to_id.json similarity index 100% rename from plugins/XmlMaterialProfile/product_to_id.json rename to resources/texts/product_to_id.json