2022-12-12 16:39:07 +00:00
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-12-14 00:07:20 +00:00
|
|
|
class ActiveListChange(ABC):
|
|
|
|
pass
|
2022-12-12 16:39:07 +00:00
|
|
|
|
|
|
|
|
2022-12-14 00:07:20 +00:00
|
|
|
class ActiveListIndexUpdated(ActiveListChange):
|
|
|
|
def __init__(self, index: int, oldValue, newValue):
|
|
|
|
self.index = index
|
|
|
|
self.oldValue = oldValue
|
|
|
|
self.newValue = newValue
|
|
|
|
|
|
|
|
|
|
|
|
class ActiveListIndexAppended(ActiveListChange):
|
|
|
|
def __init__(self, index: int, newValue):
|
|
|
|
self.index = index
|
|
|
|
self.newValue = newValue
|
|
|
|
|
|
|
|
|
|
|
|
class ActiveListIndexDeleted(ActiveListChange):
|
|
|
|
def __init__(self, index: int, oldValue):
|
|
|
|
self.index = index
|
|
|
|
self.oldValue = oldValue
|
|
|
|
|
|
|
|
|
|
|
|
class ActiveListListener(ABC):
|
2022-12-12 16:39:07 +00:00
|
|
|
@abstractmethod
|
2022-12-14 00:07:20 +00:00
|
|
|
def onListChange(self, changes: list[ActiveListChange]):
|
2022-12-12 16:39:07 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ActiveList:
|
|
|
|
def __init__(self, elements: list = None):
|
|
|
|
self.delegate = elements.copy() if elements is not None else []
|
|
|
|
self.listeners = []
|
|
|
|
|
|
|
|
def addListener(self, listener: ActiveListListener):
|
|
|
|
if listener in self.listeners:
|
|
|
|
return
|
|
|
|
self.listeners.append(listener)
|
|
|
|
|
|
|
|
def removeListener(self, listener: ActiveListListener):
|
|
|
|
if listener not in self.listeners:
|
|
|
|
return
|
|
|
|
self.listeners.remove(listener)
|
|
|
|
|
|
|
|
def append(self, value):
|
|
|
|
self.delegate.append(value)
|
2022-12-14 00:07:20 +00:00
|
|
|
self.__fireChanges([ActiveListIndexAppended(len(self) - 1, value)])
|
|
|
|
|
|
|
|
def __fireChanges(self, changes: list[ActiveListChange]):
|
2022-12-12 16:39:07 +00:00
|
|
|
for listener in self.listeners:
|
|
|
|
try:
|
2022-12-14 00:07:20 +00:00
|
|
|
listener.onListChange(changes)
|
2022-12-12 16:39:07 +00:00
|
|
|
except Exception:
|
2022-12-14 00:07:20 +00:00
|
|
|
logger.exception("Exception during onListChange notification")
|
2022-12-12 16:39:07 +00:00
|
|
|
|
|
|
|
def remove(self, value):
|
|
|
|
self.__delitem__(self.delegate.index(value))
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
2022-12-14 00:07:20 +00:00
|
|
|
if self.delegate[key] == value:
|
|
|
|
return
|
|
|
|
oldValue = self.delegate[key]
|
2022-12-12 16:39:07 +00:00
|
|
|
self.delegate[key] = value
|
2022-12-14 00:07:20 +00:00
|
|
|
self.__fireChanges([ActiveListIndexUpdated(key, oldValue, value)])
|
2022-12-12 16:39:07 +00:00
|
|
|
|
|
|
|
def __delitem__(self, key):
|
2022-12-14 00:07:20 +00:00
|
|
|
oldValue = self.delegate[key]
|
2022-12-12 16:39:07 +00:00
|
|
|
del self.delegate[key]
|
2022-12-14 00:07:20 +00:00
|
|
|
self.__fireChanges([ActiveListIndexDeleted(key, oldValue)])
|
2022-12-12 16:39:07 +00:00
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return self.delegate[key]
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.delegate)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self.delegate.__iter__()
|