Add typing and always add error message if loading failed

There were some places where it would return None. Then in the QML it would give a QML error that the null object has no dictionary items.

Contributes to issue CURA-5929.
This commit is contained in:
Ghostkeeper 2018-11-12 11:02:43 +01:00
parent 4551b73d5c
commit 9c555bf67f
No known key found for this signature in database
GPG key ID: 86BEF881AE2CF276
2 changed files with 11 additions and 12 deletions

View file

@ -419,13 +419,13 @@ class ContainerManager(QObject):
self._container_name_filters[name_filter] = entry self._container_name_filters[name_filter] = entry
## Import single profile, file_url does not have to end with curaprofile ## Import single profile, file_url does not have to end with curaprofile
@pyqtSlot(QUrl, result="QVariantMap") @pyqtSlot(QUrl, result = "QVariantMap")
def importProfile(self, file_url: QUrl): def importProfile(self, file_url: QUrl) -> Dict[str, str]:
if not file_url.isValid(): if not file_url.isValid():
return return {"status": "error", "message": catalog.i18nc("@info:status", "Invalid file URL:") + " " + file_url}
path = file_url.toLocalFile() path = file_url.toLocalFile()
if not path: if not path:
return return {"status": "error", "message": catalog.i18nc("@info:status", "Invalid file URL:") + " " + file_url}
return self._container_registry.importProfile(path) return self._container_registry.importProfile(path)
@pyqtSlot(QObject, QUrl, str) @pyqtSlot(QObject, QUrl, str)

View file

@ -5,8 +5,7 @@ import os
import re import re
import configparser import configparser
from typing import cast, Optional from typing import cast, Dict, Optional
from PyQt5.QtWidgets import QMessageBox from PyQt5.QtWidgets import QMessageBox
from UM.Decorators import override from UM.Decorators import override
@ -161,20 +160,20 @@ class CuraContainerRegistry(ContainerRegistry):
## Imports a profile from a file ## Imports a profile from a file
# #
# \param file_name \type{str} the full path and filename of the profile to import # \param file_name The full path and filename of the profile to import.
# \return \type{Dict} dict with a 'status' key containing the string 'ok' or 'error', and a 'message' key # \return Dict with a 'status' key containing the string 'ok' or 'error',
# containing a message for the user # and a 'message' key containing a message for the user.
def importProfile(self, file_name): def importProfile(self, file_name: str) -> Dict[str, str]:
Logger.log("d", "Attempting to import profile %s", file_name) Logger.log("d", "Attempting to import profile %s", file_name)
if not file_name: if not file_name:
return { "status": "error", "message": catalog.i18nc("@info:status Don't translate the XML tags <filename> or <message>!", "Failed to import profile from <filename>{0}</filename>: <message>{1}</message>", file_name, "Invalid path")} return { "status": "error", "message": catalog.i18nc("@info:status Don't translate the XML tags <filename>!", "Failed to import profile from <filename>{0}</filename>: {1}", file_name, "Invalid path")}
plugin_registry = PluginRegistry.getInstance() plugin_registry = PluginRegistry.getInstance()
extension = file_name.split(".")[-1] extension = file_name.split(".")[-1]
global_stack = Application.getInstance().getGlobalContainerStack() global_stack = Application.getInstance().getGlobalContainerStack()
if not global_stack: if not global_stack:
return return {"status": "error", "message": catalog.i18nc("@info:status Don't translate the XML tags <filename>!", "Can't import profile from <filename>{0}</filename> before a printer is added.", file_name)}
machine_extruders = [] machine_extruders = []
for position in sorted(global_stack.extruders): for position in sorted(global_stack.extruders):