core (disco): added checkFeatures to check several features at once + identities are now managed with a tuple in findFeaturesSet and checkFeatures

This commit is contained in:
Goffi 2015-08-25 16:05:01 +02:00
commit 9895ceb32f
2 changed files with 25 additions and 16 deletions

View file

@ -703,6 +703,9 @@ class SAT(service.Service):
def checkFeature(self, *args, **kwargs):
return self.memory.disco.checkFeature(*args, **kwargs)
def checkFeatures(self, *args, **kwargs):
return self.memory.disco.checkFeatures(*args, **kwargs)
def getDiscoInfos(self, *args, **kwargs):
return self.memory.disco.getInfos(*args, **kwargs)

View file

@ -81,14 +81,30 @@ class Discovery(object):
@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 @@ class Discovery(object):
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 @@ class Discovery(object):
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)