changeset 733:66beef5b943d

browser_side: add functions JIDSet and JIDDict
author souliane <souliane@mailoo.org>
date Wed, 11 Nov 2015 11:49:32 +0100
parents 9596da27cd7c
children 26046f13e93b
files src/browser/sat_browser/contact_list.py
diffstat 1 files changed, 40 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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)