openwebrx-clone/owrx/property/__init__.py

231 lines
6.1 KiB
Python
Raw Normal View History

from abc import ABC, abstractmethod
import logging
logger = logging.getLogger(__name__)
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
2020-03-24 19:36:26 +00:00
@abstractmethod
def keys(self):
pass
2020-03-24 21:16:11 +00:00
def filter(self, *props):
return PropertyFilter(self, *props)
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, name, value):
for c in self.subscribers:
try:
if c.getName() is None:
c.call(name, value)
elif c.getName() == name:
c.call(value)
except Exception as e:
logger.exception(e)
class PropertyLayer(PropertyManager):
2020-03-23 23:29:59 +00:00
def __init__(self):
super().__init__()
self.properties = {}
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()}
2020-03-24 19:36:26 +00:00
def keys(self):
return self.properties.keys()
class PropertyFilter(PropertyManager):
def __init__(self, pm: PropertyManager, *props: str):
super().__init__()
self.pm = pm
self.props = props
self.pm.wire(self.receiveEvent)
def receiveEvent(self, name, value):
if name not in self.props:
return
self._fireCallbacks(name, value)
def __getitem__(self, item):
if item not in self.props:
raise KeyError(item)
return self.pm.__getitem__(item)
def __setitem__(self, key, value):
if key not in self.props:
raise KeyError(key)
return self.pm.__setitem__(key, value)
def __contains__(self, item):
if item not in self.props:
return False
return self.pm.__contains__(item)
def __dict__(self):
return {k: v for k, v in self.pm.__dict__().items() if k in self.props}
2020-03-22 20:51:49 +00:00
2020-03-24 19:36:26 +00:00
def keys(self):
return [k for k in self.pm.keys() if k in self.props]
2020-03-22 20:51:49 +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
"""
2020-03-24 21:11:54 +00:00
self._fireChanges(self._addLayer(priority, pm))
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
2020-03-23 23:08:48 +00:00
def eventClosure(name, value):
self.receiveEvent(pm, name, value)
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:
2020-03-24 21:11:54 +00:00
self._fireChanges(self._removeLayer(layer))
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] = None
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._fireChanges(changes)
def _fireChanges(self, changes):
for k, v in changes.items():
self._fireCallbacks(k, v)
def receiveEvent(self, layer, name, value):
if layer != self._getTopLayer(name):
return
self._fireCallbacks(name, value)
2020-03-22 20:51:49 +00:00
2020-03-23 23:08:48 +00:00
def _getTopLayer(self, item):
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 by default
2020-03-24 19:36:26 +00:00
if layers:
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 keys(self):
return set([key for l in self.layers for key in l["props"].keys()])