From: Christian Heller Date: Tue, 5 Aug 2025 17:48:09 +0000 (+0200) Subject: Various smaller code-style improvements. X-Git-Url: https://plomlompom.com/repos/booking/static/%7B%7Bdb.prefix%7D%7D/reset_cookie?a=commitdiff_plain;ds=sidebyside;p=ircplom Various smaller code-style improvements. --- diff --git a/ircplom/client.py b/ircplom/client.py index bba5dfb..598c62f 100644 --- a/ircplom/client.py +++ b/ircplom/client.py @@ -198,7 +198,7 @@ class Client(ABC, ClientQueueMixin): port=self.conn_setup.port, _q_out=self._q_out, client_id=self.client_id) - self._cputs('on_connect') + self._cputs('_on_connect') except IrcConnAbortException as e: self.log.alert(str(e)) except Exception as e: # pylint: disable=broad-exception-caught @@ -206,7 +206,7 @@ class Client(ABC, ClientQueueMixin): Thread(target=connect, daemon=True, args=(self,)).start() - def on_connect(self) -> None: + def _on_connect(self) -> None: 'Steps to perform right after connection.' assert self.conn is not None self.log.add('connected to server (SSL: ' @@ -236,7 +236,7 @@ class Client(ABC, ClientQueueMixin): self.log.add(msg.raw, prefix=_LOG_PREFIX_SEND_RAW, chat=':raw') def update_login(self, nick_confirmed: bool, nickname: str = '') -> None: - '''Manage conn_setup..nickname, .nick_confirmed. + '''Manage conn_setup.nickname, .nick_confirmed. (Useful for subclass extension.) ''' @@ -253,7 +253,7 @@ class Client(ABC, ClientQueueMixin): self.log.add(f'{prefix} {"" if nick_confirmed else "un"}' 'confirmed') - def close(self) -> None: + def _close(self) -> None: 'Close both recv Loop and socket.' self.log.add(msg='disconnecting from server', chat=CHAT_GLOB) self._caps.clear() @@ -269,7 +269,7 @@ class Client(ABC, ClientQueueMixin): case 'PING': self.send(IrcMessage(verb='PONG', params=(msg.params[0],))) case 'ERROR': - self.close() + self._close() case '001' | 'NICK': self.update_login(nickname=msg.params[0], nick_confirmed=True) case 'PRIVMSG': diff --git a/ircplom/client_tui.py b/ircplom/client_tui.py index d382618..b602b87 100644 --- a/ircplom/client_tui.py +++ b/ircplom/client_tui.py @@ -43,8 +43,8 @@ class _ClientWindow(Window, ClientQueueMixin): super().__init__(**kwargs) @property - def status_title(self) -> str: - return f'{super().status_title}|{self.client_id}|{self.chat}' + def _status_title(self) -> str: + return f'{super()._status_title}|{self.client_id}|{self.chat}' def _send_msg(self, verb: str, params: tuple[str, ...], **kwargs) -> None: self._cputs('send', msg=IrcMessage(verb=verb, params=params), **kwargs) @@ -96,18 +96,18 @@ class ClientTui(BaseTui): def _new_client_window(self, client_id: str, chat: str = '' ) -> _ClientWindow: - new_idx = len(self.windows) + new_idx = len(self._windows) win_class = (_PrivmsgWindow if (chat and chat[0].isalpha()) else _ClientWindow) - win = win_class(parent=self, idx=new_idx, term=self.term, + win = win_class(parent=self, idx=new_idx, term=self._term, _q_out=self._q_out, client_id=client_id, chat=chat) - self.windows += [win] + self._windows += [win] self._switch_window(new_idx) return win @property def _all_client_wins(self) -> list[_ClientWindow]: - return [win for win in self.windows if isinstance(win, _ClientWindow)] + return [win for win in self._windows if isinstance(win, _ClientWindow)] def client_wins(self, client_id: str = '') -> list[_ClientWindow]: 'All _ClientWindows matching client_id; if none, create one.' @@ -139,7 +139,7 @@ class ClientTui(BaseTui): split = host_port.split(':', maxsplit=1) hostname = split[0] if hostname in [win.client_id for win in self._all_client_wins]: - self.log.alert(f'already set up connection to {hostname}') + self._log.alert(f'already set up connection to {hostname}') return port = -1 if len(split) > 1: @@ -147,7 +147,7 @@ class ClientTui(BaseTui): if to_int.isdigit(): port = int(split[1]) else: - self.log.alert(f'invalid port number: {to_int}') + self._log.alert(f'invalid port number: {to_int}') return split = nickname_pw.split(':', maxsplit=1) nickname = split[0] if nickname_pw else getuser() diff --git a/ircplom/events.py b/ircplom/events.py index b1bbe36..3adbe70 100644 --- a/ircplom/events.py +++ b/ircplom/events.py @@ -9,15 +9,13 @@ _LOG_PREFIX_DEFAULT = '# ' _LOG_PREFIX_ALERT = f'{_LOG_PREFIX_DEFAULT}ALERT: ' -@dataclass -class Event: +class Event: # pylint: disable=too-few-public-methods 'Communication unit between threads.' QuitEvent = type('QuitEvent', (Event,), {}) -@dataclass class AffectiveEvent(Event, ABC): 'For Events that are to affect other objects.' diff --git a/ircplom/tui_base.py b/ircplom/tui_base.py index 9dbc1c9..d7c4ba5 100644 --- a/ircplom/tui_base.py +++ b/ircplom/tui_base.py @@ -19,7 +19,7 @@ _B64_PREFIX = 'b64:' _OSC52_PREFIX = b']52;c;' _PASTE_DELIMITER = '\007' -PROMPT_TEMPLATE = '> ' +_PROMPT_TEMPLATE = '> ' _PROMPT_ELL_IN = '<…' _PROMPT_ELL_OUT = '…>' @@ -181,7 +181,7 @@ class PromptWidget(_ScrollableWidget): @property def prefix(self) -> str: 'Main input prefix.' - return PROMPT_TEMPLATE[:] + return _PROMPT_TEMPLATE[:] @property def _input_buffer(self) -> str: @@ -313,7 +313,7 @@ class Window(_Widget): return True @property - def status_title(self) -> str: + def _status_title(self) -> str: 'Window title to display in status line.' return str(self.idx) @@ -329,7 +329,7 @@ class Window(_Widget): for y, line in enumerate(lines): self._term.write(line, y) return False - title_box = f'[{self.status_title}]' + title_box = f'[{self._status_title}]' status_line = title_box + '=' * (self._term.size.x - len(title_box)) self.log.draw() self._term.write(status_line, self._y_status) @@ -360,24 +360,24 @@ class BaseTui(QueueMixin): def __init__(self, term: 'Terminal', **kwargs) -> None: super().__init__(**kwargs) - self.term = term + self._term = term self._window_idx = 0 - self.windows = [Window(idx=self._window_idx, term=self.term)] - self.log = Logger( + self._windows = [Window(idx=self._window_idx, term=self._term)] + self._log = Logger( lambda msg: # pylint: disable=unnecessary-lambda # to keep … - self.window.log.append(msg)) # … up-to-date _what_ window's .log - self.set_screen() - signal(SIGWINCH, lambda *_: self.set_screen()) + self._window.log.append(msg)) # … up-to-date _what_ window's .log + self._set_screen() + signal(SIGWINCH, lambda *_: self._set_screen()) def redraw_affected(self) -> None: 'On focused window call .draw_tainted, then flush screen.' - self.window.draw_tainted() - self.term.flush() + self._window.draw_tainted() + self._term.flush() - def set_screen(self) -> None: - 'Calc screen geometry into .windows, then call .redraw_affected.' - self.term.calc_geometry() - for window in self.windows: + def _set_screen(self) -> None: + 'Calc screen geometry into windows, then call .redraw_affected.' + self._term.calc_geometry() + for window in self._windows: window.set_geometry() self.redraw_affected() @@ -398,18 +398,18 @@ class BaseTui(QueueMixin): return None @property - def window(self) -> Window: + def _window(self) -> Window: 'Currently selected Window.' - return self.windows[self._window_idx] + return self._windows[self._window_idx] def _switch_window(self, idx: int) -> None: self._window_idx = idx - self.window.draw() + self._window.draw() def handle_keyboard_event(self, typed_in: str) -> None: 'Translate keyboard input into appropriate actions.' if typed_in[0] == _CHAR_RESIZE: - self.set_screen() + self._set_screen() return if typed_in in _KEYBINDINGS: cmd_data = _KEYBINDINGS[typed_in] @@ -428,16 +428,16 @@ class BaseTui(QueueMixin): to_paste += ' ' else: to_paste += '#' - self.window.prompt.insert(to_paste) + self._window.prompt.insert(to_paste) elif len(typed_in) == 1: - self.window.prompt.insert(typed_in) + self._window.prompt.insert(typed_in) else: - self.log.alert(f'unknown keyboard input: {typed_in}') + self._log.alert(f'unknown keyboard input: {typed_in}') self.redraw_affected() def cmd__prompt_enter(self) -> None: - 'Get prompt content from .window.prompt.enter, parse to & run command.' - to_parse = self.window.prompt.enter() + 'Get prompt content from ._window.prompt.enter, parse to & run command' + to_parse = self._window.prompt.enter() if not to_parse: return alert: Optional[str] = None @@ -465,7 +465,7 @@ class BaseTui(QueueMixin): else: alert = 'not prefixed by /' if alert: - self.log.alert(f'invalid prompt command: {alert}') + self._log.alert(f'invalid prompt command: {alert}') def cmd__quit(self) -> None: 'Trigger program exit.' @@ -473,7 +473,7 @@ class BaseTui(QueueMixin): def cmd__window(self, towards: str) -> Optional[str]: 'Switch window selection.' - n_windows = len(self.windows) + n_windows = len(self._windows) if n_windows < 2: return 'no alternate window to move into' if towards in {'left', 'right'}: