From 21d59e93492dc66198312bdd8dd17d91bc44d252 Mon Sep 17 00:00:00 2001 From: "j.delarago" Date: Mon, 30 May 2022 17:29:59 +0200 Subject: Write active material metadata to ufp when saving. Add function to fetch package_id using only information from XmlMaterialProfile material container. The only piece of information associating the material container and the package together is the file_name. To find the package that owns a material we have to search each of the material package paths. It would be great to find a cleaner solution (preferable one that doesn't require invalidating the cached containers). CURA-8610 --- cura/CuraPackageManager.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'cura/CuraPackageManager.py') diff --git a/cura/CuraPackageManager.py b/cura/CuraPackageManager.py index 17d6832ac6..b23422fbbd 100644 --- a/cura/CuraPackageManager.py +++ b/cura/CuraPackageManager.py @@ -1,5 +1,6 @@ # Copyright (c) 2018 Ultimaker B.V. # Cura is released under the terms of the LGPLv3 or higher. +import os from typing import Any, cast, Dict, List, Set, Tuple, TYPE_CHECKING, Optional @@ -51,6 +52,25 @@ class CuraPackageManager(PackageManager): super().initialize() + def getMaterialFilePackageId(self, file_name: str, guid: str) -> str: + """Get the id of the material package that contains file_name""" + for material_package in [f for f in os.scandir(self._installation_dirs_dict["materials"]) if f.is_dir()]: + package_id = material_package.name + + for root, _, file_names in os.walk(material_package.path): + if file_name not in file_names: + #File with the name we are looking for is not in this directory + continue + + with open(root + "/" + file_name, encoding="utf-8") as f: + # Make sure the file we found has the same guid as our material + # Parsing this xml would be better but the namespace is needed to search it. + if guid in f.read(): + return package_id + continue + + + def getMachinesUsingPackage(self, package_id: str) -> Tuple[List[Tuple[GlobalStack, str, str]], List[Tuple[GlobalStack, str, str]]]: """Returns a list of where the package is used -- cgit v1.2.3