changeset 1063:4b69f69c6ffd

server: new ProgressHandler helper class, to handle progressing actions
author Goffi <goffi@goffi.org>
date Sun, 11 Mar 2018 19:30:37 +0100
parents c80649cdadd5
children abc5d545dbaa
files src/server/server.py src/server/utils.py
diffstat 2 files changed, 80 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/server/server.py	Sun Mar 11 19:29:23 2018 +0100
+++ b/src/server/server.py	Sun Mar 11 19:30:37 2018 +0100
@@ -54,7 +54,7 @@
 import libervia
 from libervia.server import websockets
 from libervia.server.pages import LiberviaPage
-from libervia.server.utils import quote
+from libervia.server.utils import quote, ProgressHandler
 from functools import partial
 
 try:
@@ -1483,6 +1483,12 @@
         self.bridge.register_signal("psEventRaw", partial(LiberviaPage.onNodeEvent, self), "plugin")
         self.bridge.register_signal("messageNew", partial(LiberviaPage.onSignal, self, "messageNew"))
 
+        # Progress handling
+        self.bridge.register_signal("progressStarted", partial(ProgressHandler._signal, "started"))
+        self.bridge.register_signal("progressFinished", partial(ProgressHandler._signal, "finished"))
+        self.bridge.register_signal("progressError", partial(ProgressHandler._signal, "error"))
+
+
         # media dirs
         # FIXME: get rid of dirname and "/" in C.XXX_DIR
         self.putChild(os.path.dirname(C.MEDIA_DIR), ProtectedFile(self.media_dir))
--- a/src/server/utils.py	Sun Mar 11 19:29:23 2018 +0100
+++ b/src/server/utils.py	Sun Mar 11 19:30:37 2018 +0100
@@ -16,9 +16,82 @@
 
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
+from sat.core.i18n import _
+from twisted.internet import reactor
+from twisted.internet import defer
+from sat.core import exceptions
+from sat.core.log import getLogger
 import urllib
+log = getLogger(__name__)
 
 
 def quote(value, safe='@'):
     """shortcut to quote an unicode value for URL"""
     return urllib.quote(value.encode('utf-8'), safe=safe)
+
+
+class ProgressHandler(object):
+    """class to help the management of progressions"""
+    handlers = {}
+
+    def __init__(self, host, progress_id, profile):
+        self.host = host
+        self.progress_id = progress_id
+        self.profile = profile
+
+    @classmethod
+    def _signal(cls, name, progress_id, data, profile):
+        handlers = cls.handlers
+        if profile in handlers and progress_id in handlers[profile]:
+            handler_data = handlers[profile][progress_id]
+            timeout = handler_data[u'timeout']
+            if timeout.active():
+                timeout.cancel()
+            cb = handler_data[name]
+            if cb is not None:
+                cb(data)
+            if name == u'started':
+                pass
+            elif name == u'finished':
+                handler_data[u'deferred'].callback(data)
+                handler_data[u'instance'].unregister_handler()
+            elif name == u'error':
+                handler_data[u'deferred'].errback(Exception(data))
+                handler_data[u'instance'].unregister_handler()
+
+    def _timeout(self):
+        log.warning(_(u"No progress received, cancelling handler: {progress_id} [{profile}]").format(
+            progress_id = self.progress_id, profile = self.profile))
+
+    def unregister_handler(self):
+        """remove a previously registered handler"""
+        try:
+            del self.handlers[self.profile][self.progress_id]
+        except KeyError:
+            log.warning(_(u"Trying to remove unknown handler: {progress_id} [{profile}]").format(
+                progress_id = self.progress_id, profile = self.profile))
+        else:
+            if not self.handlers[self.profile]:
+                self.handlers[self.profile]
+
+    def register(self, started_cb=None, finished_cb=None, error_cb=None, timeout=30):
+        """register the signals to handle progression
+
+        @param started_cb(callable, None): method to call when progressStarted signal is received
+        @param finished_cb(callable, None): method to call when progressFinished signal is received
+        @param error_cb(callable, None): method to call when progressError signal is received
+        @param timeout(int): progress time out
+            if nothing happen in this progression during this delay,
+            an exception is raised
+        @return (D(dict[unicode,unicode])): a deferred called when progression is finished
+        """
+        handler_data = self.handlers.setdefault(self.profile, {}).setdefault(self.progress_id, {})
+        if handler_data:
+            raise exceptions.ConflictError(u"There is already one handler for this progression")
+        handler_data[u'instance'] = self
+        deferred = handler_data[u'deferred'] = defer.Deferred()
+        handler_data[u'started'] = started_cb
+        handler_data[u'finished'] = finished_cb
+        handler_data[u'error'] = error_cb
+        handler_data[u'timeout'] = reactor.callLater(timeout, self._timeout)
+        return deferred