home · contact · privacy
Simplify topic setting code.
authorChristian Heller <c.heller@plomlompom.de>
Thu, 4 Sep 2025 01:36:37 +0000 (03:36 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Thu, 4 Sep 2025 01:36:37 +0000 (03:36 +0200)
ircplom/client.py
ircplom/client_tui.py

index dbeff78738b147e73d59a4ccab325a322f30192d..67d3edef598dc601bf8e7bdc101e68db4a8b6046 100644 (file)
@@ -6,7 +6,7 @@ from dataclasses import dataclass, InitVar
 from enum import Enum, auto
 from getpass import getuser
 from threading import Thread
-from typing import Any, Callable, Generic, Optional, Self, TypeVar
+from typing import Any, Callable, Generic, NamedTuple, Optional, Self, TypeVar
 from uuid import uuid4
 # ourselves
 from ircplom.events import (
@@ -140,7 +140,7 @@ class _UpdatingMixin(AutoAttrMixin):
         if isinstance(self, _CompletableStringsList):
             return self.completed
         if isinstance(self, _Topic):
-            return self.into_parent()
+            return Topic(*self.completed)
         for cls in [cls for cls in self.__class__.__mro__
                     if AutoAttrMixin not in cls.__mro__]:
             obj = cls()
@@ -306,65 +306,42 @@ class _IrcConnection(BaseIrcConnection, _ClientIdMixin):
                                     client_id=self.client_id).kw(e=e)
 
 
-_TopicWho = TypeVar('_TopicWho', bound=NickUserHost)
+class Topic(NamedTuple):
+    'Collects both setter and content of channel topic.'
+    what: str = ''
+    who: Optional[NickUserHost] = None
 
 
-class Topic[_TopicWho]:
-    'Encapsulates content and setter of channel topic.'
-    _what = ''
-    _who: Optional[_TopicWho] = None
+class _Topic:
+    _what: str = ''
+    _who: Optional[NickUserHost] = None
 
-    def __eq__(self, other) -> bool:
-        return isinstance(self, Topic) and isinstance(other, Topic)\
-                and self.what == other.what\
-                and self.who == other.who
-
-    def _set_what(self, what: str) -> None:
-        pass
-
-    def _set_who(self, who: _TopicWho) -> None:
-        pass
+    def __init__(self) -> None:
+        self.completed: tuple[str, Optional[NickUserHost]] = ('', None)
 
     @property
     def what(self) -> str:
-        'Content of channel topic.'
-        return self._what[:]
+        'Content of topic.'
+        return self._what
 
     @what.setter
     def what(self, what: str) -> None:
-        self._set_what(what)
+        self._what = what
 
     @property
-    def who(self) -> Optional[_TopicWho]:
-        'Setter of channel topic.'
+    def who(self) -> Optional[NickUserHost]:
+        'Setter of topic.'
         return self._who
 
     @who.setter
-    def who(self, who: _TopicWho) -> None:
-        self._set_who(who)
-
-
-class _Topic(Topic['_NickUserHost']):
-
-    def __init__(self) -> None:
-        self.completed: tuple[str, Optional['_NickUserHost']] = ('', None)
-
-    def _set_what(self, what: str) -> None:
-        self._what = what
-
-    def _set_who(self, who: '_NickUserHost') -> None:
-        self._who = who.copy()
+    def who(self, who: NickUserHost) -> None:
+        copy = _NickUserHost.from_str(str(who))
+        self._who = NickUserHost(copy.nick, copy.user, copy.host)
 
     def complete(self) -> None:
         'Declare data collection finished, return full or empty content.'
         self.completed = (('', None) if self._who is None
-                          else (self._what, self._who.copy()))
-
-    def into_parent(self) -> Topic:
-        'Transform into as presentable as impotent parent class variant.'
-        obj = Topic[NickUserHost]()
-        obj._what, obj._who = self.completed
-        return obj
+                          else (self._what, self._who))
 
 
 class _Channel:
@@ -446,10 +423,6 @@ class _NickUserHost(NickUserHost):
                         )[-1]
         return name + str(0 if not digits else (int(digits) + 1))
 
-    def copy(self) -> Self:
-        'Create indepedentent copy.'
-        return self.from_str(str(self))
-
 
 class _UpdatingServerCapability(_UpdatingMixin, ServerCapability):
     pass
index 6c2aee08a0c6c6daa53c884827422d9e0f72598e..ae0da1208c5cda8bd922d8c8d7d34f27c5d6de96 100644 (file)
@@ -191,7 +191,7 @@ class _ChannelWindow(_ChatWindow):
 
 class _UpdatingChannel(_UpdatingNode):
     user_ids: tuple[str, ...] = tuple()
-    topic: Topic[NickUserHost] = Topic()
+    topic: Topic = Topic()
     log_scopes = {tuple(): LogScope.CHAT}
 
     def set_and_check_for_change(self, update: _Update