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
This commit is contained in:
Goffi 2015-11-15 23:42:21 +01:00
commit 6d2df889a1
2 changed files with 238 additions and 114 deletions

View file

@ -19,9 +19,6 @@
from sat.core.i18n import _ from sat.core.i18n import _
global pbar_available
pbar_available = True #checked before using ProgressBar
### logging ### ### logging ###
import logging as log import logging as log
log.basicConfig(level=log.DEBUG, log.basicConfig(level=log.DEBUG,
@ -32,7 +29,7 @@ import sys
import locale import locale
import os.path import os.path
import argparse import argparse
from gi.repository import GLib, GObject from gi.repository import GLib
from glob import iglob from glob import iglob
from importlib import import_module from importlib import import_module
from sat_frontends.tools.jid import JID from sat_frontends.tools.jid import JID
@ -48,15 +45,17 @@ except ImportError:
progressbar=None progressbar=None
#consts #consts
prog_name = u"jp" PROG_NAME = u"jp"
description = """This software is a command line tool for XMPP. DESCRIPTION = """This software is a command line tool for XMPP.
Get the latest version at """ + C.APP_URL 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 program comes with ABSOLUTELY NO WARRANTY;
This is free software, and you are welcome to redistribute it under certain conditions. 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): def unicode_decoder(arg):
# Needed to have unicode strings from arguments # Needed to have unicode strings from arguments
@ -73,6 +72,18 @@ class Jp(object):
""" """
def __init__(self): 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: try:
self.bridge = DBusBridgeFrontend() self.bridge = DBusBridgeFrontend()
except exceptions.BridgeExceptionNoService: except exceptions.BridgeExceptionNoService:
@ -83,15 +94,20 @@ class Jp(object):
sys.exit(1) sys.exit(1)
self.parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, self.parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
description=description) description=DESCRIPTION)
self._make_parents() self._make_parents()
self.add_parser_options() self.add_parser_options()
self.subparsers = self.parser.add_subparsers(title=_('Available commands'), dest='subparser_name') self.subparsers = self.parser.add_subparsers(title=_('Available commands'), dest='subparser_name')
self._auto_loop = False # when loop is used for internal reasons self._auto_loop = False # when loop is used for internal reasons
self.need_loop = False # to set by commands when loop is needed self.need_loop = False # to set by commands when loop is needed
# progress attributes
self._progress_id = None # TODO: manage several progress ids 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 @property
def version(self): def version(self):
@ -105,6 +121,19 @@ class Jp(object):
def progress_id(self, value): def progress_id(self, value):
self._progress_id = 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 @property
def verbosity(self): def verbosity(self):
@ -165,7 +194,7 @@ class Jp(object):
verbose_parent.add_argument('--verbose', '-v', action='count', help=_(u"Add a verbosity level (can be used multiple times)")) verbose_parent.add_argument('--verbose', '-v', action='count', help=_(u"Add a verbosity level (can be used multiple times)"))
def add_parser_options(self): 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): def import_commands(self):
""" Automaticaly import commands to jp """ Automaticaly import commands to jp
@ -344,41 +373,65 @@ class Jp(object):
"""Return the full jid if possible (add main resource when find a bare jid)""" """Return the full jid if possible (add main resource when find a bare jid)"""
_jid = JID(param_jid) _jid = JID(param_jid)
if not _jid.resource: if not _jid.resource:
#if the resource is not given, we try to add the last known resource #if the resource is not given, we try to add the main resource
last_resource = self.bridge.getMainResource(param_jid, self.profile) main_resource = self.bridge.getMainResource(param_jid, self.profile)
if last_resource: if main_resource:
return "%s/%s" % (_jid.bare, last_resource) return "%s/%s" % (_jid.bare, main_resource)
return param_jid return param_jid
def watch_progress(self): def _onProgressStarted(self, uid, profile):
self.pbar = None if profile != self.profile:
GObject.timeout_add(10, self._progress_cb) 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 _progress_cb(self):
if self.progress_id: """This method is continualy called to update the progress bar"""
data = self.bridge.getProgress(self.progress_id, self.profile) data = self.bridge.progressGet(self.progress_id, self.profile)
if data: if data:
if not data['position']: try:
data['position'] = '0' size = data['size']
if not self.pbar: except KeyError:
#first answer, we must construct the bar self.disp(_(u"file size is not known, we can't show a progress bar"), 1, error=True)
self.pbar = progressbar.ProgressBar(int(data['size']),
[_("Progress: "),progressbar.Percentage(),
" ",
progressbar.Bar(),
" ",
progressbar.FileTransferSpeed(),
" ",
progressbar.ETA()])
self.pbar.start()
self.pbar.update(int(data['position']))
elif self.pbar:
self.pbar.finish()
if self.quit_on_progress_end:
self.quit()
return False 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']))
elif self.pbar is not None:
return False
return True return True
@ -391,6 +444,7 @@ class CommandBase(object):
@param name(unicode): name of the new command @param name(unicode): name of the new command
@param use_profile(bool): if True, add profile selection/connection commands @param use_profile(bool): if True, add profile selection/connection commands
@param use_progress(bool): if True, add progress bar activation 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 use_verbose(bool): if True, add verbosity command
@param need_connect(bool, None): True if profile connection is needed @param need_connect(bool, None): True if profile connection is needed
False else (profile session must still be started) False else (profile session must still be started)
@ -476,13 +530,17 @@ class CommandBase(object):
connect_profile(self.connected) connect_profile(self.connected)
try: try:
if self.args.progress: show_progress = self.args.progress
watch_progress = self.host.watch_progress
except AttributeError: except AttributeError:
# the command doesn't use progress bar # the command doesn't use progress bar
pass pass
else: 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): def connected(self):
if not self.need_loop: if not self.need_loop:
@ -490,33 +548,32 @@ class CommandBase(object):
class CommandAnswering(CommandBase): 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): to manage action_types answer,
""" Callback used for file transfer, accept files depending on parameters""" """
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: if profile != self.profile:
debug("Ask confirmation ignored: not our profile")
return return
if confirm_type == self.confirm_type: try:
if self.dest_jids and not JID(data['from']).bare in [JID(_jid).bare for _jid in self.dest_jids()]: action_type = action_data['meta_type']
return #file is not sent by a filtered jid except KeyError:
pass
else:
try:
callback = self.action_callbacks[action_type]
except KeyError:
pass
else: else:
self.ask(data, confirm_id) callback(action_data, action_id, security_limit, profile)
def ask(self):
"""
The return value is used to answer to the bridge.
@return: bool or dict
"""
raise NotImplementedError
def connected(self): def connected(self):
"""Auto reply to confirmations requests""" """Auto reply to confirmations requests"""
self.need_loop = True self.need_loop = True
super(CommandAnswering, self).connected() super(CommandAnswering, self).connected()
# we watch confirmation signals self.host.bridge.register("actionNew", self.onActionNew)
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)

View file

@ -17,7 +17,6 @@
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from logging import debug, info, error, warning
import base import base
import sys import sys
@ -25,12 +24,21 @@ import os
import os.path import os.path
import tarfile import tarfile
from sat.core.i18n import _ from sat.core.i18n import _
from sat_frontends.jp.constants import Const as C
from sat_frontends.tools import jid
import tempfile
import xml.etree.ElementTree as ET # FIXME: used temporarily to manage XMLUI
__commands__ = ["File"] __commands__ = ["File"]
class Send(base.CommandBase): class Send(base.CommandBase):
def __init__(self, host): def __init__(self, host):
super(Send, self).__init__(host, 'send', use_progress=True, help=_('Send a file to a contact')) 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): def add_parser_options(self):
self.parser.add_argument("files", type=str, nargs = '+', help=_("A list of file")) self.parser.add_argument("files", type=str, nargs = '+', help=_("A list of file"))
@ -43,79 +51,138 @@ class Send(base.CommandBase):
super(Send, self).connected() super(Send, self).connected()
self.send_files() self.send_files()
def gotId(self, data, file_):
"""Called when a progress id has been received
@param pid(unicode): progress id
@param file_(str): file path
"""
#FIXME: this show progress only for last progress_id
self.disp(_(u"File request sent to {jid}".format(jid=self.full_dest_jid)), 1)
self.progress_id = data['progress']
def error(self, failure):
self.disp(_("Error while trying to send a file: {reason}").format(reason=failure), error=True)
self.host.quit(1)
def send_files(self): def send_files(self):
for file_ in self.args.files: for file_ in self.args.files:
if not os.path.exists(file_): if not os.path.exists(file_):
error (_(u"file [%s] doesn't exist !") % file_) self.disp(_(u"file [{}] doesn't exist !").format(file_), error=True)
self.host.quit(1) self.host.quit(1)
if not self.args.bz2 and os.path.isdir(file_): if not self.args.bz2 and os.path.isdir(file_):
error (_("[%s] is a dir ! Please send files inside or use compression") % file_) self.disp(_(u"[{}] is a dir ! Please send files inside or use compression").format(file_))
self.host.quit(1) self.host.quit(1)
full_dest_jid = self.host.get_full_jid(self.args.jid) self.full_dest_jid = self.host.get_full_jid(self.args.jid)
if self.args.bz2: if self.args.bz2:
tmpfile = (os.path.basename(self.args.files[0]) or os.path.basename(os.path.dirname(self.args.files[0])) ) + '.tar.bz2' #FIXME: tmp, need an algorithm to find a good name/path with tempfile.NamedTemporaryFile('wb', delete=False) as buf:
if os.path.exists(tmpfile): self.host.addOnQuitCallback(os.unlink, buf.name)
error (_("tmp file_ (%s) already exists ! Please remove it"), tmpfile) self.disp(_(u"bz2 is an experimental option, use with caution"))
exit(1) #FIXME: check free space
warning(_("bz2 is an experimental option at an early dev stage, use with caution")) self.disp(_(u"Starting compression, please wait..."))
#FIXME: check free space, writting perm, tmp dir, filename (watch for OS used) sys.stdout.flush()
print _(u"Starting compression, please wait...") bz2 = tarfile.open(mode="w:bz2", fileobj=buf)
sys.stdout.flush() archive_name = u'{}.tar.bz2'.format(os.path.basename(self.args.files[0]) or u'compressed_files')
bz2 = tarfile.open(tmpfile, "w:bz2") for file_ in self.args.files:
for file_ in self.args.files: self.disp(_(u"Adding {}").format(file_), 1)
print _(u"Adding %s") % file_ bz2.add(file_)
bz2.add(file_) bz2.close()
bz2.close() self.disp(_(u"Done !"), 1)
print _(u"Done !")
path = os.path.abspath(tmpfile) self.host.bridge.fileSend(self.full_dest_jid, buf.name, archive_name, '', self.profile, callback=lambda pid, file_=buf.name: self.gotId(pid, file_), errback=self.error)
self.progress_id = self.host.bridge.sendFile(full_dest_jid, path, {}, self.profile)
else: else:
for file_ in self.args.files: for file_ in self.args.files:
path = os.path.abspath(file_) path = os.path.abspath(file_)
self.progress_id = self.host.bridge.sendFile(full_dest_jid, path, {}, self.profile) #FIXME: show progress only for last progress_id self.host.bridge.fileSend(self.full_dest_jid, path, '', '', self.profile, callback=lambda pid, file_=file_: self.gotId(pid, file_), errback=self.error)
class Receive(base.CommandAnswering): class Receive(base.CommandAnswering):
confirm_type = "FILE_TRANSFER"
def __init__(self, host): def __init__(self, host):
super(Receive, self).__init__(host, 'recv', use_progress=True, help=_('Wait for a file to be sent by a contact')) super(Receive, self).__init__(host, 'receive', use_progress=True, use_verbose=True, help=_('Wait for a file to be sent by a contact'))
self._overwrite_refused = False # True when one overwrite as already been refused
self.action_callbacks = {C.META_TYPE_FILE: self.onFileAction,
C.META_TYPE_OVERWRITE: self.onOverwriteAction}
@property def getXmluiId(self, action_data):
def dest_jids(self): # FIXME: we temporarily use ElementTree, but a real XMLUI managing module
return self.args.jids # should be available in the futur
# TODO: XMLUI module
try:
xml_ui = action_data['xmlui']
except KeyError:
self.disp(_(u"Action has no XMLUI"), 1)
else:
ui = ET.fromstring(xml_ui.encode('utf-8'))
xmlui_id = ui.get('submit')
if not xmlui_id:
self.disp(_(u"Invalid XMLUI received"), error=True)
return xmlui_id
def onFileAction(self, action_data, action_id, security_limit, profile):
xmlui_id = self.getXmluiId(action_data)
if xmlui_id is None:
return self.host.quitFromSignal(1)
try:
from_jid = jid.JID(action_data['meta_from_jid'])
except KeyError:
self.disp(_(u"Ignoring action without from_jid data"), 1)
return
try:
progress_id = action_data['meta_progress_id']
except KeyError:
self.disp(_(u"ignoring action without progress id"), 1)
return
if not self.bare_jids or from_jid.bare in self.bare_jids:
if self._overwrite_refused:
self.disp(_(u"File refused because overwrite is needed"), error=True)
self.host.bridge.launchAction(xmlui_id, {'cancelled': C.BOOL_TRUE}, profile_key=profile)
return self.host.quitFromSignal(2)
self.progress_id = progress_id
xmlui_data = {'path': self.path}
self.host.bridge.launchAction(xmlui_id, xmlui_data, profile_key=profile)
def onOverwriteAction(self, action_data, action_id, security_limit, profile):
xmlui_id = self.getXmluiId(action_data)
if xmlui_id is None:
return self.host.quitFromSignal(1)
try:
progress_id = action_data['meta_progress_id']
except KeyError:
self.disp(_(u"ignoring action without progress id"), 1)
return
self.disp(_(u"Overwriting needed"), 1)
if progress_id == self.progress_id:
if self.args.force:
self.disp(_(u"Overwrite accepted"), 2)
else:
self.disp(_(u"Refused to overwrite"), 2)
self._overwrite_refused = True
xmlui_data = {'answer': C.boolConst(self.args.force)}
self.host.bridge.launchAction(xmlui_id, xmlui_data, profile_key=profile)
def add_parser_options(self): def add_parser_options(self):
self.parser.add_argument("jids", type=base.unicode_decoder, nargs="*", help=_('Jids accepted (none means "accept everything")')) self.parser.add_argument("jids", type=base.unicode_decoder, nargs="*", help=_(u'JIDs accepted (accept everything if none is specified)'))
self.parser.add_argument("-m", "--multiple", action="store_true", help=_("Accept multiple files (you'll have to stop manually)")) self.parser.add_argument("-m", "--multiple", action="store_true", help=_(u"accept multiple files (you'll have to stop manually)"))
self.parser.add_argument("-f", "--force", action="store_true", help=_("Force overwritting of existing files")) self.parser.add_argument("-f", "--force", action="store_true", help=_(u"force overwritting of existing files (/!\\ name is choosed by sended)"))
self.parser.add_argument("--path", default='.', metavar='DIR', help=_(u"destination path (default: working directory)"))
def ask(self, data, confirm_id):
answer_data = {}
answer_data["dest_path"] = os.path.join(os.getcwd(), data['filename'])
if self.args.force or not os.path.exists(answer_data["dest_path"]):
self.host.bridge.confirmationAnswer(confirm_id, True, answer_data, self.profile)
info(_("Accepted file [%(filename)s] from %(sender)s") % {'filename':data['filename'], 'sender':data['from']})
self.progress_id = confirm_id
else:
self.host.bridge.confirmationAnswer(confirm_id, False, answer_data, self.profile)
warning(_("Refused file [%(filename)s] from %(sender)s: a file with the same name already exist") % {'filename':data['filename'], 'sender':data['from']})
if not self.args.multiple:
self.host.quit()
if not self.args.multiple and not self.args.progress:
#we just accept one file
self.host.quit()
def run(self): def run(self):
super(Receive, self).run() super(Receive, self).run()
self.bare_jids = [jid.JID(jid_).bare for jid_ in self.args.jids]
self.path = os.path.abspath(self.args.path)
if not os.path.isdir(self.path):
self.disp(_(u"Given path is not a directory !", error=True))
self.quit(2)
if self.args.multiple: if self.args.multiple:
self.host.quit_on_progress_end = False self.host.quit_on_progress_end = False
self.disp(_(u"waiting for incoming file request"),2)
class File(base.CommandBase): class File(base.CommandBase):