MockContainer.py 4.0 KB

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