ContainerNode.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Any, Dict, Optional
  4. from UM.ConfigurationErrorMessage import ConfigurationErrorMessage
  5. from UM.Settings.ContainerRegistry import ContainerRegistry
  6. from UM.Logger import Logger
  7. from UM.Settings.InstanceContainer import InstanceContainer
  8. ## A node in the container tree. It represents one container.
  9. #
  10. # The container it represents is referenced by its container_id. During normal
  11. # use of the tree, this container is not constructed. Only when parts of the
  12. # tree need to get loaded in the container stack should it get constructed.
  13. class ContainerNode:
  14. ## Creates a new node for the container tree.
  15. # \param container_id The ID of the container that this node should
  16. # represent.
  17. def __init__(self, container_id: str) -> None:
  18. self.container_id = container_id
  19. self._container = None # type: Optional[InstanceContainer]
  20. self.children_map = {} # type: Dict[str, ContainerNode] # Mapping from container ID to container node.
  21. ## Gets the metadata of the container that this node represents.
  22. # Getting the metadata from the container directly is about 10x as fast.
  23. # \return The metadata of the container in this node.
  24. def getMetadata(self) -> Dict[str, Any]:
  25. return ContainerRegistry.getInstance().findContainersMetadata(id = self.container_id)[0]
  26. ## Get an entry from the metadata of the container that this node contains.
  27. #
  28. # This is just a convenience function.
  29. # \param entry The metadata entry key to return.
  30. # \param default If the metadata is not present or the container is not
  31. # found, the value of this default is returned.
  32. # \return The value of the metadata entry, or the default if it was not
  33. # present.
  34. def getMetaDataEntry(self, entry: str, default: Any = None) -> Any:
  35. container_metadata = ContainerRegistry.getInstance().findContainersMetadata(id = self.container_id)
  36. if len(container_metadata) == 0:
  37. return default
  38. return container_metadata[0].get(entry, default)
  39. ## The container that this node's container ID refers to.
  40. #
  41. # This can be used to finally instantiate the container in order to put it
  42. # in the container stack.
  43. # \return A container.
  44. @property
  45. def container(self) -> Optional[InstanceContainer]:
  46. if not self._container:
  47. container_list = ContainerRegistry.getInstance().findInstanceContainers(id = self.container_id)
  48. if len(container_list) == 0:
  49. Logger.log("e", "Failed to lazy-load container [{container_id}]. Cannot find it.".format(container_id = self.container_id))
  50. error_message = ConfigurationErrorMessage.getInstance()
  51. error_message.addFaultyContainers(self.container_id)
  52. return None
  53. self._container = container_list[0]
  54. return self._container
  55. def __str__(self) -> str:
  56. return "%s[%s]" % (self.__class__.__name__, self.container_id)