ContainerNode.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional, Any, Dict, Union, TYPE_CHECKING
  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[str, Any]] = None) -> None:
  21. self._metadata = metadata
  22. self._container = None # type: Optional[InstanceContainer]
  23. self.children_map = OrderedDict() # type: ignore # This is because it's children are supposed to override it.
  24. ## Get an entry value from the metadata
  25. def getMetaDataEntry(self, entry: str, default: Any = None) -> Any:
  26. if self._metadata is None:
  27. return default
  28. return self._metadata.get(entry, default)
  29. def getMetadata(self) -> Dict[str, Any]:
  30. if self._metadata is None:
  31. return {}
  32. return self._metadata
  33. def getChildNode(self, child_key: str) -> Optional["ContainerNode"]:
  34. return self.children_map.get(child_key)
  35. def getContainer(self) -> Optional["InstanceContainer"]:
  36. if self._metadata is None:
  37. Logger.log("e", "Cannot get container for a ContainerNode without metadata.")
  38. return None
  39. if self._container is None:
  40. container_id = self._metadata["id"]
  41. from UM.Settings.ContainerRegistry import ContainerRegistry
  42. container_list = ContainerRegistry.getInstance().findInstanceContainers(id = container_id)
  43. if not container_list:
  44. Logger.log("e", "Failed to lazy-load container [{container_id}]. Cannot find it.".format(container_id = container_id))
  45. error_message = ConfigurationErrorMessage.getInstance()
  46. error_message.addFaultyContainers(container_id)
  47. return None
  48. self._container = container_list[0]
  49. return self._container
  50. def __str__(self) -> str:
  51. return "%s[%s]" % (self.__class__.__name__, self.getMetaDataEntry("id"))