2020-03-23 22:56:05 +00:00
|
|
|
from abc import ABC, abstractmethod
|
2020-03-21 21:40:39 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Subscription(object):
|
2020-03-23 22:56:05 +00:00
|
|
|
def __init__(self, subscriptee, name, subscriber):
|
2020-03-21 21:40:39 +00:00
|
|
|
self.subscriptee = subscriptee
|
2020-03-23 22:56:05 +00:00
|
|
|
self.name = name
|
2020-03-21 21:40:39 +00:00
|
|
|
self.subscriber = subscriber
|
|
|
|
|
2020-03-23 22:56:05 +00:00
|
|
|
def getName(self):
|
|
|
|
return self.name
|
|
|
|
|
2020-03-21 21:40:39 +00:00
|
|
|
def call(self, *args, **kwargs):
|
|
|
|
self.subscriber(*args, **kwargs)
|
|
|
|
|
|
|
|
def cancel(self):
|
|
|
|
self.subscriptee.unwire(self)
|
|
|
|
|
|
|
|
|
2020-03-23 22:56:05 +00:00
|
|
|
class PropertyManager(ABC):
|
|
|
|
def __init__(self):
|
2020-03-21 21:40:39 +00:00
|
|
|
self.subscribers = []
|
|
|
|
|
2020-03-23 22:56:05 +00:00
|
|
|
@abstractmethod
|
|
|
|
def __getitem__(self, item):
|
|
|
|
pass
|
2020-03-21 21:40:39 +00:00
|
|
|
|
2020-03-23 22:56:05 +00:00
|
|
|
@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):
|
2020-03-23 22:56:05 +00:00
|
|
|
return PropertyFilter(self, *props)
|
2020-03-21 21:40:39 +00:00
|
|
|
|
|
|
|
def wire(self, callback):
|
2020-03-23 22:56:05 +00:00
|
|
|
sub = Subscription(self, None, callback)
|
2020-03-21 21:40:39 +00:00
|
|
|
self.subscribers.append(sub)
|
2020-03-23 22:56:05 +00:00
|
|
|
return sub
|
|
|
|
|
|
|
|
def wireProperty(self, name, callback):
|
|
|
|
sub = Subscription(self, name, callback)
|
|
|
|
self.subscribers.append(sub)
|
|
|
|
if name in self:
|
|
|
|
sub.call(self[name])
|
2020-03-21 21:40:39 +00:00
|
|
|
return sub
|
|
|
|
|
|
|
|
def unwire(self, sub):
|
|
|
|
try:
|
|
|
|
self.subscribers.remove(sub)
|
|
|
|
except ValueError:
|
|
|
|
# happens when already removed before
|
|
|
|
pass
|
|
|
|
return self
|
|
|
|
|
2020-03-23 22:56:05 +00:00
|
|
|
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)
|
2020-03-21 21:40:39 +00:00
|
|
|
|
|
|
|
|
2020-03-23 22:56:05 +00:00
|
|
|
class PropertyLayer(PropertyManager):
|
2020-03-23 23:29:59 +00:00
|
|
|
def __init__(self):
|
2020-03-23 22:56:05 +00:00
|
|
|
super().__init__()
|
2020-03-21 21:40:39 +00:00
|
|
|
self.properties = {}
|
|
|
|
|
|
|
|
def __contains__(self, name):
|
2020-03-23 22:56:05 +00:00
|
|
|
return name in self.properties
|
2020-03-21 21:40:39 +00:00
|
|
|
|
|
|
|
def __getitem__(self, name):
|
2020-03-23 22:56:05 +00:00
|
|
|
return self.properties[name]
|
2020-03-21 21:40:39 +00:00
|
|
|
|
|
|
|
def __setitem__(self, name, value):
|
2020-03-23 23:18:10 +00:00
|
|
|
if name in self.properties and self.properties[name] == value:
|
|
|
|
return
|
2020-03-23 22:56:05 +00:00
|
|
|
self.properties[name] = value
|
|
|
|
self._fireCallbacks(name, value)
|
2020-03-21 21:40:39 +00:00
|
|
|
|
|
|
|
def __dict__(self):
|
2020-03-23 22:56:05 +00:00
|
|
|
return {k: v for k, v in self.properties.items()}
|
2020-03-21 21:40:39 +00:00
|
|
|
|
2020-03-24 19:36:26 +00:00
|
|
|
def keys(self):
|
|
|
|
return self.properties.keys()
|
|
|
|
|
2020-03-21 21:40:39 +00:00
|
|
|
|
2020-03-23 22:56:05 +00:00
|
|
|
class PropertyFilter(PropertyManager):
|
|
|
|
def __init__(self, pm: PropertyManager, *props: str):
|
|
|
|
super().__init__()
|
|
|
|
self.pm = pm
|
|
|
|
self.props = props
|
|
|
|
self.pm.wire(self.receiveEvent)
|
2020-03-21 21:40:39 +00:00
|
|
|
|
2020-03-23 22:56:05 +00:00
|
|
|
def receiveEvent(self, name, value):
|
|
|
|
if name not in self.props:
|
|
|
|
return
|
|
|
|
self._fireCallbacks(name, value)
|
2020-03-21 21:40:39 +00:00
|
|
|
|
2020-03-23 22:56:05 +00:00
|
|
|
def __getitem__(self, item):
|
|
|
|
if item not in self.props:
|
|
|
|
raise KeyError(item)
|
|
|
|
return self.pm.__getitem__(item)
|
2020-03-21 21:40:39 +00:00
|
|
|
|
2020-03-23 22:56:05 +00:00
|
|
|
def __setitem__(self, key, value):
|
|
|
|
if key not in self.props:
|
|
|
|
raise KeyError(key)
|
|
|
|
return self.pm.__setitem__(key, value)
|
2020-03-21 21:40:39 +00:00
|
|
|
|
2020-03-23 22:56:05 +00:00
|
|
|
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
|
|
|
|
2020-03-23 22:56:05 +00:00
|
|
|
class PropertyStack(PropertyManager):
|
2020-03-22 20:51:49 +00:00
|
|
|
def __init__(self):
|
2020-03-23 22:56:05 +00:00
|
|
|
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]
|
|
|
|
|
2020-03-25 16:59:00 +00:00
|
|
|
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:
|
2020-03-23 22:56:05 +00:00
|
|
|
return m
|
|
|
|
# return top layer by default
|
2020-03-24 19:36:26 +00:00
|
|
|
if layers:
|
|
|
|
return layers[0]
|
2020-03-23 22:56:05 +00:00
|
|
|
|
|
|
|
def __getitem__(self, item):
|
2020-03-23 23:08:48 +00:00
|
|
|
layer = self._getTopLayer(item)
|
2020-03-23 22:56:05 +00:00
|
|
|
return layer.__getitem__(item)
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
2020-03-23 23:08:48 +00:00
|
|
|
layer = self._getTopLayer(key)
|
2020-03-23 22:56:05 +00:00
|
|
|
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
|
2020-03-23 22:56:05 +00:00
|
|
|
|
|
|
|
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()])
|