diff sat_frontends/primitivus/contact_list.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 81b70eeb710f
children 003b8b4b56a7
line wrap: on
line diff
--- a/sat_frontends/primitivus/contact_list.py	Wed Jun 27 07:51:29 2018 +0200
+++ b/sat_frontends/primitivus/contact_list.py	Wed Jun 27 20:14:46 2018 +0200
@@ -27,30 +27,33 @@
 from sat_frontends.primitivus.widget import PrimitivusWidget
 from sat_frontends.tools import jid
 from sat.core import log as logging
+
 log = logging.getLogger(__name__)
 from sat_frontends.quick_frontend import quick_widgets
 
 
 class ContactList(PrimitivusWidget, QuickContactList):
-    PROFILES_MULTIPLE=False
-    PROFILES_ALLOW_NONE=False
-    signals = ['click','change']
+    PROFILES_MULTIPLE = False
+    PROFILES_ALLOW_NONE = False
+    signals = ["click", "change"]
     # FIXME: Only single profile is managed so far
 
-    def __init__(self, host, target, on_click=None, on_change=None, user_data=None, profiles=None):
+    def __init__(
+        self, host, target, on_click=None, on_change=None, user_data=None, profiles=None
+    ):
         QuickContactList.__init__(self, host, profiles)
         self.contact_list = self.host.contact_lists[self.profile]
 
-        #we now build the widget
+        # we now build the widget
         self.status_bar = StatusBar(host)
         self.frame = sat_widgets.FocusFrame(self._buildList(), None, self.status_bar)
-        PrimitivusWidget.__init__(self, self.frame, _(u'Contacts'))
+        PrimitivusWidget.__init__(self, self.frame, _(u"Contacts"))
         if on_click:
-            urwid.connect_signal(self, 'click', on_click, user_data)
+            urwid.connect_signal(self, "click", on_click, user_data)
         if on_change:
-            urwid.connect_signal(self, 'change', on_change, user_data)
-        self.host.addListener('notification', self.onNotification, [self.profile])
-        self.host.addListener('notificationsClear', self.onNotification, [self.profile])
+            urwid.connect_signal(self, "change", on_change, user_data)
+        self.host.addListener("notification", self.onNotification, [self.profile])
+        self.host.addListener("notificationsClear", self.onNotification, [self.profile])
         self.postInit()
 
     def update(self, entities=None, type_=None, profile=None):
@@ -64,21 +67,31 @@
             except IndexError:
                 pass
         self._invalidate()
-        self.host.redraw() # FIXME: check if can be avoided
+        self.host.redraw()  # FIXME: check if can be avoided
 
     def keypress(self, size, key):
         # FIXME: we have a temporary behaviour here: FOCUS_SWITCH change focus globally in the parent,
         #        and FOCUS_UP/DOWN is transwmitter to parent if we are respectively on the first or last element
         if key in sat_widgets.FOCUS_KEYS:
-            if (key == a_key['FOCUS_SWITCH'] or  (key == a_key['FOCUS_UP'] and self.frame.focus_position == 'body') or
-               (key == a_key['FOCUS_DOWN'] and self.frame.focus_position == 'footer')):
+            if (
+                key == a_key["FOCUS_SWITCH"]
+                or (key == a_key["FOCUS_UP"] and self.frame.focus_position == "body")
+                or (key == a_key["FOCUS_DOWN"] and self.frame.focus_position == "footer")
+            ):
                 return key
-        if key == a_key['STATUS_HIDE']: #user wants to (un)hide contacts' statuses
+        if key == a_key["STATUS_HIDE"]:  # user wants to (un)hide contacts' statuses
             self.contact_list.show_status = not self.contact_list.show_status
             self.update()
-        elif key == a_key['DISCONNECTED_HIDE']: #user wants to (un)hide disconnected contacts
-            self.host.bridge.setParam(C.SHOW_OFFLINE_CONTACTS, C.boolConst(not self.contact_list.show_disconnected), "General", profile_key=self.profile)
-        elif key == a_key['RESOURCES_HIDE']: #user wants to (un)hide contacts resources
+        elif (
+            key == a_key["DISCONNECTED_HIDE"]
+        ):  # user wants to (un)hide disconnected contacts
+            self.host.bridge.setParam(
+                C.SHOW_OFFLINE_CONTACTS,
+                C.boolConst(not self.contact_list.show_disconnected),
+                "General",
+                profile_key=self.profile,
+            )
+        elif key == a_key["RESOURCES_HIDE"]:  # user wants to (un)hide contacts resources
             self.contact_list.showResources(not self.contact_list.show_resources)
             self.update()
         return super(ContactList, self).keypress(size, key)
