From 6555f4d4f3599c9aaa806db77f57f8921c93d4d7 Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Wed, 17 Oct 2018 13:31:03 +0200 Subject: [PATCH] Add typing Because of boyscouting. CURA-5814 --- cura/PrintInformation.py | 103 ++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 46 deletions(-) diff --git a/cura/PrintInformation.py b/cura/PrintInformation.py index 85cf6651fa..9cbe46ebb1 100644 --- a/cura/PrintInformation.py +++ b/cura/PrintInformation.py @@ -6,7 +6,7 @@ import math import os import unicodedata import re # To create abbreviations for printer names. -from typing import Dict +from typing import Dict, List, Optional from PyQt5.QtCore import QObject, pyqtSignal, pyqtProperty, pyqtSlot @@ -16,6 +16,12 @@ from UM.Scene.SceneNode import SceneNode from UM.i18n import i18nCatalog from UM.MimeTypeDatabase import MimeTypeDatabase + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from cura.CuraApplication import CuraApplication + catalog = i18nCatalog("cura") @@ -47,24 +53,26 @@ class PrintInformation(QObject): ActiveMachineChanged = 3 Other = 4 - def __init__(self, application, parent = None): + UNTITLED_JOB_NAME = "Untitled" + + def __init__(self, application: "CuraApplication", parent = None) -> None: super().__init__(parent) self._application = application - self.UNTITLED_JOB_NAME = "Untitled" - self.initializeCuraMessagePrintTimeProperties() - self._material_lengths = {} # indexed by build plate number - self._material_weights = {} - self._material_costs = {} - self._material_names = {} + # Indexed by build plate number + self._material_lengths = {} # type: Dict[int, List[float]] + self._material_weights = {} # type: Dict[int, List[float]] + self._material_costs = {} # type: Dict[int, List[float]] + self._material_names = {} # type: Dict[int, List[str]] self._pre_sliced = False self._backend = self._application.getBackend() if self._backend: self._backend.printDurationMessage.connect(self._onPrintDurationMessage) + self._application.getController().getScene().sceneChanged.connect(self._onSceneChanged) self._is_user_specified_job_name = False @@ -76,25 +84,23 @@ class PrintInformation(QObject): self._multi_build_plate_model = self._application.getMultiBuildPlateModel() - ss = self._multi_build_plate_model.maxBuildPlate - self._application.globalContainerStackChanged.connect(self._updateJobName) self._application.globalContainerStackChanged.connect(self.setToZeroPrintInformation) self._application.fileLoaded.connect(self.setBaseName) self._application.workspaceLoaded.connect(self.setProjectName) - self._multi_build_plate_model.activeBuildPlateChanged.connect(self._onActiveBuildPlateChanged) - + self._application.getMachineManager().rootMaterialChanged.connect(self._onActiveMaterialsChanged) self._application.getInstance().getPreferences().preferenceChanged.connect(self._onPreferencesChanged) - self._application.getMachineManager().rootMaterialChanged.connect(self._onActiveMaterialsChanged) + self._multi_build_plate_model.activeBuildPlateChanged.connect(self._onActiveBuildPlateChanged) + self._onActiveMaterialsChanged() - self._material_amounts = [] + self._material_amounts = [] # type: List[float] # Crate cura message translations and using translation keys initialize empty time Duration object for total time # and time for each feature - def initializeCuraMessagePrintTimeProperties(self): - self._current_print_time = {} # Duration(None, self) + def initializeCuraMessagePrintTimeProperties(self) -> None: + self._current_print_time = {} # type: Dict[int, Duration] self._print_time_message_translations = { "inset_0": catalog.i18nc("@tooltip", "Outer Wall"), @@ -110,15 +116,15 @@ class PrintInformation(QObject): "none": catalog.i18nc("@tooltip", "Other") } - self._print_time_message_values = {} + self._print_time_message_values = {} # type: Dict[int, Dict[str, Duration]] - def _initPrintTimeMessageValues(self, build_plate_number): + def _initPrintTimeMessageValues(self, build_plate_number: int) -> None: # Full fill message values using keys from _print_time_message_translations self._print_time_message_values[build_plate_number] = {} for key in self._print_time_message_translations.keys(): self._print_time_message_values[build_plate_number][key] = Duration(None, self) - def _initVariablesWithBuildPlate(self, build_plate_number): + def _initVariablesWithBuildPlate(self, build_plate_number: int) -> None: if build_plate_number not in self._print_time_message_values: self._initPrintTimeMessageValues(build_plate_number) if self._active_build_plate not in self._material_lengths: @@ -130,23 +136,24 @@ class PrintInformation(QObject): if self._active_build_plate not in self._material_names: self._material_names[self._active_build_plate] = [] if self._active_build_plate not in self._current_print_time: - self._current_print_time[self._active_build_plate] = Duration(None, self) + self._current_print_time[self._active_build_plate] = Duration(parent = self) currentPrintTimeChanged = pyqtSignal() preSlicedChanged = pyqtSignal() @pyqtProperty(bool, notify=preSlicedChanged) - def preSliced(self): + def preSliced(self) -> bool: return self._pre_sliced - def setPreSliced(self, pre_sliced): - self._pre_sliced = pre_sliced - self._updateJobName() - self.preSlicedChanged.emit() + def setPreSliced(self, pre_sliced: bool) -> None: + if self._pre_sliced != pre_sliced: + self._pre_sliced = pre_sliced + self._updateJobName() + self.preSlicedChanged.emit() @pyqtProperty(Duration, notify = currentPrintTimeChanged) - def currentPrintTime(self): + def currentPrintTime(self) -> Duration: return self._current_print_time[self._active_build_plate] materialLengthsChanged = pyqtSignal() @@ -176,33 +183,37 @@ class PrintInformation(QObject): def printTimes(self): return self._print_time_message_values[self._active_build_plate] - def _onPrintDurationMessage(self, build_plate_number, print_time: Dict[str, int], material_amounts: list): + def _onPrintDurationMessage(self, build_plate_number: int, print_time: Dict[str, int], material_amounts: List[float]) -> None: self._updateTotalPrintTimePerFeature(build_plate_number, print_time) self.currentPrintTimeChanged.emit() self._material_amounts = material_amounts self._calculateInformation(build_plate_number) - def _updateTotalPrintTimePerFeature(self, build_plate_number, print_time: Dict[str, int]): + def _updateTotalPrintTimePerFeature(self, build_plate_number: int, print_times: Dict[str, int]) -> None: total_estimated_time = 0 if build_plate_number not in self._print_time_message_values: self._initPrintTimeMessageValues(build_plate_number) - for feature, time in print_time.items(): + for feature, time in print_times.items(): + if feature not in self._print_time_message_values[build_plate_number]: + self._print_time_message_values[build_plate_number][feature] = Duration(parent=self) + duration = self._print_time_message_values[build_plate_number][feature] + if time != time: # Check for NaN. Engine can sometimes give us weird values. - self._print_time_message_values[build_plate_number].get(feature).setDuration(0) + duration.setDuration(0) Logger.log("w", "Received NaN for print duration message") continue total_estimated_time += time - self._print_time_message_values[build_plate_number].get(feature).setDuration(time) + duration.setDuration(time) if build_plate_number not in self._current_print_time: self._current_print_time[build_plate_number] = Duration(None, self) self._current_print_time[build_plate_number].setDuration(total_estimated_time) - def _calculateInformation(self, build_plate_number): + def _calculateInformation(self, build_plate_number: int) -> None: global_stack = self._application.getGlobalContainerStack() if global_stack is None: return @@ -227,7 +238,7 @@ class PrintInformation(QObject): radius = extruder_stack.getProperty("material_diameter", "value") / 2 weight = float(amount) * float(density) / 1000 - cost = 0 + cost = 0. material_name = catalog.i18nc("@label unknown material", "Unknown") if material: material_guid = material.getMetaDataEntry("GUID") @@ -258,14 +269,14 @@ class PrintInformation(QObject): self.materialCostsChanged.emit() self.materialNamesChanged.emit() - def _onPreferencesChanged(self, preference): + def _onPreferencesChanged(self, preference: str) -> None: if preference != "cura/material_settings": return for build_plate_number in range(self._multi_build_plate_model.maxBuildPlate + 1): self._calculateInformation(build_plate_number) - def _onActiveBuildPlateChanged(self): + def _onActiveBuildPlateChanged(self) -> None: new_active_build_plate = self._multi_build_plate_model.activeBuildPlate if new_active_build_plate != self._active_build_plate: self._active_build_plate = new_active_build_plate @@ -279,14 +290,14 @@ class PrintInformation(QObject): self.materialNamesChanged.emit() self.currentPrintTimeChanged.emit() - def _onActiveMaterialsChanged(self, *args, **kwargs): + def _onActiveMaterialsChanged(self, *args, **kwargs) -> None: for build_plate_number in range(self._multi_build_plate_model.maxBuildPlate + 1): self._calculateInformation(build_plate_number) # Manual override of job name should also set the base name so that when the printer prefix is updated, it the # prefix can be added to the manually added name, not the old base name @pyqtSlot(str, bool) - def setJobName(self, name, is_user_specified_job_name = False): + def setJobName(self, name: str, is_user_specified_job_name = False) -> None: self._is_user_specified_job_name = is_user_specified_job_name self._job_name = name self._base_name = name.replace(self._abbr_machine + "_", "") @@ -300,7 +311,7 @@ class PrintInformation(QObject): def jobName(self): return self._job_name - def _updateJobName(self): + def _updateJobName(self) -> None: if self._base_name == "": self._job_name = self.UNTITLED_JOB_NAME self._is_user_specified_job_name = False @@ -335,12 +346,12 @@ class PrintInformation(QObject): self.jobNameChanged.emit() @pyqtSlot(str) - def setProjectName(self, name): + def setProjectName(self, name: str) -> None: self.setBaseName(name, is_project_file = True) baseNameChanged = pyqtSignal() - def setBaseName(self, base_name: str, is_project_file: bool = False): + def setBaseName(self, base_name: str, is_project_file: bool = False) -> None: self._is_user_specified_job_name = False # Ensure that we don't use entire path but only filename @@ -384,7 +395,7 @@ class PrintInformation(QObject): ## Created an acronym-like abbreviated machine name from the currently # active machine name. # Called each time the global stack is switched. - def _defineAbbreviatedMachineName(self): + def _defineAbbreviatedMachineName(self) -> None: global_container_stack = self._application.getGlobalContainerStack() if not global_container_stack: self._abbr_machine = "" @@ -408,8 +419,8 @@ class PrintInformation(QObject): self._abbr_machine = abbr_machine ## Utility method that strips accents from characters (eg: รข -> a) - def _stripAccents(self, str): - return ''.join(char for char in unicodedata.normalize('NFD', str) if unicodedata.category(char) != 'Mn') + def _stripAccents(self, to_strip: str) -> str: + return ''.join(char for char in unicodedata.normalize('NFD', to_strip) if unicodedata.category(char) != 'Mn') @pyqtSlot(result = "QVariantMap") def getFeaturePrintTimes(self): @@ -424,7 +435,7 @@ class PrintInformation(QObject): return result # Simulate message with zero time duration - def setToZeroPrintInformation(self, build_plate = None): + def setToZeroPrintInformation(self, build_plate: Optional[int] = None) -> None: if build_plate is None: build_plate = self._active_build_plate @@ -434,12 +445,12 @@ class PrintInformation(QObject): self._print_time_message_values[build_plate] = {} for key in self._print_time_message_values[build_plate].keys(): temp_message[key] = 0 - temp_material_amounts = [0] + temp_material_amounts = [0.] self._onPrintDurationMessage(build_plate, temp_message, temp_material_amounts) ## Listen to scene changes to check if we need to reset the print information - def _onSceneChanged(self, scene_node): + def _onSceneChanged(self, scene_node: SceneNode) -> None: # Ignore any changes that are not related to sliceable objects if not isinstance(scene_node, SceneNode)\ or not scene_node.callDecoration("isSliceable")\