Don't crash when we receive contacts that have a group tag assigned multiple times.

Remove some usage of has_key() in the contacts module. It is a bit slower (method overhead) and will be deprecated soon.
In roster_window. make a small if-clause more pythonic.
This commit is contained in:
Stephan Erb 2008-07-06 21:35:19 +00:00
parent 2f857870f3
commit eda061e15d
2 changed files with 26 additions and 21 deletions

View File

@ -171,7 +171,7 @@ class Contacts:
def add_account(self, account): def add_account(self, account):
self._contacts[account] = {} self._contacts[account] = {}
self._gc_contacts[account] = {} self._gc_contacts[account] = {}
if not self._metacontacts_tags.has_key(account): if account not in self._metacontacts_tags:
self._metacontacts_tags[account] = {} self._metacontacts_tags[account] = {}
def get_accounts(self): def get_accounts(self):
@ -187,7 +187,14 @@ class Contacts:
caps_hash_method=None, caps_hash=None, our_chatstate=None, caps_hash_method=None, caps_hash=None, our_chatstate=None,
chatstate=None, last_status_time=None, composing_xep=None, chatstate=None, last_status_time=None, composing_xep=None,
mood={}, tune={}, activity={}): mood={}, tune={}, activity={}):
return Contact(jid=jid, name=name, groups=groups, show=show,
# We don't want duplicated group values
groups_unique = []
for group in groups:
if group not in groups_unique:
groups_unique.append(group)
return Contact(jid=jid, name=name, groups=groups_unique, show=show,
status=status, sub=sub, ask=ask, resource=resource, priority=priority, status=status, sub=sub, ask=ask, resource=resource, priority=priority,
keyID=keyID, caps_node=caps_node, caps_hash_method=caps_hash_method, keyID=keyID, caps_node=caps_node, caps_hash_method=caps_hash_method,
caps_hash=caps_hash, our_chatstate=our_chatstate, chatstate=chatstate, caps_hash=caps_hash, our_chatstate=our_chatstate, chatstate=chatstate,
@ -205,11 +212,11 @@ class Contacts:
def add_contact(self, account, contact): def add_contact(self, account, contact):
# No such account before ? # No such account before ?
if not self._contacts.has_key(account): if account not in self._contacts:
self._contacts[account] = {contact.jid : [contact]} self._contacts[account] = {contact.jid : [contact]}
return return
# No such jid before ? # No such jid before ?
if not self._contacts[account].has_key(contact.jid): if contact.jid not in self._contacts[account]:
self._contacts[account][contact.jid] = [contact] self._contacts[account][contact.jid] = [contact]
return return
contacts = self._contacts[account][contact.jid] contacts = self._contacts[account][contact.jid]
@ -226,9 +233,9 @@ class Contacts:
contacts.append(contact) contacts.append(contact)
def remove_contact(self, account, contact): def remove_contact(self, account, contact):
if not self._contacts.has_key(account): if account not in self._contacts:
return return
if not self._contacts[account].has_key(contact.jid): if contact.jid not in self._contacts[account]:
return return
if contact in self._contacts[account][contact.jid]: if contact in self._contacts[account][contact.jid]:
self._contacts[account][contact.jid].remove(contact) self._contacts[account][contact.jid].remove(contact)
@ -240,9 +247,9 @@ class Contacts:
def remove_jid(self, account, jid, remove_meta=True): def remove_jid(self, account, jid, remove_meta=True):
'''Removes all contacts for a given jid''' '''Removes all contacts for a given jid'''
if not self._contacts.has_key(account): if account not in self._contacts:
return return
if not self._contacts[account].has_key(jid): if jid not in self._contacts[account]:
return return
del self._contacts[account][jid] del self._contacts[account][jid]
if remove_meta: if remove_meta:
@ -386,7 +393,7 @@ class Contacts:
while old_tag: while old_tag:
self.remove_metacontact(account, jid) self.remove_metacontact(account, jid)
old_tag = self.get_metacontacts_tag(account, jid) old_tag = self.get_metacontacts_tag(account, jid)
if not self._metacontacts_tags[account].has_key(tag): if tag not in self._metacontacts_tags[account]:
self._metacontacts_tags[account][tag] = [{'jid': jid, 'tag': tag}] self._metacontacts_tags[account][tag] = [{'jid': jid, 'tag': tag}]
else: else:
self._metacontacts_tags[account][tag].append({'jid': jid, self._metacontacts_tags[account][tag].append({'jid': jid,
@ -557,12 +564,12 @@ class Contacts:
def add_gc_contact(self, account, gc_contact): def add_gc_contact(self, account, gc_contact):
# No such account before ? # No such account before ?
if not self._gc_contacts.has_key(account): if account not in self._gc_contacts:
self._contacts[account] = {gc_contact.room_jid : {gc_contact.name: \ self._contacts[account] = {gc_contact.room_jid : {gc_contact.name: \
gc_contact}} gc_contact}}
return return
# No such room_jid before ? # No such room_jid before ?
if not self._gc_contacts[account].has_key(gc_contact.room_jid): if gc_contact.room_jid not in self._gc_contacts[account]:
self._gc_contacts[account][gc_contact.room_jid] = {gc_contact.name: \ self._gc_contacts[account][gc_contact.room_jid] = {gc_contact.name: \
gc_contact} gc_contact}
return return
@ -570,12 +577,12 @@ class Contacts:
gc_contact gc_contact
def remove_gc_contact(self, account, gc_contact): def remove_gc_contact(self, account, gc_contact):
if not self._gc_contacts.has_key(account): if account not in self._gc_contacts:
return return
if not self._gc_contacts[account].has_key(gc_contact.room_jid): if gc_contact.room_jid not in self._gc_contacts[account]:
return return
if not self._gc_contacts[account][gc_contact.room_jid].has_key( if gc_contact.name not in self._gc_contacts[account][
gc_contact.name): gc_contact.room_jid]:
return return
del self._gc_contacts[account][gc_contact.room_jid][gc_contact.name] del self._gc_contacts[account][gc_contact.room_jid][gc_contact.name]
# It was the last nick in room ? # It was the last nick in room ?
@ -583,14 +590,14 @@ class Contacts:
del self._gc_contacts[account][gc_contact.room_jid] del self._gc_contacts[account][gc_contact.room_jid]
def remove_room(self, account, room_jid): def remove_room(self, account, room_jid):
if not self._gc_contacts.has_key(account): if account not in self._gc_contacts:
return return
if not self._gc_contacts[account].has_key(room_jid): if room_jid not in self._gc_contacts[account]:
return return
del self._gc_contacts[account][room_jid] del self._gc_contacts[account][room_jid]
def get_gc_list(self, account): def get_gc_list(self, account):
if not self._gc_contacts.has_key(account): if account not in self._gc_contacts:
return [] return []
return self._gc_contacts[account].keys() return self._gc_contacts[account].keys()

View File

@ -1468,9 +1468,7 @@ class RosterWindow:
if len(jids) > 1: if len(jids) > 1:
resource = '/'.join(jids[1:]) resource = '/'.join(jids[1:])
# get name # get name
name = array[jid]['name'] name = array[jid]['name'] or ''
if not name:
name = ''
show = 'offline' # show is offline by default show = 'offline' # show is offline by default
status = '' # no status message by default status = '' # no status message by default