From 6dc0a24311599bc327d8e75b1604d3a04a5046c2 Mon Sep 17 00:00:00 2001 From: Remco Burema Date: Thu, 24 Nov 2022 15:33:39 +0100 Subject: [PATCH] Cache and genericize ID to Product table. part of CURA-9931 --- .../XmlMaterialProfile/XmlMaterialProfile.py | 13 ++++++++++--- plugins/XmlMaterialProfile/product_to_id.json | 17 +++++++---------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/plugins/XmlMaterialProfile/XmlMaterialProfile.py b/plugins/XmlMaterialProfile/XmlMaterialProfile.py index 527fa4ec13..524b7f099c 100644 --- a/plugins/XmlMaterialProfile/XmlMaterialProfile.py +++ b/plugins/XmlMaterialProfile/XmlMaterialProfile.py @@ -1125,21 +1125,28 @@ 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: - product_to_id_map = json.load(f) - product_to_id_map = {key: [value] for key, value in product_to_id_map.items()} + 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 product_to_id_map + return cls.__product_to_id_map @staticmethod def _parseCompatibleValue(value: str): diff --git a/plugins/XmlMaterialProfile/product_to_id.json b/plugins/XmlMaterialProfile/product_to_id.json index 053c8d13f5..07e14f1276 100644 --- a/plugins/XmlMaterialProfile/product_to_id.json +++ b/plugins/XmlMaterialProfile/product_to_id.json @@ -1,14 +1,11 @@ { - "Ultimaker 2": "ultimaker2", - "Ultimaker 2 Extended": "ultimaker2_extended", - "Ultimaker 2 Extended+": "ultimaker2_extended_plus", - "Ultimaker 2 Go": "ultimaker2_go", - "Ultimaker 2+": "ultimaker2_plus", - "Ultimaker 2+ Connect": "ultimaker2_plus_connect", - "Ultimaker 3": "ultimaker3", - "Ultimaker 3 Extended": "ultimaker3_extended", - "Ultimaker S3": "ultimaker_s3", - "Ultimaker S5": "ultimaker_s5", + "Ultimaker #": "ultimaker#", + "Ultimaker # Extended": "ultimaker#_extended", + "Ultimaker # Extended+": "ultimaker#_extended_plus", + "Ultimaker # Go": "ultimaker#_go", + "Ultimaker #+": "ultimaker#_plus", + "Ultimaker #+ Connect": "ultimaker#_plus_connect", + "Ultimaker S#": "ultimaker_s#", "Ultimaker Original": "ultimaker_original", "Ultimaker Original+": "ultimaker_original_plus", "Ultimaker Original Dual Extrusion": "ultimaker_original_dual",