Mercurial > libervia-backend
annotate frontends/src/jp/cmd_file.py @ 817:c39117d00f35
jp: refactoring:
- imports from sat_frontends.jp instead of local imports
- added __init__.py
- commands now inherits from a base class: each base.CommandBase instance is a subcommand
- new arguments are added in CommandBase.add_parser_options methods, starting point si CommandBase.run or CommandBase.connected if a profile connection is needed
- commands are exported using a __commands__ variable at the top of the module
- sub-subcommand are easily added by using an other CommandBase instance as parent instead of using a Jp instance. In this case, the parent subcommand must be the one exported, and have a subcommands iterable (see cmd_file or cmd_pipe for examples).
- options which are often used (like --profile) are automatically added on demand (use_profile=True, use_progress=True)
- commands are automatically loaded when there are in a module named cmd_XXX
- restored --connect option
- restored progress bar
- restored getVersion bridge call on jp --version
- fixed file and pipe commands
- fixed forgotten translations
- fixed non SàT compliant docstrings
- better about/version dialog
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 10 Feb 2014 13:44:09 +0100 |
parents | frontends/src/jp/file.py@f8d534ed1d1e |
children | 300b4de701a6 |
rev | line source |
---|---|
815 | 1 #! /usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # jp: a SAT command line tool | |
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
814 | 20 from logging import debug, info, error, warning |
0 | 21 |
814 | 22 import base |
23 import sys | |
24 import os | |
25 import os.path | |
26 import tarfile | |
27 #import tempfile | |
771 | 28 from sat.core.i18n import _ |
402
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
29 |
817 | 30 __commands__ = ["File"] |
0 | 31 |
817 | 32 class Send(base.CommandBase): |
33 def __init__(self, host): | |
34 super(Send, self).__init__(host, 'send', use_progress=True, help=_('Send a file to a contact')) | |
0 | 35 |
817 | 36 def add_parser_options(self): |
37 self.parser.add_argument("files", type=str, nargs = '+', help=_("A list of file")) | |
38 self.parser.add_argument("jid", type=base.unicode_decoder, help=_("The destination jid")) | |
39 self.parser.add_argument("-b", "--bz2", action="store_true", help=_("Make a bzip2 tarball")) | |
0 | 40 |
817 | 41 def connected(self): |
42 """Send files to jabber contact""" | |
43 self.need_loop=True | |
44 super(Send, self).connected() | |
45 self.send_files() | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
46 |
0 | 47 def send_files(self): |
48 | |
817 | 49 for file_ in self.args.files: |
50 if not os.path.exists(file_): | |
51 error (_(u"file [%s] doesn't exist !") % file_) | |
52 self.host.quit(1) | |
53 if not self.args.bz2 and os.path.isdir(file_): | |
54 error (_("[%s] is a dir ! Please send files inside or use compression") % file_) | |
55 self.host.quit(1) | |
56 | |
57 full_dest_jid = self.host.get_full_jid(self.args.jid) | |
58 | |
59 if self.args.bz2: | |
60 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 | |
0 | 61 if os.path.exists(tmpfile): |
817 | 62 error (_("tmp file_ (%s) already exists ! Please remove it"), tmpfile) |
0 | 63 exit(1) |
70 | 64 warning(_("bz2 is an experimental option at an early dev stage, use with caution")) |
0 | 65 #FIXME: check free space, writting perm, tmp dir, filename (watch for OS used) |
817 | 66 print _(u"Starting compression, please wait...") |
0 | 67 sys.stdout.flush() |
817 | 68 bz2 = tarfile.open(tmpfile, "w:bz2") |
69 for file_ in self.args.files: | |
70 print _(u"Adding %s") % file_ | |
71 bz2.add(file_) | |
0 | 72 bz2.close() |
817 | 73 print _(u"Done !") |
74 path = os.path.abspath(tmpfile) | |
75 self.progress_id = self.host.bridge.sendFile(full_dest_jid, path, {}, self.profile) | |
0 | 76 else: |
817 | 77 for file_ in self.args.files: |
78 path = os.path.abspath(file_) | |
79 self.progress_id = self.host.bridge.sendFile(full_dest_jid, path, {}, self.profile) #FIXME: show progress only for last progress_id | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
80 |
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
81 |
817 | 82 class Receive(base.CommandAnswering): |
83 confirm_type = "FILE_TRANSFER" | |
0 | 84 |
817 | 85 def __init__(self, host): |
86 super(Receive, self).__init__(host, 'recv', use_progress=True, help=_('Wait for a file to be sent by a contact')) | |
0 | 87 |
817 | 88 @property |
89 def dest_jids(self): | |
90 return self.args.jids | |
393 | 91 |
817 | 92 def add_parser_options(self): |
93 self.parser.add_argument("jids", type=base.unicode_decoder, nargs="*", help=_('Jids accepted (none means "accept everything")')) | |
94 self.parser.add_argument("-m", "--multiple", action="store_true", help=_("Accept multiple files (you'll have to stop manually)")) | |
95 self.parser.add_argument("-f", "--force", action="store_true", help=_("Force overwritting of existing files")) | |
0 | 96 |
97 | |
817 | 98 def ask(self, data, confirm_id): |
99 answer_data = {} | |
100 answer_data["dest_path"] = os.path.join(os.getcwd(), data['filename']) | |
101 | |
102 if self.args.force or not os.path.exists(answer_data["dest_path"]): | |
103 self.host.bridge.confirmationAnswer(confirm_id, True, answer_data, self.profile) | |
104 info(_("Accepted file [%(filename)s] from %(sender)s") % {'filename':data['filename'], 'sender':data['from']}) | |
105 self.progress_id = confirm_id | |
106 else: | |
107 self.host.bridge.confirmationAnswer(confirm_id, False, answer_data, self.profile) | |
108 warning(_("Refused file [%(filename)s] from %(sender)s: a file with the same name already exist") % {'filename':data['filename'], 'sender':data['from']}) | |
109 if not self.args.multiple: | |
110 self.host.quit() | |
111 | |
112 if not self.args.multiple and not self.args.progress: | |
814 | 113 #we just accept one file |
817 | 114 self.host.quit() |
115 | |
116 def run(self): | |
117 super(Receive, self).run() | |
118 if self.args.multiple: | |
119 self.host.quit_on_progress_end = False | |
120 | |
121 | |
122 class File(base.CommandBase): | |
123 subcommands = (Send, Receive) | |
124 | |
125 def __init__(self, host): | |
126 super(File, self).__init__(host, 'file', use_profile=False, help=_('File sending/receiving')) |