ContainerNode.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. class ContainerNode:
  9. """A node in the container tree. It represents one container.
  10. The container it represents is referenced by its container_id. During normal use of the tree, this container is
  11. not constructed. Only when parts of the tree need to get loaded in the container stack should it get constructed.
  12. """
  13. def __init__(self, container_id: str) -> None:
  14. """Creates a new node for the container tree.
  15. :param container_id: The ID of the container that this node should represent.
  16. """
  17. self.container_id = container_id
  18. self._container = None # type: Optional[InstanceContainer]
  19. self.children_map = {} # type: Dict[str, ContainerNode] # Mapping from container ID to container node.
  20. def getMetadata(self) -> Dict[str, Any]:
  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. """
  25. return ContainerRegistry.getInstance().findContainersMetadata(id = self.container_id)[0]
  26. def getMetaDataEntry(self, entry: str, default: Any = None) -> Any:
  27. """Get an entry from the metadata of the container that this node contains.
  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 found, the value of this default is
  31. returned.
  32. :return: The value of the metadata entry, or the default if it was not present.
  33. """
  34. container_metadata = ContainerRegistry.getInstance().findContainersMetadata(id = self.container_id)
  35. if len(container_metadata) == 0:
  36. return default
  37. return container_metadata[0].get(entry, default)
  38. @property
  39. def container(self) -> Optional[InstanceContainer]:
  40. """The container that this node's container ID refers to.
  41. This can be used to finally instantiate the container in order to put it in the container stack.
  42. :return: A container.
  43. """
  44. if not self._container:
  45. container_list = ContainerRegistry.getInstance().findInstanceContainers(id = self.container_id)
  46. if len(container_list) == 0:
  47. Logger.log("e", "Failed to lazy-load container [{container_id}]. Cannot find it.".format(container_id = self.container_id))
  48. error_message = ConfigurationErrorMessage.getInstance()
  49. error_message.addFaultyContainers(self.container_id)
  50. return None
  51. self._container = container_list[0]
  52. return self._container
  53. def __str__(self) -> str:
  54. return "%s[%s]" % (self.__class__.__name__, self.container_id)