CuraPackageManager.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. @property
  18. def locally_installed_packages(self):
  19. """locally installed packages, lazy execution"""
  20. if self._locally_installed_packages is None:
  21. self._locally_installed_packages = list(self.iterateAllLocalPackages())
  22. return self._locally_installed_packages
  23. @locally_installed_packages.setter
  24. def locally_installed_packages(self, value):
  25. self._locally_installed_packages = value
  26. def initialize(self) -> None:
  27. self._installation_dirs_dict["materials"] = Resources.getStoragePath(CuraApplication.ResourceTypes.MaterialInstanceContainer)
  28. self._installation_dirs_dict["qualities"] = Resources.getStoragePath(CuraApplication.ResourceTypes.QualityInstanceContainer)
  29. super().initialize()
  30. def getMachinesUsingPackage(self, package_id: str) -> Tuple[List[Tuple[GlobalStack, str, str]], List[Tuple[GlobalStack, str, str]]]:
  31. """Returns a list of where the package is used
  32. It loops through all the package contents and see if some of the ids are used.
  33. :param package_id: package id to search for
  34. :return: empty if it is never used, otherwise a list consisting of 3-tuples
  35. """
  36. ids = self.getPackageContainerIds(package_id)
  37. container_stacks = self._application.getContainerRegistry().findContainerStacks()
  38. global_stacks = [container_stack for container_stack in container_stacks if isinstance(container_stack, GlobalStack)]
  39. machine_with_materials = []
  40. machine_with_qualities = []
  41. for container_id in ids:
  42. for global_stack in global_stacks:
  43. for extruder_nr, extruder_stack in enumerate(global_stack.extruderList):
  44. if container_id in (extruder_stack.material.getId(), extruder_stack.material.getMetaData().get("base_file")):
  45. machine_with_materials.append((global_stack, str(extruder_nr), container_id))
  46. if container_id == extruder_stack.quality.getId():
  47. machine_with_qualities.append((global_stack, str(extruder_nr), container_id))
  48. return machine_with_materials, machine_with_qualities
  49. def iterateAllLocalPackages(self) -> Generator[Dict[str, Any], None, None]:
  50. """ A generator which returns an unordered list of all the PackageModels"""
  51. # Get all the installed packages, add a section_title depending on package_type and user installed
  52. for packages in self.getAllInstalledPackagesInfo().values():
  53. for package_info in packages:
  54. yield package_info
  55. # Get all to be removed package_info's. These packages are still used in the current session so the user might
  56. # still want to interact with these.
  57. for package_data in self.getPackagesToRemove().values():
  58. yield package_data["package_info"]
  59. # Get all to be installed package_info's. Since the user might want to interact with these
  60. for package_data in self.getPackagesToInstall().values():
  61. yield package_data["package_info"]