CuraPackageManager.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Any, cast, Dict, List, Set, Tuple, TYPE_CHECKING, Optional
  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._local_packages: Optional[List[Dict[str, Any]]] = None
  17. self._local_packages_ids: Optional[Set[str]] = None
  18. self.installedPackagesChanged.connect(self._updateLocalPackages)
  19. def _updateLocalPackages(self) -> None:
  20. self._local_packages = self.getAllLocalPackages()
  21. self._local_packages_ids = set(pkg["package_id"] for pkg in self._local_packages)
  22. @property
  23. def local_packages(self) -> List[Dict[str, Any]]:
  24. """locally installed packages, lazy execution"""
  25. if self._local_packages is None:
  26. self._updateLocalPackages()
  27. # _updateLocalPackages always results in a list of packages, not None.
  28. # It's guaranteed to be a list now.
  29. return cast(List[Dict[str, Any]], self._local_packages)
  30. @property
  31. def local_packages_ids(self) -> Set[str]:
  32. """locally installed packages, lazy execution"""
  33. if self._local_packages_ids is None:
  34. self._updateLocalPackages()
  35. # _updateLocalPackages always results in a list of packages, not None.
  36. # It's guaranteed to be a list now.
  37. return cast(Set[str], self._local_packages_ids)
  38. def initialize(self) -> None:
  39. self._installation_dirs_dict["materials"] = Resources.getStoragePath(CuraApplication.ResourceTypes.MaterialInstanceContainer)
  40. self._installation_dirs_dict["qualities"] = Resources.getStoragePath(CuraApplication.ResourceTypes.QualityInstanceContainer)
  41. super().initialize()
  42. def getMachinesUsingPackage(self, package_id: str) -> Tuple[List[Tuple[GlobalStack, str, str]], List[Tuple[GlobalStack, str, str]]]:
  43. """Returns a list of where the package is used
  44. It loops through all the package contents and see if some of the ids are used.
  45. :param package_id: package id to search for
  46. :return: empty if it is never used, otherwise a list consisting of 3-tuples
  47. """
  48. ids = self.getPackageContainerIds(package_id)
  49. container_stacks = self._application.getContainerRegistry().findContainerStacks()
  50. global_stacks = [container_stack for container_stack in container_stacks if isinstance(container_stack, GlobalStack)]
  51. machine_with_materials = []
  52. machine_with_qualities = []
  53. for container_id in ids:
  54. for global_stack in global_stacks:
  55. for extruder_nr, extruder_stack in enumerate(global_stack.extruderList):
  56. if container_id in (extruder_stack.material.getId(), extruder_stack.material.getMetaData().get("base_file")):
  57. machine_with_materials.append((global_stack, str(extruder_nr), container_id))
  58. if container_id == extruder_stack.quality.getId():
  59. machine_with_qualities.append((global_stack, str(extruder_nr), container_id))
  60. return machine_with_materials, machine_with_qualities
  61. def getAllLocalPackages(self) -> List[Dict[str, Any]]:
  62. """ Returns an unordered list of all the package_info of installed, to be installed, or bundled packages"""
  63. packages: List[Dict[str, Any]] = []
  64. for packages_to_add in self.getAllInstalledPackagesInfo().values():
  65. packages.extend(packages_to_add)
  66. return packages