jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.

This commit is contained in:
Goffi 2015-11-19 18:13:26 +01:00
commit 7c340bb518
2 changed files with 69 additions and 38 deletions

View file

@ -105,9 +105,6 @@ class Jp(object):
# progress attributes
self._progress_id = None # TODO: manage several progress ids
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):
@ -379,35 +376,7 @@ class Jp(object):
return "%s/%s" % (_jid.bare, main_resource)
return param_jid
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):
def progressUpdate(self):
"""This method is continualy called to update the progress bar"""
data = self.bridge.progressGet(self.progress_id, self.profile)
if data:
@ -505,6 +474,43 @@ class CommandBase(object):
def progress_id(self, value):
self.host.progress_id = value
def progressStartedHandler(self, uid, metadata, profile):
if profile != self.profile:
return
self.onProgressStarted(metadata)
if self.host.watch_progress and self.progress_id and uid == self.progress_id:
GLib.timeout_add(PROGRESS_DELAY, self.host.progressUpdate)
def progressFinishedHandler(self, uid, metadata, profile):
if profile != self.profile:
return
if uid == self.progress_id:
try:
self.host.pbar.finish()
except AttributeError:
pass
self.onProgressFinished(metadata)
if self.host.quit_on_progress_end:
self.host.quit()
def progressErrorHandler(self, uid, message, profile):
if profile != self.profile:
return
if uid == self.progress_id:
self.disp('') # progress is not finished, so we skip a line
if self.host.quit_on_progress_end:
self.onProgressError(message)
self.host.quitFromSignal(1)
def onProgressStarted(self, metadata):
self.disp(_(u"Operation started"), 2)
def onProgressFinished(self, metadata):
self.disp(_(u"Operation successfully finished"), 2)
def onProgressError(self, error_msg):
self.disp(_(u"Error while doing operation: {}").format(error_msg), error=True)
def disp(self, msg, verbosity=0, error=False):
return self.host.disp(msg, verbosity, error)
@ -538,9 +544,9 @@ class CommandBase(object):
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)
self.host.bridge.register("progressStarted", self.progressStartedHandler)
self.host.bridge.register("progressFinished", self.progressFinishedHandler)
self.host.bridge.register("progressError", self.progressErrorHandler)
def connected(self):
if not self.need_loop:

View file

@ -36,9 +36,6 @@ __commands__ = ["File"]
class Send(base.CommandBase):
def __init__(self, host):
super(Send, self).__init__(host, 'send', use_progress=True, use_verbose=True, help=_('Send a file to a contact'))
self.host.progress_started = lambda dummy: self.disp(_(u'File copy started'),2)
self.host.progress_success = lambda dummy: self.disp(_(u'File copied successfully'),2)
self.host.progress_failure = lambda dummy: self.disp(_(u'Error while transfering file'),error=True)
def add_parser_options(self):
self.parser.add_argument("files", type=str, nargs = '+', help=_("A list of file"))
@ -51,6 +48,15 @@ class Send(base.CommandBase):
super(Send, self).connected()
self.send_files()
def onProgressStarted(self, metadata):
self.disp(_(u'File copy started'),2)
def onProgressFinished(self, metadata):
self.disp(_(u'File sent successfully'),2)
def onProgressError(self, error_msg):
self.disp(_(u'Error while sending file: {}').format(error_msg),error=True)
def gotId(self, data, file_):
"""Called when a progress id has been received
@ -112,6 +118,25 @@ class Receive(base.CommandAnswering):
self.action_callbacks = {C.META_TYPE_FILE: self.onFileAction,
C.META_TYPE_OVERWRITE: self.onOverwriteAction}
def onProgressStarted(self, metadata):
self.disp(_(u'File copy started'),2)
def onProgressFinished(self, metadata):
self.disp(_(u'File received successfully'),2)
if metadata.get('hash_verified', False):
try:
self.disp(_(u'hash checked: {algo}:{checksum}').format(
algo=metadata['hash_algo'],
checksum=metadata['hash']),
1)
except KeyError:
self.disp(_(u'hash is checked but hash value is missing', 1), error=True)
else:
self.disp(_(u"hash can't be verified"), 1)
def onProgressError(self, error_msg):
self.disp(_(u'Error while receiving file: {}').format(error_msg),error=True)
def getXmluiId(self, action_data):
# FIXME: we temporarily use ElementTree, but a real XMLUI managing module
# should be available in the futur