From a0277adafeba743cd0e8eafd99c761725579089d Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Thu, 11 Sep 2025 15:34:09 +0200 Subject: [PATCH] Structure .clear ability into proper interface. --- ircplom/client.py | 21 ++++++++++++--------- ircplom/client_tui.py | 2 +- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/ircplom/client.py b/ircplom/client.py index afb4502..c958de7 100644 --- a/ircplom/client.py +++ b/ircplom/client.py @@ -40,10 +40,17 @@ class AutoAttrMixin: return super().__getattribute__(key) +class _Clearable(ABC): + + @abstractmethod + def clear(self) -> None: + 'Zero internal knowledge.' + + DictItem = TypeVar('DictItem') -class Dict(Generic[DictItem]): +class Dict(_Clearable, Generic[DictItem]): 'Customized dict replacement.' def __init__(self, **kwargs) -> None: @@ -58,7 +65,6 @@ class Dict(Generic[DictItem]): return self._dict[key] def clear(self) -> None: - 'Zero content.' self._dict.clear() @property @@ -105,7 +111,7 @@ class _Completable(ABC): 'Set .completed to "complete" value if possible of current state.' -class _CompletableStringsList(_Completable): +class _CompletableStringsList(_Clearable, _Completable): def __init__(self) -> None: self._incomplete: list[str] = [] @@ -128,7 +134,6 @@ class _CompletableStringsList(_Completable): self.completed = tuple(self._incomplete) def clear(self) -> None: - 'Wipe content and declare new emptiness as complete.' self._incomplete.clear() self.complete() @@ -528,7 +533,7 @@ class _UpdatingChannelsDict(_UpdatingDict[_UpdatingChannel]): channel.remove_user(user) -class _ClientDb(_UpdatingMixin, SharedClientDbFields): +class _ClientDb(_Clearable, _UpdatingMixin, SharedClientDbFields): _keep_on_clear = set(IrcConnSetup.__annotations__.keys()) caps: _UpdatingDict[_UpdatingServerCapability] channels: _UpdatingChannelsDict @@ -556,10 +561,9 @@ class _ClientDb(_UpdatingMixin, SharedClientDbFields): return attr def clear(self) -> None: - 'Wipe updating attributes.' for key, value in [(k, v) for k, v in self._deep_annotations().items() if k not in self._keep_on_clear]: - if hasattr(value, 'clear'): + if issubclass(value, _Clearable): getattr(self, key).clear() elif issubclass(value, str): setattr(self, key, '') @@ -584,7 +588,7 @@ class _ClientDb(_UpdatingMixin, SharedClientDbFields): return toks[1] -class _CapsManager: +class _CapsManager(_Clearable): def __init__(self, sender: Callable, @@ -595,7 +599,6 @@ class _CapsManager: self.clear() def clear(self) -> None: - 'Zero internal knowledge.' self._dict.clear() self._ls = _CompletableStringsList() self._list = _CompletableStringsList() diff --git a/ircplom/client_tui.py b/ircplom/client_tui.py index 98ff459..b69ec96 100644 --- a/ircplom/client_tui.py +++ b/ircplom/client_tui.py @@ -232,7 +232,7 @@ class _UpdatingUser(_UpdatingNode, NickUserHost): return super().set_and_check_for_change(update) if super().set_and_check_for_change(update): scope = self._scope(update.path) - msg = f'RAW: ' + msg = 'RAW: ' if update.path[-1] == 'nick': return scope, msg + f'{self.prev} renames {update.value}' if update.path[-1] == 'exit_msg' and update.value: -- 2.30.2