Compare commits

..

2 Commits

Author SHA1 Message Date
John Lancaster
8eea62f403 added suppress option 2026-02-22 20:55:59 -06:00
John Lancaster
05ba036346 better new_path 2026-02-22 20:54:05 -06:00
2 changed files with 10 additions and 7 deletions

View File

@@ -1,6 +1,5 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from collections.abc import Container, Iterable, MutableMapping, MutableSequence, Sized from collections.abc import Container, Iterable, MutableMapping, MutableSequence, Sized
from copy import copy
from typing import Protocol, TypeVar from typing import Protocol, TypeVar
from . import events as e from . import events as e
@@ -57,9 +56,7 @@ class HookedContainer(ABC, Sized, Iterable[T], Container[T]):
# Custom Methods # Custom Methods
def new_path(self, key): def new_path(self, key):
new_path = copy(self._path) return (*self._path, key)
new_path.append(key)
return new_path
def get_nested(self, keys): def get_nested(self, keys):
"""Recursively call __getitem__ with each key in the iterable.""" """Recursively call __getitem__ with each key in the iterable."""

View File

@@ -30,7 +30,7 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]):
def __getitem__(self, key: T) -> MutableNesting[T]: def __getitem__(self, key: T) -> MutableNesting[T]:
if key not in self._data: if key not in self._data:
return self.__setitem__(key, {}) return HookedMapping(self.__setitem__(key, {}), hook=self.hook, path=self.new_path(key))
value = self._data[key] value = self._data[key]
match value: match value:
case MutableMapping() as mapping: case MutableMapping() as mapping:
@@ -38,7 +38,13 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]):
case _ as item: case _ as item:
return item return item
def __setitem__(self, key: T, value: MutableNesting[T]) -> MutableNesting[T] | None: def __setitem__(
self,
key: T,
value: MutableNesting[T],
*,
suppress_hook: bool = False,
) -> MutableNesting[T] | None:
new_path = self.new_path(key) new_path = self.new_path(key)
event = e.SetItemEvent(new_path, value) if key in self._data else e.AddItemEvent(new_path, value) event = e.SetItemEvent(new_path, value) if key in self._data else e.AddItemEvent(new_path, value)
match value: match value:
@@ -46,7 +52,7 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]):
self._data[key] = value self._data[key] = value
case _: case _:
self._data[key] = value self._data[key] = value
if self.hook: if self.hook and not suppress_hook:
self.hook(event) self.hook(event)
return value return value