comparison src/browser/sat_browser/contact_list.py @ 733:66beef5b943d

browser_side: add functions JIDSet and JIDDict
author souliane <souliane@mailoo.org>
date Wed, 11 Nov 2015 11:49:32 +0100
parents 3b91225b457a
children 0d5889b9313c
comparison
equal deleted inserted replaced
732:9596da27cd7c 733:66beef5b943d
28 from pyjamas.ui.Label import Label 28 from pyjamas.ui.Label import Label
29 from pyjamas import Window 29 from pyjamas import Window
30 from pyjamas import DOM 30 from pyjamas import DOM
31 31
32 from constants import Const as C 32 from constants import Const as C
33 from sat_frontends.tools import jid
33 import libervia_widget 34 import libervia_widget
34 import contact_panel 35 import contact_panel
35 import blog 36 import blog
36 import chat 37 import chat
37 38
273 # need this hack to reproduce the Twisted's behavior. 274 # need this hack to reproduce the Twisted's behavior.
274 for other in self: 275 for other in self:
275 if other == item: 276 if other == item:
276 return True 277 return True
277 return False 278 return False
279
280 def index(self, item):
281 i = 0
282 for other in self:
283 if other == item:
284 return i
285 i += 1
286 raise ValueError("JIDList.index(%(item)s): %(item)s not in list" % {"item": item})
287
288 class JIDSet(set):
289 """JID set implementation for Pyjamas"""
290
291 def __contains__(self, item):
292 return __containsJID(self, item)
293
294
295 class JIDDict(dict):
296 """JID dict implementation for Pyjamas (a dict with JID keys)"""
297
298 def __contains__(self, item):
299 return __containsJID(self, item)
300
301 def keys(self):
302 return JIDSet(dict.keys(self))
303
304
305 def __containsJID(iterable, item):
306 """Tells if the given item is in the iterable, works with JID.
307
308 @param iterable(object): list, set or another iterable object
309 @param item (object): element
310 @return: bool
311 """
312 # Pyjamas JID-friendly implementation of the "in" operator. Since our JID
313 # doesn't inherit from str, without this method the test would return True
314 # only when the objects references are the same.
315 if isinstance(item, jid.JID):
316 return hash(item) in [hash(other) for other in iterable if isinstance(other, jid.JID)]
317 return super(type(iterable), iterable).__contains__(iterable, item)