From: Christian Heller Date: Mon, 4 Aug 2025 10:15:46 +0000 (+0200) Subject: Some more Events refactoring. X-Git-Url: https://plomlompom.com/repos/booking/%7B%7B%20web_path%20%7D%7D/static/todo?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=ircplom Some more Events refactoring. --- diff --git a/ircplom/client.py b/ircplom/client.py index 9ddea32..d4e91b6 100644 --- a/ircplom/client.py +++ b/ircplom/client.py @@ -26,6 +26,15 @@ class ClientIdMixin: class ClientEvent(AffectiveEvent, ClientIdMixin): 'To affect Client identified by ClientIdMixin.' + @classmethod + def make_subtype(cls, method_to_call: str, **kwargs + ) -> type['ClientEvent']: + 'Return subclass whose .affect calls method_to_call on target, kwargs.' + class _AdaptedEvent(ClientEvent): + def affect(self, target) -> None: + getattr(target, method_to_call)(**kwargs) + return _AdaptedEvent + class _IrcConnection(BaseIrcConnection): @@ -35,15 +44,8 @@ class _IrcConnection(BaseIrcConnection): super().__init__(**kwargs) def _make_recv_event(self, msg: IrcMessage) -> ClientEvent: - - @dataclass - class _RecvEvent(ClientEvent, PayloadMixin): - payload: IrcMessage - - def affect(self, target: 'Client') -> None: - target.handle_msg(self.payload) - - return _RecvEvent(client_id=self.client_id, payload=msg) + return ClientEvent.make_subtype(method_to_call='handle_msg', msg=msg)( + client_id=self.client_id) @dataclass @@ -98,17 +100,11 @@ class Client(ABC, ClientQueueMixin): 'Start thread to set up _IrcConnection at .conn.' def connect(self) -> None: - - @dataclass - class _ConnectedEvent(ClientEvent): - def affect(self, target: 'Client') -> None: - target.on_connect() - try: self.conn = _IrcConnection(hostname=self.conn_setup.hostname, q_out=self.q_out, client_id=self.id_) - self._cput(_ConnectedEvent) + self._cput(ClientEvent.make_subtype('on_connect')) except IrcConnAbortException as e: self.log(f'# ALERT: {e}') except Exception as e: # pylint: disable=broad-exception-caught