MockContainer.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #
  46. # \return Always returns "MockContainer".
  47. def getName(self):
  48. return "MockContainer"
  49. ## Get whether the container item is stored on a read only location in the filesystem.
  50. #
  51. # \return Always returns False
  52. def isReadOnly(self):
  53. return False
  54. ## Mock get path
  55. def getPath(self):
  56. return "/path/to/the/light/side"
  57. ## Mock set path
  58. def setPath(self, path):
  59. pass
  60. def getAllKeys(self):
  61. pass
  62. def setProperty(self, key, property_name, property_value, container = None, set_from_cache = False):
  63. pass
  64. def getProperty(self, key, property_name, context=None):
  65. if key in self.items:
  66. return self.items[key]
  67. return None
  68. ## Get the value of a container item.
  69. #
  70. # Since this mock container cannot contain any items, it always returns
  71. # None.
  72. #
  73. # \return Always returns None.
  74. def getValue(self, key):
  75. pass
  76. ## Get whether the container item has a specific property.
  77. #
  78. # This method is not implemented in the mock container.
  79. def hasProperty(self, key, property_name):
  80. return key in self.items
  81. ## Serializes the container to a string representation.
  82. #
  83. # This method is not implemented in the mock container.
  84. def serialize(self, ignored_metadata_keys = None):
  85. raise NotImplementedError()
  86. ## Deserializes the container from a string representation.
  87. #
  88. # This method is not implemented in the mock container.
  89. def deserialize(self, serialized, file_name: Optional[str] = None):
  90. raise NotImplementedError()
  91. @classmethod
  92. def getConfigurationTypeFromSerialized(cls, serialized: str):
  93. raise NotImplementedError()
  94. @classmethod
  95. def getVersionFromSerialized(cls, serialized):
  96. raise NotImplementedError()
  97. def isDirty(self):
  98. return True
  99. metaDataChanged = Signal()
  100. propertyChanged = Signal()
  101. containersChanged = Signal()