MissingPackageList.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright (c) 2022 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional, TYPE_CHECKING, Dict, List
  4. from .Constants import PACKAGES_URL
  5. from .PackageModel import PackageModel
  6. from .RemotePackageList import RemotePackageList
  7. from PyQt6.QtCore import pyqtSignal, QObject, pyqtProperty, QCoreApplication
  8. from UM.TaskManagement.HttpRequestManager import HttpRequestManager # To request the package list from the API.
  9. from UM.i18n import i18nCatalog
  10. if TYPE_CHECKING:
  11. from PyQt6.QtCore import QObject, pyqtProperty, pyqtSignal
  12. catalog = i18nCatalog("cura")
  13. class MissingPackageList(RemotePackageList):
  14. def __init__(self, packages_metadata: List[Dict[str, str]], parent: Optional["QObject"] = None) -> None:
  15. super().__init__(parent)
  16. self._packages_metadata: List[Dict[str, str]] = packages_metadata
  17. self._search_type = "package_ids"
  18. self._requested_search_string = ",".join(map(lambda package: package["id"], packages_metadata))
  19. def _parseResponse(self, reply: "QNetworkReply") -> None:
  20. super()._parseResponse(reply)
  21. # At the end of the list we want to show some information about packages the user is missing that can't be found
  22. # This will add cards with some information about the missing packages
  23. if not self.hasMore:
  24. self._addPackagesMissingFromRequest()
  25. def _addPackagesMissingFromRequest(self) -> None:
  26. """Create cards for packages the user needs to install that could not be found"""
  27. returned_packages_ids = [item["package"].packageId for item in self._items]
  28. for package_metadata in self._packages_metadata:
  29. if package_metadata["id"] not in returned_packages_ids:
  30. package_type = package_metadata["type"] if "type" in package_metadata else "material"
  31. # When this feature was originally introduced only missing materials were detected. With the inclusion
  32. # of backend plugins this system was extended to also detect missing plugins. With that change the type
  33. # of the package was added to the metadata. Project files before this change do not have this type. So
  34. # if the type is not present we assume it is a material.
  35. package = PackageModel.fromIncompletePackageInformation(package_metadata["display_name"],
  36. package_metadata["package_version"],
  37. package_type)
  38. self.appendItem({"package": package})
  39. self.itemsChanged.emit()