org
This commit is contained in:
@@ -2,6 +2,7 @@ from collections.abc import MutableMapping, MutableSequence, Sequence
|
||||
from copy import copy
|
||||
from typing import TypeVar
|
||||
|
||||
from . import events as e
|
||||
from .common import HookedContainer, MutableNesting
|
||||
from .sequence import HookedList
|
||||
|
||||
@@ -23,29 +24,23 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]):
|
||||
|
||||
def __getitem__(self, key: T) -> MutableNesting[T]:
|
||||
value = self._data[key]
|
||||
new_path = copy(self._path)
|
||||
new_path.append(key)
|
||||
match value:
|
||||
case MutableMapping() as mapping:
|
||||
new_path = copy(self._path)
|
||||
new_path.append(key)
|
||||
return type(self)(self.hook, existing=mapping, path=new_path)
|
||||
return HookedMapping(hook=self.hook, existing=mapping, path=new_path)
|
||||
case MutableSequence() as seq:
|
||||
new_path = copy(self._path)
|
||||
new_path.append(key)
|
||||
return HookedList(self.hook, existing=seq, path=new_path)
|
||||
return HookedList(hook=self.hook, existing=seq, path=new_path)
|
||||
case _ as item:
|
||||
return item
|
||||
|
||||
def __setitem__(self, key: T, value: MutableNesting[T]) -> None:
|
||||
self._data[key] = value
|
||||
if self.hook:
|
||||
from . import events as e
|
||||
|
||||
self.hook(e.SetItemEvent(index=key, item=value))
|
||||
|
||||
def __delitem__(self, key: T) -> None:
|
||||
item = self._data[key]
|
||||
del self._data[key]
|
||||
if self.hook:
|
||||
from . import events as e
|
||||
|
||||
self.hook(e.RemoveItemEvent(index=key, item=item))
|
||||
self.hook(e.RemoveItemEvent(index=key, item=item, path=self._path))
|
||||
|
||||
Reference in New Issue
Block a user