Mercurial > libervia-backend
diff sat_frontends/quick_frontend/quick_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 | ce1e15d59496 |
line wrap: on
line diff
--- a/sat_frontends/quick_frontend/quick_contact_list.py Wed Jun 27 07:51:29 2018 +0200 +++ b/sat_frontends/quick_frontend/quick_contact_list.py Wed Jun 27 20:14:46 2018 +0200 @@ -22,6 +22,7 @@ from sat.core.i18n import _ from sat.core.log import getLogger + log = getLogger(__name__) from sat.core import exceptions from sat_frontends.quick_frontend.quick_widgets import QuickWidget @@ -32,9 +33,9 @@ try: # FIXME: to be removed when an acceptable solution is here - unicode('') # XXX: unicode doesn't exist in pyjamas + unicode("") # XXX: unicode doesn't exist in pyjamas except (TypeError, AttributeError): # Error raised is not the same depending on - # pyjsbuild options + # pyjsbuild options # XXX: pyjamas' max doesn't support key argument, so we implement it ourself pyjamas_max = max @@ -95,27 +96,31 @@ # (e.g. not in contact list) if they have notifications attached self.show_entities_with_notifs = True - self.host.bridge.asyncGetParamA(C.SHOW_EMPTY_GROUPS, - "General", - profile_key=profile, - callback=self._showEmptyGroups) + self.host.bridge.asyncGetParamA( + C.SHOW_EMPTY_GROUPS, + "General", + profile_key=profile, + callback=self._showEmptyGroups, + ) - self.host.bridge.asyncGetParamA(C.SHOW_OFFLINE_CONTACTS, - "General", - profile_key=profile, - callback=self._showOfflineContacts) + self.host.bridge.asyncGetParamA( + C.SHOW_OFFLINE_CONTACTS, + "General", + profile_key=profile, + callback=self._showOfflineContacts, + ) # FIXME: workaround for a pyjamas issue: calling hash on a class method always # return a different value if that method is defined directly within the # class (with the "def" keyword) self.presenceListener = self.onPresenceUpdate - self.host.addListener('presence', self.presenceListener, [self.profile]) + self.host.addListener("presence", self.presenceListener, [self.profile]) self.nickListener = self.onNickUpdate - self.host.addListener('nick', self.nickListener, [self.profile]) + self.host.addListener("nick", self.nickListener, [self.profile]) self.notifListener = self.onNotification - self.host.addListener('notification', self.notifListener, [self.profile]) + self.host.addListener("notification", self.notifListener, [self.profile]) # notifListener only update the entity, so we can re-use it - self.host.addListener('notificationsClear', self.notifListener, [self.profile]) + self.host.addListener("notificationsClear", self.notifListener, [self.profile]) def _showEmptyGroups(self, show_str): # Called only by __init__ @@ -155,8 +160,13 @@ @return (set[jid.JID]) """ - return set([entity for entity in self._roster - if self.getCache(entity, C.PRESENCE_SHOW) is not None]) + return set( + [ + entity + for entity in self._roster + if self.getCache(entity, C.PRESENCE_SHOW) is not None + ] + ) @property def roster_entities_by_group(self): @@ -165,7 +175,7 @@ This also includes the empty group (None key). @return (dict[unicode,set(jid.JID)]) """ - return {group: self._groups[group]['jids'] for group in self._groups} + return {group: self._groups[group]["jids"] for group in self._groups} @property def roster_groups_by_entities(self): @@ -175,7 +185,7 @@ """ result = {} for group, data in self._groups.iteritems(): - for entity in data['jids']: + for entity in data["jids"]: result.setdefault(entity, set()).add(group) return result @@ -195,7 +205,6 @@ """ return self._cache.iteritems() - @property def items(self): """Return item representation for all visible entities in cache @@ -203,9 +212,11 @@ entities are not sorted key: bare jid, value: data """ - return {jid_:cache for jid_, cache in self._cache.iteritems() - if self.entityVisible(jid_)} - + return { + jid_: cache + for jid_, cache in self._cache.iteritems() + if self.entityVisible(jid_) + } def getItem(self, entity): """Return item representation of requested entity @@ -275,7 +286,7 @@ # full cache is requested return cache - if name in ('status', C.PRESENCE_STATUSES, C.PRESENCE_PRIORITY, C.PRESENCE_SHOW): + if name in ("status", C.PRESENCE_STATUSES, C.PRESENCE_PRIORITY, C.PRESENCE_SHOW): # these data are related to the resource if not entity.resource: main_resource = cache[C.CONTACT_MAIN_RESOURCE] @@ -287,9 +298,9 @@ else: cache = cache[C.CONTACT_RESOURCES].setdefault(entity.resource, {}) - if name == 'status': # XXX: we get the first status for 'status' key + if name == "status": # XXX: we get the first status for 'status' key # TODO: manage main language for statuses - return cache[C.PRESENCE_STATUSES].get(C.PRESENCE_STATUSES_DEFAULT, '') + return cache[C.PRESENCE_STATUSES].get(C.PRESENCE_STATUSES_DEFAULT, "") elif entity.resource: try: @@ -332,7 +343,7 @@ @param name: name of the data (can't be "jids") @param value: value to set """ - assert name is not 'jids' + assert name is not "jids" self._groups[group][name] = value def getGroupData(self, group, name=None): @@ -388,8 +399,10 @@ for entity in self._specials: if bare and entity.resource: continue - if (special_type is not None - and self.getCache(entity, C.CONTACT_SPECIAL) != special_type): + if ( + special_type is not None + and self.getCache(entity, C.CONTACT_SPECIAL) != special_type + ): continue yield entity @@ -440,9 +453,14 @@ if in_roster: self._roster.add(entity_bare) - cache = self._cache.setdefault(entity_bare, {C.CONTACT_RESOURCES: {}, - C.CONTACT_MAIN_RESOURCE: None, - C.CONTACT_SELECTED: set()}) + cache = self._cache.setdefault( + entity_bare, + { + C.CONTACT_RESOURCES: {}, + C.CONTACT_MAIN_RESOURCE: None, + C.CONTACT_SELECTED: set(), + }, + ) # we don't want forbidden data in attributes assert not C.CONTACT_DATA_FORBIDDEN.intersection(attributes) @@ -454,13 +472,15 @@ if C.CONTACT_GROUPS in cache: # XXX: don't use set(cache[C.CONTACT_GROUPS]).difference(groups) because # it won't work in Pyjamas if None is in cache[C.CONTACT_GROUPS] - for group in [group for group in cache[C.CONTACT_GROUPS] - if group not in groups]: - self._groups[group]['jids'].remove(entity_bare) + for group in [ + group for group in cache[C.CONTACT_GROUPS] if group not in groups + ]: + self._groups[group]["jids"].remove(entity_bare) cache[C.CONTACT_GROUPS] = groups for group in groups: - self._groups.setdefault(group, {}).setdefault('jids', set()).add( - entity_bare) + self._groups.setdefault(group, {}).setdefault("jids", set()).add( + entity_bare + ) # special entities management if C.CONTACT_SPECIAL in attributes: @@ -473,8 +493,11 @@ # now the attributes we keep in cache # XXX: if entity is a full jid, we store the value for the resource only - cache_attr = (cache[C.CONTACT_RESOURCES].setdefault(entity.resource, {}) - if entity.resource else cache) + cache_attr = ( + cache[C.CONTACT_RESOURCES].setdefault(entity.resource, {}) + if entity.resource + else cache + ) for attribute, value in attributes.iteritems(): if value is None: # XXX: pyjamas hack: we need to use pop instead of del @@ -483,7 +506,9 @@ except KeyError: pass else: - if attribute == 'nick' and self.isSpecial(entity, C.CONTACT_SPECIAL_GROUP): + if attribute == "nick" and self.isSpecial( + entity, C.CONTACT_SPECIAL_GROUP + ): # we don't want to keep nick for MUC rooms # FIXME: this is here as plugin XEP-0054 can link resource's nick # with bare jid which in the case of MUC @@ -518,12 +543,15 @@ selected = self._selected else: selected = {selected.bare for selected in self._selected} - return ((show is not None and show != C.PRESENCE_UNAVAILABLE) - or self.show_disconnected - or entity in selected - or (self.show_entities_with_notifs - and next(self.host.getNotifs(entity.bare, profile=self.profile), None)) - ) + return ( + (show is not None and show != C.PRESENCE_UNAVAILABLE) + or self.show_disconnected + or entity in selected + or ( + self.show_entities_with_notifs + and next(self.host.getNotifs(entity.bare, profile=self.profile), None) + ) + ) def anyEntityVisible(self, entities, check_resources=False): """Tell if in a list of entities, at least one should be shown @@ -564,8 +592,8 @@ pass del self._cache[entity_bare] for group in groups: - self._groups[group]['jids'].remove(entity_bare) - if not self._groups[group]['jids']: + self._groups[group]["jids"].remove(entity_bare) + if not self._groups[group]["jids"]: # FIXME: we use pop because of pyjamas: # http://wiki.goffi.org/wiki/Issues_with_Pyjamas/en self._groups.pop(group) @@ -599,14 +627,20 @@ try: del cache[C.CONTACT_RESOURCES][entity.resource] except KeyError: - log.error(u"Presence unavailable received " - u"for an unknown resource [{}]".format(entity)) + log.error( + u"Presence unavailable received " + u"for an unknown resource [{}]".format(entity) + ) if not cache[C.CONTACT_RESOURCES]: cache[C.CONTACT_MAIN_RESOURCE] = None else: if not entity.resource: - log.warning(_(u"received presence from entity " - u"without resource: {}".format(entity))) + log.warning( + _( + u"received presence from entity " + u"without resource: {}".format(entity) + ) + ) resources_data = cache[C.CONTACT_RESOURCES] resource_data = resources_data.setdefault(entity.resource, {}) resource_data[C.PRESENCE_SHOW] = show @@ -616,9 +650,12 @@ if entity.bare not in self._specials: # we may have resources with no priority # (when a cached value is added for a not connected resource) - priority_resource = max(resources_data, - key=lambda res: resources_data[res].get( - C.PRESENCE_PRIORITY, -2**32)) + priority_resource = max( + resources_data, + key=lambda res: resources_data[res].get( + C.PRESENCE_PRIORITY, -2 ** 32 + ), + ) cache[C.CONTACT_MAIN_RESOURCE] = priority_resource if self.entityVisible(entity.bare): update_type = C.UPDATE_MODIFY if was_visible else C.UPDATE_ADD @@ -634,7 +671,7 @@ @param profile: %(doc_profile)s """ assert profile == self.profile - self.setCache(entity, 'nick', new_nick) + self.setCache(entity, "nick", new_nick) def onNotification(self, entity, notif, profile): """Update entity with notification @@ -723,18 +760,18 @@ class QuickContactListHandler(object): - def __init__(self, host): super(QuickContactListHandler, self).__init__() self.host = host global handler if handler is not None: - raise exceptions.InternalError(u"QuickContactListHandler must be " - u"instanciated only once") + raise exceptions.InternalError( + u"QuickContactListHandler must be " u"instanciated only once" + ) handler = self - self._clist = {} # key: profile, value: ProfileContactList + self._clist = {} # key: profile, value: ProfileContactList self._widgets = set() - self._update_locked = False # se to True to ignore updates + self._update_locked = False # se to True to ignore updates def __getitem__(self, profile): """Return ProfileContactList instance for the requested profile""" @@ -845,16 +882,16 @@ return self.items_sort(self.items) def items_sort(self, items): - """sort items + """sort items @param items(dict): items to sort (will be emptied !) @return (OrderedDict): sorted items """ - ordered_items = OrderedDict() - bare_jids = sorted(items.keys()) - for jid_ in bare_jids: - ordered_items[jid_] = items.pop(jid_) - return ordered_items + ordered_items = OrderedDict() + bare_jids = sorted(items.keys()) + for jid_ in bare_jids: + ordered_items[jid_] = items.pop(jid_) + return ordered_items def register(self, widget): """Register a QuickContactList widget @@ -940,8 +977,11 @@ remaining = to_fill.difference(filled) if remaining != to_fill: - log.debug(u"Not re-filling already filled contact list(s) for {}".format( - u', '.join(to_fill.intersection(filled)))) + log.debug( + u"Not re-filling already filled contact list(s) for {}".format( + u", ".join(to_fill.intersection(filled)) + ) + ) for profile in remaining: self._clist[profile]._fill() @@ -973,8 +1013,11 @@ if set to False, widget state can be inconsistent, be sure to know what youa re doing! """ - log.debug(u"Contact lists updates are now {}".format( - u"LOCKED" if locked else u"UNLOCKED")) + log.debug( + u"Contact lists updates are now {}".format( + u"LOCKED" if locked else u"UNLOCKED" + ) + ) self._update_locked = locked if not locked and do_update: self.update() @@ -987,10 +1030,11 @@ class QuickContactList(QuickWidget): """This class manage the visual representation of contacts""" - SINGLE=False - PROFILES_MULTIPLE=True + + SINGLE = False + PROFILES_MULTIPLE = True # Can be linked to no profile (e.g. at the early frontend start) - PROFILES_ALLOW_NONE=True + PROFILES_ALLOW_NONE = True def __init__(self, host, profiles): super(QuickContactList, self).__init__(host, None, profiles) @@ -998,10 +1042,10 @@ # options # for next values, None means use indivual value per profile # True or False mean override these values for all profiles - self.show_disconnected = None # TODO - self.show_empty_groups = None # TODO - self.show_resources = None # TODO - self.show_status = None # TODO + self.show_disconnected = None # TODO + self.show_empty_groups = None # TODO + self.show_resources = None # TODO + self.show_status = None # TODO def postInit(self): """Method to be called by frontend after widget is initialised"""