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.
This commit is contained in:
Diego Prado Gesto 2018-06-13 10:55:57 +02:00
parent f860b9c99e
commit 6e364f0895
10 changed files with 44 additions and 34 deletions

View file

@ -23,10 +23,10 @@ from UM.Settings.InstanceContainer import InstanceContainer
class ContainerNode: class ContainerNode:
__slots__ = ("metadata", "container", "children_map") __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.metadata = metadata
self.container = None self.container = None
self.children_map = OrderedDict() self.children_map = OrderedDict() #type: OrderedDict[str, ContainerNode]
## Get an entry value from the metadata ## Get an entry value from the metadata
def getMetaDataEntry(self, entry: str, default: Any = None) -> Any: def getMetaDataEntry(self, entry: str, default: Any = None) -> Any:
@ -56,4 +56,4 @@ class ContainerNode:
return self.container return self.container
def __str__(self) -> str: def __str__(self) -> str:
return "%s[%s]" % (self.__class__.__name__, self.metadata.get("id")) return "%s[%s]" % (self.__class__.__name__, self.getMetaDataEntry("id"))

View file

@ -18,7 +18,7 @@ from cura.Machines.MaterialNode import MaterialNode #For type checking.
class MaterialGroup: class MaterialGroup:
__slots__ = ("name", "is_read_only", "root_material_node", "derived_material_node_list") __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.name = name
self.is_read_only = False self.is_read_only = False
self.root_material_node = root_material_node self.root_material_node = root_material_node

View file

@ -1,7 +1,6 @@
# Copyright (c) 2018 Ultimaker B.V. # Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
from typing import Optional, Dict
from typing import Optional
from .ContainerNode import ContainerNode from .ContainerNode import ContainerNode
@ -15,7 +14,6 @@ from .ContainerNode import ContainerNode
class MaterialNode(ContainerNode): class MaterialNode(ContainerNode):
__slots__ = ("material_map", "children_map") __slots__ = ("material_map", "children_map")
def __init__(self, metadata: Optional[dict] = None): def __init__(self, metadata: Optional[dict] = None) -> None:
super().__init__(metadata = metadata) super().__init__(metadata = metadata)
self.material_map = {} # material_root_id -> material_node self.material_map = {} # type: Dict[str, MaterialNode] # material_root_id -> material_node
self.children_map = {} # mapping for the child nodes

View file

@ -99,7 +99,7 @@ class QualityProfilesDropDownMenuModel(ListModel):
container = quality_group.node_for_global.getContainer() container = quality_group.node_for_global.getContainer()
layer_height = default_layer_height 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") layer_height = container.getProperty("layer_height", "value")
else: else:
# Look for layer_height in the GlobalStack from material -> definition # Look for layer_height in the GlobalStack from material -> definition

View file

@ -1,22 +1,27 @@
# Copyright (c) 2018 Ultimaker B.V. # Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
from typing import TYPE_CHECKING
from UM.Application import Application from UM.Application import Application
from UM.ConfigurationErrorMessage import ConfigurationErrorMessage from UM.ConfigurationErrorMessage import ConfigurationErrorMessage
from .QualityGroup import QualityGroup from .QualityGroup import QualityGroup
if TYPE_CHECKING:
from cura.Machines.QualityNode import QualityNode
class QualityChangesGroup(QualityGroup): 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) super().__init__(name, quality_type, parent)
self._container_registry = Application.getInstance().getContainerRegistry() self._container_registry = Application.getInstance().getContainerRegistry()
def addNode(self, node: "QualityNode"): 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. 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 return
if extruder_position is None: #Then we're a global quality changes profile. if extruder_position is None: #Then we're a global quality changes profile.

View file

@ -4,7 +4,7 @@
from typing import Dict, Optional, List, Set from typing import Dict, Optional, List, Set
from PyQt5.QtCore import QObject, pyqtSlot 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. # 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: def __init__(self, name: str, quality_type: str, parent = None) -> None:
super().__init__(parent) super().__init__(parent)
self.name = name self.name = name
self.node_for_global = None # type: Optional["QualityGroup"] self.node_for_global = None # type: Optional[ContainerNode]
self.nodes_for_extruders = {} # type: Dict[int, "QualityGroup"] self.nodes_for_extruders = {} # type: Dict[int, ContainerNode]
self.quality_type = quality_type self.quality_type = quality_type
self.is_available = False self.is_available = False
@ -38,10 +38,12 @@ class QualityGroup(QObject):
for node in [self.node_for_global] + list(self.nodes_for_extruders.values()): for node in [self.node_for_global] + list(self.nodes_for_extruders.values()):
if node is None: if node is None:
continue continue
result.update(node.getContainer().getAllKeys()) container = node.getContainer()
if container:
result.update(container.getAllKeys())
return result return result
def getAllNodes(self) -> List["QualityGroup"]: def getAllNodes(self) -> List[ContainerNode]:
result = [] result = []
if self.node_for_global is not None: if self.node_for_global is not None:
result.append(self.node_for_global) result.append(self.node_for_global)

View file

@ -1,7 +1,7 @@
# Copyright (c) 2018 Ultimaker B.V. # Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # 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 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: 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() 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: if is_global_quality:
# For global qualities, save data in the machine node # For global qualities, save data in the machine node
@ -102,7 +102,7 @@ class QualityManager(QObject):
# too. # too.
if variant_name not in machine_node.children_map: if variant_name not in machine_node.children_map:
machine_node.children_map[variant_name] = QualityNode() 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 root_material_id is None:
# If only variant_name is specified but material is not, add the quality/quality_changes metadata # 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. # material node.
if root_material_id not in variant_node.children_map: if root_material_id not in variant_node.children_map:
variant_node.children_map[root_material_id] = QualityNode() 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) material_node.addQualityMetadata(quality_type, metadata)
@ -123,7 +123,7 @@ class QualityManager(QObject):
if root_material_id is not None: if root_material_id is not None:
if root_material_id not in machine_node.children_map: if root_material_id not in machine_node.children_map:
machine_node.children_map[root_material_id] = QualityNode() 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) material_node.addQualityMetadata(quality_type, metadata)
@ -351,7 +351,7 @@ class QualityManager(QObject):
def removeQualityChangesGroup(self, quality_changes_group: "QualityChangesGroup"): def removeQualityChangesGroup(self, quality_changes_group: "QualityChangesGroup"):
Logger.log("i", "Removing quality changes group [%s]", quality_changes_group.name) Logger.log("i", "Removing quality changes group [%s]", quality_changes_group.name)
for node in quality_changes_group.getAllNodes(): 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. # 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) new_name = self._container_registry.uniqueName(new_name)
for node in quality_changes_group.getAllNodes(): 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 quality_changes_group.name = new_name

View file

@ -1,7 +1,7 @@
# Copyright (c) 2018 Ultimaker B.V. # Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # 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 .ContainerNode import ContainerNode
from .QualityChangesGroup import QualityChangesGroup from .QualityChangesGroup import QualityChangesGroup
@ -12,9 +12,9 @@ from .QualityChangesGroup import QualityChangesGroup
# #
class QualityNode(ContainerNode): class QualityNode(ContainerNode):
def __init__(self, metadata: Optional[dict] = None): def __init__(self, metadata: Optional[dict] = None) -> None:
super().__init__(metadata = metadata) 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): def addQualityMetadata(self, quality_type: str, metadata: dict):
if quality_type not in self.quality_type_map: if quality_type not in self.quality_type_map:

View file

@ -1,13 +1,15 @@
# Copyright (c) 2018 Ultimaker B.V. # Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # 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 cura.PrinterOutput.PrinterOutputController import PrinterOutputController
from PyQt5.QtCore import QTimer from PyQt5.QtCore import QTimer
MYPY = False if TYPE_CHECKING:
if MYPY:
from cura.PrinterOutput.PrintJobOutputModel import PrintJobOutputModel from cura.PrinterOutput.PrintJobOutputModel import PrintJobOutputModel
from cura.PrinterOutput.PrinterOutputModel import PrinterOutputModel from cura.PrinterOutput.PrinterOutputModel import PrinterOutputModel
from cura.PrinterOutput.ExtruderOutputModel import ExtruderOutputModel
class GenericOutputController(PrinterOutputController): class GenericOutputController(PrinterOutputController):
@ -121,7 +123,7 @@ class GenericOutputController(PrinterOutputController):
if not self._preheat_hotends: if not self._preheat_hotends:
self._preheat_hotends_timer.stop() self._preheat_hotends_timer.stop()
def preheatHotend(self, extruder: ExtruderOutputModel, temperature, duration): def preheatHotend(self, extruder: "ExtruderOutputModel", temperature, duration):
position = extruder.getPosition() position = extruder.getPosition()
number_of_extruders = len(extruder.getPrinter().extruders) number_of_extruders = len(extruder.getPrinter().extruders)
if position >= number_of_extruders: if position >= number_of_extruders:
@ -139,7 +141,7 @@ class GenericOutputController(PrinterOutputController):
self._preheat_hotends.add(extruder) self._preheat_hotends.add(extruder)
extruder.updateIsPreheating(True) extruder.updateIsPreheating(True)
def cancelPreheatHotend(self, extruder: ExtruderOutputModel): def cancelPreheatHotend(self, extruder: "ExtruderOutputModel"):
self.setTargetHotendTemperature(extruder.getPrinter(), extruder.getPosition(), temperature=0) self.setTargetHotendTemperature(extruder.getPrinter(), extruder.getPosition(), temperature=0)
if extruder in self._preheat_hotends: if extruder in self._preheat_hotends:
extruder.updateIsPreheating(False) extruder.updateIsPreheating(False)

View file

@ -1109,7 +1109,8 @@ class MachineManager(QObject):
nodes = [quality_changes_group.node_for_global] + list(quality_changes_group.nodes_for_extruders.values()) 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] containers = [n.getContainer() for n in nodes if n is not None]
for container in containers: 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" quality_changes_group.quality_type = "not_supported"
def _setQualityChangesGroup(self, quality_changes_group: QualityChangesGroup) -> None: def _setQualityChangesGroup(self, quality_changes_group: QualityChangesGroup) -> None: