Don't pollute the package_info with section_title

The previous implementation added a section_title to the package_info
which was also stored in the packages.json. The section_title is now
provided to the PackageModel as an extra optional argument.

Contributes to CURA-8558
This commit is contained in:
j.spijker@ultimaker.com 2021-11-03 14:11:07 +01:00 committed by Jelle Spijker
parent edc71f12a3
commit f9f43b79b0
No known key found for this signature in database
GPG key ID: 6662DC033BE6B99A
2 changed files with 31 additions and 30 deletions

View file

@ -1,8 +1,8 @@
# Copyright (c) 2021 Ultimaker B.V. # Copyright (c) 2021 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
from PyQt5.QtCore import pyqtSlot, Qt
from typing import Any, Dict, Generator, TYPE_CHECKING from typing import Any, Dict, Generator, TYPE_CHECKING
from PyQt5.QtCore import pyqtSlot, Qt
from UM.i18n import i18nCatalog from UM.i18n import i18nCatalog
@ -30,11 +30,11 @@ class LocalPackageList(PackageList):
"plugin": catalog.i18nc("@label:property", "Bundled Plugins"), "plugin": catalog.i18nc("@label:property", "Bundled Plugins"),
"material": catalog.i18nc("@label:property", "Bundled Materials") "material": catalog.i18nc("@label:property", "Bundled Materials")
} }
} # The section headers to be used for the different package categories } # The section headers to be used for the different package categories
def __init__(self, parent: "QObject" = None) -> None: def __init__(self, parent: "QObject" = None) -> None:
super().__init__(parent) super().__init__(parent)
self._application = CuraApplication.getInstance() self._manager = CuraApplication.getInstance().getPackageManager()
self._has_footer = False self._has_footer = False
@pyqtSlot() @pyqtSlot()
@ -50,52 +50,52 @@ class LocalPackageList(PackageList):
def _getLocalPackages(self) -> None: def _getLocalPackages(self) -> None:
""" Obtain the local packages. """ Obtain the local packages.
The list is sorted per category as in the order of the PACKAGE_SECTION_HEADER dictionary, whereas the packages The list is sorted per category as in the order of the PACKAGE_SECTION_HEADER dictionary, whereas the packages
for the sections are sorted alphabetically on the display name for the sections are sorted alphabetically on the display name
""" """
sorted_sections = {} sorted_sections = {}
# Filter the package list per section_title and sort these # Filter the packages per section title and sort these alphabetically
for section in self._getSections(): for section in self._getSections():
packages = filter(lambda p: p["section_title"] == section, self._allPackageInfo()) packages = filter(lambda p: p.sectionTitle == section, self._allPackageInfo())
sorted_sections[section] = sorted(packages, key = lambda p: p["display_name"]) sorted_sections[section] = sorted(packages, key = lambda p: p.displayName)
# Create a PackageModel from the sorted package_info and append them to the list # Append the order PackageModels to the list
for section in sorted_sections.values(): for section in sorted_sections.values():
for package_data in section: for package_data in section:
package = PackageModel(package_data, parent = self) self.appendItem({"package": package_data})
self.appendItem({"package": package})
self.setIsLoading(False) self.setIsLoading(False)
self.setHasMore(False) # All packages should have been loaded at this time self.setHasMore(False) # All packages should have been loaded at this time
def _getSections(self) -> Generator[str]: def _getSections(self) -> Generator[str, None, None]:
""" Flatten and order the PACKAGE_SECTION_HEADER such that it can be used in obtaining the packages in the """ Flatten and order the PACKAGE_SECTION_HEADER such that it can be used in obtaining the packages in the
correct order""" correct order"""
for package_type in self.PACKAGE_SECTION_HEADER.values(): for package_type in self.PACKAGE_SECTION_HEADER.values():
for section in package_type.values(): for section in package_type.values():
yield section yield section
def _allPackageInfo(self) -> Generator[Dict[str, Any]]: def _allPackageInfo(self) -> Generator[PackageModel, None, None]:
""" A generator which returns a unordered list of package_info, the section_title is appended to the each """ A generator which returns a unordered list of all the PackageModels"""
package_info"""
manager = self._application.getPackageManager()
# Get all the installed packages, add a section_title depending on package_type and user installed # Get all the installed packages, add a section_title depending on package_type and user installed
for package_type, packages in manager.getAllInstalledPackagesInfo().items(): for packages in self._manager.getAllInstalledPackagesInfo().values():
for package_data in packages: for package_info in packages:
bundled_or_installed = "installed" if manager.isUserInstalledPackage(package_data["package_id"]) else "bundled" yield self._makePackageModel(package_info)
package_data["section_title"] = self.PACKAGE_SECTION_HEADER[bundled_or_installed][package_type]
yield package_data
# Get all to be removed package_info's. These packages are still used in the current session so the user might # Get all to be removed package_info's. These packages are still used in the current session so the user might
# to interact with these in the list # still want to interact with these.
for package_data in manager.getPackagesToRemove().values(): for package_data in self._manager.getPackagesToRemove().values():
yield package_data["package_info"] yield self._makePackageModel(package_data["package_info"])
for package_data in manager.getPackagesToInstall().values(): # Get all to be installed package_info's. Since the user might want to interact with these
package_info = package_data["package_info"] for package_data in self._manager.getPackagesToInstall().values():
package_type = package_info["package_type"] yield self._makePackageModel(package_data["package_info"])
package_info["section_title"] = self.PACKAGE_SECTION_HEADER["installed"][package_type]
yield package_info def _makePackageModel(self, package_info: Dict[str, Any]) -> PackageModel:
""" Create a PackageModel from the package_info and determine its section_title"""
bundled_or_installed = "installed" if self._manager.isUserInstalledPackage(package_info["package_id"]) else "bundled"
package_type = package_info["package_type"]
section_title = self.PACKAGE_SECTION_HEADER[bundled_or_installed][package_type]
return PackageModel(package_info, section_title = section_title, parent = self)

View file

@ -16,16 +16,17 @@ class PackageModel(QObject):
QML. The model can also be constructed directly from a response received by the API. QML. The model can also be constructed directly from a response received by the API.
""" """
def __init__(self, package_data: Dict[str, Any], parent: QObject = None) -> None: def __init__(self, package_data: Dict[str, Any], section_title: Optional[str] = None, parent: Optional[QObject] = None) -> None:
""" """
Constructs a new model for a single package. Constructs a new model for a single package.
:param package_data: The data received from the Marketplace API about the package to create. :param package_data: The data received from the Marketplace API about the package to create.
:param section_title: If the packages are to be categorized per section provide the section_title
:param parent: The parent QML object that controls the lifetime of this model (normally a PackageList). :param parent: The parent QML object that controls the lifetime of this model (normally a PackageList).
""" """
super().__init__(parent) super().__init__(parent)
self._package_id = package_data.get("package_id", "UnknownPackageId") self._package_id = package_data.get("package_id", "UnknownPackageId")
self._display_name = package_data.get("display_name", catalog.i18nc("@label:property", "Unknown Package")) self._display_name = package_data.get("display_name", catalog.i18nc("@label:property", "Unknown Package"))
self._section_title = package_data.get("section_title", None) self._section_title = section_title
@pyqtProperty(str, constant = True) @pyqtProperty(str, constant = True)
def packageId(self) -> str: def packageId(self) -> str: