Merge branch 'removeCmp' into 'master'

Remove cmp_to_key functions

See merge request gajim/gajim!197
This commit is contained in:
Philipp Hörist 2018-01-27 17:27:53 +01:00
commit c609d3c5ae
2 changed files with 25 additions and 108 deletions

View file

@ -108,37 +108,16 @@ def compute_caps_hash(identities, features, dataforms=None, hash_method='sha-1')
""" """
if dataforms is None: if dataforms is None:
dataforms = [] dataforms = []
def sort_identities_func(i1, i2):
cat1 = i1['category']
cat2 = i2['category']
if cat1 < cat2:
return -1
if cat1 > cat2:
return 1
type1 = i1.get('type', '')
type2 = i2.get('type', '')
if type1 < type2:
return -1
if type1 > type2:
return 1
lang1 = i1.get('xml:lang', '')
lang2 = i2.get('xml:lang', '')
if lang1 < lang2:
return -1
if lang1 > lang2:
return 1
return 0
def sort_dataforms_func(d1, d2): def sort_identities_key(i):
f1 = d1.getField('FORM_TYPE') return (i['category'], i.get('type', ''), i.get('xml:lang', ''))
f2 = d2.getField('FORM_TYPE')
if f1 and f2 and (f1.getValue() < f2.getValue()): def sort_dataforms_key(dataform):
return -1 f = dataform.getField('FORM_TYPE')
return 1 return (bool(f), f.getValue())
S = '' S = ''
from functools import cmp_to_key identities.sort(key=sort_identities_key)
identities.sort(key=cmp_to_key(sort_identities_func))
for i in identities: for i in identities:
c = i['category'] c = i['category']
type_ = i.get('type', '') type_ = i.get('type', '')
@ -148,7 +127,7 @@ def compute_caps_hash(identities, features, dataforms=None, hash_method='sha-1')
features.sort() features.sort()
for f in features: for f in features:
S += '%s<' % f S += '%s<' % f
dataforms.sort(key=cmp_to_key(sort_dataforms_func)) dataforms.sort(key=sort_dataforms_key)
for dataform in dataforms: for dataform in dataforms:
# fields indexed by var # fields indexed by var
fields = {} fields = {}

View file

@ -28,8 +28,6 @@
## along with Gajim. If not, see <http://www.gnu.org/licenses/>. ## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
## ##
from functools import cmp_to_key
try: try:
from gajim.common import caps_cache from gajim.common import caps_cache
from gajim.common.account import Account from gajim.common.account import Account
@ -788,88 +786,29 @@ class MetacontactManager():
answers.append(data) answers.append(data)
return answers return answers
def _compare_metacontacts(self, data1, data2): def _metacontact_key(self, data):
""" """
Compare 2 metacontacts
Data is {'jid': jid, 'account': account, 'order': order} order is Data is {'jid': jid, 'account': account, 'order': order} order is
optional optional
""" """
jid1 = data1['jid']
jid2 = data2['jid']
account1 = data1['account']
account2 = data2['account']
contact1 = self._contacts.get_contact_with_highest_priority(account1, jid1)
contact2 = self._contacts.get_contact_with_highest_priority(account2, jid2)
show_list = ['not in roster', 'error', 'offline', 'invisible', 'dnd', show_list = ['not in roster', 'error', 'offline', 'invisible', 'dnd',
'xa', 'away', 'chat', 'online', 'requested', 'message'] 'xa', 'away', 'chat', 'online', 'requested', 'message']
jid = data['jid']
account = data['account']
# contact can be null when a jid listed in the metacontact data # contact can be null when a jid listed in the metacontact data
# is not in our roster # is not in our roster
if not contact1: contact = self._contacts.get_contact_with_highest_priority(
if contact2: account, jid)
return -1 # prefer the known contact show = show_list.index(contact.show) if contact else 0
else: priority = contact.priority if contact else 0
show1 = 0 has_order = 'order' in data
priority1 = 0 order = data.get('order', 0)
else: transport = common.app.get_transport_name_from_jid(jid)
show1 = show_list.index(contact1.show) server = common.app.get_server_from_jid(jid)
priority1 = contact1.priority myserver = common.app.config.get_per('accounts', account, 'hostname')
if not contact2: return (bool(contact), show > 2, has_order, order, bool(transport),
if contact1: show, priority, server == myserver, jid, account)
return 1 # prefer the known contact
else:
show2 = 0
priority2 = 0
else:
show2 = show_list.index(contact2.show)
priority2 = contact2.priority
# If only one is offline, it's always second
if show1 > 2 and show2 < 3:
return 1
if show2 > 2 and show1 < 3:
return -1
if 'order' in data1 and 'order' in data2:
if data1['order'] > data2['order']:
return 1
if data1['order'] < data2['order']:
return -1
if 'order' in data1:
return 1
if 'order' in data2:
return -1
transport1 = common.app.get_transport_name_from_jid(jid1)
transport2 = common.app.get_transport_name_from_jid(jid2)
if transport2 and not transport1:
return 1
if transport1 and not transport2:
return -1
if show1 > show2:
return 1
if show2 > show1:
return -1
if priority1 > priority2:
return 1
if priority2 > priority1:
return -1
server1 = common.app.get_server_from_jid(jid1)
server2 = common.app.get_server_from_jid(jid2)
myserver1 = common.app.config.get_per('accounts', account1, 'hostname')
myserver2 = common.app.config.get_per('accounts', account2, 'hostname')
if server1 == myserver1:
if server2 != myserver2:
return 1
elif server2 == myserver2:
return -1
if jid1 > jid2:
return 1
if jid2 > jid1:
return -1
# If all is the same, compare accounts, they can't be the same
if account1 > account2:
return 1
if account2 > account1:
return -1
return 0
def get_nearby_family_and_big_brother(self, family, account): def get_nearby_family_and_big_brother(self, family, account):
""" """
@ -901,8 +840,7 @@ class MetacontactManager():
Which of the family will be the big brother under wich all others will be Which of the family will be the big brother under wich all others will be
? ?
""" """
family.sort(key=cmp_to_key(self._compare_metacontacts)) return max(family, key=self._metacontact_key)
return family[-1]
if __name__ == "__main__": if __name__ == "__main__":