From 6e364f08958466f20144271cc3fc58e30723d3d2 Mon Sep 17 00:00:00 2001 From: Diego Prado Gesto Date: Wed, 13 Jun 2018 10:55:57 +0200 Subject: [PATCH] CURA-5330 Fix typing and code-style in the ContainerNode and all the related children, and QualityGroup and its children. Also fix the related calls in the managers. --- cura/Machines/ContainerNode.py | 6 +++--- cura/Machines/MaterialGroup.py | 2 +- cura/Machines/MaterialNode.py | 8 +++----- .../Models/QualityProfilesDropDownMenuModel.py | 2 +- cura/Machines/QualityChangesGroup.py | 11 ++++++++--- cura/Machines/QualityGroup.py | 12 +++++++----- cura/Machines/QualityManager.py | 16 +++++++++------- cura/Machines/QualityNode.py | 6 +++--- cura/PrinterOutput/GenericOutputController.py | 12 +++++++----- cura/Settings/MachineManager.py | 3 ++- 10 files changed, 44 insertions(+), 34 deletions(-) diff --git a/cura/Machines/ContainerNode.py b/cura/Machines/ContainerNode.py index ff2fe0216e..14739eedd8 100644 --- a/cura/Machines/ContainerNode.py +++ b/cura/Machines/ContainerNode.py @@ -23,10 +23,10 @@ from UM.Settings.InstanceContainer import InstanceContainer class ContainerNode: __slots__ = ("metadata", "container", "children_map") - def __init__(self, metadata: Optional[Dict[str, Any]] = None): + def __init__(self, metadata: Optional[Dict[str, Any]] = None) -> None: self.metadata = metadata self.container = None - self.children_map = OrderedDict() + self.children_map = OrderedDict() #type: OrderedDict[str, ContainerNode] ## Get an entry value from the metadata def getMetaDataEntry(self, entry: str, default: Any = None) -> Any: @@ -56,4 +56,4 @@ class ContainerNode: return self.container def __str__(self) -> str: - return "%s[%s]" % (self.__class__.__name__, self.metadata.get("id")) + return "%s[%s]" % (self.__class__.__name__, self.getMetaDataEntry("id")) diff --git a/cura/Machines/MaterialGroup.py b/cura/Machines/MaterialGroup.py index 93c8a227a8..eea9e41d62 100644 --- a/cura/Machines/MaterialGroup.py +++ b/cura/Machines/MaterialGroup.py @@ -18,7 +18,7 @@ from cura.Machines.MaterialNode import MaterialNode #For type checking. class MaterialGroup: __slots__ = ("name", "is_read_only", "root_material_node", "derived_material_node_list") - def __init__(self, name: str, root_material_node: MaterialNode): + def __init__(self, name: str, root_material_node: MaterialNode) -> None: self.name = name self.is_read_only = False self.root_material_node = root_material_node diff --git a/cura/Machines/MaterialNode.py b/cura/Machines/MaterialNode.py index fde11186c2..04423d7b2c 100644 --- a/cura/Machines/MaterialNode.py +++ b/cura/Machines/MaterialNode.py @@ -1,7 +1,6 @@ # Copyright (c) 2018 Ultimaker B.V. # Cura is released under the terms of the LGPLv3 or higher. - -from typing import Optional +from typing import Optional, Dict from .ContainerNode import ContainerNode @@ -15,7 +14,6 @@ from .ContainerNode import ContainerNode class MaterialNode(ContainerNode): __slots__ = ("material_map", "children_map") - def __init__(self, metadata: Optional[dict] = None): + def __init__(self, metadata: Optional[dict] = None) -> None: super().__init__(metadata = metadata) - self.material_map = {} # material_root_id -> material_node - self.children_map = {} # mapping for the child nodes + self.material_map = {} # type: Dict[str, MaterialNode] # material_root_id -> material_node diff --git a/cura/Machines/Models/QualityProfilesDropDownMenuModel.py b/cura/Machines/Models/QualityProfilesDropDownMenuModel.py index ec8236e887..02de23b10a 100644 --- a/cura/Machines/Models/QualityProfilesDropDownMenuModel.py +++ b/cura/Machines/Models/QualityProfilesDropDownMenuModel.py @@ -99,7 +99,7 @@ class QualityProfilesDropDownMenuModel(ListModel): container = quality_group.node_for_global.getContainer() layer_height = default_layer_height - if container.hasProperty("layer_height", "value"): + if container and container.hasProperty("layer_height", "value"): layer_height = container.getProperty("layer_height", "value") else: # Look for layer_height in the GlobalStack from material -> definition diff --git a/cura/Machines/QualityChangesGroup.py b/cura/Machines/QualityChangesGroup.py index 5ff5af3657..2d0e655ed8 100644 --- a/cura/Machines/QualityChangesGroup.py +++ b/cura/Machines/QualityChangesGroup.py @@ -1,22 +1,27 @@ # Copyright (c) 2018 Ultimaker B.V. # Cura is released under the terms of the LGPLv3 or higher. +from typing import TYPE_CHECKING + from UM.Application import Application from UM.ConfigurationErrorMessage import ConfigurationErrorMessage from .QualityGroup import QualityGroup +if TYPE_CHECKING: + from cura.Machines.QualityNode import QualityNode + class QualityChangesGroup(QualityGroup): - def __init__(self, name: str, quality_type: str, parent = None): + def __init__(self, name: str, quality_type: str, parent = None) -> None: super().__init__(name, quality_type, parent) self._container_registry = Application.getInstance().getContainerRegistry() def addNode(self, node: "QualityNode"): - extruder_position = node.metadata.get("position") + extruder_position = node.getMetaDataEntry("position") if extruder_position is None and self.node_for_global is not None or extruder_position in self.nodes_for_extruders: #We would be overwriting another node. - ConfigurationErrorMessage.getInstance().addFaultyContainers(node.metadata["id"]) + ConfigurationErrorMessage.getInstance().addFaultyContainers(node.getMetaDataEntry("id")) return if extruder_position is None: #Then we're a global quality changes profile. diff --git a/cura/Machines/QualityGroup.py b/cura/Machines/QualityGroup.py index b1c564fadf..90ef63af51 100644 --- a/cura/Machines/QualityGroup.py +++ b/cura/Machines/QualityGroup.py @@ -4,7 +4,7 @@ from typing import Dict, Optional, List, Set from PyQt5.QtCore import QObject, pyqtSlot - +from cura.Machines.ContainerNode import ContainerNode # # A QualityGroup represents a group of containers that must be applied to each ContainerStack when it's used. @@ -24,8 +24,8 @@ class QualityGroup(QObject): def __init__(self, name: str, quality_type: str, parent = None) -> None: super().__init__(parent) self.name = name - self.node_for_global = None # type: Optional["QualityGroup"] - self.nodes_for_extruders = {} # type: Dict[int, "QualityGroup"] + self.node_for_global = None # type: Optional[ContainerNode] + self.nodes_for_extruders = {} # type: Dict[int, ContainerNode] self.quality_type = quality_type self.is_available = False @@ -38,10 +38,12 @@ class QualityGroup(QObject): for node in [self.node_for_global] + list(self.nodes_for_extruders.values()): if node is None: continue - result.update(node.getContainer().getAllKeys()) + container = node.getContainer() + if container: + result.update(container.getAllKeys()) return result - def getAllNodes(self) -> List["QualityGroup"]: + def getAllNodes(self) -> List[ContainerNode]: result = [] if self.node_for_global is not None: result.append(self.node_for_global) diff --git a/cura/Machines/QualityManager.py b/cura/Machines/QualityManager.py index cb0a2f5922..8033057f77 100644 --- a/cura/Machines/QualityManager.py +++ b/cura/Machines/QualityManager.py @@ -1,7 +1,7 @@ # Copyright (c) 2018 Ultimaker B.V. # Cura is released under the terms of the LGPLv3 or higher. -from typing import TYPE_CHECKING, Optional +from typing import TYPE_CHECKING, Optional, cast from PyQt5.QtCore import QObject, QTimer, pyqtSignal, pyqtSlot @@ -90,7 +90,7 @@ class QualityManager(QObject): if definition_id not in self._machine_variant_material_quality_type_to_quality_dict: self._machine_variant_material_quality_type_to_quality_dict[definition_id] = QualityNode() - machine_node = self._machine_variant_material_quality_type_to_quality_dict[definition_id] + machine_node = cast(QualityNode, self._machine_variant_material_quality_type_to_quality_dict[definition_id]) if is_global_quality: # For global qualities, save data in the machine node @@ -102,7 +102,7 @@ class QualityManager(QObject): # too. if variant_name not in machine_node.children_map: machine_node.children_map[variant_name] = QualityNode() - variant_node = machine_node.children_map[variant_name] + variant_node = cast(QualityNode, machine_node.children_map[variant_name]) if root_material_id is None: # If only variant_name is specified but material is not, add the quality/quality_changes metadata @@ -114,7 +114,7 @@ class QualityManager(QObject): # material node. if root_material_id not in variant_node.children_map: variant_node.children_map[root_material_id] = QualityNode() - material_node = variant_node.children_map[root_material_id] + material_node = cast(QualityNode, variant_node.children_map[root_material_id]) material_node.addQualityMetadata(quality_type, metadata) @@ -123,7 +123,7 @@ class QualityManager(QObject): if root_material_id is not None: if root_material_id not in machine_node.children_map: machine_node.children_map[root_material_id] = QualityNode() - material_node = machine_node.children_map[root_material_id] + material_node = cast(QualityNode, machine_node.children_map[root_material_id]) material_node.addQualityMetadata(quality_type, metadata) @@ -351,7 +351,7 @@ class QualityManager(QObject): def removeQualityChangesGroup(self, quality_changes_group: "QualityChangesGroup"): Logger.log("i", "Removing quality changes group [%s]", quality_changes_group.name) for node in quality_changes_group.getAllNodes(): - self._container_registry.removeContainer(node.metadata["id"]) + self._container_registry.removeContainer(node.getMetaDataEntry("id")) # # Rename a set of quality changes containers. Returns the new name. @@ -365,7 +365,9 @@ class QualityManager(QObject): new_name = self._container_registry.uniqueName(new_name) for node in quality_changes_group.getAllNodes(): - node.getContainer().setName(new_name) + container = node.getContainer() + if container: + container.setName(new_name) quality_changes_group.name = new_name diff --git a/cura/Machines/QualityNode.py b/cura/Machines/QualityNode.py index a30e219da3..922227b022 100644 --- a/cura/Machines/QualityNode.py +++ b/cura/Machines/QualityNode.py @@ -1,7 +1,7 @@ # Copyright (c) 2018 Ultimaker B.V. # Cura is released under the terms of the LGPLv3 or higher. -from typing import Optional +from typing import Optional, Dict from .ContainerNode import ContainerNode from .QualityChangesGroup import QualityChangesGroup @@ -12,9 +12,9 @@ from .QualityChangesGroup import QualityChangesGroup # class QualityNode(ContainerNode): - def __init__(self, metadata: Optional[dict] = None): + def __init__(self, metadata: Optional[dict] = None) -> None: super().__init__(metadata = metadata) - self.quality_type_map = {} # quality_type -> QualityNode for InstanceContainer + self.quality_type_map = {} # type: Dict[str, QualityNode] # quality_type -> QualityNode for InstanceContainer def addQualityMetadata(self, quality_type: str, metadata: dict): if quality_type not in self.quality_type_map: diff --git a/cura/PrinterOutput/GenericOutputController.py b/cura/PrinterOutput/GenericOutputController.py index 3e7793b61c..e6310e5bff 100644 --- a/cura/PrinterOutput/GenericOutputController.py +++ b/cura/PrinterOutput/GenericOutputController.py @@ -1,13 +1,15 @@ # Copyright (c) 2018 Ultimaker B.V. # Cura is released under the terms of the LGPLv3 or higher. -from cura.PrinterOutput.ExtruderOutputModel import ExtruderOutputModel + +from typing import TYPE_CHECKING + from cura.PrinterOutput.PrinterOutputController import PrinterOutputController from PyQt5.QtCore import QTimer -MYPY = False -if MYPY: +if TYPE_CHECKING: from cura.PrinterOutput.PrintJobOutputModel import PrintJobOutputModel from cura.PrinterOutput.PrinterOutputModel import PrinterOutputModel + from cura.PrinterOutput.ExtruderOutputModel import ExtruderOutputModel class GenericOutputController(PrinterOutputController): @@ -121,7 +123,7 @@ class GenericOutputController(PrinterOutputController): if not self._preheat_hotends: self._preheat_hotends_timer.stop() - def preheatHotend(self, extruder: ExtruderOutputModel, temperature, duration): + def preheatHotend(self, extruder: "ExtruderOutputModel", temperature, duration): position = extruder.getPosition() number_of_extruders = len(extruder.getPrinter().extruders) if position >= number_of_extruders: @@ -139,7 +141,7 @@ class GenericOutputController(PrinterOutputController): self._preheat_hotends.add(extruder) extruder.updateIsPreheating(True) - def cancelPreheatHotend(self, extruder: ExtruderOutputModel): + def cancelPreheatHotend(self, extruder: "ExtruderOutputModel"): self.setTargetHotendTemperature(extruder.getPrinter(), extruder.getPosition(), temperature=0) if extruder in self._preheat_hotends: extruder.updateIsPreheating(False) diff --git a/cura/Settings/MachineManager.py b/cura/Settings/MachineManager.py index 2df7ce1f15..8f521f559a 100755 --- a/cura/Settings/MachineManager.py +++ b/cura/Settings/MachineManager.py @@ -1109,7 +1109,8 @@ class MachineManager(QObject): nodes = [quality_changes_group.node_for_global] + list(quality_changes_group.nodes_for_extruders.values()) containers = [n.getContainer() for n in nodes if n is not None] for container in containers: - container.setMetaDataEntry("quality_type", "not_supported") + if container: + container.setMetaDataEntry("quality_type", "not_supported") quality_changes_group.quality_type = "not_supported" def _setQualityChangesGroup(self, quality_changes_group: QualityChangesGroup) -> None: