diff frontends/src/jp/base.py @ 1606:de785fcf9a7b

jp (base, file): file command and progress fixes and adaptation to new API: - progress use new API, and signals to monitor progression and avoid use of progressGet before progress is actually started - progress behaviour on event is managed by callbacks which are Jp attributes - progress with unknown or 0 size don't show the progress bar - better handling of errors - CommandAnswering is update to manage actions instead of the deprecated askConfirmation - managed actions use callback easy to associate with an action type. - file command is updated to manage these changes and the recent changes in backend behaviour - verbosity is used to display more or less message when sending/receiving a file - destination path can be specified in file receive - file receive doesn't stop yet, still need some work in the backend
author Goffi <goffi@goffi.org>
date Sun, 15 Nov 2015 23:42:21 +0100
parents 0aded9648c5c
children ec48b35309dc
line wrap: on
line diff
--- a/frontends/src/jp/base.py	Sun Nov 15 23:25:58 2015 +0100
+++ b/frontends/src/jp/base.py	Sun Nov 15 23:42:21 2015 +0100
@@ -19,9 +19,6 @@
 
 from sat.core.i18n import _
 
-global pbar_available
-pbar_available = True #checked before using ProgressBar
-
 ### logging ###
 import logging as log
 log.basicConfig(level=log.DEBUG,
@@ -32,7 +29,7 @@
 import locale
 import os.path
 import argparse
-from gi.repository import GLib, GObject
+from gi.repository import GLib
 from glob import iglob
 from importlib import import_module
 from sat_frontends.tools.jid import JID
@@ -48,15 +45,17 @@
     progressbar=None
 
 #consts
-prog_name = u"jp"
-description = """This software is a command line tool for XMPP.
+PROG_NAME = u"jp"
+DESCRIPTION = """This software is a command line tool for XMPP.
 Get the latest version at """ + C.APP_URL
 
-copyleft = u"""Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (aka Goffi)
+COPYLEFT = u"""Copyright (C) 2009-2015 Jérôme Poisson, Adrien Cossa
 This program comes with ABSOLUTELY NO WARRANTY;
 This is free software, and you are welcome to redistribute it under certain conditions.
 """
 
+PROGRESS_DELAY = 10 # the progression will be checked every PROGRESS_DELAY ms
+
 
 def unicode_decoder(arg):
     # Needed to have unicode strings from arguments
@@ -73,6 +72,18 @@
 
     """
     def __init__(self):
+        """
+
+        @attribute need_loop(bool): to set by commands when loop is needed
+        @attribute quit_on_progress_end (bool): set to False if you manage yourself exiting,
+            or if you want the user to stop by himself
+        @attribute progress_success(callable): method to call when progress just started
+            by default display a message
+        @attribute progress_success(callable): method to call when progress is successfully finished
+            by default display a message
+        @attribute progress_failure(callable): method to call when progress failed
+            by default display a message
+        """
         try:
             self.bridge = DBusBridgeFrontend()
         except exceptions.BridgeExceptionNoService:
@@ -83,15 +94,20 @@
             sys.exit(1)
 
         self.parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
-                                              description=description)
+                                              description=DESCRIPTION)
 
         self._make_parents()
         self.add_parser_options()
         self.subparsers = self.parser.add_subparsers(title=_('Available commands'), dest='subparser_name')
         self._auto_loop = False # when loop is used for internal reasons
         self.need_loop = False # to set by commands when loop is needed
+
+        # progress attributes
         self._progress_id = None # TODO: manage several progress ids
-        self.quit_on_progress_end = True # set to False if you manage yourself exiting, or if you want the user to stop by himself
+        self.quit_on_progress_end = True
+        self.progress_started = lambda dummy: self.disp(_(u"Operation started"), 2)
+        self.progress_success = lambda dummy: self.disp(_(u"Operation successfully finished"), 2)
+        self.progress_failure = lambda dummy: self.disp(_(u"Error while doing operation"), error=True)
 
     @property
     def version(self):
@@ -105,6 +121,19 @@
     def progress_id(self, value):
         self._progress_id = value
 
+    @property
+    def watch_progress(self):
+        try:
+            self.pbar
+        except AttributeError:
+            return False
+        else:
+            return True
+
+    @watch_progress.setter
+    def watch_progress(self, watch_progress):
+        if watch_progress:
+            self.pbar = None
 
     @property
     def verbosity(self):
@@ -165,7 +194,7 @@
         verbose_parent.add_argument('--verbose', '-v', action='count', help=_(u"Add a verbosity level (can be used multiple times)"))
 
     def add_parser_options(self):
-        self.parser.add_argument('--version', action='version', version=("%(name)s %(version)s %(copyleft)s" % {'name': prog_name, 'version': self.version, 'copyleft': copyleft}))
+        self.parser.add_argument('--version', action='version', version=("%(name)s %(version)s %(copyleft)s" % {'name': PROG_NAME, 'version': self.version, 'copyleft': COPYLEFT}))
 
     def import_commands(self):
         """ Automaticaly import commands to jp
@@ -344,41 +373,65 @@
         """Return the full jid if possible (add main resource when find a bare jid)"""
         _jid = JID(param_jid)
         if not _jid.resource:
-            #if the resource is not given, we try to add the last known resource
-            last_resource = self.bridge.getMainResource(param_jid, self.profile)
-            if last_resource:
-                return "%s/%s" % (_jid.bare, last_resource)
+            #if the resource is not given, we try to add the main resource
+            main_resource = self.bridge.getMainResource(param_jid, self.profile)
+            if main_resource:
+                return "%s/%s" % (_jid.bare, main_resource)
         return param_jid
 
-    def watch_progress(self):
-        self.pbar = None
-        GObject.timeout_add(10, self._progress_cb)
+    def _onProgressStarted(self, uid, profile):
+        if profile != self.profile:
+            return
+        self.progress_started(None)
+        if self.watch_progress and self.progress_id and uid == self.progress_id:
+            GLib.timeout_add(PROGRESS_DELAY, self._progress_cb)
+
+    def _onProgressFinished(self, uid, profile):
+        if profile != self.profile:
+            return
+        if uid == self.progress_id:
+            try:
+                self.pbar.finish()
+            except AttributeError:
+                pass
+            self.progress_success(None)
+            if self.quit_on_progress_end:
+                self.quit()
+
+    def _onProgressError(self, uid, profile):
+        if profile != self.profile:
+            return
+        if uid == self.progress_id:
+            self.disp('') # progress is not finished, so we skip a line
+            if self.quit_on_progress_end:
+                self.progress_failure(None)
+                self.quitFromSignal(1)
 
     def _progress_cb(self):
-        if self.progress_id:
-            data = self.bridge.getProgress(self.progress_id, self.profile)
-            if data:
-                if not data['position']:
-                    data['position'] = '0'
-                if not self.pbar:
-                    #first answer, we must construct the bar
-                    self.pbar = progressbar.ProgressBar(int(data['size']),
-                                                        [_("Progress: "),progressbar.Percentage(),
-                                                         " ",
-                                                         progressbar.Bar(),
-                                                         " ",
-                                                         progressbar.FileTransferSpeed(),
-                                                         " ",
-                                                         progressbar.ETA()])
-                    self.pbar.start()
+        """This method is continualy called to update the progress bar"""
+        data = self.bridge.progressGet(self.progress_id, self.profile)
+        if data:
+            try:
+                size = data['size']
+            except KeyError:
+                self.disp(_(u"file size is not known, we can't show a progress bar"), 1, error=True)
+                return False
+            if self.pbar is None:
+                #first answer, we must construct the bar
+                self.pbar = progressbar.ProgressBar(int(size),
+                                                    [_(u"Progress: "),progressbar.Percentage(),
+                                                     " ",
+                                                     progressbar.Bar(),
+                                                     " ",
+                                                     progressbar.FileTransferSpeed(),
+                                                     " ",
+                                                     progressbar.ETA()])
+                self.pbar.start()
 
-                self.pbar.update(int(data['position']))
+            self.pbar.update(int(data['position']))
 
-            elif self.pbar:
-                self.pbar.finish()
-                if self.quit_on_progress_end:
-                    self.quit()
-                return False
+        elif self.pbar is not None:
+            return False
 
         return True
 
@@ -391,6 +444,7 @@
         @param name(unicode): name of the new command
         @param use_profile(bool): if True, add profile selection/connection commands
         @param use_progress(bool): if True, add progress bar activation commands
+            progress* signals will be handled
         @param use_verbose(bool): if True, add verbosity command
         @param need_connect(bool, None): True if profile connection is needed
             False else (profile session must still be started)
@@ -476,13 +530,17 @@
             connect_profile(self.connected)
 
         try:
-            if self.args.progress:
-                watch_progress = self.host.watch_progress
+            show_progress = self.args.progress
         except AttributeError:
             # the command doesn't use progress bar
             pass
         else:
-            watch_progress()
+            if show_progress:
+                self.host.watch_progress = True
+            # we need to register the following signal even if we don't display the progress bas
+            self.host.bridge.register("progressStarted", self.host._onProgressStarted)
+            self.host.bridge.register("progressFinished", self.host._onProgressFinished)
+            self.host.bridge.register("progressError", self.host._onProgressError)
 
     def connected(self):
         if not self.need_loop:
@@ -490,33 +548,32 @@
 
 
 class CommandAnswering(CommandBase):
-    #FIXME: temp, will be refactored when progress_bar/confirmations will be refactored
+    """Specialised commands which answer to specific actions
 
-    def _ask_confirmation(self, confirm_id, confirm_type, data, profile):
-        """ Callback used for file transfer, accept files depending on parameters"""
+    to manage action_types answer,
+    """
+    action_callbacks = {} # XXX: set managed action types in an dict here:
+                          # key is the action_type, value is the callable
+                          # which will manage the answer. profile filtering is
+                          # already managed when callback is called
+
+    def onActionNew(self, action_data, action_id, security_limit, profile):
         if profile != self.profile:
-            debug("Ask confirmation ignored: not our profile")
             return
-        if confirm_type == self.confirm_type:
-            if self.dest_jids and not JID(data['from']).bare in [JID(_jid).bare for _jid in self.dest_jids()]:
-                return #file is not sent by a filtered jid
+        try:
+            action_type = action_data['meta_type']
+        except KeyError:
+            pass
+        else:
+            try:
+                callback = self.action_callbacks[action_type]
+            except KeyError:
+                pass
             else:
-                self.ask(data, confirm_id)
-
-    def ask(self):
-        """
-        The return value is used to answer to the bridge.
-        @return: bool or dict
-        """
-        raise NotImplementedError
+                callback(action_data, action_id, security_limit, profile)
 
     def connected(self):
         """Auto reply to confirmations requests"""
         self.need_loop = True
         super(CommandAnswering, self).connected()
-        # we watch confirmation signals
-        self.host.bridge.register("ask_confirmation", self._ask_confirmation)
-
-        #and we ask those we have missed
-        for confirm_id, confirm_type, data in self.host.bridge.getWaitingConf(self.profile):
-            self._ask_confirmation(confirm_id, confirm_type, data, self.profile)
+        self.host.bridge.register("actionNew", self.onActionNew)