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.
# 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 PyQt5.QtCore import pyqtSlot, Qt
from UM.i18n import i18nCatalog
@ -34,7 +34,7 @@ class LocalPackageList(PackageList):
def __init__(self, parent: "QObject" = None) -> None:
super().__init__(parent)
self._application = CuraApplication.getInstance()
self._manager = CuraApplication.getInstance().getPackageManager()
self._has_footer = False
@pyqtSlot()
@ -50,52 +50,52 @@ class LocalPackageList(PackageList):
def _getLocalPackages(self) -> None:
""" Obtain the local 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
"""
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():
packages = filter(lambda p: p["section_title"] == section, self._allPackageInfo())
sorted_sections[section] = sorted(packages, key = lambda p: p["display_name"])
packages = filter(lambda p: p.sectionTitle == section, self._allPackageInfo())
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 package_data in section:
package = PackageModel(package_data, parent = self)
self.appendItem({"package": package})
self.appendItem({"package": package_data})
self.setIsLoading(False)
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
correct order"""
for package_type in self.PACKAGE_SECTION_HEADER.values():
for section in package_type.values():
yield section
def _allPackageInfo(self) -> Generator[Dict[str, Any]]:
""" A generator which returns a unordered list of package_info, the section_title is appended to the each
package_info"""
manager = self._application.getPackageManager()
def _allPackageInfo(self) -> Generator[PackageModel, None, None]:
""" A generator which returns a unordered list of all the PackageModels"""
# 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 package_data in packages:
bundled_or_installed = "installed" if manager.isUserInstalledPackage(package_data["package_id"]) else "bundled"
package_data["section_title"] = self.PACKAGE_SECTION_HEADER[bundled_or_installed][package_type]
yield package_data
for packages in self._manager.getAllInstalledPackagesInfo().values():
for package_info in packages:
yield self._makePackageModel(package_info)
# 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
for package_data in manager.getPackagesToRemove().values():
yield package_data["package_info"]
# still want to interact with these.
for package_data in self._manager.getPackagesToRemove().values():
yield self._makePackageModel(package_data["package_info"])
for package_data in manager.getPackagesToInstall().values():
package_info = package_data["package_info"]
# Get all to be installed package_info's. Since the user might want to interact with these
for package_data in self._manager.getPackagesToInstall().values():
yield self._makePackageModel(package_data["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"]
package_info["section_title"] = self.PACKAGE_SECTION_HEADER["installed"][package_type]
yield package_info
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.
"""
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.
: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).
"""
super().__init__(parent)
self._package_id = package_data.get("package_id", "UnknownPackageId")
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)
def packageId(self) -> str: