Mercurial > libervia-backend
diff frontends/src/jp/file.py @ 814:59c7bc51c323
jp: refactoring using ArgParse
author | Dal <kedals0@gmail.com> |
---|---|
date | Wed, 05 Feb 2014 14:35:26 +0100 |
parents | frontends/src/jp/jp@1fe00f0c9a91 |
children | f8d534ed1d1e |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/frontends/src/jp/file.py Wed Feb 05 14:35:26 2014 +0100 @@ -0,0 +1,119 @@ +from logging import debug, info, error, warning + +import base +import sys +import os +import os.path +import tarfile +#import tempfile +from os.path import abspath, basename, dirname +from sat.core.i18n import _ + +file_parser = base.subparser.add_parser('file', + help = "File managment") + +file_parser.add_argument("-p", "--profile", action="store", type=str, default='@DEFAULT@', + help=_("Use PROFILE profile key (default: %(default)s)")) +file_subparser = file_parser.add_subparsers() +file_send = file_subparser.add_parser('send', + help = "Send a file to a contact") +file_send.add_argument("file", type=str, nargs = '*', + help=_("A list of file")) +file_send.add_argument("jid", type=str, + help=_("The destination jid")) +file_send.add_argument("-b", "--bz2", action="store_true", default=False, + help=_("Make a bzip2 tarball")) + +file_send.set_defaults(func=lambda args : FileSend(args.profile, args.jid, args.file, args.bz2).go()) + + +file_recv = file_subparser.add_parser('recv', + help = "Receive a file to a contact") +file_recv.add_argument("jids", type=str, nargs="*", + help=_("A list of destination jids")) +file_recv.add_argument("-m", "--multiple", action="store_true", default=False, + help=_("Wait for a file to be sent by a contact")) +file_recv.add_argument("-f", "--force", action="store_true", default=False, + help=_("Force overwritting of existing files")) + +file_recv.set_defaults(func=lambda args : FileRecv(args.profile, + args.jids, + args.multiple, + args.force).go()) + + + +class FileSend(base.JPWithProfile): + def __init__(self, profile, dest_jid, files, bz2): + base.JPWithProfile.__init__(self,profile) + self.dest_jid = dest_jid + self.files = files + self.bz2 = bz2 + + def send_files(self): + """Send files to jabber contact""" + for file in self.files: + if not os.path.exists(file): + error (_(u"File [%s] doesn't exist !") % file) + exit(1) + if not self.bz2 and os.path.isdir(file): + error (_("[%s] is a dir ! Please send files inside or use compression") % file) + exit(1) + + full_dest_jid = self._getFullJid(self.dest_jid) + if self.bz2: + tmpfile = (basename(self.files[0]) or basename(dirname(self.files[0])) ) + '.tar.bz2' #FIXME: tmp, need an algorithm to find a good name/path + if os.path.exists(tmpfile): + error (_("tmp file (%s) already exists ! Please remove it"), tmpfile) + exit(1) + warning(_("bz2 is an experimental option at an early dev stage, use with caution")) + #FIXME: check free space, writting perm, tmp dir, filename (watch for OS used) + info(_("Starting compression, please wait...")) + sys.stdout.flush() + bz2=tarfile.open(tmpfile, "w:bz2") + for file in self.files: + info(_("Adding %s"), file) + bz2.add(file) + bz2.close() + info(_("OK !")) + path = abspath(tmpfile) + self.transfer_data = self.bridge.sendFile(full_dest_jid, path, {}, self.profile) + else: + for file in self.files: + path = abspath(file) + self.transfer_data = self.bridge.sendFile(full_dest_jid, path, {}, self.profile) #FIXME: show progress only for last transfer_id + + def run(self): + self.send_files() + + +class FileRecv(base.JPAsk): + def __init__(self, profile, dest_jids, multiple, force, progress = False): + base.JPAsk.__init__(self,profile, start_mainloop = True) + self._dest_jids = dest_jids + self.multiple = multiple + self.force = force + self.progress = progress + + def dest_jids(self): + return self._dest_jids + + def confirm_type(self): + return "FILE_TRANSFER" + + def ask(self, data): + answer_data = {} + answer_data["dest_path"] = os.getcwd()+'/'+data['filename'] + + if self.force or not os.path.exists(answer_data["dest_path"]): + self.answer(True, answer_data) + info(_("Accepted file [%(filename)s] from %(sender)s") % {'filename':data['filename'], 'sender':data['from']}) + # self.transfer_data = self.confirm_id # Used by progress + else: + self.answer(False, answer_data) + 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.multiple and not self.progress: + #we just accept one file + self.loop.quit()