implement profile removal behaviour

This commit is contained in:
Jakob Ketterl 2021-03-05 19:09:51 +01:00
parent 2ba2ec38e0
commit 2e28694b49
3 changed files with 41 additions and 4 deletions

View File

@ -345,9 +345,13 @@ class PropertyStack(PropertyManager):
class PropertyCarousel(PropertyDelegator): class PropertyCarousel(PropertyDelegator):
def __init__(self): def __init__(self):
# start with an empty dummy layer # start with an empty dummy layer
super().__init__(PropertyLayer()) self.emptyLayer = PropertyLayer().readonly()
super().__init__(self.emptyLayer)
self.layers = {} self.layers = {}
def _getDefaultLayer(self):
return self.emptyLayer
def addLayer(self, key, value): def addLayer(self, key, value):
self.layers[key] = value self.layers[key] = value
@ -355,12 +359,14 @@ class PropertyCarousel(PropertyDelegator):
return key in self.layers return key in self.layers
def removeLayer(self, key): def removeLayer(self, key):
if key in self.layers and self.layers[key] is self.pm:
self.switch()
del self.layers[key] del self.layers[key]
def switch(self, key): def switch(self, key=None):
before = self.pm before = self.pm
self.subscription.cancel() self.subscription.cancel()
self.pm = self.layers[key] self.pm = self._getDefaultLayer() if key is None else self.layers[key]
self.subscription = self.pm.wire(self._fireCallbacks) self.subscription = self.pm.wire(self._fireCallbacks)
changes = {} changes = {}
for key in set(list(before.keys()) + list(self.keys())): for key in set(list(before.keys()) + list(self.keys())):

View File

@ -82,6 +82,12 @@ class SdrProfileCarousel(PropertyCarousel):
elif not self.hasLayer(profile_id): elif not self.hasLayer(profile_id):
self.addLayer(profile_id, profile) self.addLayer(profile_id, profile)
def _getDefaultLayer(self):
# return the first available profile, or the default empty layer if we don't have any
if self.layers:
return next(iter(self.layers.values()))
return super()._getDefaultLayer()
class SdrSource(ABC): class SdrSource(ABC):
def __init__(self, id, props): def __init__(self, id, props):

View File

@ -1,6 +1,6 @@
from unittest import TestCase from unittest import TestCase
from unittest.mock import Mock from unittest.mock import Mock
from owrx.property import PropertyCarousel, PropertyLayer, PropertyDeleted from owrx.property import PropertyCarousel, PropertyLayer, PropertyDeleted, PropertyWriteError
class PropertyCarouselTest(TestCase): class PropertyCarouselTest(TestCase):
@ -92,3 +92,28 @@ class PropertyCarouselTest(TestCase):
pc.removeLayer("x") pc.removeLayer("x")
with self.assertRaises(KeyError): with self.assertRaises(KeyError):
pc.switch("x") pc.switch("x")
def testPropertyResetAfterRemoval(self):
pc = PropertyCarousel()
pl = PropertyLayer(testkey="testvalue")
pc.addLayer("x", pl)
pc.switch("x")
self.assertEqual(pc["testkey"], "testvalue")
pc.removeLayer("x")
with self.assertRaises(KeyError):
x = pc["testkey"]
def testEmptySwitch(self):
pc = PropertyCarousel()
pl = PropertyLayer(testkey="testvalue")
pc.addLayer("x", pl)
pc.switch("x")
self.assertEqual(pc["testkey"], "testvalue")
pc.switch()
with self.assertRaises(KeyError):
x = pc["testkey"]
def testErrorOnWriteOnDefaultLayer(self):
pc = PropertyCarousel()
with self.assertRaises(PropertyWriteError):
pc["testkey"] = "testvalue"