MockContainer.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. from typing import Optional
  2. from UM.Settings.Interfaces import ContainerInterface
  3. import UM.PluginObject
  4. from UM.Signal import Signal
  5. class MockContainer(ContainerInterface, UM.PluginObject.PluginObject):
  6. """Fake container class to add to the container registry.
  7. This allows us to test the container registry without testing the container
  8. class. If something is wrong in the container class it won't influence this
  9. test.
  10. """
  11. def __init__(self, metadata = None):
  12. """Initialise a new definition container.
  13. The container will have the specified ID and all metadata in the
  14. provided dictionary.
  15. """
  16. super().__init__()
  17. if metadata is None:
  18. self._metadata = {}
  19. else:
  20. self._metadata = metadata
  21. self._plugin_id = "MockContainerPlugin"
  22. def getId(self):
  23. """Gets the ID that was provided at initialisation.
  24. :return: The ID of the container.
  25. """
  26. return self._metadata["id"]
  27. def getMetaData(self):
  28. """Gets all metadata of this container.
  29. This returns the metadata dictionary that was provided in the
  30. constructor of this mock container.
  31. :return: The metadata for this container.
  32. """
  33. return self._metadata
  34. def getMetaDataEntry(self, entry, default = None):
  35. """Gets a metadata entry from the metadata dictionary.
  36. :param key: The key of the metadata entry.
  37. :return: The value of the metadata entry, or None if there is no such
  38. entry.
  39. """
  40. if entry in self._metadata:
  41. return self._metadata[entry]
  42. return default
  43. def getName(self):
  44. """Gets a human-readable name for this container.
  45. :return: The name from the metadata, or "MockContainer" if there was no
  46. name provided.
  47. """
  48. return self._metadata.get("name", "MockContainer")
  49. @property
  50. def isEnabled(self):
  51. """Get whether a container stack is enabled or not.
  52. :return: Always returns True.
  53. """
  54. return True
  55. def isReadOnly(self):
  56. """Get whether the container item is stored on a read only location in the filesystem.
  57. :return: Always returns False
  58. """
  59. return False
  60. def getPath(self):
  61. """Mock get path"""
  62. return "/path/to/the/light/side"
  63. def setPath(self, path):
  64. """Mock set path"""
  65. pass
  66. def getAllKeys(self):
  67. pass
  68. # Should return false (or even throw an exception) if trust (or other verification) is invalidated.
  69. def _trustHook(self, file_name: Optional[str]) -> bool:
  70. return True
  71. def setProperty(self, key, property_name, property_value, container = None, set_from_cache = False):
  72. pass
  73. def getProperty(self, key, property_name, context=None):
  74. if key in self.items:
  75. return self.items[key]
  76. return None
  77. def getValue(self, key):
  78. """Get the value of a container item.
  79. Since this mock container cannot contain any items, it always returns None.
  80. :return: Always returns None.
  81. """
  82. pass
  83. def hasProperty(self, key, property_name):
  84. """Get whether the container item has a specific property.
  85. This method is not implemented in the mock container.
  86. """
  87. return key in self.items
  88. def serialize(self, ignored_metadata_keys = None):
  89. """Serializes the container to a string representation.
  90. This method is not implemented in the mock container.
  91. """
  92. raise NotImplementedError()
  93. def deserialize(self, serialized, file_name: Optional[str] = None):
  94. """Deserializes the container from a string representation.
  95. This method is not implemented in the mock container.
  96. """
  97. raise NotImplementedError()
  98. @classmethod
  99. def getConfigurationTypeFromSerialized(cls, serialized: str):
  100. raise NotImplementedError()
  101. @classmethod
  102. def getVersionFromSerialized(cls, serialized):
  103. raise NotImplementedError()
  104. def isDirty(self):
  105. return True
  106. def setDirty(self, dirty):
  107. pass
  108. metaDataChanged = Signal()
  109. propertyChanged = Signal()
  110. containersChanged = Signal()