ContainerNode.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional
  4. from collections import OrderedDict
  5. from UM.ConfigurationErrorMessage import ConfigurationErrorMessage
  6. from UM.Logger import Logger
  7. from UM.Settings.InstanceContainer import InstanceContainer
  8. ##
  9. # A metadata / container combination. Use getContainer() to get the container corresponding to the metadata.
  10. #
  11. # ContainerNode is a multi-purpose class. It has two main purposes:
  12. # 1. It encapsulates an InstanceContainer. It contains that InstanceContainer's
  13. # - metadata (Always)
  14. # - container (lazy-loaded when needed)
  15. # 2. It also serves as a node in a hierarchical InstanceContainer lookup table/tree.
  16. # This is used in Variant, Material, and Quality Managers.
  17. #
  18. class ContainerNode:
  19. __slots__ = ("metadata", "container", "children_map")
  20. def __init__(self, metadata: Optional[dict] = None):
  21. self.metadata = metadata
  22. self.container = None
  23. self.children_map = OrderedDict()
  24. def getChildNode(self, child_key: str) -> Optional["ContainerNode"]:
  25. return self.children_map.get(child_key)
  26. def getContainer(self) -> Optional["InstanceContainer"]:
  27. if self.metadata is None:
  28. Logger.log("e", "Cannot get container for a ContainerNode without metadata.")
  29. return None
  30. if self.container is None:
  31. container_id = self.metadata["id"]
  32. from UM.Settings.ContainerRegistry import ContainerRegistry
  33. container_list = ContainerRegistry.getInstance().findInstanceContainers(id = container_id)
  34. if not container_list:
  35. Logger.log("e", "Failed to lazy-load container [{container_id}]. Cannot find it.".format(container_id = container_id))
  36. error_message = ConfigurationErrorMessage.getInstance()
  37. error_message.addFaultyContainers(container_id)
  38. return None
  39. self.container = container_list[0]
  40. return self.container
  41. def __str__(self) -> str:
  42. return "%s[%s]" % (self.__class__.__name__, self.metadata.get("id"))