From 4a3d1c76e3da6cca7ff378291d351428ddff4074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Mon, 8 Apr 2019 14:27:40 +0100 Subject: ChatTab: Introduce self.jid as a JID object MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- poezio/tabs/basetabs.py | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/poezio/tabs/basetabs.py b/poezio/tabs/basetabs.py index 213f39d7..048a8187 100644 --- a/poezio/tabs/basetabs.py +++ b/poezio/tabs/basetabs.py @@ -18,9 +18,9 @@ import string import time from datetime import datetime from xml.etree import cElementTree as ET -from typing import Any, Callable, Dict, List, Optional +from typing import Any, Callable, Dict, List, Optional, Union -from slixmpp import JID, Message +from slixmpp import JID, InvalidJID, Message from poezio.core.structs import Command, Completion, Status from poezio import timed_events @@ -462,7 +462,7 @@ class ChatTab(Tab): plugin_keys = {} # type: Dict[str, Callable] message_type = 'chat' - def __init__(self, core, jid=''): + def __init__(self, core, jid: Union[JID, str]): Tab.__init__(self, core) self.name = jid self.text_win = None @@ -509,12 +509,48 @@ class ChatTab(Tab): for message in logs: self._text_buffer.add_message(**message) + @property + def name(self) -> str: + if self.jid is not None: + return self.jid.full + return self.name + + @name.setter + def name(self, value: Union[JID, str]) -> None: + if isinstance(value, JID): + self.jid = value + elif isinstance(value, str): + try: + value = JID(value) + if value.domain: + self.jid = value + self.name = value.full + except InvalidJID: + self.name = value + else: + raise TypeError("Name must be of type JID or str.") + + @property + def jid(self) -> Optional[JID]: + return self.jid + + @jid.setter + def jid(self, value: Optional[JID]) -> None: + if value is None: + self.jid = None + return None + if not isinstance(value, JID): + raise TypeError("Jid must be of type Optional[JID].") + if not value.domain: + raise ValueError("Jid must contain at least a domain.") + self.jid = value + @property def general_jid(self) -> JID: raise NotImplementedError def load_logs(self, log_nb: int) -> Optional[List[Dict[str, Any]]]: - logs = logger.get_logs(safeJID(self.name).bare, log_nb) + logs = logger.get_logs(self.jid.bare, log_nb) return logs def log_message(self, -- cgit v1.2.3