diff src/memory/disco.py @ 1491:704ca56f5ca9

core (disco): added checkFeatures to check several features at once + identities are now managed with a tuple in findFeaturesSet and checkFeatures
author Goffi <goffi@goffi.org>
date Tue, 25 Aug 2015 16:05:01 +0200
parents 05b821a0ff5a
children 1285c714a6cc
line wrap: on
line diff
--- a/src/memory/disco.py	Tue Aug 25 15:39:16 2015 +0200
+++ b/src/memory/disco.py	Tue Aug 25 16:05:01 2015 +0200
@@ -81,14 +81,30 @@
         @param feature: feature namespace
         @param jid_: jid of the target, or None for profile's server
         @param profile_key: %(doc_profile_key)s
-        @return: None if feature is found
 
         @raise: exceptions.FeatureNotFound
         """
         disco_infos = yield self.getInfos(jid_, profile_key)
         if not feature in disco_infos.features:
             raise exceptions.FeatureNotFound
-        defer.returnValue(feature in disco_infos)
+
+    @defer.inlineCallbacks
+    def checkFeatures(self, features, jid_=None, identity=None, profile_key=C.PROF_KEY_NONE):
+        """Like checkFeature, but check several features at once, and check also identity
+
+        @param features(iterable[unicode]): features to check
+        @param jid_(jid.JID): jid of the target, or None for profile's server
+        @param identity(None, tuple(unicode, unicode): if not None, the entity must have an identity with this (category, type) tuple
+        @param profile_key: %(doc_profile_key)s
+
+        @raise: exceptions.FeatureNotFound
+        """
+        disco_infos = yield self.getInfos(jid_, profile_key)
+        if not set(features).issubset(disco_infos.features):
+            raise exceptions.FeatureNotFound
+
+        if identity is not None and identity not in disco_infos.identities:
+            raise exceptions.FeatureNotFound
 
     def getInfos(self, jid_=None, profile_key=C.PROF_KEY_NONE):
         """get disco infos from jid_, filling capability hash if needed
@@ -177,12 +193,11 @@
         reactor.callLater(TIMEOUT, d.cancel) # FIXME: one bad service make a general timeout
         return d
 
-    def findFeaturesSet(self, features, category=None, type_=None, jid_=None, profile_key=C.PROF_KEY_NONE):
+    def findFeaturesSet(self, features, identity=None, jid_=None, profile_key=C.PROF_KEY_NONE):
         """Return entities (including jid_ and its items) offering features
 
         @param features: iterable of features which must be present
-        @param category: if not None, accept only this category
-        @param type_: if not None, accept only this type
+        @param identity(None, tuple(unicode, unicode)): if not None, accept only this (category/type) identity
         @param jid_: the jid of the target server (None for profile's server)
         @param profile_key: %(doc_profile_key)s
         @return: a set of found entities
@@ -194,17 +209,8 @@
         found_entities = set()
 
         def infosCb(infos, entity):
-            if category is not None or type_ is not None:
-                categories = set()
-                types = set()
-                for identity in infos.identities:
-                    id_cat, id_type = identity
-                    categories.add(id_cat)
-                    types.add(id_type)
-                if category is not None and category not in categories:
-                    return
-                if type_ is not None and type_ not in types:
-                    return
+            if identity is not None and identity not in infos.identities:
+                return
             if features.issubset(infos.features):
                 found_entities.add(entity)