Mercurial > libervia-backend
comparison src/plugins/plugin_xep_0060.py @ 1451:9b88b19b1ca8
plugins xep-0060, xep-0277: added methods to serialise getItems result (before bridge transmission):
serItemsData is used for normal callbacks, and serItemsDataD for callbacks which return deferred. These methods simplifly the code by making the re-use of getItems more easy.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 15 Aug 2015 22:20:56 +0200 |
parents | 389357fd79ce |
children | 5116d70ddd1c |
comparison
equal
deleted
inserted
replaced
1450:7797dda847ae | 1451:9b88b19b1ca8 |
---|---|
23 log = getLogger(__name__) | 23 log = getLogger(__name__) |
24 | 24 |
25 from sat.tools import sat_defer | 25 from sat.tools import sat_defer |
26 | 26 |
27 from twisted.words.protocols.jabber import jid | 27 from twisted.words.protocols.jabber import jid |
28 from twisted.internet import defer | |
28 from wokkel import disco | 29 from wokkel import disco |
29 from wokkel import pubsub | 30 from wokkel import pubsub |
30 from wokkel import rsm | 31 from wokkel import rsm |
31 from zope.interface import implements | 32 from zope.interface import implements |
32 # from twisted.internet import defer | 33 # from twisted.internet import defer |
199 client = self.host.getClient(profile_key) | 200 client = self.host.getClient(profile_key) |
200 return client.pubsub_client.subscriptions(service, nodeIdentifier) | 201 return client.pubsub_client.subscriptions(service, nodeIdentifier) |
201 | 202 |
202 ## methods to manage several stanzas/jids at once ## | 203 ## methods to manage several stanzas/jids at once ## |
203 | 204 |
205 def serItemsData(self, items_data, item_cb=lambda item: item.toXml()): | |
206 """Helper method to serialise result from [getItems] | |
207 | |
208 the items_data must be a tuple(list[domish.Element], dict[unicode, unicode]) | |
209 as returned by [getItems]. metadata values are then casted to unicode and | |
210 each item is passed to items_cb | |
211 @param items_data(tuple): tuple returned by [getItems] | |
212 @param item_cb(callable): method to transform each item | |
213 @return (tuple): a serialised form ready to go throught bridge | |
214 """ | |
215 items, metadata = items_data | |
216 return [item_cb(item) for item in items], {key: unicode(value) for key, value in metadata.iteritems()} | |
217 | |
218 def serItemsDataD(self, items_data, item_cb): | |
219 """Helper method to serialise result from [getItems], deferred version | |
220 | |
221 the items_data must be a tuple(list[domish.Element], dict[unicode, unicode]) | |
222 as returned by [getItems]. metadata values are then casted to unicode and | |
223 each item is passed to items_cb | |
224 An errback is added to item_cb, and when it is fired the value is filtered from final items | |
225 @param items_data(tuple): tuple returned by [getItems] | |
226 @param item_cb(callable): method to transform each item (must return a deferred) | |
227 @return (tuple): a deferred which fire a serialised form ready to go throught bridge | |
228 """ | |
229 items, metadata = items_data | |
230 def eb(failure): | |
231 log.warning("Error while serialising/parsing item: {}".format(unicode(failure.value))) | |
232 d = defer.gatherResults([item_cb(item).addErrback(eb) for item in items]) | |
233 def finishSerialisation(serialised_items): | |
234 return [item for item in serialised_items if item is not None], {key: unicode(value) for key, value in metadata.iteritems()} | |
235 d.addCallback(finishSerialisation) | |
236 return d | |
237 | |
238 def serDList(self, results, failure_result=None): | |
239 """Serialise a DeferredList result | |
240 | |
241 @param results: DeferredList results | |
242 @param failure_result: value to use as value for failed Deferred | |
243 (default: empty tuple) | |
244 @return (list): list with: | |
245 - failure: empty in case of success, else error message | |
246 - result | |
247 """ | |
248 if failure_result is None: | |
249 failure_result = () | |
250 return [('', result) if success else (unicode(result.result) or UNSPECIFIED, failure_result) for success, result in results] | |
251 | |
204 # subscribe # | 252 # subscribe # |
205 | 253 |
206 def _manySubscribeRTResult(self, session_id, profile_key=C.PROF_KEY_DEFAULT): | 254 def _manySubscribeRTResult(self, session_id, profile_key=C.PROF_KEY_DEFAULT): |
207 """Get real-time results for subcribeToManu session | 255 """Get real-time results for subcribeToManu session |
208 | 256 |