comparison 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
comparison
equal deleted inserted replaced
1490:55cff13b1f10 1491:704ca56f5ca9
79 """Like hasFeature, but raise an exception is feature is not Found 79 """Like hasFeature, but raise an exception is feature is not Found
80 80
81 @param feature: feature namespace 81 @param feature: feature namespace
82 @param jid_: jid of the target, or None for profile's server 82 @param jid_: jid of the target, or None for profile's server
83 @param profile_key: %(doc_profile_key)s 83 @param profile_key: %(doc_profile_key)s
84 @return: None if feature is found
85 84
86 @raise: exceptions.FeatureNotFound 85 @raise: exceptions.FeatureNotFound
87 """ 86 """
88 disco_infos = yield self.getInfos(jid_, profile_key) 87 disco_infos = yield self.getInfos(jid_, profile_key)
89 if not feature in disco_infos.features: 88 if not feature in disco_infos.features:
90 raise exceptions.FeatureNotFound 89 raise exceptions.FeatureNotFound
91 defer.returnValue(feature in disco_infos) 90
91 @defer.inlineCallbacks
92 def checkFeatures(self, features, jid_=None, identity=None, profile_key=C.PROF_KEY_NONE):
93 """Like checkFeature, but check several features at once, and check also identity
94
95 @param features(iterable[unicode]): features to check
96 @param jid_(jid.JID): jid of the target, or None for profile's server
97 @param identity(None, tuple(unicode, unicode): if not None, the entity must have an identity with this (category, type) tuple
98 @param profile_key: %(doc_profile_key)s
99
100 @raise: exceptions.FeatureNotFound
101 """
102 disco_infos = yield self.getInfos(jid_, profile_key)
103 if not set(features).issubset(disco_infos.features):
104 raise exceptions.FeatureNotFound
105
106 if identity is not None and identity not in disco_infos.identities:
107 raise exceptions.FeatureNotFound
92 108
93 def getInfos(self, jid_=None, profile_key=C.PROF_KEY_NONE): 109 def getInfos(self, jid_=None, profile_key=C.PROF_KEY_NONE):
94 """get disco infos from jid_, filling capability hash if needed 110 """get disco infos from jid_, filling capability hash if needed
95 111
96 @param jid_: jid of the target, or None for profile's server 112 @param jid_: jid of the target, or None for profile's server
175 d.addCallback(gotItems) 191 d.addCallback(gotItems)
176 d.addCallback(lambda dummy: found_entities) 192 d.addCallback(lambda dummy: found_entities)
177 reactor.callLater(TIMEOUT, d.cancel) # FIXME: one bad service make a general timeout 193 reactor.callLater(TIMEOUT, d.cancel) # FIXME: one bad service make a general timeout
178 return d 194 return d
179 195
180 def findFeaturesSet(self, features, category=None, type_=None, jid_=None, profile_key=C.PROF_KEY_NONE): 196 def findFeaturesSet(self, features, identity=None, jid_=None, profile_key=C.PROF_KEY_NONE):
181 """Return entities (including jid_ and its items) offering features 197 """Return entities (including jid_ and its items) offering features
182 198
183 @param features: iterable of features which must be present 199 @param features: iterable of features which must be present
184 @param category: if not None, accept only this category 200 @param identity(None, tuple(unicode, unicode)): if not None, accept only this (category/type) identity
185 @param type_: if not None, accept only this type
186 @param jid_: the jid of the target server (None for profile's server) 201 @param jid_: the jid of the target server (None for profile's server)
187 @param profile_key: %(doc_profile_key)s 202 @param profile_key: %(doc_profile_key)s
188 @return: a set of found entities 203 @return: a set of found entities
189 """ 204 """
190 client = self.host.getClient(profile_key) 205 client = self.host.getClient(profile_key)
192 jid_ = jid.JID(client.jid.host) 207 jid_ = jid.JID(client.jid.host)
193 features = set(features) 208 features = set(features)
194 found_entities = set() 209 found_entities = set()
195 210
196 def infosCb(infos, entity): 211 def infosCb(infos, entity):
197 if category is not None or type_ is not None: 212 if identity is not None and identity not in infos.identities:
198 categories = set() 213 return
199 types = set()
200 for identity in infos.identities:
201 id_cat, id_type = identity
202 categories.add(id_cat)
203 types.add(id_type)
204 if category is not None and category not in categories:
205 return
206 if type_ is not None and type_ not in types:
207 return
208 if features.issubset(infos.features): 214 if features.issubset(infos.features):
209 found_entities.add(entity) 215 found_entities.add(entity)
210 216
211 def gotItems(items): 217 def gotItems(items):
212 defer_list = [] 218 defer_list = []