From a591a71e0f424441a163d4d5d66547d0c9483da4 Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Tue, 17 Sep 2024 11:07:09 +0100 Subject: [PATCH] WIP --- echo/containers.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/echo/containers.py b/echo/containers.py index b26cdd2f..e598b8e7 100644 --- a/echo/containers.py +++ b/echo/containers.py @@ -7,7 +7,12 @@ class ContainerMixin: + def _setup_container(self): + self. + def _prepare_add(self, value): + for validator in self._item_validators: + value = validator(value) if isinstance(value, list): value = CallbackList(self.notify_all, value) elif isinstance(value, dict): @@ -24,6 +29,11 @@ def _cleanup_remove(self, value): elif isinstance(value, (CallbackList, CallbackDict)): value.callbacks.remove(self.notify_all) + def add_item_validator(self, validator, priority=0): + self._item_validators.append(validator, priority=priority) + + # TODO: implement remove_item_validator + class CallbackList(list, ContainerMixin): """ @@ -37,6 +47,7 @@ def __init__(self, callback, *args, **kwargs): super(CallbackList, self).__init__(*args, **kwargs) self.callbacks = CallbackContainer() self.callbacks.append(callback) + self._item_validators = CallbackContainer() for index, value in enumerate(self): super().__setitem__(index, self._prepare_add(value)) @@ -115,6 +126,7 @@ def __init__(self, callback, *args, **kwargs): super(CallbackDict, self).__init__(*args, **kwargs) self.callbacks = CallbackContainer() self.callbacks.append(callback) + self._item_validators = CallbackContainer() for key, value in self.items(): super().__setitem__(key, self._prepare_add(value))