Mercurial > libervia-backend
comparison 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 |
comparison
equal
deleted
inserted
replaced
813:1a1600491d9d | 814:59c7bc51c323 |
---|---|
1 from logging import debug, info, error, warning | |
2 | |
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 | |
10 from sat.core.i18n import _ | |
11 | |
12 file_parser = base.subparser.add_parser('file', | |
13 help = "File managment") | |
14 | |
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")) | |
26 | |
27 file_send.set_defaults(func=lambda args : FileSend(args.profile, args.jid, args.file, args.bz2).go()) | |
28 | |
29 | |
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")) | |
38 | |
39 file_recv.set_defaults(func=lambda args : FileRecv(args.profile, | |
40 args.jids, | |
41 args.multiple, | |
42 args.force).go()) | |
43 | |
44 | |
45 | |
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 | |
52 | |
53 def send_files(self): | |
54 """Send files to jabber contact""" | |
55 for file in self.files: | |
56 if not os.path.exists(file): | |
57 error (_(u"File [%s] doesn't exist !") % file) | |
58 exit(1) | |
59 if not self.bz2 and os.path.isdir(file): | |
60 error (_("[%s] is a dir ! Please send files inside or use compression") % file) | |
61 exit(1) | |
62 | |
63 full_dest_jid = self._getFullJid(self.dest_jid) | |
64 if self.bz2: | |
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): | |
67 error (_("tmp file (%s) already exists ! Please remove it"), tmpfile) | |
68 exit(1) | |
69 warning(_("bz2 is an experimental option at an early dev stage, use with caution")) | |
70 #FIXME: check free space, writting perm, tmp dir, filename (watch for OS used) | |
71 info(_("Starting compression, please wait...")) | |
72 sys.stdout.flush() | |
73 bz2=tarfile.open(tmpfile, "w:bz2") | |
74 for file in self.files: | |
75 info(_("Adding %s"), file) | |
76 bz2.add(file) | |
77 bz2.close() | |
78 info(_("OK !")) | |
79 path = abspath(tmpfile) | |
80 self.transfer_data = self.bridge.sendFile(full_dest_jid, path, {}, self.profile) | |
81 else: | |
82 for file in self.files: | |
83 path = abspath(file) | |
84 self.transfer_data = self.bridge.sendFile(full_dest_jid, path, {}, self.profile) #FIXME: show progress only for last transfer_id | |
85 | |
86 def run(self): | |
87 self.send_files() | |
88 | |
89 | |
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 | |
97 | |
98 def dest_jids(self): | |
99 return self._dest_jids | |
100 | |
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'] | |
107 | |
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 | |
112 else: | |
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']}) | |
115 | |
116 | |
117 if not self.multiple and not self.progress: | |
118 #we just accept one file | |
119 self.loop.quit() |