home · contact · privacy
Get rid of events.py:Logger.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 10 Aug 2025 13:23:53 +0000 (15:23 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 10 Aug 2025 13:23:53 +0000 (15:23 +0200)
ircplom/client.py
ircplom/client_tui.py
ircplom/events.py
ircplom/tui_base.py

index 3c95ce59992286b7c68ee8d4559b0ad2f157537d..fd192a81c2dcc794c7cafd4c5881653e7e68b4d1 100644 (file)
@@ -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,
index f4aa60db2b08ce26bd57e6ca75405ee00e6c5577..24969ba0b5525d33c5d0eabbc6ee4df6beeb06b7 100644 (file)
@@ -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]
index 21620809fa3404f4f5fc49950ae3f2a95a2862ad..6f89020d3f320356443f7aae2cfc3babe16b7329 100644 (file)
@@ -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)
index 354a533a2a614952185987d67f2a8b50bcf61b58..b40500ee647e2587434f7143695ed8abb347dcd9 100644 (file)
@@ -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)