MachineActionManager.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from UM.Logger import Logger
  4. from UM.PluginRegistry import PluginRegistry # So MachineAction can be added as plugin type
  5. from UM.Settings.ContainerRegistry import ContainerRegistry
  6. from UM.Settings.DefinitionContainer import DefinitionContainer
  7. from PyQt5.QtCore import QObject, pyqtSlot
  8. ## Raised when trying to add an unknown machine action as a required action
  9. class UnknownMachineActionError(Exception):
  10. pass
  11. ## Raised when trying to add a machine action that does not have an unique key.
  12. class NotUniqueMachineActionError(Exception):
  13. pass
  14. class MachineActionManager(QObject):
  15. def __init__(self, parent = None):
  16. super().__init__(parent)
  17. self._machine_actions = {} # Dict of all known machine actions
  18. self._required_actions = {} # Dict of all required actions by definition ID
  19. self._supported_actions = {} # Dict of all supported actions by definition ID
  20. self._first_start_actions = {} # Dict of all actions that need to be done when first added by definition ID
  21. # Add machine_action as plugin type
  22. PluginRegistry.addType("machine_action", self.addMachineAction)
  23. # Ensure that all containers that were registered before creation of this registry are also handled.
  24. # This should not have any effect, but it makes it safer if we ever refactor the order of things.
  25. for container in ContainerRegistry.getInstance().findDefinitionContainers():
  26. self._onContainerAdded(container)
  27. ContainerRegistry.getInstance().containerAdded.connect(self._onContainerAdded)
  28. def _onContainerAdded(self, container):
  29. ## Ensure that the actions are added to this manager
  30. if isinstance(container, DefinitionContainer):
  31. supported_actions = container.getMetaDataEntry("supported_actions", [])
  32. for action in supported_actions:
  33. self.addSupportedAction(container.getId(), action)
  34. required_actions = container.getMetaDataEntry("required_actions", [])
  35. for action in required_actions:
  36. self.addRequiredAction(container.getId(), action)
  37. first_start_actions = container.getMetaDataEntry("first_start_actions", [])
  38. for action in first_start_actions:
  39. self.addFirstStartAction(container.getId(), action)
  40. ## Add a required action to a machine
  41. # Raises an exception when the action is not recognised.
  42. def addRequiredAction(self, definition_id, action_key):
  43. if action_key in self._machine_actions:
  44. if definition_id in self._required_actions:
  45. self._required_actions[definition_id] |= {self._machine_actions[action_key]}
  46. else:
  47. self._required_actions[definition_id] = {self._machine_actions[action_key]}
  48. else:
  49. raise UnknownMachineActionError("Action %s, which is required for %s is not known." % (action_key, definition_id))
  50. ## Add a supported action to a machine.
  51. def addSupportedAction(self, definition_id, action_key):
  52. if action_key in self._machine_actions:
  53. if definition_id in self._supported_actions:
  54. self._supported_actions[definition_id] |= {self._machine_actions[action_key]}
  55. else:
  56. self._supported_actions[definition_id] = {self._machine_actions[action_key]}
  57. else:
  58. Logger.log("w", "Unable to add %s to %s, as the action is not recognised", action_key, definition_id)
  59. ## Add an action to the first start list of a machine.
  60. def addFirstStartAction(self, definition_id, action_key, index = None):
  61. if action_key in self._machine_actions:
  62. if definition_id in self._first_start_actions:
  63. if index is not None:
  64. self._first_start_actions[definition_id].insert(index, self._machine_actions[action_key])
  65. else:
  66. self._first_start_actions[definition_id].append(self._machine_actions[action_key])
  67. else:
  68. self._first_start_actions[definition_id] = [self._machine_actions[action_key]]
  69. else:
  70. Logger.log("w", "Unable to add %s to %s, as the action is not recognised", action_key, definition_id)
  71. ## Add a (unique) MachineAction
  72. # if the Key of the action is not unique, an exception is raised.
  73. def addMachineAction(self, action):
  74. if action.getKey() not in self._machine_actions:
  75. self._machine_actions[action.getKey()] = action
  76. else:
  77. raise NotUniqueMachineActionError("MachineAction with key %s was already added. Actions must have unique keys.", action.getKey())
  78. ## Get all actions supported by given machine
  79. # \param definition_id The ID of the definition you want the supported actions of
  80. # \returns set of supported actions.
  81. @pyqtSlot(str, result = "QVariantList")
  82. def getSupportedActions(self, definition_id):
  83. if definition_id in self._supported_actions:
  84. return list(self._supported_actions[definition_id])
  85. else:
  86. return set()
  87. ## Get all actions required by given machine
  88. # \param definition_id The ID of the definition you want the required actions of
  89. # \returns set of required actions.
  90. def getRequiredActions(self, definition_id):
  91. if definition_id in self._required_actions:
  92. return self._required_actions[definition_id]
  93. else:
  94. return set()
  95. ## Get all actions that need to be performed upon first start of a given machine.
  96. # Note that contrary to required / supported actions a list is returned (as it could be required to run the same
  97. # action multiple times).
  98. # \param definition_id The ID of the definition that you want to get the "on added" actions for.
  99. # \returns List of actions.
  100. @pyqtSlot(str, result="QVariantList")
  101. def getFirstStartActions(self, definition_id):
  102. if definition_id in self._first_start_actions:
  103. return self._first_start_actions[definition_id]
  104. else:
  105. return []
  106. ## Remove Machine action from manager
  107. # \param action to remove
  108. def removeMachineAction(self, action):
  109. try:
  110. del self._machine_actions[action.getKey()]
  111. except KeyError:
  112. Logger.log("w", "Trying to remove MachineAction (%s) that was already removed", action.getKey())
  113. ## Get MachineAction by key
  114. # \param key String of key to select
  115. # \return Machine action if found, None otherwise
  116. def getMachineAction(self, key):
  117. if key in self._machine_actions:
  118. return self._machine_actions[key]
  119. else:
  120. return None