comparison frontends/src/jp/base.py @ 1832:39545dc527a1

jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
author Goffi <goffi@goffi.org>
date Sat, 23 Jan 2016 20:04:28 +0100
parents d17772b0fe22
children 9eabf7fadfdd
comparison
equal deleted inserted replaced
1831:68c0dc13d821 1832:39545dc527a1
391 main_resource = self.bridge.getMainResource(param_jid, self.profile) 391 main_resource = self.bridge.getMainResource(param_jid, self.profile)
392 if main_resource: 392 if main_resource:
393 return "%s/%s" % (_jid.bare, main_resource) 393 return "%s/%s" % (_jid.bare, main_resource)
394 return param_jid 394 return param_jid
395 395
396 def progressUpdate(self):
397 """This method is continualy called to update the progress bar"""
398 data = self.bridge.progressGet(self.progress_id, self.profile)
399 if data:
400 try:
401 size = data['size']
402 except KeyError:
403 self.disp(_(u"file size is not known, we can't show a progress bar"), 1, error=True)
404 return False
405 if self.pbar is None:
406 #first answer, we must construct the bar
407 self.pbar = progressbar.ProgressBar(int(size),
408 [_(u"Progress: "),progressbar.Percentage(),
409 " ",
410 progressbar.Bar(),
411 " ",
412 progressbar.FileTransferSpeed(),
413 " ",
414 progressbar.ETA()])
415 self.pbar.start()
416
417 self.pbar.update(int(data['position']))
418
419 elif self.pbar is not None:
420 return False
421
422 return True
423
424 396
425 class CommandBase(object): 397 class CommandBase(object):
426 398
427 def __init__(self, host, name, use_profile=True, use_progress=False, use_verbose=False, need_connect=None, help=None, **kwargs): 399 def __init__(self, host, name, use_profile=True, use_progress=False, use_verbose=False, need_connect=None, help=None, **kwargs):
428 """ Initialise CommandBase 400 """ Initialise CommandBase
504 except AttributeError: 476 except AttributeError:
505 self.host.progress_ids_cache = [cache_data] 477 self.host.progress_ids_cache = [cache_data]
506 else: 478 else:
507 if self.host.watch_progress and uid == self.progress_id: 479 if self.host.watch_progress and uid == self.progress_id:
508 self.onProgressStarted(metadata) 480 self.onProgressStarted(metadata)
509 GLib.timeout_add(PROGRESS_DELAY, self.host.progressUpdate) 481 GLib.timeout_add(PROGRESS_DELAY, self.progressUpdate)
510 482
511 def progressFinishedHandler(self, uid, metadata, profile): 483 def progressFinishedHandler(self, uid, metadata, profile):
512 if profile != self.profile: 484 if profile != self.profile:
513 return 485 return
514 if uid == self.progress_id: 486 if uid == self.progress_id:
528 self.disp('') # progress is not finished, so we skip a line 500 self.disp('') # progress is not finished, so we skip a line
529 if self.host.quit_on_progress_end: 501 if self.host.quit_on_progress_end:
530 self.onProgressError(message) 502 self.onProgressError(message)
531 self.host.quitFromSignal(1) 503 self.host.quitFromSignal(1)
532 504
505 def progressUpdate(self):
506 """This method is continualy called to update the progress bar"""
507 data = self.host.bridge.progressGet(self.progress_id, self.profile)
508 if data:
509 try:
510 size = data['size']
511 except KeyError:
512 self.disp(_(u"file size is not known, we can't show a progress bar"), 1, error=True)
513 return False
514 if self.host.pbar is None:
515 #first answer, we must construct the bar
516 self.host.pbar = progressbar.ProgressBar(int(size),
517 [_(u"Progress: "),progressbar.Percentage(),
518 " ",
519 progressbar.Bar(),
520 " ",
521 progressbar.FileTransferSpeed(),
522 " ",
523 progressbar.ETA()])
524 self.host.pbar.start()
525
526 self.host.pbar.update(int(data['position']))
527
528 elif self.host.pbar is not None:
529 return False
530
531 self.onProgressUpdate(data)
532
533 return True
534
533 def onProgressStarted(self, metadata): 535 def onProgressStarted(self, metadata):
536 """Called when progress has just started
537
538 can be overidden by a command
539 @param metadata(dict): metadata as sent by bridge.progressStarted
540 """
534 self.disp(_(u"Operation started"), 2) 541 self.disp(_(u"Operation started"), 2)
535 542
543 def onProgressUpdate(self, metadata):
544 """Method called on each progress updata
545
546 can be overidden by a command to handle progress metadata
547 @para metadata(dict): metadata as returned by bridge.progressGet
548 """
549 pass
550
536 def onProgressFinished(self, metadata): 551 def onProgressFinished(self, metadata):
552 """Called when progress has just finished
553
554 can be overidden by a command
555 @param metadata(dict): metadata as sent by bridge.progressFinished
556 """
537 self.disp(_(u"Operation successfully finished"), 2) 557 self.disp(_(u"Operation successfully finished"), 2)
538 558
539 def onProgressError(self, error_msg): 559 def onProgressError(self, error_msg):
560 """Called when a progress failed
561
562 @param error_msg(unicode): error message as sent by bridge.progressError
563 """
540 self.disp(_(u"Error while doing operation: {}").format(error_msg), error=True) 564 self.disp(_(u"Error while doing operation: {}").format(error_msg), error=True)
541 565
542 def disp(self, msg, verbosity=0, error=False): 566 def disp(self, msg, verbosity=0, error=False):
543 return self.host.disp(msg, verbosity, error) 567 return self.host.disp(msg, verbosity, error)
544 568