openwebrx-clone/owrx/property/__init__.py

412 lines
12 KiB
Python
Raw Normal View History

from abc import ABC, abstractmethod
from owrx.property.validators import Validator
from owrx.property.filter import Filter, ByPropertyName
import logging
logger = logging.getLogger(__name__)
class PropertyError(Exception):
pass
class PropertyDeletion(object):
pass
# a special object that will be sent in events when a deletion occured
# it can also represent deletion of a key in internal storage, but should not be return from standard dict apis
PropertyDeleted = PropertyDeletion()
class Subscription(object):
def __init__(self, subscriptee, name, subscriber):
self.subscriptee = subscriptee
self.name = name
self.subscriber = subscriber
def getName(self):
return self.name
def call(self, *args, **kwargs):
self.subscriber(*args, **kwargs)
def cancel(self):
self.subscriptee.unwire(self)
class PropertyManager(ABC):
def __init__(self):
self.subscribers = []
@abstractmethod
def __getitem__(self, item):
pass
@abstractmethod
def __setitem__(self, key, value):
pass
@abstractmethod
def __contains__(self, item):
pass
@abstractmethod
def __dict__(self):
pass
@abstractmethod
def __delitem__(self, key):
pass
2020-03-24 19:36:26 +00:00
@abstractmethod
def keys(self):
pass
2021-03-06 22:34:27 +00:00
@abstractmethod
def values(self):
pass
@abstractmethod
def items(self):
2021-03-06 22:34:27 +00:00
pass
def __len__(self):
return self.__dict__().__len__()
2020-03-24 21:16:11 +00:00
def filter(self, *props):
return PropertyFilter(self, ByPropertyName(*props))
def readonly(self):
return PropertyReadOnly(self)
def wire(self, callback):
sub = Subscription(self, None, callback)
self.subscribers.append(sub)
return sub
def wireProperty(self, name, callback):
sub = Subscription(self, name, callback)
self.subscribers.append(sub)
if name in self:
sub.call(self[name])
return sub
def unwire(self, sub):
try:
self.subscribers.remove(sub)
except ValueError:
# happens when already removed before
pass
return self
def _fireCallbacks(self, changes):
if not changes:
return
for c in self.subscribers:
try:
if c.getName() is None:
c.call(changes)
except Exception:
logger.exception("exception while firing changes")
for name in changes:
for c in self.subscribers:
try:
if c.getName() == name:
c.call(changes[name])
except Exception:
logger.exception("exception while firing changes")
class PropertyLayer(PropertyManager):
2021-02-11 18:31:44 +00:00
def __init__(self, **kwargs):
super().__init__()
2021-02-11 18:31:44 +00:00
# copy, don't re-use
self.properties = {k: v for k, v in kwargs.items()}
def __contains__(self, name):
return name in self.properties
def __getitem__(self, name):
return self.properties[name]
def __setitem__(self, name, value):
if name in self.properties and self.properties[name] == value:
return
self.properties[name] = value
self._fireCallbacks({name: value})
def __dict__(self):
return {k: v for k, v in self.properties.items()}
def __delitem__(self, key):
self.properties.__delitem__(key)
self._fireCallbacks({key: PropertyDeleted})
2020-03-24 19:36:26 +00:00
def keys(self):
return self.properties.keys()
2021-03-06 22:34:27 +00:00
def values(self):
return self.properties.values()
def items(self):
return self.properties.items()
class PropertyFilter(PropertyManager):
def __init__(self, pm: PropertyManager, filter: Filter):
super().__init__()
self.pm = pm
self._filter = filter
self.pm.wire(self.receiveEvent)
def receiveEvent(self, changes):
changesToForward = {name: value for name, value in changes.items() if self._filter.apply(name)}
self._fireCallbacks(changesToForward)
def __getitem__(self, item):
if not self._filter.apply(item):
raise KeyError(item)
return self.pm.__getitem__(item)
def __setitem__(self, key, value):
if not self._filter.apply(key):
raise KeyError(key)
return self.pm.__setitem__(key, value)
def __contains__(self, item):
if not self._filter.apply(item):
return False
return self.pm.__contains__(item)
def __dict__(self):
return {k: v for k, v in self.pm.__dict__().items() if self._filter.apply(k)}
2020-03-22 20:51:49 +00:00
def __delitem__(self, key):
if not self._filter.apply(key):
raise KeyError(key)
return self.pm.__delitem__(key)
2020-03-24 19:36:26 +00:00
def keys(self):
return [k for k in self.pm.keys() if self._filter.apply(k)]
2020-03-24 19:36:26 +00:00
2021-03-06 22:34:27 +00:00
def values(self):
return [v for k, v in self.pm.items() if self._filter.apply(k)]
def items(self):
return self.__dict__().items()
2020-03-22 20:51:49 +00:00
class PropertyDelegator(PropertyManager):
def __init__(self, pm: PropertyManager):
self.pm = pm
self.subscription = self.pm.wire(self._fireCallbacks)
super().__init__()
def __getitem__(self, item):
return self.pm.__getitem__(item)
def __setitem__(self, key, value):
return self.pm.__setitem__(key, value)
def __contains__(self, item):
return self.pm.__contains__(item)
def __dict__(self):
return self.pm.__dict__()
def __delitem__(self, key):
return self.pm.__delitem__(key)
def keys(self):
return self.pm.keys()
2021-03-06 22:34:27 +00:00
def values(self):
return self.pm.values()
def items(self):
return self.pm.items()
class PropertyValidationError(PropertyError):
2021-01-24 20:19:45 +00:00
def __init__(self, key, value):
super().__init__('Invalid value for property "{key}": "{value}"'.format(key=key, value=str(value)))
class PropertyValidator(PropertyDelegator):
2021-01-24 20:19:45 +00:00
def __init__(self, pm: PropertyManager, validators=None):
super().__init__(pm)
2021-01-24 20:19:45 +00:00
if validators is None:
self.validators = {}
else:
self.validators = {k: Validator.of(v) for k, v in validators.items()}
def validate(self, key, value):
if key not in self.validators:
return
if not self.validators[key].isValid(value):
raise PropertyValidationError(key, value)
def setValidator(self, key, validator):
self.validators[key] = Validator.of(validator)
def __setitem__(self, key, value):
self.validate(key, value)
return self.pm.__setitem__(key, value)
class PropertyWriteError(PropertyError):
def __init__(self, key):
super().__init__('Key "{key}" is not writeable'.format(key=key))
2021-01-24 20:19:45 +00:00
class PropertyReadOnly(PropertyDelegator):
def __setitem__(self, key, value):
raise PropertyWriteError(key)
2021-01-24 20:19:45 +00:00
class PropertyStack(PropertyManager):
2020-03-22 20:51:49 +00:00
def __init__(self):
super().__init__()
2020-03-22 20:51:49 +00:00
self.layers = []
def addLayer(self, priority: int, pm: PropertyManager):
"""
highest priority = 0
"""
self._fireCallbacks(self._addLayer(priority, pm))
2020-03-24 21:11:54 +00:00
def _addLayer(self, priority: int, pm: PropertyManager):
changes = {}
2020-03-24 19:36:26 +00:00
for key in pm.keys():
if key not in self or self[key] != pm[key]:
2020-03-24 21:11:54 +00:00
changes[key] = pm[key]
2020-03-22 20:51:49 +00:00
def eventClosure(changes):
self.receiveEvent(pm, changes)
2020-03-23 23:08:48 +00:00
2020-03-24 21:11:54 +00:00
sub = pm.wire(eventClosure)
2020-03-23 23:08:48 +00:00
2020-03-24 21:11:54 +00:00
self.layers.append({"priority": priority, "props": pm, "sub": sub})
return changes
2020-03-23 23:08:48 +00:00
2020-03-22 20:51:49 +00:00
def removeLayer(self, pm: PropertyManager):
for layer in self.layers:
if layer["props"] == pm:
self._fireCallbacks(self._removeLayer(layer))
2020-03-24 21:11:54 +00:00
def _removeLayer(self, layer):
layer["sub"].cancel()
self.layers.remove(layer)
changes = {}
pm = layer["props"]
for key in pm.keys():
if key in self:
if self[key] != pm[key]:
changes[key] = self[key]
else:
changes[key] = PropertyDeleted
2020-03-24 21:11:54 +00:00
return changes
def replaceLayer(self, priority: int, pm: PropertyManager):
layers = [x for x in self.layers if x["priority"] == priority]
originalState = self.__dict__()
changes = self._removeLayer(layers[0]) if layers else {}
changes = {**changes, **self._addLayer(priority, pm)}
changes = {k: v for k, v in changes.items() if k not in originalState or originalState[k] != v}
2020-03-24 21:11:54 +00:00
self._fireCallbacks(changes)
2020-03-24 21:11:54 +00:00
def receiveEvent(self, layer, changes):
changesToForward = {name: value for name, value in changes.items() if layer == self._getTopLayer(name)}
2021-03-06 22:48:31 +00:00
# deletions need to be handled separately:
# * send a deletion if the key was deleted in all layers
# * send lower value if the key is still present in a lower layer
deletionsToForward = {
2021-03-06 22:48:31 +00:00
name: PropertyDeleted if self._getTopLayer(name, False) is None else self[name]
for name, value in changes.items()
2021-03-06 22:48:31 +00:00
if value is PropertyDeleted
}
self._fireCallbacks({**changesToForward, **deletionsToForward})
def _getTopLayer(self, item, fallback=True):
2020-03-22 20:51:49 +00:00
layers = [la["props"] for la in sorted(self.layers, key=lambda l: l["priority"])]
for m in layers:
if item in m:
return m
# return top layer as fallback
if fallback and layers:
2020-03-24 19:36:26 +00:00
return layers[0]
def __getitem__(self, item):
2020-03-23 23:08:48 +00:00
layer = self._getTopLayer(item)
return layer.__getitem__(item)
def __setitem__(self, key, value):
2020-03-23 23:08:48 +00:00
layer = self._getTopLayer(key)
return layer.__setitem__(key, value)
def __contains__(self, item):
2020-03-23 23:08:48 +00:00
layer = self._getTopLayer(item)
2020-03-24 19:36:26 +00:00
if layer:
return layer.__contains__(item)
return False
def __dict__(self):
2020-03-24 19:36:26 +00:00
return {k: self.__getitem__(k) for k in self.keys()}
def __delitem__(self, key):
for layer in self.layers:
2021-02-11 18:31:44 +00:00
if layer["props"].__contains__(key):
layer["props"].__delitem__(key)
2020-03-24 19:36:26 +00:00
def keys(self):
return set([key for l in self.layers for key in l["props"].keys()])
2021-03-06 22:34:27 +00:00
def values(self):
return [self.__getitem__(k) for k in self.keys()]
def items(self):
return self.__dict__().items()
class PropertyCarousel(PropertyDelegator):
def __init__(self):
# start with an empty dummy layer
2021-03-05 18:09:51 +00:00
self.emptyLayer = PropertyLayer().readonly()
super().__init__(self.emptyLayer)
self.layers = {}
2021-03-05 18:09:51 +00:00
def _getDefaultLayer(self):
return self.emptyLayer
def addLayer(self, key, value):
if key in self.layers and self.layers[key] is self.pm:
self.layers[key] = value
# switch after introducing the new value
self.switch(key)
else:
self.layers[key] = value
def removeLayer(self, key):
2021-03-05 18:09:51 +00:00
if key in self.layers and self.layers[key] is self.pm:
self.switch()
del self.layers[key]
2021-03-05 18:09:51 +00:00
def switch(self, key=None):
before = self.pm
self.subscription.cancel()
2021-03-05 18:09:51 +00:00
self.pm = self._getDefaultLayer() if key is None else self.layers[key]
self.subscription = self.pm.wire(self._fireCallbacks)
changes = {}
for key in set(list(before.keys()) + list(self.keys())):
if key not in self:
changes[key] = PropertyDeleted
else:
if key not in before or before[key] != self[key]:
changes[key] = self[key]
self._fireCallbacks(changes)