From: Christian Heller Date: Tue, 5 Aug 2025 16:20:23 +0000 (+0200) Subject: Replace AffectiveEvent.make_subtype with .affector of better inheritance handling. X-Git-Url: https://plomlompom.com/repos/booking/%7B%7B%20web_path%20%7D%7D/static/%7B%7Bdb.prefix%7D%7D/todo?a=commitdiff_plain;h=12cb638dcd07b172b9f3edd508b57c9bfefbf054;p=ircplom Replace AffectiveEvent.make_subtype with .affector of better inheritance handling. --- diff --git a/ircplom/client.py b/ircplom/client.py index e2d3859..bba5dfb 100644 --- a/ircplom/client.py +++ b/ircplom/client.py @@ -42,9 +42,8 @@ class _IrcConnection(BaseIrcConnection, ClientIdMixin): **kwargs) def _make_recv_event(self, msg: IrcMessage) -> ClientEvent: - # pylint: disable=unexpected-keyword-arg # confused by client_id= - return ClientEvent.make_subtype(method_to_call='handle_msg', msg=msg)( - client_id=self.client_id) + return ClientEvent.affector()( + client_id=self.client_id, t_method='handle_msg').kw(msg=msg) @dataclass @@ -54,8 +53,9 @@ class ClientQueueMixin(QueueMixin, ClientIdMixin): def _cput(self, event_class, **kwargs) -> None: self._put(event_class(client_id=self.client_id, **kwargs)) - def _cputs(self, method_to_call: str, **kwargs) -> None: - self._cput(ClientEvent.make_subtype(method_to_call, **kwargs)) + def _cputs(self, t_method: str, **kwargs) -> None: + self._put(ClientEvent.affector()( + client_id=self.client_id, t_method=t_method).kw(**kwargs)) @dataclass diff --git a/ircplom/events.py b/ircplom/events.py index 0da7574..7c3e236 100644 --- a/ircplom/events.py +++ b/ircplom/events.py @@ -23,16 +23,29 @@ class AffectiveEvent(Event, ABC): 'To be run by main loop on target.' @classmethod - def make_subtype(cls: Any, method_to_call: str, **kwargs): - '''Return subclass whose .affect calls target.method_to_call(**kwargs). + def affector(cls): + '''Return subclass whose .affect calls target method on **kwargs. This will often be more convenient than a full subclass definition, which mostly only matters for what the main loop gotta differentiate. ''' - class _AdaptedEvent(cls): + class _Affector: + def __init__(self, t_method: str, **kwargs) -> None: + super().__init__(**kwargs) + self.t_method = t_method + self.kwargs: dict[str, Any] = {} + + def kw(self, **kwargs) -> Self: + 'Collect .kwargs expanded, return self for chaining.' + for k, v in kwargs.items(): + self.kwargs[k] = v + return self + def affect(self, target) -> None: - getattr(target, method_to_call)(**kwargs) - return _AdaptedEvent + 'Call target.t_method(**.kwargs).' + getattr(target, self.t_method)(**self.kwargs) + + return type('', (_Affector, cls), {}) @dataclass diff --git a/ircplom/tui_base.py b/ircplom/tui_base.py index ca427a9..dc2585f 100644 --- a/ircplom/tui_base.py +++ b/ircplom/tui_base.py @@ -602,6 +602,6 @@ class Terminal(QueueMixin): to_yield += c else: to_yield = 'esc:' + ':'.join([str(int(b)) for b in chars]) - yield (TuiEvent.make_subtype('handle_keyboard_event', - typed_in=to_yield)() if to_yield + yield (TuiEvent.affector()('handle_keyboard_event' + ).kw(typed_in=to_yield) if to_yield else None)