CuraPackageManager.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. # Get all the installed packages, add a section_title depending on package_type and user installed
  55. for packages in self.getAllInstalledPackagesInfo().values():
  56. for package_info in packages:
  57. yield package_info
  58. # Get all to be removed package_info's. These packages are still used in the current session so the user might
  59. # still want to interact with these.
  60. for package_data in self.getPackagesToRemove().values():
  61. yield package_data["package_info"]
  62. # Get all to be installed package_info's. Since the user might want to interact with these
  63. for package_data in self.getPackagesToInstall().values():
  64. yield package_data["package_info"]