@@ -128,7 +141,7 @@
     def _groupClicked(self, group_wid):
         group = group_wid.getValue()
         data = self.contact_list.getGroupData(group)
-        data[C.GROUP_DATA_FOLDED] =  not data.setdefault(C.GROUP_DATA_FOLDED, False)
+        data[C.GROUP_DATA_FOLDED] = not data.setdefault(C.GROUP_DATA_FOLDED, False)
         self.setFocus(group)
         self.update()
 
@@ -141,7 +154,7 @@
         """
         entity = contact_wid.data
         self.host.modeHint(C.MODE_INSERTION)
-        self._emit('click', entity)
+        self._emit("click", entity)
 
     def onNotification(self, entity, notif, profile):
         notifs = list(self.host.getNotifs(C.ENTITY_ALL, profile=self.profile))
@@ -153,7 +166,17 @@
 
     # Methods to build the widget
 
-    def _buildEntityWidget(self, entity, keys=None, use_bare_jid=False, with_notifs=True, with_show_attr=True, markup_prepend=None, markup_append=None, special=False):
+    def _buildEntityWidget(
+        self,
+        entity,
+        keys=None,
+        use_bare_jid=False,
+        with_notifs=True,
+        with_show_attr=True,
+        markup_prepend=None,
+        markup_append=None,
+        special=False,
+    ):
         """Build one contact markup data
 
         @param entity (jid.JID): entity to build
@@ -180,7 +203,7 @@
         else:
             cache = self.contact_list.getCache(entity)
             for key in keys:
-                if key.startswith('cache_'):
+                if key.startswith("cache_"):
                     entity_txt = cache.get(key[6:])
                 else:
                     entity_txt = getattr(entity, key)
@@ -193,18 +216,22 @@
             show = self.contact_list.getCache(entity, C.PRESENCE_SHOW)
             if show is None:
                 show = C.PRESENCE_UNAVAILABLE
-            show_icon, entity_attr = C.PRESENCE.get(show, ('', 'default'))
+            show_icon, entity_attr = C.PRESENCE.get(show, ("", "default"))
             markup.insert(0, u"{} ".format(show_icon))
         else:
-            entity_attr = 'default'
+            entity_attr = "default"
 
-        notifs = list(self.host.getNotifs(entity, exact_jid=special, profile=self.profile))
+        notifs = list(
+            self.host.getNotifs(entity, exact_jid=special, profile=self.profile)
+        )
         if notifs:
-            header = [('cl_notifs', u'({})'.format(len(notifs))), u' ']
-            if list(self.host.getNotifs(entity.bare, C.NOTIFY_MENTION, profile=self.profile)):
-                header = ('cl_mention', header)
+            header = [("cl_notifs", u"({})".format(len(notifs))), u" "]
+            if list(
+                self.host.getNotifs(entity.bare, C.NOTIFY_MENTION, profile=self.profile)
+            ):
+                header = ("cl_mention", header)
         else:
-            header = u''
+            header = u""
 
         markup.append((entity_attr, entity_txt))
         if markup_prepend:
@@ -212,12 +239,14 @@
         if markup_append:
             markup.extend(markup_append)
 
-        widget = sat_widgets.SelectableText(markup,
-                                            selected = entity in selected,
-                                            header = header)
+        widget = sat_widgets.SelectableText(
+            markup, selected=entity in selected, header=header
+        )
         widget.data = entity
-        widget.comp = entity_txt.lower() # value to use for sorting
-        urwid.connect_signal(widget, 'change', self._contactClicked, user_args=[use_bare_jid])
+        widget.comp = entity_txt.lower()  # value to use for sorting
+        urwid.connect_signal(
+            widget, "change", self._contactClicked, user_args=[use_bare_jid]
+        )
         return widget
 
     def _buildEntities(self, content, entities):
@@ -231,25 +260,40 @@
         widgets = []  # list of built widgets
 
         for entity in entities:
-            if entity in self.contact_list._specials or not self.contact_list.entityVisible(entity):
+            if (
+                entity in self.contact_list._specials
+                or not self.contact_list.entityVisible(entity)
+            ):
                 continue
             markup_extra = []
             if self.contact_list.show_resources:
                 for resource in self.contact_list.getCache(entity, C.CONTACT_RESOURCES):
