Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
astrofrog committed Sep 17, 2024
1 parent 6b8d742 commit a591a71
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions echo/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
"""
Expand All @@ -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))

Expand Down Expand Up @@ -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))

Expand Down

0 comments on commit a591a71

Please sign in to comment.