From: Christian Heller Date: Sun, 10 Aug 2025 13:23:53 +0000 (+0200) Subject: Get rid of events.py:Logger. X-Git-Url: https://plomlompom.com/repos/booking/static/%7B%7Bdb.prefix%7D%7D/%7B%7Bprefix%7D%7D/%7B%7Bpath_up_incl%7D%7D%7B%7B%20booking.id_%20-%201%20%7D%7D?a=commitdiff_plain;p=ircplom Get rid of events.py:Logger. --- diff --git a/ircplom/client.py b/ircplom/client.py index 3c95ce5..fd192a8 100644 --- a/ircplom/client.py +++ b/ircplom/client.py @@ -7,7 +7,7 @@ from getpass import getuser from threading import Thread from typing import Callable, Optional # ourselves -from ircplom.events import AffectiveEvent, ExceptionEvent, Logger, QueueMixin +from ircplom.events import AffectiveEvent, ExceptionEvent, QueueMixin from ircplom.irc_conn import (BaseIrcConnection, IrcConnAbortException, IrcMessage, PORT_SSL) @@ -175,7 +175,6 @@ 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).add self.update_login(nick_confirmed=False, nickname=self.conn_setup.nickname) self._start_connecting() @@ -212,8 +211,8 @@ class Client(ABC, ClientQueueMixin): self.send(IrcMessage(verb='NICK', params=(self.conn_setup.nickname,))) @abstractmethod - def _do_log(self, msg: str, stream: str = STREAM_SERVER, **kwargs) -> None: - 'Write msg into log of stream, whatever shape that may have.' + def _log(self, msg: str, stream: str = STREAM_SERVER, **kwargs) -> None: + pass def send(self, msg: IrcMessage, diff --git a/ircplom/client_tui.py b/ircplom/client_tui.py index f4aa60d..24969ba 100644 --- a/ircplom/client_tui.py +++ b/ircplom/client_tui.py @@ -94,8 +94,8 @@ class _ClientWindowsManager: def __init__(self, client_id: str, new_window: Callable, tui_log: Callable ) -> None: - self.log = lambda msg, **kw:tui_log(msg=msg, client_id=client_id, **kw - ) or True + self.log = lambda msg, **kw: tui_log(msg=msg, client_id=client_id, **kw + ) or True self.windows: list[_ClientWindow] = [] self._tui_new_window = new_window for stream in (STREAM_SERVER, STREAM_RAW): @@ -160,7 +160,7 @@ class ClientTui(BaseTui): 'Forward todo to appropriate _ClientWindowsManager.' if client_id not in self._client_mngrs: self._client_mngrs[client_id] = _ClientWindowsManager( - client_id=client_id, tui_log=self._targeted_log, + client_id=client_id, tui_log=self._log, new_window=lambda cls, **kw: self._new_window( cls, _q_out=self._q_out, client_id=client_id, **kw)) if getattr(self._client_mngrs[client_id], todo)(**kwargs): @@ -210,7 +210,7 @@ class _ClientKnowingTui(Client): return self._start_connecting() - def _do_log(self, msg: str, stream: str = STREAM_SERVER, **kwargs) -> None: + def _log(self, msg: str, stream: str = STREAM_SERVER, **kwargs) -> None: to_log = [] if msg: to_log += [msg] diff --git a/ircplom/events.py b/ircplom/events.py index 2162080..6f89020 100644 --- a/ircplom/events.py +++ b/ircplom/events.py @@ -6,9 +6,6 @@ from queue import SimpleQueue, Empty as QueueEmpty from threading import Thread from typing import Any, Callable, Iterator, Literal, Self -_LOG_PREFIX_DEFAULT = '# ' -_LOG_PREFIX_ALERT = f'{_LOG_PREFIX_DEFAULT}ALERT: ' - class Event: # pylint: disable=too-few-public-methods 'Communication unit between threads.' @@ -107,22 +104,3 @@ class Loop(QueueMixin): self._put(it_yield) except Exception as e: # pylint: disable=broad-exception-caught self._put(ExceptionEvent(e)) - - -class Logger: - 'Wrapper for logging tasks.' - - def __init__(self, to_call: Callable[[str], None]) -> None: - self._to_call = to_call - - def add(self, - msg: str, - prefix: str = _LOG_PREFIX_DEFAULT, - alert=False, - **kwargs - ) -> None: - 'Add msg to log.' - if alert: - prefix = _LOG_PREFIX_ALERT + prefix - time = str(datetime.now())[11:19] - self._to_call(f'{time} {prefix}{msg}', **kwargs) diff --git a/ircplom/tui_base.py b/ircplom/tui_base.py index 354a533..b40500e 100644 --- a/ircplom/tui_base.py +++ b/ircplom/tui_base.py @@ -3,6 +3,7 @@ from abc import ABC, abstractmethod from base64 import b64decode from contextlib import contextmanager +from datetime import datetime from inspect import _empty as inspect_empty, signature, stack from signal import SIGWINCH, signal from typing import (Callable, Generator, Iterator, NamedTuple, Optional, @@ -10,7 +11,10 @@ from typing import (Callable, Generator, Iterator, NamedTuple, Optional, # requirements.txt from blessed import Terminal as BlessedTerminal # ourselves -from ircplom.events import AffectiveEvent, Logger, Loop, QueueMixin, QuitEvent +from ircplom.events import AffectiveEvent, Loop, QueueMixin, QuitEvent + +_LOG_PREFIX_DEFAULT = '# ' +_LOG_PREFIX_ALERT = f'{_LOG_PREFIX_DEFAULT}ALERT: ' _MIN_HEIGHT = 4 _MIN_WIDTH = 32 @@ -370,7 +374,6 @@ class BaseTui(QueueMixin): self._window_idx = 0 self._windows: list[Window] = [] self._new_window() - self._log = Logger(self._targeted_log).add self._set_screen() signal(SIGWINCH, lambda *_: self._set_screen()) @@ -378,7 +381,11 @@ class BaseTui(QueueMixin): # separated to serve as hook for subclass window selection return [self.window] - def _targeted_log(self, msg: str, **kwargs) -> None: + def _log(self, msg: str, **kwargs) -> None: + prefix = kwargs.get('prefix', _LOG_PREFIX_DEFAULT) + if kwargs.get('alert', False): + prefix = _LOG_PREFIX_ALERT + prefix + msg = f'{str(datetime.now())[11:19]} {prefix}{msg}' for win in self._log_target_wins(**kwargs): win.history.append(msg)