Fix typing in MachineActionManager

CURA-5812
This commit is contained in:
Lipu Fei 2018-10-19 09:55:53 +02:00
parent 59704e4c0e
commit 0e772beb14
2 changed files with 19 additions and 15 deletions

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, List, Set from typing import TYPE_CHECKING, Optional, List, Set, Dict
from PyQt5.QtCore import QObject from PyQt5.QtCore import QObject
@ -26,7 +26,7 @@ class NotUniqueMachineActionError(Exception):
class MachineActionManager(QObject): class MachineActionManager(QObject):
def __init__(self, application: "CuraApplication", parent: Optional["QObject"] = None): def __init__(self, application: "CuraApplication", parent: Optional["QObject"] = None) -> None:
super().__init__(parent = parent) super().__init__(parent = parent)
self._application = application self._application = application
self._container_registry = self._application.getContainerRegistry() self._container_registry = self._application.getContainerRegistry()
@ -34,10 +34,14 @@ class MachineActionManager(QObject):
# Keeps track of which machines have already been processed so we don't do that again. # Keeps track of which machines have already been processed so we don't do that again.
self._definition_ids_with_default_actions_added = set() # type: Set[str] self._definition_ids_with_default_actions_added = set() # type: Set[str]
self._machine_actions = {} # Dict of all known machine actions # Dict of all known machine actions
self._required_actions = {} # Dict of all required actions by definition ID self._machine_actions = {} # type: Dict[str, MachineAction]
self._supported_actions = {} # Dict of all supported actions by definition ID # Dict of all required actions by definition ID
self._first_start_actions = {} # Dict of all actions that need to be done when first added by definition ID self._required_actions = {} # type: Dict[str, List[MachineAction]]
# Dict of all supported actions by definition ID
self._supported_actions = {} # type: Dict[str, List[MachineAction]]
# Dict of all actions that need to be done when first added by definition ID
self._first_start_actions = {} # type: Dict[str, List[MachineAction]]
def initialize(self): def initialize(self):
# Add machine_action as plugin type # Add machine_action as plugin type
@ -53,16 +57,16 @@ class MachineActionManager(QObject):
return return
supported_actions = global_stack.getMetaDataEntry("supported_actions", []) supported_actions = global_stack.getMetaDataEntry("supported_actions", [])
for action in supported_actions: for action_key in supported_actions:
self.addSupportedAction(definition_id, action) self.addSupportedAction(definition_id, action_key)
required_actions = global_stack.getMetaDataEntry("required_actions", []) required_actions = global_stack.getMetaDataEntry("required_actions", [])
for action in required_actions: for action_key in required_actions:
self.addRequiredAction(definition_id, action) self.addRequiredAction(definition_id, action_key)
first_start_actions = global_stack.getMetaDataEntry("first_start_actions", []) first_start_actions = global_stack.getMetaDataEntry("first_start_actions", [])
for action in first_start_actions: for action_key in first_start_actions:
self.addFirstStartAction(definition_id, action) self.addFirstStartAction(definition_id, action_key)
self._definition_ids_with_default_actions_added.add(definition_id) self._definition_ids_with_default_actions_added.add(definition_id)
Logger.log("i", "Default machine actions added for machine definition [%s]", definition_id) Logger.log("i", "Default machine actions added for machine definition [%s]", definition_id)
@ -121,11 +125,11 @@ class MachineActionManager(QObject):
## Get all actions required by given machine ## Get all actions required by given machine
# \param definition_id The ID of the definition you want the required actions of # \param definition_id The ID of the definition you want the required actions of
# \returns set of required actions. # \returns set of required actions.
def getRequiredActions(self, definition_id: str) -> Set["MachineAction"]: def getRequiredActions(self, definition_id: str) -> List["MachineAction"]:
if definition_id in self._required_actions: if definition_id in self._required_actions:
return self._required_actions[definition_id] return self._required_actions[definition_id]
else: else:
return set() return list()
## Get all actions that need to be performed upon first start of a given machine. ## Get all actions that need to be performed upon first start of a given machine.
# Note that contrary to required / supported actions a list is returned (as it could be required to run the same # Note that contrary to required / supported actions a list is returned (as it could be required to run the same

View file

@ -44,7 +44,7 @@ def test_addMachineAction(machine_action_manager):
assert machine_action_manager.getSupportedActions(test_machine) == [test_action, test_action_2] assert machine_action_manager.getSupportedActions(test_machine) == [test_action, test_action_2]
# Check that the machine has no required actions yet. # Check that the machine has no required actions yet.
assert machine_action_manager.getRequiredActions(test_machine) == set() assert machine_action_manager.getRequiredActions(test_machine) == list()
## Ensure that only known actions can be added. ## Ensure that only known actions can be added.
with pytest.raises(UnknownMachineActionError): with pytest.raises(UnknownMachineActionError):