Mercurial > libervia-backend
annotate 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 |
rev | line source |
---|---|
814 | 1 from logging import debug, info, error, warning |
0 | 2 |
814 | 3 import base |
4 import sys | |
5 import os | |
6 import os.path | |
7 import tarfile | |
8 #import tempfile | |
9 from os.path import abspath, basename, dirname | |
771 | 10 from sat.core.i18n import _ |
402
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
11 |
814 | 12 file_parser = base.subparser.add_parser('file', |
13 help = "File managment") | |
0 | 14 |
814 | 15 file_parser.add_argument("-p", "--profile", action="store", type=str, default='@DEFAULT@', |
16 help=_("Use PROFILE profile key (default: %(default)s)")) | |
17 file_subparser = file_parser.add_subparsers() | |
18 file_send = file_subparser.add_parser('send', | |
19 help = "Send a file to a contact") | |
20 file_send.add_argument("file", type=str, nargs = '*', | |
21 help=_("A list of file")) | |
22 file_send.add_argument("jid", type=str, | |
23 help=_("The destination jid")) | |
24 file_send.add_argument("-b", "--bz2", action="store_true", default=False, | |
25 help=_("Make a bzip2 tarball")) | |
0 | 26 |
814 | 27 file_send.set_defaults(func=lambda args : FileSend(args.profile, args.jid, args.file, args.bz2).go()) |
28 | |
0 | 29 |
814 | 30 file_recv = file_subparser.add_parser('recv', |
31 help = "Receive a file to a contact") | |
32 file_recv.add_argument("jids", type=str, nargs="*", | |
33 help=_("A list of destination jids")) | |
34 file_recv.add_argument("-m", "--multiple", action="store_true", default=False, | |
35 help=_("Wait for a file to be sent by a contact")) | |
36 file_recv.add_argument("-f", "--force", action="store_true", default=False, | |
37 help=_("Force overwritting of existing files")) | |
0 | 38 |
814 | 39 file_recv.set_defaults(func=lambda args : FileRecv(args.profile, |
40 args.jids, | |
41 args.multiple, | |
42 args.force).go()) | |
0 | 43 |
44 | |
45 | |
814 | 46 class FileSend(base.JPWithProfile): |
47 def __init__(self, profile, dest_jid, files, bz2): | |
48 base.JPWithProfile.__init__(self,profile) | |
49 self.dest_jid = dest_jid | |
50 self.files = files | |
51 self.bz2 = bz2 | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
52 |
0 | 53 def send_files(self): |
54 """Send files to jabber contact""" | |
55 for file in self.files: | |
56 if not os.path.exists(file): | |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
57 error (_(u"File [%s] doesn't exist !") % file) |
0 | 58 exit(1) |
814 | 59 if not self.bz2 and os.path.isdir(file): |
70 | 60 error (_("[%s] is a dir ! Please send files inside or use compression") % file) |
0 | 61 exit(1) |
62 | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
63 full_dest_jid = self._getFullJid(self.dest_jid) |
814 | 64 if self.bz2: |
0 | 65 tmpfile = (basename(self.files[0]) or basename(dirname(self.files[0])) ) + '.tar.bz2' #FIXME: tmp, need an algorithm to find a good name/path |
66 if os.path.exists(tmpfile): | |
70 | 67 error (_("tmp file (%s) already exists ! Please remove it"), tmpfile) |
0 | 68 exit(1) |
70 | 69 warning(_("bz2 is an experimental option at an early dev stage, use with caution")) |
0 | 70 #FIXME: check free space, writting perm, tmp dir, filename (watch for OS used) |
70 | 71 info(_("Starting compression, please wait...")) |
0 | 72 sys.stdout.flush() |
73 bz2=tarfile.open(tmpfile, "w:bz2") | |
74 for file in self.files: | |
70 | 75 info(_("Adding %s"), file) |
0 | 76 bz2.add(file) |
77 bz2.close() | |
70 | 78 info(_("OK !")) |
0 | 79 path = abspath(tmpfile) |
720 | 80 self.transfer_data = self.bridge.sendFile(full_dest_jid, path, {}, self.profile) |
0 | 81 else: |
82 for file in self.files: | |
83 path = abspath(file) | |
720 | 84 self.transfer_data = self.bridge.sendFile(full_dest_jid, path, {}, self.profile) #FIXME: show progress only for last transfer_id |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
85 |
814 | 86 def run(self): |
87 self.send_files() | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
88 |
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
89 |
814 | 90 class FileRecv(base.JPAsk): |
91 def __init__(self, profile, dest_jids, multiple, force, progress = False): | |
92 base.JPAsk.__init__(self,profile, start_mainloop = True) | |
93 self._dest_jids = dest_jids | |
94 self.multiple = multiple | |
95 self.force = force | |
96 self.progress = progress | |
0 | 97 |
814 | 98 def dest_jids(self): |
99 return self._dest_jids | |
0 | 100 |
814 | 101 def confirm_type(self): |
102 return "FILE_TRANSFER" | |
103 | |
104 def ask(self, data): | |
105 answer_data = {} | |
106 answer_data["dest_path"] = os.getcwd()+'/'+data['filename'] | |
393 | 107 |
814 | 108 if self.force or not os.path.exists(answer_data["dest_path"]): |
109 self.answer(True, answer_data) | |
110 info(_("Accepted file [%(filename)s] from %(sender)s") % {'filename':data['filename'], 'sender':data['from']}) | |
111 # self.transfer_data = self.confirm_id # Used by progress | |
0 | 112 else: |
814 | 113 self.answer(False, answer_data) |
114 warning(_("Refused file [%(filename)s] from %(sender)s: a file with the same name already exist") % {'filename':data['filename'], 'sender':data['from']}) | |
0 | 115 |
116 | |
814 | 117 if not self.multiple and not self.progress: |
118 #we just accept one file | |
119 self.loop.quit() |