From d347364c8b0a8b33983a0c22dfefa94dcddd91cc Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Sun, 10 Aug 2025 12:29:13 +0200 Subject: [PATCH] Refactor ._log.add() to mere ._log(), now that .alert is out of the way. --- ircplom/client.py | 44 +++++++++++++++++++++----------------------- ircplom/tui_base.py | 15 ++++++++------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/ircplom/client.py b/ircplom/client.py index e950735..bd6b9a6 100644 --- a/ircplom/client.py +++ b/ircplom/client.py @@ -183,10 +183,10 @@ class Client(ABC, ClientQueueMixin): self.conn_setup.port = PORT_SSL self.client_id = self.conn_setup.hostname super().__init__(client_id=self.client_id, **kwargs) - self._log = Logger(self._do_log) + self._log = Logger(self._do_log).add self.update_login(nick_confirmed=False, nickname=self.conn_setup.nickname) - self._log.add(f'connecting {self.conn_setup}') + self._log(f'connecting {self.conn_setup}') self.start_connecting() def start_connecting(self) -> None: @@ -203,7 +203,7 @@ class Client(ABC, ClientQueueMixin): client_id=self.client_id) self._client_trigger('_on_connect') except IrcConnAbortException as e: - self._log.add(str(e), alert=True) + self._log(str(e), alert=True) except Exception as e: # pylint: disable=broad-exception-caught self._put(ExceptionEvent(e)) @@ -212,9 +212,9 @@ class Client(ABC, ClientQueueMixin): def _on_connect(self) -> None: 'Steps to perform right after connection.' assert self.conn is not None - self._log.add('connected to server (SSL: ' - f'{"yes" if self.conn.ssl else "no"})', - stream=STREAM_ALL) + self._log('connected to server (SSL: ' + f'{"yes" if self.conn.ssl else "no"})', + stream=STREAM_ALL) self._caps.challenge('LS', '302') self.send(IrcMessage(verb='USER', params=(getuser(), '0', '*', @@ -232,13 +232,13 @@ class Client(ABC, ClientQueueMixin): ) -> None: 'Send line-separator-delimited message over socket.' if not self.conn: - self._log.add('cannot send, connection seems closed', alert=True, - stream=stream) + self._log('cannot send, connection seems closed', alert=True, + stream=stream) return self.conn.send(msg) if to_log: - self._log.add(to_log, prefix='', stream=stream) - self._log.add(msg.raw, prefix=_LOG_PREFIX_SEND_RAW, stream=STREAM_RAW) + self._log(to_log, prefix='', stream=stream) + self._log(msg.raw, prefix=_LOG_PREFIX_SEND_RAW, stream=STREAM_RAW) def update_login(self, nick_confirmed: bool, nickname: str = '') -> None: '''Manage conn_setup.nickname, .nick_confirmed. @@ -251,17 +251,16 @@ class Client(ABC, ClientQueueMixin): verb = ('set' if first_run else f'changed from "{self.conn_setup.nickname}"') self.conn_setup.nickname = nickname - self._log.add(f'{prefix} {verb} to "{nickname}"', - stream=STREAM_ALL) + self._log(f'{prefix} {verb} to "{nickname}"', stream=STREAM_ALL) if first_run or nick_confirmed != self.nick_confirmed: self.nick_confirmed = nick_confirmed if not first_run: - self._log.add(f'{prefix} {"" if nick_confirmed else "un"}' - 'confirmed') + self._log( + f'{prefix} {"" if nick_confirmed else "un"}confirmed') def close(self) -> None: 'Close both recv Loop and socket.' - self._log.add(msg='disconnecting from server', stream=STREAM_ALL) + self._log(msg='disconnecting from server', stream=STREAM_ALL) self._caps.clear() if self.conn: self.conn.close() @@ -270,12 +269,12 @@ class Client(ABC, ClientQueueMixin): def on_handled_loop_exception(self, e: IrcConnAbortException) -> None: 'Gracefully handle broken connection.' - self._log.add(f'connection broken: {e}', alert=True) + self._log(f'connection broken: {e}', alert=True) self.close() def handle_msg(self, msg: IrcMessage) -> None: 'Process incoming msg towards appropriate client steps.' - self._log.add(msg.raw, prefix=_LOG_PREFIX_RECV_RAW, stream=STREAM_RAW) + self._log(msg.raw, prefix=_LOG_PREFIX_RECV_RAW, stream=STREAM_RAW) match msg.verb: case 'PING': self.send(IrcMessage(verb='PONG', params=(msg.params[0],))) @@ -285,18 +284,17 @@ class Client(ABC, ClientQueueMixin): self.update_login(nickname=msg.params[0], nick_confirmed=True) case 'PRIVMSG': nickname = msg.source.split('!')[0] - self._log.add(f'< [{nickname}] {msg.params[-1]}', - stream=nickname, - prefix=_LOG_PREFIX_PRIVMSG) + self._log(f'< [{nickname}] {msg.params[-1]}', stream=nickname, + prefix=_LOG_PREFIX_PRIVMSG) case 'CAP': if (result := self._caps.process_msg(msg.params[1:])): if isinstance(result, str): - self._log.add(result) + self._log(result) else: for line in (['server capabilities (enabled: "+"):'] + [cap.str_for_log(c_name) for c_name, cap in self._caps.dict.items()]): - self._log.add(line) + self._log(line) if self._caps.sasl_wait: if self.conn_setup.password: self.send(IrcMessage('AUTHENTICATE', ('PLAIN',))) @@ -311,7 +309,7 @@ class Client(ABC, ClientQueueMixin): self.send(IrcMessage('AUTHENTICATE', (auth,))) case '903' | '904': if msg.verb == '904': - self._log.add('SASL authentication failed', alert=True) + self._log('SASL authentication failed', alert=True) self._caps.challenge('END') diff --git a/ircplom/tui_base.py b/ircplom/tui_base.py index 6b8bd9b..8d02901 100644 --- a/ircplom/tui_base.py +++ b/ircplom/tui_base.py @@ -371,7 +371,8 @@ class BaseTui(QueueMixin): self._new_window() self._log = Logger( lambda msg: # pylint: disable=unnecessary-lambda # to keep up-to… - self.window.history.append(msg)) # …-date _what_ window's .history + self.window.history.append(msg) # …-date _what_… + ).add # …window's .history self._set_screen() signal(SIGWINCH, lambda *_: self._set_screen()) @@ -453,7 +454,7 @@ class BaseTui(QueueMixin): elif len(typed_in) == 1: self.window.prompt.insert(typed_in) else: - self._log.add(f'unknown keyboard input: {typed_in}', alert=True) + self._log(f'unknown keyboard input: {typed_in}', alert=True) self.redraw_affected() def cmd__prompt_enter(self) -> None: @@ -487,11 +488,11 @@ class BaseTui(QueueMixin): else: alert = 'not prefixed by /' if alert: - self._log.add(f'invalid prompt command: {alert}', alert=True) + self._log(f'invalid prompt command: {alert}', alert=True) def cmd__help(self) -> None: 'Print available commands.' - self._log.add('commands available in this window:') + self._log('commands available in this window:') to_log = [] for cmd_name, cmd_data in self._commands.items(): args = [] @@ -502,13 +503,13 @@ class BaseTui(QueueMixin): args += [arg] to_log += [f'{cmd_name} {" ".join(args)}'] for item in sorted(to_log): - self._log.add(f' /{item}') + self._log(f' /{item}') def cmd__list(self) -> None: 'List available windows.' - self._log.add('windows available via /window:') + self._log('windows available via /window:') for win in self._windows: - self._log.add(f' {win.status_title}') + self._log(f' {win.status_title}') def cmd__quit(self) -> None: 'Trigger program exit.' -- 2.30.2