# HG changeset patch # User souliane # Date 1447238972 -3600 # Node ID 66beef5b943d5f144466cab0447d6d5bbcc0723a # Parent 9596da27cd7c9aeca300de5edae4d914590816e8 browser_side: add functions JIDSet and JIDDict diff -r 9596da27cd7c -r 66beef5b943d src/browser/sat_browser/contact_list.py --- a/src/browser/sat_browser/contact_list.py Mon Nov 09 18:26:05 2015 +0100 +++ b/src/browser/sat_browser/contact_list.py Wed Nov 11 11:49:32 2015 +0100 @@ -30,6 +30,7 @@ from pyjamas import DOM from constants import Const as C +from sat_frontends.tools import jid import libervia_widget import contact_panel import blog @@ -275,3 +276,42 @@ if other == item: return True return False + + def index(self, item): + i = 0 + for other in self: + if other == item: + return i + i += 1 + raise ValueError("JIDList.index(%(item)s): %(item)s not in list" % {"item": item}) + +class JIDSet(set): + """JID set implementation for Pyjamas""" + + def __contains__(self, item): + return __containsJID(self, item) + + +class JIDDict(dict): + """JID dict implementation for Pyjamas (a dict with JID keys)""" + + def __contains__(self, item): + return __containsJID(self, item) + + def keys(self): + return JIDSet(dict.keys(self)) + + +def __containsJID(iterable, item): + """Tells if the given item is in the iterable, works with JID. + + @param iterable(object): list, set or another iterable object + @param item (object): element + @return: bool + """ + # Pyjamas JID-friendly implementation of the "in" operator. Since our JID + # doesn't inherit from str, without this method the test would return True + # only when the objects references are the same. + if isinstance(item, jid.JID): + return hash(item) in [hash(other) for other in iterable if isinstance(other, jid.JID)] + return super(type(iterable), iterable).__contains__(iterable, item)