CuraPackageManager.py 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Any, Dict, List, Tuple, TYPE_CHECKING, Optional, Generator
  4. from cura.CuraApplication import CuraApplication #To find some resource types.
  5. from cura.Settings.GlobalStack import GlobalStack
  6. from UM.PackageManager import PackageManager #The class we're extending.
  7. from UM.Resources import Resources #To find storage paths for some resource types.
  8. from UM.i18n import i18nCatalog
  9. catalog = i18nCatalog("cura")
  10. if TYPE_CHECKING:
  11. from UM.Qt.QtApplication import QtApplication
  12. from PyQt5.QtCore import QObject
  13. class CuraPackageManager(PackageManager):
  14. def __init__(self, application: "QtApplication", parent: Optional["QObject"] = None) -> None:
  15. super().__init__(application, parent)
  16. self._locally_installed_packages = None
  17. self.installedPackagesChanged.connect(self._updateLocallyInstalledPackages)
  18. def _updateLocallyInstalledPackages(self):
  19. self._locally_installed_packages = list(self.iterateAllLocalPackages())
  20. @property
  21. def locally_installed_packages(self):
  22. """locally installed packages, lazy execution"""
  23. if self._locally_installed_packages is None:
  24. self._updateLocallyInstalledPackages()
  25. return self._locally_installed_packages
  26. @locally_installed_packages.setter
  27. def locally_installed_packages(self, value):
  28. self._locally_installed_packages = value
  29. def initialize(self) -> None:
  30. self._installation_dirs_dict["materials"] = Resources.getStoragePath(CuraApplication.ResourceTypes.MaterialInstanceContainer)
  31. self._installation_dirs_dict["qualities"] = Resources.getStoragePath(CuraApplication.ResourceTypes.QualityInstanceContainer)
  32. super().initialize()
  33. def getMachinesUsingPackage(self, package_id: str) -> Tuple[List[Tuple[GlobalStack, str, str]], List[Tuple[GlobalStack, str, str]]]:
  34. """Returns a list of where the package is used
  35. It loops through all the package contents and see if some of the ids are used.
  36. :param package_id: package id to search for
  37. :return: empty if it is never used, otherwise a list consisting of 3-tuples
  38. """
  39. ids = self.getPackageContainerIds(package_id)
  40. container_stacks = self._application.getContainerRegistry().findContainerStacks()
  41. global_stacks = [container_stack for container_stack in container_stacks if isinstance(container_stack, GlobalStack)]
  42. machine_with_materials = []
  43. machine_with_qualities = []
  44. for container_id in ids:
  45. for global_stack in global_stacks:
  46. for extruder_nr, extruder_stack in enumerate(global_stack.extruderList):
  47. if container_id in (extruder_stack.material.getId(), extruder_stack.material.getMetaData().get("base_file")):
  48. machine_with_materials.append((global_stack, str(extruder_nr), container_id))
  49. if container_id == extruder_stack.quality.getId():
  50. machine_with_qualities.append((global_stack, str(extruder_nr), container_id))
  51. return machine_with_materials, machine_with_qualities
  52. def iterateAllLocalPackages(self) -> Generator[Dict[str, Any], None, None]:
  53. """ A generator which returns an unordered list of all the PackageModels"""
  54. handled_packages = set()
  55. for packages in self.getAllInstalledPackagesInfo().values():
  56. for package_info in packages:
  57. if not handled_packages.__contains__(package_info["package_id"]):
  58. handled_packages.add(package_info["package_id"])
  59. yield package_info
  60. # Get all to be removed package_info's. These packages are still used in the current session so the user might
  61. # still want to interact with these.
  62. for package_data in self.getPackagesToRemove().values():
  63. for package_data in self.getPackagesToRemove().values():
  64. if not handled_packages.__contains__(package_data["package_info"]["package_id"]):
  65. handled_packages.add(package_data["package_info"]["package_id"])
  66. yield package_data["package_info"]
  67. # Get all to be installed package_info's. Since the user might want to interact with these
  68. for package_data in self.getPackagesToInstall().values():
  69. if not handled_packages.__contains__(package_data["package_info"]["package_id"]):
  70. handled_packages.add(package_data["package_info"]["package_id"])
  71. yield package_data["package_info"]