diff --git a/owrx/property/__init__.py b/owrx/property/__init__.py index 7ef892a..655cd42 100644 --- a/owrx/property/__init__.py +++ b/owrx/property/__init__.py @@ -119,3 +119,25 @@ class PropertyManager(object): if p.getValue() is None: p.setValue(other_pm[key]) return self + + +class PropertyLayers(object): + def __init__(self): + self.layers = [] + + def addLayer(self, priority: int, pm: PropertyManager): + """ + highest priority = 0 + """ + self.layers.append({"priority": priority, "props": pm}) + + def removeLayer(self, pm: PropertyManager): + for layer in self.layers: + if layer["props"] == pm: + self.layers.remove(layer) + + def __getitem__(self, item): + layers = [la["props"] for la in sorted(self.layers, key=lambda l: l["priority"])] + for m in layers: + if item in m: + return m[item] \ No newline at end of file diff --git a/test/property/test_property_overlay.py b/test/property/test_property_overlay.py new file mode 100644 index 0000000..efa330a --- /dev/null +++ b/test/property/test_property_overlay.py @@ -0,0 +1,43 @@ +from unittest import TestCase +from unittest.mock import Mock +from owrx.property import PropertyManager, PropertyLayers + + +class TestPropertyMock(TestCase): + def testLayer(self): + om = PropertyLayers() + pm = PropertyManager() + pm["testkey"] = "testvalue" + om.addLayer(1, pm) + self.assertEqual(om["testkey"], "testvalue") + + def testHighPriority(self): + om = PropertyLayers() + low_pm = PropertyManager() + high_pm = PropertyManager() + low_pm["testkey"] = "low value" + high_pm["testkey"] = "high value" + om.addLayer(1, low_pm) + om.addLayer(0, high_pm) + self.assertEqual(om["testkey"], "high value") + + def testPriorityFallback(self): + om = PropertyLayers() + low_pm = PropertyManager() + high_pm = PropertyManager() + low_pm["testkey"] = "low value" + om.addLayer(1, low_pm) + om.addLayer(0, high_pm) + self.assertEqual(om["testkey"], "low value") + + def testLayerRemoval(self): + om = PropertyLayers() + low_pm = PropertyManager() + high_pm = PropertyManager() + low_pm["testkey"] = "low value" + high_pm["testkey"] = "high value" + om.addLayer(1, low_pm) + om.addLayer(0, high_pm) + self.assertEqual(om["testkey"], "high value") + om.removeLayer(high_pm) + self.assertEqual(om["testkey"], "low value")