add stack event handling

This commit is contained in:
Jakob Ketterl 2020-03-24 00:08:48 +01:00
parent c83d8580ba
commit d5c2f8414e
2 changed files with 38 additions and 4 deletions

View File

@ -144,12 +144,22 @@ class PropertyStack(PropertyManager):
""" """
self.layers.append({"priority": priority, "props": pm}) self.layers.append({"priority": priority, "props": pm})
def eventClosure(name, value):
self.receiveEvent(pm, name, value)
pm.wire(eventClosure)
def receiveEvent(self, layer, name, value):
if layer != self._getTopLayer(name):
return
self._fireCallbacks(name, value)
def removeLayer(self, pm: PropertyManager): def removeLayer(self, pm: PropertyManager):
for layer in self.layers: for layer in self.layers:
if layer["props"] == pm: if layer["props"] == pm:
self.layers.remove(layer) self.layers.remove(layer)
def _getLayer(self, item): def _getTopLayer(self, item):
layers = [la["props"] for la in sorted(self.layers, key=lambda l: l["priority"])] layers = [la["props"] for la in sorted(self.layers, key=lambda l: l["priority"])]
for m in layers: for m in layers:
if item in m: if item in m:
@ -158,15 +168,15 @@ class PropertyStack(PropertyManager):
return layers[0] return layers[0]
def __getitem__(self, item): def __getitem__(self, item):
layer = self._getLayer(item) layer = self._getTopLayer(item)
return layer.__getitem__(item) return layer.__getitem__(item)
def __setitem__(self, key, value): def __setitem__(self, key, value):
layer = self._getLayer(key) layer = self._getTopLayer(key)
return layer.__setitem__(key, value) return layer.__setitem__(key, value)
def __contains__(self, item): def __contains__(self, item):
layer = self._getLayer(item) layer = self._getTopLayer(item)
return layer.__contains__(item) return layer.__contains__(item)
def __dict__(self): def __dict__(self):

View File

@ -41,3 +41,27 @@ class PropertyStackTest(TestCase):
self.assertEqual(om["testkey"], "high value") self.assertEqual(om["testkey"], "high value")
om.removeLayer(high_pm) om.removeLayer(high_pm)
self.assertEqual(om["testkey"], "low value") self.assertEqual(om["testkey"], "low value")
def testPropertyChange(self):
layer = PropertyLayer()
stack = PropertyStack()
stack.addLayer(0, layer)
mock = Mock()
stack.wire(mock.method)
layer["testkey"] = "testvalue"
mock.method.assert_called_once_with("testkey", "testvalue")
def testPropertyChangeEventPriority(self):
low_layer = PropertyLayer()
high_layer = PropertyLayer()
low_layer["testkey"] = "initial low value"
high_layer["testkey"] = "initial high value"
stack = PropertyStack()
stack.addLayer(1, low_layer)
stack.addLayer(0, high_layer)
mock = Mock()
stack.wire(mock.method)
low_layer["testkey"] = "modified low value"
mock.method.assert_not_called()
high_layer["testkey"] = "modified high value"
mock.method.assert_called_once_with("testkey", "modified high value")