comparison src/memory/disco.py @ 1552:e0bde0d0b321

core (disco): use of “profile” instead of “profile_key” in several disco methods
author Goffi <goffi@goffi.org>
date Mon, 02 Nov 2015 22:02:41 +0100
parents 1285c714a6cc
children fd143578fe89
comparison
equal deleted inserted replaced
1551:591c32dbfb0b 1552:e0bde0d0b321
62 self.host = host 62 self.host = host
63 self.hashes = {} # key: capability hash, value: disco features/identities 63 self.hashes = {} # key: capability hash, value: disco features/identities
64 # TODO: save hashes in databse, remove legacy hashes 64 # TODO: save hashes in databse, remove legacy hashes
65 65
66 @defer.inlineCallbacks 66 @defer.inlineCallbacks
67 def hasFeature(self, feature, jid_=None, profile_key=C.PROF_KEY_NONE): 67 def hasFeature(self, feature, jid_=None, profile=C.PROF_KEY_NONE):
68 """Tell if an entity has the required feature 68 """Tell if an entity has the required feature
69 69
70 @param feature: feature namespace 70 @param feature: feature namespace
71 @param jid_: jid of the target, or None for profile's server 71 @param jid_: jid of the target, or None for profile's server
72 @param profile_key: %(doc_profile_key)s 72 @param profile: %(doc_profile)s
73 @return: a Deferred which fire a boolean (True if feature is available) 73 @return: a Deferred which fire a boolean (True if feature is available)
74 """ 74 """
75 disco_infos = yield self.getInfos(jid_, profile_key) 75 disco_infos = yield self.getInfos(jid_, profile)
76 defer.returnValue(feature in disco_infos.features) 76 defer.returnValue(feature in disco_infos.features)
77 77
78 @defer.inlineCallbacks 78 @defer.inlineCallbacks
79 def checkFeature(self, feature, jid_=None, profile_key=C.PROF_KEY_NONE): 79 def checkFeature(self, feature, jid_=None, profile=C.PROF_KEY_NONE):
80 """Like hasFeature, but raise an exception is feature is not Found 80 """Like hasFeature, but raise an exception is feature is not Found
81 81
82 @param feature: feature namespace 82 @param feature: feature namespace
83 @param jid_: jid of the target, or None for profile's server 83 @param jid_: jid of the target, or None for profile's server
84 @param profile_key: %(doc_profile_key)s 84 @param profile: %(doc_profile)s
85 85
86 @raise: exceptions.FeatureNotFound 86 @raise: exceptions.FeatureNotFound
87 """ 87 """
88 disco_infos = yield self.getInfos(jid_, profile_key) 88 disco_infos = yield self.getInfos(jid_, profile)
89 if not feature in disco_infos.features: 89 if not feature in disco_infos.features:
90 raise failure.Failure(exceptions.FeatureNotFound) 90 raise failure.Failure(exceptions.FeatureNotFound)
91 91
92 @defer.inlineCallbacks 92 @defer.inlineCallbacks
93 def checkFeatures(self, features, jid_=None, identity=None, profile_key=C.PROF_KEY_NONE): 93 def checkFeatures(self, features, jid_=None, identity=None, profile=C.PROF_KEY_NONE):
94 """Like checkFeature, but check several features at once, and check also identity 94 """Like checkFeature, but check several features at once, and check also identity
95 95
96 @param features(iterable[unicode]): features to check 96 @param features(iterable[unicode]): features to check
97 @param jid_(jid.JID): jid of the target, or None for profile's server 97 @param jid_(jid.JID): jid of the target, or None for profile's server
98 @param identity(None, tuple(unicode, unicode): if not None, the entity must have an identity with this (category, type) tuple 98 @param identity(None, tuple(unicode, unicode): if not None, the entity must have an identity with this (category, type) tuple
99 @param profile_key: %(doc_profile_key)s 99 @param profile: %(doc_profile)s
100 100
101 @raise: exceptions.FeatureNotFound 101 @raise: exceptions.FeatureNotFound
102 """ 102 """
103 disco_infos = yield self.getInfos(jid_, profile_key) 103 disco_infos = yield self.getInfos(jid_, profile)
104 if not set(features).issubset(disco_infos.features): 104 if not set(features).issubset(disco_infos.features):
105 raise failure.Failure(exceptions.FeatureNotFound()) 105 raise failure.Failure(exceptions.FeatureNotFound())
106 106
107 if identity is not None and identity not in disco_infos.identities: 107 if identity is not None and identity not in disco_infos.identities:
108 raise failure.Failure(exceptions.FeatureNotFound()) 108 raise failure.Failure(exceptions.FeatureNotFound())
109 109
110 def getInfos(self, jid_=None, profile_key=C.PROF_KEY_NONE): 110 def getInfos(self, jid_=None, profile=C.PROF_KEY_NONE):
111 """get disco infos from jid_, filling capability hash if needed 111 """get disco infos from jid_, filling capability hash if needed
112 112
113 @param jid_: jid of the target, or None for profile's server 113 @param jid_: jid of the target, or None for profile's server
114 @param profile_key: %(doc_profile_key)s 114 @param profile: %(doc_profile)s
115 @return: a Deferred which fire disco.DiscoInfo 115 @return: a Deferred which fire disco.DiscoInfo
116 """ 116 """
117 client = self.host.getClient(profile_key) 117 client = self.host.getClient(profile)
118 if jid_ is None: 118 if jid_ is None:
119 jid_ = jid.JID(client.jid.host) 119 jid_ = jid.JID(client.jid.host)
120 try: 120 try:
121 cap_hash = self.host.memory.getEntityData(jid_, [C.ENTITY_CAP_HASH], client.profile)[C.ENTITY_CAP_HASH] 121 cap_hash = self.host.memory.getEntityData(jid_, [C.ENTITY_CAP_HASH], client.profile)[C.ENTITY_CAP_HASH]
122 except (KeyError, exceptions.UnknownEntityError): 122 except (KeyError, exceptions.UnknownEntityError):
132 else: 132 else:
133 disco_infos = self.hashes[cap_hash] 133 disco_infos = self.hashes[cap_hash]
134 return defer.succeed(disco_infos) 134 return defer.succeed(disco_infos)
135 135
136 @defer.inlineCallbacks 136 @defer.inlineCallbacks
137 def getItems(self, jid_=None, nodeIdentifier='', profile_key=C.PROF_KEY_NONE): 137 def getItems(self, jid_=None, nodeIdentifier='', profile=C.PROF_KEY_NONE):
138 """get disco items from jid_, cache them for our own server 138 """get disco items from jid_, cache them for our own server
139 139
140 @param jid_ (jid.JID): jid of the target, or None for profile's server 140 @param jid_ (jid.JID): jid of the target, or None for profile's server
141 @param nodeIdentifier (str): optional node identifier 141 @param nodeIdentifier (str): optional node identifier
142 @param profile_key: %(doc_profile_key)s 142 @param profile: %(doc_profile)s
143 @return: a Deferred which fire disco.DiscoItems 143 @return: a Deferred which fire disco.DiscoItems
144 """ 144 """
145 client = self.host.getClient(profile_key) 145 client = self.host.getClient(profile)
146 if jid_ is None: 146 if jid_ is None:
147 jid_ = jid.JID(client.jid.host) 147 jid_ = jid.JID(client.jid.host)
148 # we cache items only for our own server 148 # we cache items only for our own server
149 try: 149 try:
150 items = self.host.memory.getEntityData(jid_, ["DISCO_ITEMS"], client.profile)["DISCO_ITEMS"] 150 items = self.host.memory.getEntityData(jid_, ["DISCO_ITEMS"], client.profile)["DISCO_ITEMS"]
162 def _infosEb(self, failure_, entity_jid): 162 def _infosEb(self, failure_, entity_jid):
163 failure_.trap(StanzaError) 163 failure_.trap(StanzaError)
164 log.warning(_(u"Error while requesting [%(jid)s]: %(error)s") % {'jid': entity_jid.full(), 164 log.warning(_(u"Error while requesting [%(jid)s]: %(error)s") % {'jid': entity_jid.full(),
165 'error': failure_.getErrorMessage()}) 165 'error': failure_.getErrorMessage()})
166 166
167 def findServiceEntities(self, category, type_, jid_=None, profile_key=C.PROF_KEY_NONE): 167 def findServiceEntities(self, category, type_, jid_=None, profile=C.PROF_KEY_NONE):
168 """Return all available items of an entity which correspond to (category, type_) 168 """Return all available items of an entity which correspond to (category, type_)
169 169
170 @param category: identity's category 170 @param category: identity's category
171 @param type_: identitiy's type 171 @param type_: identitiy's type
172 @param jid_: the jid of the target server (None for profile's server) 172 @param jid_: the jid of the target server (None for profile's server)
173 @param profile_key: %(doc_profile_key)s 173 @param profile: %(doc_profile)s
174 @return: a set of found entities 174 @return: a set of found entities
175 @raise CancelError: the request timed out 175 @raise CancelError: the request timed out
176 """ 176 """
177 found_entities = set() 177 found_entities = set()
178 178
181 found_entities.add(entity_jid) 181 found_entities.add(entity_jid)
182 182
183 def gotItems(items): 183 def gotItems(items):
184 defers_list = [] 184 defers_list = []
185 for item in items: 185 for item in items:
186 info_d = self.getInfos(item.entity, profile_key) 186 info_d = self.getInfos(item.entity, profile)
187 info_d.addCallbacks(infosCb, self._infosEb, [item.entity], None, [item.entity]) 187 info_d.addCallbacks(infosCb, self._infosEb, [item.entity], None, [item.entity])
188 defers_list.append(info_d) 188 defers_list.append(info_d)
189 return defer.DeferredList(defers_list) 189 return defer.DeferredList(defers_list)
190 190
191 d = self.getItems(jid_, profile_key=profile_key) 191 d = self.getItems(jid_, profile=profile)
192 d.addCallback(gotItems) 192 d.addCallback(gotItems)
193 d.addCallback(lambda dummy: found_entities) 193 d.addCallback(lambda dummy: found_entities)
194 reactor.callLater(TIMEOUT, d.cancel) # FIXME: one bad service make a general timeout 194 reactor.callLater(TIMEOUT, d.cancel) # FIXME: one bad service make a general timeout
195 return d 195 return d
196 196
197 def findFeaturesSet(self, features, identity=None, jid_=None, profile_key=C.PROF_KEY_NONE): 197 def findFeaturesSet(self, features, identity=None, jid_=None, profile=C.PROF_KEY_NONE):
198 """Return entities (including jid_ and its items) offering features 198 """Return entities (including jid_ and its items) offering features
199 199
200 @param features: iterable of features which must be present 200 @param features: iterable of features which must be present
201 @param identity(None, tuple(unicode, unicode)): if not None, accept only this (category/type) identity 201 @param identity(None, tuple(unicode, unicode)): if not None, accept only this (category/type) identity
202 @param jid_: the jid of the target server (None for profile's server) 202 @param jid_: the jid of the target server (None for profile's server)
203 @param profile_key: %(doc_profile_key)s 203 @param profile: %(doc_profile)s
204 @return: a set of found entities 204 @return: a set of found entities
205 """ 205 """
206 client = self.host.getClient(profile_key) 206 client = self.host.getClient(profile)
207 if jid_ is None: 207 if jid_ is None:
208 jid_ = jid.JID(client.jid.host) 208 jid_ = jid.JID(client.jid.host)
209 features = set(features) 209 features = set(features)
210 found_entities = set() 210 found_entities = set()
211 211
216 found_entities.add(entity) 216 found_entities.add(entity)
217 217
218 def gotItems(items): 218 def gotItems(items):
219 defer_list = [] 219 defer_list = []
220 for entity in [jid_] + [item.entity for item in items]: 220 for entity in [jid_] + [item.entity for item in items]:
221 infos_d = self.getInfos(entity, profile_key) 221 infos_d = self.getInfos(entity, profile)
222 infos_d.addCallbacks(infosCb, self._infosEb, [entity], None, [entity]) 222 infos_d.addCallbacks(infosCb, self._infosEb, [entity], None, [entity])
223 defer_list.append(infos_d) 223 defer_list.append(infos_d)
224 return defer.DeferredList(defer_list) 224 return defer.DeferredList(defer_list)
225 225
226 d = self.getItems(jid_, profile_key=profile_key) 226 d = self.getItems(jid_, profile=profile)
227 d.addCallback(gotItems) 227 d.addCallback(gotItems)
228 d.addCallback(lambda dummy: found_entities) 228 d.addCallback(lambda dummy: found_entities)
229 reactor.callLater(TIMEOUT, d.cancel) # FIXME: one bad service make a general timeout 229 reactor.callLater(TIMEOUT, d.cancel) # FIXME: one bad service make a general timeout
230 return d 230 return d
231 231
257 @defer.inlineCallbacks 257 @defer.inlineCallbacks
258 def _discoInfos(self, entity_jid_s, profile_key=C.PROF_KEY_NONE): 258 def _discoInfos(self, entity_jid_s, profile_key=C.PROF_KEY_NONE):
259 """ Discovery method for the bridge 259 """ Discovery method for the bridge
260 @param entity_jid_s: entity we want to discover 260 @param entity_jid_s: entity we want to discover
261 261
262 @return: list of tu""" 262 @return: list of tuples
263 """
264 profile = self.host.memory.getProfileName(profile_key)
263 entity = jid.JID(entity_jid_s) 265 entity = jid.JID(entity_jid_s)
264 disco_infos = yield self.getInfos(entity, profile_key) 266 disco_infos = yield self.getInfos(entity, profile)
265 extensions = {} 267 extensions = {}
266 for form_type, form in disco_infos.extensions.items(): 268 for form_type, form in disco_infos.extensions.items():
267 fields = [] 269 fields = []
268 for field in form.fieldList: 270 for field in form.fieldList:
269 data = {'type': field.fieldType} 271 data = {'type': field.fieldType}
284 @defer.inlineCallbacks 286 @defer.inlineCallbacks
285 def _discoItems(self, entity_jid_s, profile_key=C.PROF_KEY_NONE): 287 def _discoItems(self, entity_jid_s, profile_key=C.PROF_KEY_NONE):
286 """ Discovery method for the bridge 288 """ Discovery method for the bridge
287 @param entity_jid_s: entity we want to discover 289 @param entity_jid_s: entity we want to discover
288 290
289 @return: list of tu""" 291 @return: list of tuples"""
292 profile = self.host.memory.getProfileName(profile_key)
290 entity = jid.JID(entity_jid_s) 293 entity = jid.JID(entity_jid_s)
291 disco_items = yield self.getItems(entity, profile_key=profile_key) 294 disco_items = yield self.getItems(entity, profile=profile)
292 defer.returnValue([(item.entity.full(), item.nodeIdentifier or '', item.name or '') for item in disco_items]) 295 defer.returnValue([(item.entity.full(), item.nodeIdentifier or '', item.name or '') for item in disco_items])