-                    resource_disp = ('resource_main' if resource == self.contact_list.getCache(entity, C.CONTACT_MAIN_RESOURCE) else 'resource', "\n  " + resource)
+                    resource_disp = (
+                        "resource_main"
+                        if resource
+                        == self.contact_list.getCache(entity, C.CONTACT_MAIN_RESOURCE)
+                        else "resource",
+                        "\n  " + resource,
+                    )
                     markup_extra.append(resource_disp)
                     if self.contact_list.show_status:
-                        status = self.contact_list.getCache(jid.JID('%s/%s' % (entity, resource)), 'status')
-                        status_disp = ('status', "\n    " + status) if status else ""
+                        status = self.contact_list.getCache(
+                            jid.JID("%s/%s" % (entity, resource)), "status"
+                        )
+                        status_disp = ("status", "\n    " + status) if status else ""
                         markup_extra.append(status_disp)
 
-
             else:
                 if self.contact_list.show_status:
-                    status = self.contact_list.getCache(entity, 'status')
-                    status_disp = ('status', "\n  " + status) if status else ""
+                    status = self.contact_list.getCache(entity, "status")
+                    status_disp = ("status", "\n  " + status) if status else ""
                     markup_extra.append(status_disp)
-            widget = self._buildEntityWidget(entity, ('cache_nick', 'cache_name', 'node'), use_bare_jid=True, markup_append=markup_extra)
+            widget = self._buildEntityWidget(
+                entity,
+                ("cache_nick", "cache_name", "node"),
+                use_bare_jid=True,
+                markup_append=markup_extra,
+            )
             widgets.append(widget)
 
         widgets.sort(key=lambda widget: widget.comp)
@@ -264,13 +308,20 @@
         for entity in specials:
             if current is not None and current.bare == entity.bare:
                 # nested entity (e.g. MUC private conversations)
-                widget = self._buildEntityWidget(entity, ('resource',), markup_prepend='  ', special=True)
+                widget = self._buildEntityWidget(
+                    entity, ("resource",), markup_prepend="  ", special=True
+                )
             else:
                 # the special widgets
                 if entity.resource:
-                    widget = self._buildEntityWidget(entity, ('resource',), special=True)
+                    widget = self._buildEntityWidget(entity, ("resource",), special=True)
                 else:
-                    widget = self._buildEntityWidget(entity, ('cache_nick', 'cache_name', 'node'), with_show_attr=False, special=True)
+                    widget = self._buildEntityWidget(
+                        entity,
+                        ("cache_nick", "cache_name", "node"),
+                        with_show_attr=False,
+                        special=True,
+                    )
             content.append(widget)
 
     def _buildList(self):
@@ -279,26 +330,35 @@
 
         self._buildSpecials(content)
         if self.contact_list._specials:
-            content.append(urwid.Divider('='))
+            content.append(urwid.Divider("="))
 
         groups = list(self.contact_list._groups)
         groups.sort(key=lambda x: x.lower() if x else x)
         for group in groups:
             data = self.contact_list.getGroupData(group)
             folded = data.get(C.GROUP_DATA_FOLDED, False)
-            jids = list(data['jids'])
-            if group is not None and (self.contact_list.anyEntityVisible(jids) or self.contact_list.show_empty_groups):
-                header = '[-]' if not folded else '[+]'
-                widget = sat_widgets.ClickableText(group, header=header + ' ')
+            jids = list(data["jids"])
+            if group is not None and (
+                self.contact_list.anyEntityVisible(jids)
+                or self.contact_list.show_empty_groups
+            ):
+                header = "[-]" if not folded else "[+]"
+                widget = sat_widgets.ClickableText(group, header=header + " ")
                 content.append(widget)
-                urwid.connect_signal(widget, 'click', self._groupClicked)
+                urwid.connect_signal(widget, "click", self._groupClicked)
             if not folded:
                 self._buildEntities(content, jids)
-        not_in_roster = set(self.contact_list._cache).difference(self.contact_list._roster).difference(self.contact_list._specials).difference((self.contact_list.whoami.bare,))
+        not_in_roster = (
+            set(self.contact_list._cache)
+            .difference(self.contact_list._roster)
+            .difference(self.contact_list._specials)
+            .difference((self.contact_list.whoami.bare,))
+        )
         if not_in_roster:
-            content.append(urwid.Divider('-'))
+            content.append(urwid.Divider("-"))
             self._buildEntities(content, not_in_roster)
 
         return urwid.ListBox(content)
 
+
 quick_widgets.register(QuickContactList, ContactList)