Added missing typing

This commit is contained in:
Jaime van Kessel 2018-05-03 20:42:06 +02:00
parent 34483b4b34
commit 7d2257838b
2 changed files with 36 additions and 37 deletions

View file

@ -15,6 +15,7 @@ from UM.Logger import Logger
from UM.Resources import Resources from UM.Resources import Resources
from UM.Version import Version from UM.Version import Version
class CuraPackageManager(QObject): class CuraPackageManager(QObject):
Version = 1 Version = 1

View file

@ -1,7 +1,7 @@
# Copyright (c) 2018 Ultimaker B.V. # Copyright (c) 2018 Ultimaker B.V.
# Toolbox is released under the terms of the LGPLv3 or higher. # Toolbox is released under the terms of the LGPLv3 or higher.
from typing import Dict from typing import Dict, Optional, Union, Any
import json import json
import os import os
import tempfile import tempfile
@ -25,9 +25,10 @@ from .ConfigsModel import ConfigsModel
i18n_catalog = i18nCatalog("cura") i18n_catalog = i18nCatalog("cura")
## The Toolbox class is responsible of communicating with the server through the API ## The Toolbox class is responsible of communicating with the server through the API
class Toolbox(QObject, Extension): class Toolbox(QObject, Extension):
def __init__(self, parent=None): def __init__(self, parent=None) -> None:
super().__init__(parent) super().__init__(parent)
self._application = Application.getInstance() self._application = Application.getInstance()
@ -142,7 +143,7 @@ class Toolbox(QObject, Extension):
def getLicenseDialogLicenseContent(self) -> str: def getLicenseDialogLicenseContent(self) -> str:
return self._license_dialog_license_content return self._license_dialog_license_content
def openLicenseDialog(self, plugin_name: str, license_content: str, plugin_file_location: str): def openLicenseDialog(self, plugin_name: str, license_content: str, plugin_file_location: str) -> None:
self._license_dialog_plugin_name = plugin_name self._license_dialog_plugin_name = plugin_name
self._license_dialog_license_content = license_content self._license_dialog_license_content = license_content
self._license_dialog_plugin_file_location = plugin_file_location self._license_dialog_plugin_file_location = plugin_file_location
@ -150,11 +151,11 @@ class Toolbox(QObject, Extension):
# This is a plugin, so most of the components required are not ready when # This is a plugin, so most of the components required are not ready when
# this is initialized. Therefore, we wait until the application is ready. # this is initialized. Therefore, we wait until the application is ready.
def _onAppInitialized(self): def _onAppInitialized(self) -> None:
self._package_manager = Application.getInstance().getCuraPackageManager() self._package_manager = Application.getInstance().getCuraPackageManager()
@pyqtSlot() @pyqtSlot()
def browsePackages(self): def browsePackages(self) -> None:
# Create the network manager: # Create the network manager:
# This was formerly its own function but really had no reason to be as # This was formerly its own function but really had no reason to be as
# it was never called more than once ever. # it was never called more than once ever.
@ -181,14 +182,14 @@ class Toolbox(QObject, Extension):
# Apply enabled/disabled state to installed plugins # Apply enabled/disabled state to installed plugins
self.enabledChanged.emit() self.enabledChanged.emit()
def _createDialog(self, qml_name: str): def _createDialog(self, qml_name: str) -> Optional[QObject]:
Logger.log("d", "Toolbox: Creating dialog [%s].", qml_name) Logger.log("d", "Toolbox: Creating dialog [%s].", qml_name)
path = os.path.join(PluginRegistry.getInstance().getPluginPath(self.getPluginId()), "resources", "qml", qml_name) path = os.path.join(PluginRegistry.getInstance().getPluginPath(self.getPluginId()), "resources", "qml", qml_name)
dialog = Application.getInstance().createQmlComponent(path, {"toolbox": self}) dialog = Application.getInstance().createQmlComponent(path, {"toolbox": self})
return dialog return dialog
@pyqtSlot() @pyqtSlot()
def _updateInstalledModels(self): def _updateInstalledModels(self) -> None:
all_packages = self._package_manager.getAllInstalledPackagesInfo() all_packages = self._package_manager.getAllInstalledPackagesInfo()
if "plugin" in all_packages: if "plugin" in all_packages:
self._metadata["plugins_installed"] = all_packages["plugin"] self._metadata["plugins_installed"] = all_packages["plugin"]
@ -200,7 +201,7 @@ class Toolbox(QObject, Extension):
self.metadataChanged.emit() self.metadataChanged.emit()
@pyqtSlot(str) @pyqtSlot(str)
def install(self, file_path: str): def install(self, file_path: str) -> None:
self._package_manager.installPackage(file_path) self._package_manager.installPackage(file_path)
self.installChanged.emit() self.installChanged.emit()
self._updateInstalledModels() self._updateInstalledModels()
@ -209,7 +210,7 @@ class Toolbox(QObject, Extension):
self.restartRequiredChanged.emit() self.restartRequiredChanged.emit()
@pyqtSlot(str) @pyqtSlot(str)
def uninstall(self, plugin_id: str): def uninstall(self, plugin_id: str) -> None:
self._package_manager.removePackage(plugin_id) self._package_manager.removePackage(plugin_id)
self.installChanged.emit() self.installChanged.emit()
self._updateInstalledModels() self._updateInstalledModels()
@ -218,7 +219,7 @@ class Toolbox(QObject, Extension):
self.restartRequiredChanged.emit() self.restartRequiredChanged.emit()
@pyqtSlot(str) @pyqtSlot(str)
def enable(self, plugin_id: str): def enable(self, plugin_id: str) -> None:
self._plugin_registry.enablePlugin(plugin_id) self._plugin_registry.enablePlugin(plugin_id)
self.enabledChanged.emit() self.enabledChanged.emit()
Logger.log("i", "%s was set as 'active'.", plugin_id) Logger.log("i", "%s was set as 'active'.", plugin_id)
@ -226,7 +227,7 @@ class Toolbox(QObject, Extension):
self.restartRequiredChanged.emit() self.restartRequiredChanged.emit()
@pyqtSlot(str) @pyqtSlot(str)
def disable(self, plugin_id: str): def disable(self, plugin_id: str) -> None:
self._plugin_registry.disablePlugin(plugin_id) self._plugin_registry.disablePlugin(plugin_id)
self.enabledChanged.emit() self.enabledChanged.emit()
Logger.log("i", "%s was set as 'deactive'.", plugin_id) Logger.log("i", "%s was set as 'deactive'.", plugin_id)
@ -234,11 +235,11 @@ class Toolbox(QObject, Extension):
self.restartRequiredChanged.emit() self.restartRequiredChanged.emit()
@pyqtProperty(bool, notify = metadataChanged) @pyqtProperty(bool, notify = metadataChanged)
def dataReady(self): def dataReady(self) -> bool:
return self._packages_model is not None return self._packages_model is not None
@pyqtProperty(bool, notify = restartRequiredChanged) @pyqtProperty(bool, notify = restartRequiredChanged)
def restartRequired(self): def restartRequired(self) -> bool:
return self._restart_required return self._restart_required
@pyqtSlot() @pyqtSlot()
@ -246,8 +247,6 @@ class Toolbox(QObject, Extension):
self._package_manager._removeAllScheduledPackages() self._package_manager._removeAllScheduledPackages()
CuraApplication.getInstance().windowClosed() CuraApplication.getInstance().windowClosed()
# Checks # Checks
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
@pyqtSlot(str, result = bool) @pyqtSlot(str, result = bool)
@ -286,18 +285,16 @@ class Toolbox(QObject, Extension):
return True return True
return False return False
# Make API Calls # Make API Calls
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
def _makeRequestByType(self, type: str): def _makeRequestByType(self, type: str) -> None:
Logger.log("i", "Toolbox: Requesting %s metadata from server.", type) Logger.log("i", "Toolbox: Requesting %s metadata from server.", type)
request = QNetworkRequest(self._request_urls[type]) request = QNetworkRequest(self._request_urls[type])
request.setRawHeader(*self._request_header) request.setRawHeader(*self._request_header)
self._network_manager.get(request) self._network_manager.get(request)
@pyqtSlot(str) @pyqtSlot(str)
def startDownload(self, url: str): def startDownload(self, url: str) -> None:
Logger.log("i", "Toolbox: Attempting to download & install package from %s.", url) Logger.log("i", "Toolbox: Attempting to download & install package from %s.", url)
url = QUrl(url) url = QUrl(url)
self._download_request = QNetworkRequest(url) self._download_request = QNetworkRequest(url)
@ -314,12 +311,11 @@ class Toolbox(QObject, Extension):
self._download_reply.downloadProgress.connect(self._onDownloadProgress) self._download_reply.downloadProgress.connect(self._onDownloadProgress)
@pyqtSlot() @pyqtSlot()
def cancelDownload(self): def cancelDownload(self) -> None:
Logger.log("i", "Toolbox: User cancelled the download of a plugin.") Logger.log("i", "Toolbox: User cancelled the download of a plugin.")
self.resetDownload() self.resetDownload()
return
def resetDownload(self): def resetDownload(self) -> None:
if self._download_reply: if self._download_reply:
self._download_reply.abort() self._download_reply.abort()
self._download_reply.downloadProgress.disconnect(self._onDownloadProgress) self._download_reply.downloadProgress.disconnect(self._onDownloadProgress)
@ -328,15 +324,13 @@ class Toolbox(QObject, Extension):
self.setDownloadProgress(0) self.setDownloadProgress(0)
self.setIsDownloading(False) self.setIsDownloading(False)
# Handlers for Network Events # Handlers for Network Events
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
def _onNetworkAccessibleChanged(self, accessible: int): def _onNetworkAccessibleChanged(self, accessible: int) -> None:
if accessible == 0: if accessible == 0:
self.resetDownload() self.resetDownload()
def _onRequestFinished(self, reply: QNetworkReply): def _onRequestFinished(self, reply: QNetworkReply) -> None:
if reply.error() == QNetworkReply.TimeoutError: if reply.error() == QNetworkReply.TimeoutError:
Logger.log("w", "Got a timeout.") Logger.log("w", "Got a timeout.")
@ -402,21 +396,20 @@ class Toolbox(QObject, Extension):
# Ignore any operation that is not a get operation # Ignore any operation that is not a get operation
pass pass
def _onDownloadProgress(self, bytes_sent: int, bytes_total: int): def _onDownloadProgress(self, bytes_sent: int, bytes_total: int) -> None:
if bytes_total > 0: if bytes_total > 0:
new_progress = bytes_sent / bytes_total * 100 new_progress = bytes_sent / bytes_total * 100
self.setDownloadProgress(new_progress) self.setDownloadProgress(new_progress)
if bytes_sent == bytes_total: if bytes_sent == bytes_total:
self.setIsDownloading(False) self.setIsDownloading(False)
self._download_reply.downloadProgress.disconnect(self._onDownloadProgress) self._download_reply.downloadProgress.disconnect(self._onDownloadProgress)
# must not delete the temporary file on Windows # <ust not delete the temporary file on Windows
self._temp_plugin_file = tempfile.NamedTemporaryFile(mode = "w+b", suffix = ".curapackage", delete = False) self._temp_plugin_file = tempfile.NamedTemporaryFile(mode = "w+b", suffix = ".curapackage", delete = False)
file_path = self._temp_plugin_file.name file_path = self._temp_plugin_file.name
# write first and close, otherwise on Windows, it cannot read the file # Write first and close, otherwise on Windows, it cannot read the file
self._temp_plugin_file.write(self._download_reply.readAll()) self._temp_plugin_file.write(self._download_reply.readAll())
self._temp_plugin_file.close() self._temp_plugin_file.close()
self._onDownloadComplete(file_path) self._onDownloadComplete(file_path)
return
def _onDownloadComplete(self, file_path: str): def _onDownloadComplete(self, file_path: str):
Logger.log("i", "Toolbox: Download complete.") Logger.log("i", "Toolbox: Download complete.")
@ -435,39 +428,44 @@ class Toolbox(QObject, Extension):
# Getter & Setters for Properties: # Getter & Setters for Properties:
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
def setDownloadProgress(self, progress: int): def setDownloadProgress(self, progress: Union[int, float]) -> None:
if progress != self._download_progress: if progress != self._download_progress:
self._download_progress = progress self._download_progress = progress
self.onDownloadProgressChanged.emit() self.onDownloadProgressChanged.emit()
@pyqtProperty(int, fset = setDownloadProgress, notify = onDownloadProgressChanged) @pyqtProperty(int, fset = setDownloadProgress, notify = onDownloadProgressChanged)
def downloadProgress(self) -> int: def downloadProgress(self) -> int:
return self._download_progress return self._download_progress
def setIsDownloading(self, is_downloading: bool): def setIsDownloading(self, is_downloading: bool) -> None:
if self._is_downloading != is_downloading: if self._is_downloading != is_downloading:
self._is_downloading = is_downloading self._is_downloading = is_downloading
self.onIsDownloadingChanged.emit() self.onIsDownloadingChanged.emit()
@pyqtProperty(bool, fset = setIsDownloading, notify = onIsDownloadingChanged) @pyqtProperty(bool, fset = setIsDownloading, notify = onIsDownloadingChanged)
def isDownloading(self) -> bool: def isDownloading(self) -> bool:
return self._is_downloading return self._is_downloading
def setActivePackage(self, package: dict): def setActivePackage(self, package: Dict[str, Any]) -> None:
self._active_package = package self._active_package = package
self.activePackageChanged.emit() self.activePackageChanged.emit()
@pyqtProperty(QObject, fset = setActivePackage, notify = activePackageChanged)
def activePackage(self) -> dict: @pyqtProperty("QVariantMap", fset = setActivePackage, notify = activePackageChanged)
def activePackage(self) -> Optional[Dict[str, Any]]:
return self._active_package return self._active_package
def setViewCategory(self, category: str = "plugin"): def setViewCategory(self, category: str = "plugin") -> None:
self._view_category = category self._view_category = category
self.viewChanged.emit() self.viewChanged.emit()
@pyqtProperty(str, fset = setViewCategory, notify = viewChanged) @pyqtProperty(str, fset = setViewCategory, notify = viewChanged)
def viewCategory(self) -> str: def viewCategory(self) -> str:
return self._view_category return self._view_category
def setViewPage(self, page: str = "overview"): def setViewPage(self, page: str = "overview") -> None:
self._view_page = page self._view_page = page
self.viewChanged.emit() self.viewChanged.emit()
@pyqtProperty(str, fset = setViewPage, notify = viewChanged) @pyqtProperty(str, fset = setViewPage, notify = viewChanged)
def viewPage(self) -> str: def viewPage(self) -> str:
return self._view_page return self._view_page