home · contact · privacy
Differentiate log files by server/"client ID" directories.
authorChristian Heller <c.heller@plomlompom.de>
Mon, 29 Sep 2025 00:04:41 +0000 (02:04 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 29 Sep 2025 00:04:41 +0000 (02:04 +0200)
src/ircplom/client_tui.py

index f92215c00a2d1b23dabd6dee608f22392f5581fd..51398f265865ecdbc8e4bb695a43d67e417f01fa 100644 (file)
@@ -43,26 +43,33 @@ class _ClientWindow(Window, ClientQueueMixin):
     def __init__(self, path_logs: Optional[Path], **kwargs) -> None:
         self._path_logs = path_logs
         super().__init__(**kwargs)
-        self._title = f'{self.client_id} :DEBUG'
+        self._title = ':DEBUG'
+
+    @property
+    def title(self) -> str:
+        return f'{self.client_id} {self._title}'
 
     def log(self, msg: str) -> None:
         super().log(msg)
         if self._path_logs is None:
             return
-        escaped = ''
-        for char in self._title:
-            if (char.isalpha() and char.isascii()) or char in '-.':
-                escaped += char
-            else:
-                escaped += '_'
-                escaped += '.'.join(str(int(b)) for b in char.encode('utf8'))
-                escaped += '_'
-        ldir = _PATH_LOGS.joinpath(escaped)
-        if not ldir.exists():
-            ldir.mkdir(parents=True)
-        assert ldir.is_dir()
-        with ldir.joinpath(f'{self._last_today}.txt'
-                           ).open('a', encoding='utf8') as f:
+
+        def escaped(word: str) -> str:
+            ret = ''
+            for char in word:
+                if (char.isalpha() and char.isascii()) or char in '-.':
+                    ret += char
+                else:
+                    ret += '_'
+                    ret += '.'.join(str(int(b)) for b in char.encode('utf8'))
+                    ret += '_'
+            return ret
+        client_path = _PATH_LOGS.joinpath(escaped(self.client_id))
+        chat_path = client_path.joinpath(escaped(self._title))
+        if not chat_path.exists():
+            chat_path.mkdir(parents=True)
+        with chat_path.joinpath(f'{self._last_today}.txt'
+                                ).open('a', encoding='utf8') as f:
             f.write(msg + '\n')
 
     def _send_msg(self, verb: str, params: tuple[str, ...]) -> None:
@@ -123,12 +130,16 @@ class _ChatWindow(_ClientWindow):
 
     def __init__(self, chatname: str, get_nick_data: Callable, **kwargs
                  ) -> None:
-        self.chatname = chatname
         self._get_nick_data = get_nick_data
         super().__init__(**kwargs)
-        self._title = f'{self.client_id} {self.chatname}'
+        self._title = chatname
         self.set_prompt_prefix()
 
+    @property
+    def chatname(self) -> str:
+        'Name of channel or user with whom we chat.'
+        return self._title
+
     def set_prompt_prefix(self) -> None:
         'Look up relevant DB data to update prompt prefix.'
         self.prompt.set_prefix_data(self._get_nick_data())