diff 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
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0060.py	Sat Aug 15 22:20:43 2015 +0200
+++ b/src/plugins/plugin_xep_0060.py	Sat Aug 15 22:20:56 2015 +0200
@@ -25,6 +25,7 @@
 from sat.tools import sat_defer
 
 from twisted.words.protocols.jabber import jid
+from twisted.internet import defer
 from wokkel import disco
 from wokkel import pubsub
 from wokkel import rsm
@@ -201,6 +202,53 @@
 
     ## methods to manage several stanzas/jids at once ##
 
+    def serItemsData(self, items_data, item_cb=lambda item: item.toXml()):
+        """Helper method to serialise result from [getItems]
+
+        the items_data must be a tuple(list[domish.Element], dict[unicode, unicode])
+        as returned by [getItems]. metadata values are then casted to unicode and
+        each item is passed to items_cb
+        @param items_data(tuple): tuple returned by [getItems]
+        @param item_cb(callable): method to transform each item
+        @return (tuple): a serialised form ready to go throught bridge
+        """
+        items, metadata = items_data
+        return [item_cb(item) for item in items], {key: unicode(value) for key, value in metadata.iteritems()}
+
+    def serItemsDataD(self, items_data, item_cb):
+        """Helper method to serialise result from [getItems], deferred version
+
+        the items_data must be a tuple(list[domish.Element], dict[unicode, unicode])
+        as returned by [getItems]. metadata values are then casted to unicode and
+        each item is passed to items_cb
+        An errback is added to item_cb, and when it is fired the value is filtered from final items
+        @param items_data(tuple): tuple returned by [getItems]
+        @param item_cb(callable): method to transform each item (must return a deferred)
+        @return (tuple): a deferred which fire a serialised form ready to go throught bridge
+        """
+        items, metadata = items_data
+        def eb(failure):
+            log.warning("Error while serialising/parsing item: {}".format(unicode(failure.value)))
+        d = defer.gatherResults([item_cb(item).addErrback(eb) for item in items])
+        def finishSerialisation(serialised_items):
+            return [item for item in serialised_items if item is not None], {key: unicode(value) for key, value in metadata.iteritems()}
+        d.addCallback(finishSerialisation)
+        return d
+
+    def serDList(self, results, failure_result=None):
+        """Serialise a DeferredList result
+
+        @param results: DeferredList results
+        @param failure_result: value to use as value for failed Deferred
+            (default: empty tuple)
+        @return (list): list with:
+            - failure: empty in case of success, else error message
+            - result
+        """
+        if failure_result is None:
+            failure_result = ()
+        return [('', result) if success else (unicode(result.result) or UNSPECIFIED, failure_result) for success, result in results]
+
     # subscribe #
 
     def _manySubscribeRTResult(self, session_id, profile_key=C.PROF_KEY_DEFAULT):