diff sat_frontends/primitivus/chat.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents 4d5b9d4c7448
children ab7e8ade848a
line wrap: on
line diff
--- a/sat_frontends/primitivus/chat.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat_frontends/primitivus/chat.py	Tue Aug 13 19:08:41 2019 +0200
@@ -35,7 +35,7 @@
 import bisect
 
 
-OCCUPANTS_FOOTER = _(u"{} occupants")
+OCCUPANTS_FOOTER = _("{} occupants")
 
 
 class MessageWidget(urwid.WidgetWrap, quick_chat.MessageWidget):
@@ -113,16 +113,16 @@
 
         # message status
         if d.status is None:
-            markup.append(u" ")
+            markup.append(" ")
         elif d.status == "delivered":
-            markup.append(("msg_status_received", u"✔"))
+            markup.append(("msg_status_received", "✔"))
         else:
-            log.warning(u"Unknown status: {}".format(d.status))
+            log.warning("Unknown status: {}".format(d.status))
 
         # timestamp
         if self.parent.show_timestamp:
             attr = "msg_mention" if mention else "date"
-            markup.append((attr, u"[{}]".format(d.time_text)))
+            markup.append((attr, "[{}]".format(d.time_text)))
         else:
             if mention:
                 markup.append(("msg_mention", "[*]"))
@@ -134,13 +134,13 @@
             )
         else:
             markup.append(
-                ("my_nick" if d.own_mess else "other_nick", u"[{}] ".format(d.nick or ""))
+                ("my_nick" if d.own_mess else "other_nick", "[{}] ".format(d.nick or ""))
             )
 
         msg = self.message  # needed to generate self.selected_lang
 
         if d.selected_lang:
-            markup.append(("msg_lang", u"[{}] ".format(d.selected_lang)))
+            markup.append(("msg_lang", "[{}] ".format(d.selected_lang)))
 
         # message body
         markup.append(msg)
@@ -171,6 +171,9 @@
         )
         super(OccupantWidget, self).__init__(text)
 
+    def __hash__(self):
+        return id(self)
+
     def __eq__(self, other):
         if other is None:
             return False
@@ -218,11 +221,11 @@
         o = self.occupant_data
         markup = []
         markup.append(
-            ("info_msg", u"{}{} ".format(o.role[0].upper(), o.affiliation[0].upper()))
+            ("info_msg", "{}{} ".format(o.role[0].upper(), o.affiliation[0].upper()))
         )
         markup.append(o.nick)
         if o.state is not None:
-            markup.append(u" {}".format(C.CHAT_STATE_ICON[o.state]))
+            markup.append(" {}".format(C.CHAT_STATE_ICON[o.state]))
         return markup
 
     # events
@@ -240,7 +243,7 @@
             urwid.ListBox(self.occupants_walker), footer=self.occupants_footer
         )
         super(OccupantsWidget, self).__init__(occupants_widget)
-        occupants_list = sorted(self.parent.occupants.keys(), key=lambda o: o.lower())
+        occupants_list = sorted(list(self.parent.occupants.keys()), key=lambda o: o.lower())
         for occupant in occupants_list:
             occupant_data = self.parent.occupants[occupant]
             self.occupants_walker.append(OccupantWidget(occupant_data))
@@ -253,7 +256,7 @@
         txt = OCCUPANTS_FOOTER.format(len(self.parent.occupants))
         self.occupants_footer.set_text(txt)
 
-    def getNicks(self, start=u""):
+    def getNicks(self, start=""):
         """Return nicks of all occupants
 
         @param start(unicode): only return nicknames which start with this text
@@ -281,16 +284,16 @@
 class Chat(PrimitivusWidget, quick_chat.QuickChat):
     def __init__(self, host, target, type_=C.CHAT_ONE2ONE, nick=None, occupants=None,
                  subject=None, profiles=None):
-        quick_chat.QuickChat.__init__(
-            self, host, target, type_, nick, occupants, subject, profiles=profiles
-        )
         self.filters = []  # list of filter callbacks to apply
         self.mess_walker = urwid.SimpleListWalker([])
         self.mess_widgets = urwid.ListBox(self.mess_walker)
         self.chat_widget = urwid.Frame(self.mess_widgets)
         self.chat_colums = urwid.Columns([("weight", 8, self.chat_widget)])
         self.pile = urwid.Pile([self.chat_colums])
-        PrimitivusWidget.__init__(self, self.pile, self.target)
+        PrimitivusWidget.__init__(self, self.pile, target)
+        quick_chat.QuickChat.__init__(
+            self, host, target, type_, nick, occupants, subject, profiles=profiles
+        )
 
         # we must adapt the behaviour with the type
         if type_ == C.CHAT_GROUP:
@@ -365,7 +368,7 @@
             if word_idx == len(words):
                 word_idx = 0
         word = completion_data["last_word"] = words[word_idx]
-        return u"{}{}{}".format(text[: space + 1], word, ": " if space < 0 else "")
+        return "{}{}{}".format(text[: space + 1], word, ": " if space < 0 else "")
 
     def getMenu(self):
         """Return Menu bar"""
@@ -479,7 +482,7 @@
         if wid.mess_data.mention:
             from_jid = wid.mess_data.from_jid
             msg = _(
-                u"You have been mentioned by {nick} in {room}".format(
+                "You have been mentioned by {nick} in {room}".format(
                     nick=wid.mess_data.nick, room=self.target
                 )
             )
@@ -490,7 +493,7 @@
             return
         elif self.type == C.CHAT_ONE2ONE:
             from_jid = wid.mess_data.from_jid
-            msg = _(u"{entity} is talking to you".format(entity=from_jid))
+            msg = _("{entity} is talking to you".format(entity=from_jid))
             self.host.notify(
                 C.NOTIFY_MESSAGE, from_jid, msg, widget=self, profile=self.profile
             )
@@ -557,7 +560,7 @@
         """Set title for a group chat"""
         quick_chat.QuickChat.setSubject(self, subject)
         self.subj_wid = urwid.Text(
-            unicode(subject.replace("\n", "|") if wrap == "clip" else subject),
+            str(subject.replace("\n", "|") if wrap == "clip" else subject),
             align="left" if wrap == "clip" else "center",
             wrap=wrap,
         )
@@ -573,7 +576,7 @@
         """
         if clear:
             del self.mess_walker[:]
-        for message in self.messages.itervalues():
+        for message in self.messages.values():
             self.appendMessage(message, minor_notifs=False)
 
     def redraw(self):
@@ -589,13 +592,13 @@
         if filters and "search" in filters:
             self.mess_walker.append(
                 urwid.Text(
-                    _(u"Results for searching the globbing pattern: {}").format(
+                    _("Results for searching the globbing pattern: {}").format(
                         filters["search"]
                     )
                 )
             )
             self.mess_walker.append(
-                urwid.Text(_(u"Type ':history <lines>' to reset the chat history"))
+                urwid.Text(_("Type ':history <lines>' to reset the chat history"))
             )
         super(Chat, self).updateHistory(size, filters, profile)
 
@@ -675,8 +678,8 @@
 
     def onSubjectDialog(self, new_subject=None):
         dialog = sat_widgets.InputDialog(
-            _(u"Change title"),
-            _(u"Enter the new title"),
+            _("Change title"),
+            _("Enter the new title"),
             default_txt=new_subject if new_subject is not None else self.subject,
         )
         dialog.setCallback("ok", self._onSubjectDialogCb, dialog)