annotate sat_frontends/jp/cmd_file.py @ 2609:75d2ac872965

jp (base): do not crash when item is specified (e.g. in URL) but not needed in command, but display a message on stderr.
author Goffi <goffi@goffi.org>
date Wed, 06 Jun 2018 19:24:15 +0200
parents c0401a72cbb4
children 56f94936df1e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1960
3e168cde7a7d jp: fixed shebang python call
Goffi <goffi@goffi.org>
parents: 1880
diff changeset
1 #!/usr/bin/env python2
815
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
2 # -*- coding: utf-8 -*-
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
3
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
4 # jp: a SAT command line tool
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
815
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
6
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
7 # This program is free software: you can redistribute it and/or modify
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
10 # (at your option) any later version.
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
11
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
12 # This program is distributed in the hope that it will be useful,
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
15 # GNU Affero General Public License for more details.
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
16
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
19
0
goffi@necton2
parents:
diff changeset
20
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
21 import base
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
22 import sys
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
23 import os
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
24 import os.path
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
25 import tarfile
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 721
diff changeset
26 from sat.core.i18n import _
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
27 from sat_frontends.jp.constants import Const as C
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
28 from sat_frontends.jp import common
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
29 from sat_frontends.tools import jid
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
30 from sat.tools.common.ansi import ANSI as A
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
31 import tempfile
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
32 import xml.etree.ElementTree as ET # FIXME: used temporarily to manage XMLUI
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
33 from functools import partial
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
34 import json
402
f03688bdb858 jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents: 401
diff changeset
35
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
36 __commands__ = ["File"]
0
goffi@necton2
parents:
diff changeset
37
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
38
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
39 class Send(base.CommandBase):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
40 def __init__(self, host):
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
41 super(Send, self).__init__(host, 'send', use_progress=True, use_verbose=True, help=_('send a file to a contact'))
1864
96ba685162f6 jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents: 1824
diff changeset
42 self.need_loop=True
0
goffi@necton2
parents:
diff changeset
43
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
44 def add_parser_options(self):
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
45 self.parser.add_argument("files", type=str, nargs='+', metavar='file', help=_(u"a list of file"))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
46 self.parser.add_argument("jid", type=base.unicode_decoder, help=_(u"the destination jid"))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
47 self.parser.add_argument("-b", "--bz2", action="store_true", help=_(u"make a bzip2 tarball"))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
48 self.parser.add_argument("-d", "--path", type=base.unicode_decoder, help=(u"path to the directory where the file must be stored"))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
49 self.parser.add_argument("-N", "--namespace", type=base.unicode_decoder, help=(u"namespace of the file"))
2516
842bd1594077 jp (file/send): --name can now be specified
Goffi <goffi@goffi.org>
parents: 2505
diff changeset
50 self.parser.add_argument("-n", "--name", type=base.unicode_decoder, default=u'', help=(u"name to use (DEFAULT: use source file name)"))
0
goffi@necton2
parents:
diff changeset
51
1864
96ba685162f6 jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents: 1824
diff changeset
52 def start(self):
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
53 """Send files to jabber contact"""
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
54 self.send_files()
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
55
1627
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
56 def onProgressStarted(self, metadata):
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
57 self.disp(_(u'File copy started'),2)
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
58
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
59 def onProgressFinished(self, metadata):
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
60 self.disp(_(u'File sent successfully'),2)
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
61
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
62 def onProgressError(self, error_msg):
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
63 if error_msg == C.PROGRESS_ERROR_DECLINED:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
64 self.disp(_(u'The file has been refused by your contact'))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
65 else:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
66 self.disp(_(u'Error while sending file: {}').format(error_msg),error=True)
1627
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
67
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
68 def gotId(self, data, file_):
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
69 """Called when a progress id has been received
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
70
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
71 @param pid(unicode): progress id
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
72 @param file_(str): file path
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
73 """
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
74 #FIXME: this show progress only for last progress_id
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
75 self.disp(_(u"File request sent to {jid}".format(jid=self.full_dest_jid)), 1)
1621
a17a91531fbe jp (file): print a message and quit if progress_id is not received
Goffi <goffi@goffi.org>
parents: 1606
diff changeset
76 try:
a17a91531fbe jp (file): print a message and quit if progress_id is not received
Goffi <goffi@goffi.org>
parents: 1606
diff changeset
77 self.progress_id = data['progress']
a17a91531fbe jp (file): print a message and quit if progress_id is not received
Goffi <goffi@goffi.org>
parents: 1606
diff changeset
78 except KeyError:
a17a91531fbe jp (file): print a message and quit if progress_id is not received
Goffi <goffi@goffi.org>
parents: 1606
diff changeset
79 # TODO: if 'xmlui' key is present, manage xmlui message display
a17a91531fbe jp (file): print a message and quit if progress_id is not received
Goffi <goffi@goffi.org>
parents: 1606
diff changeset
80 self.disp(_(u"Can't send file to {jid}".format(jid=self.full_dest_jid)), error=True)
a17a91531fbe jp (file): print a message and quit if progress_id is not received
Goffi <goffi@goffi.org>
parents: 1606
diff changeset
81 self.host.quit(2)
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
82
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
83 def error(self, failure):
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
84 self.disp(_("Error while trying to send a file: {reason}").format(reason=failure), error=True)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
85 self.host.quit(1)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
86
0
goffi@necton2
parents:
diff changeset
87 def send_files(self):
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
88 for file_ in self.args.files:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
89 if not os.path.exists(file_):
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
90 self.disp(_(u"file [{}] doesn't exist !").format(file_), error=True)
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
91 self.host.quit(1)
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
92 if not self.args.bz2 and os.path.isdir(file_):
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
93 self.disp(_(u"[{}] is a dir ! Please send files inside or use compression").format(file_))
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
94 self.host.quit(1)
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
95
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
96 self.full_dest_jid = self.host.get_full_jid(self.args.jid)
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
97 extra = {}
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
98 if self.args.path:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
99 extra[u'path'] = self.args.path
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
100 if self.args.namespace:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
101 extra[u'namespace'] = self.args.namespace
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
102
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
103 if self.args.bz2:
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
104 with tempfile.NamedTemporaryFile('wb', delete=False) as buf:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
105 self.host.addOnQuitCallback(os.unlink, buf.name)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
106 self.disp(_(u"bz2 is an experimental option, use with caution"))
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
107 #FIXME: check free space
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
108 self.disp(_(u"Starting compression, please wait..."))
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
109 sys.stdout.flush()
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
110 bz2 = tarfile.open(mode="w:bz2", fileobj=buf)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
111 archive_name = u'{}.tar.bz2'.format(os.path.basename(self.args.files[0]) or u'compressed_files')
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
112 for file_ in self.args.files:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
113 self.disp(_(u"Adding {}").format(file_), 1)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
114 bz2.add(file_)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
115 bz2.close()
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
116 self.disp(_(u"Done !"), 1)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
117
2516
842bd1594077 jp (file/send): --name can now be specified
Goffi <goffi@goffi.org>
parents: 2505
diff changeset
118 self.host.bridge.fileSend(self.full_dest_jid, buf.name, self.args.name or archive_name, '', extra, self.profile,
842bd1594077 jp (file/send): --name can now be specified
Goffi <goffi@goffi.org>
parents: 2505
diff changeset
119 callback=lambda pid, file_=buf.name: self.gotId(pid, file_), errback=self.error)
0
goffi@necton2
parents:
diff changeset
120 else:
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
121 for file_ in self.args.files:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
122 path = os.path.abspath(file_)
2516
842bd1594077 jp (file/send): --name can now be specified
Goffi <goffi@goffi.org>
parents: 2505
diff changeset
123 self.host.bridge.fileSend(self.full_dest_jid, path, self.args.name, '', extra, self.profile,
842bd1594077 jp (file/send): --name can now be specified
Goffi <goffi@goffi.org>
parents: 2505
diff changeset
124 callback=lambda pid, file_=file_: self.gotId(pid, file_), errback=self.error)
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
125
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
126
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
127 class Request(base.CommandBase):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
128
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
129 def __init__(self, host):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
130 super(Request, self).__init__(host, 'request', use_progress=True, use_verbose=True, help=_('request a file from a contact'))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
131 self.need_loop=True
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
132
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
133 @property
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
134 def filename(self):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
135 return self.args.name or self.args.hash or u"output"
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
136
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
137 def add_parser_options(self):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
138 self.parser.add_argument("jid", type=base.unicode_decoder, help=_(u"the destination jid"))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
139 self.parser.add_argument("-D", "--dest", type=base.unicode_decoder, help=_(u"destination path where the file will be saved (default: [current_dir]/[name|hash])"))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
140 self.parser.add_argument("-n", "--name", type=base.unicode_decoder, default=u'', help=_(u"name of the file"))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
141 self.parser.add_argument("-H", "--hash", type=base.unicode_decoder, default=u'', help=_(u"hash of the file"))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
142 self.parser.add_argument("-a", "--hash-algo", type=base.unicode_decoder, default=u'sha-256', help=_(u"hash algorithm use for --hash (default: sha-256)"))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
143 self.parser.add_argument("-d", "--path", type=base.unicode_decoder, help=(u"path to the directory containing the file"))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
144 self.parser.add_argument("-N", "--namespace", type=base.unicode_decoder, help=(u"namespace of the file"))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
145 self.parser.add_argument("-f", "--force", action='store_true', help=_(u"overwrite existing file without confirmation"))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
146
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
147 def onProgressStarted(self, metadata):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
148 self.disp(_(u'File copy started'),2)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
149
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
150 def onProgressFinished(self, metadata):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
151 self.disp(_(u'File received successfully'),2)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
152
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
153 def onProgressError(self, error_msg):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
154 if error_msg == C.PROGRESS_ERROR_DECLINED:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
155 self.disp(_(u'The file request has been refused'))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
156 else:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
157 self.disp(_(u'Error while requesting file: {}').format(error_msg), error=True)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
158
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
159 def gotId(self, progress_id):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
160 """Called when a progress id has been received
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
161
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
162 @param progress_id(unicode): progress id
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
163 """
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
164 self.progress_id = progress_id
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
165
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
166 def error(self, failure):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
167 self.disp(_("Error while trying to send a file: {reason}").format(reason=failure), error=True)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
168 self.host.quit(1)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
169
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
170 def start(self):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
171 if not self.args.name and not self.args.hash:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
172 self.parser.error(_(u'at least one of --name or --hash must be provided'))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
173 # extra = dict(self.args.extra)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
174 if self.args.dest:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
175 path = os.path.abspath(os.path.expanduser(self.args.dest))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
176 if os.path.isdir(path):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
177 path = os.path.join(path, self.filename)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
178 else:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
179 path = os.path.abspath(self.filename)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
180
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
181 if os.path.exists(path) and not self.args.force:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
182 message = _(u'File {path} already exists! Do you want to overwrite?').format(path=path)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
183 confirm = raw_input(u"{} (y/N) ".format(message).encode('utf-8'))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
184 if confirm not in (u"y", u"Y"):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
185 self.disp(_(u"file request cancelled"))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
186 self.host.quit(2)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
187
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
188 self.full_dest_jid = self.host.get_full_jid(self.args.jid)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
189 extra = {}
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
190 if self.args.path:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
191 extra[u'path'] = self.args.path
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
192 if self.args.namespace:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
193 extra[u'namespace'] = self.args.namespace
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
194 self.host.bridge.fileJingleRequest(self.full_dest_jid,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
195 path,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
196 self.args.name,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
197 self.args.hash,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
198 self.args.hash_algo if self.args.hash else u'',
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
199 extra,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
200 self.profile,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
201 callback=self.gotId,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
202 errback=partial(self.errback,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
203 msg=_(u"can't request file: {}"),
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
204 exit_code=C.EXIT_BRIDGE_ERRBACK))
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
205
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
206
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
207 class Receive(base.CommandAnswering):
0
goffi@necton2
parents:
diff changeset
208
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
209 def __init__(self, host):
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
210 super(Receive, self).__init__(host, 'receive', use_progress=True, use_verbose=True, help=_('wait for a file to be sent by a contact'))
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
211 self._overwrite_refused = False # True when one overwrite as already been refused
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
212 self.action_callbacks = {C.META_TYPE_FILE: self.onFileAction,
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
213 C.META_TYPE_OVERWRITE: self.onOverwriteAction}
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
214
1627
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
215 def onProgressStarted(self, metadata):
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
216 self.disp(_(u'File copy started'),2)
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
217
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
218 def onProgressFinished(self, metadata):
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
219 self.disp(_(u'File received successfully'),2)
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
220 if metadata.get('hash_verified', False):
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
221 try:
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
222 self.disp(_(u'hash checked: {algo}:{checksum}').format(
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
223 algo=metadata['hash_algo'],
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
224 checksum=metadata['hash']),
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
225 1)
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
226 except KeyError:
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
227 self.disp(_(u'hash is checked but hash value is missing', 1), error=True)
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
228 else:
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
229 self.disp(_(u"hash can't be verified"), 1)
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
230
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
231 def onProgressError(self, error_msg):
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
232 self.disp(_(u'Error while receiving file: {}').format(error_msg),error=True)
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1621
diff changeset
233
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
234 def getXmluiId(self, action_data):
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
235 # FIXME: we temporarily use ElementTree, but a real XMLUI managing module
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
236 # should be available in the futur
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
237 # TODO: XMLUI module
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
238 try:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
239 xml_ui = action_data['xmlui']
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
240 except KeyError:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
241 self.disp(_(u"Action has no XMLUI"), 1)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
242 else:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
243 ui = ET.fromstring(xml_ui.encode('utf-8'))
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
244 xmlui_id = ui.get('submit')
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
245 if not xmlui_id:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
246 self.disp(_(u"Invalid XMLUI received"), error=True)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
247 return xmlui_id
0
goffi@necton2
parents:
diff changeset
248
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
249 def onFileAction(self, action_data, action_id, security_limit, profile):
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
250 xmlui_id = self.getXmluiId(action_data)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
251 if xmlui_id is None:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
252 return self.host.quitFromSignal(1)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
253 try:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
254 from_jid = jid.JID(action_data['meta_from_jid'])
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
255 except KeyError:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
256 self.disp(_(u"Ignoring action without from_jid data"), 1)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
257 return
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
258 try:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
259 progress_id = action_data['meta_progress_id']
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
260 except KeyError:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
261 self.disp(_(u"ignoring action without progress id"), 1)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
262 return
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
263
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
264 if not self.bare_jids or from_jid.bare in self.bare_jids:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
265 if self._overwrite_refused:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
266 self.disp(_(u"File refused because overwrite is needed"), error=True)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
267 self.host.bridge.launchAction(xmlui_id, {'cancelled': C.BOOL_TRUE}, profile_key=profile)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
268 return self.host.quitFromSignal(2)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
269 self.progress_id = progress_id
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
270 xmlui_data = {'path': self.path}
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
271 self.host.bridge.launchAction(xmlui_id, xmlui_data, profile_key=profile)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
272
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
273 def onOverwriteAction(self, action_data, action_id, security_limit, profile):
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
274 xmlui_id = self.getXmluiId(action_data)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
275 if xmlui_id is None:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
276 return self.host.quitFromSignal(1)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
277 try:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
278 progress_id = action_data['meta_progress_id']
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
279 except KeyError:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
280 self.disp(_(u"ignoring action without progress id"), 1)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
281 return
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
282 self.disp(_(u"Overwriting needed"), 1)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
283
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
284 if progress_id == self.progress_id:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
285 if self.args.force:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
286 self.disp(_(u"Overwrite accepted"), 2)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
287 else:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
288 self.disp(_(u"Refused to overwrite"), 2)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
289 self._overwrite_refused = True
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
290
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
291 xmlui_data = {'answer': C.boolConst(self.args.force)}
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
292 self.host.bridge.launchAction(xmlui_id, xmlui_data, profile_key=profile)
393
393b35aa86d2 jp: added --connect option
Goffi <goffi@goffi.org>
parents: 391
diff changeset
293
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
294 def add_parser_options(self):
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
295 self.parser.add_argument("jids", type=base.unicode_decoder, nargs="*", help=_(u'jids accepted (accept everything if none is specified)'))
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
296 self.parser.add_argument("-m", "--multiple", action="store_true", help=_(u"accept multiple files (you'll have to stop manually)"))
2375
4b521490bd8d jp (file): typo
Goffi <goffi@goffi.org>
parents: 1960
diff changeset
297 self.parser.add_argument("-f", "--force", action="store_true", help=_(u"force overwritting of existing files (/!\\ name is choosed by sender)"))
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
298 self.parser.add_argument("--path", default='.', metavar='DIR', help=_(u"destination path (default: working directory)"))
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
299
1864
96ba685162f6 jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents: 1824
diff changeset
300 def start(self):
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
301 self.bare_jids = [jid.JID(jid_).bare for jid_ in self.args.jids]
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
302 self.path = os.path.abspath(self.args.path)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
303 if not os.path.isdir(self.path):
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
304 self.disp(_(u"Given path is not a directory !", error=True))
1880
68944e270c40 jp (file): fixed bad quit call
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
305 self.host.quit(2)
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
306 if self.args.multiple:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
307 self.host.quit_on_progress_end = False
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
308 self.disp(_(u"waiting for incoming file request"),2)
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
309
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
310
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
311 class Upload(base.CommandBase):
1864
96ba685162f6 jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents: 1824
diff changeset
312
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
313 def __init__(self, host):
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
314 super(Upload, self).__init__(host, 'upload', use_progress=True, use_verbose=True, help=_('upload a file'))
1864
96ba685162f6 jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents: 1824
diff changeset
315 self.need_loop=True
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
316
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
317 def add_parser_options(self):
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
318 self.parser.add_argument("file", type=str, help=_("file to upload"))
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
319 self.parser.add_argument("jid", type=base.unicode_decoder, nargs='?', help=_("jid of upload component (nothing to autodetect)"))
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
320 self.parser.add_argument("--ignore-tls-errors", action="store_true", help=_("ignore invalide TLS certificate"))
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
321
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
322 def onProgressStarted(self, metadata):
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
323 self.disp(_(u'File upload started'),2)
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
324
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
325 def onProgressFinished(self, metadata):
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
326 self.disp(_(u'File uploaded successfully'),2)
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
327 try:
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
328 url = metadata['url']
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
329 except KeyError:
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
330 self.disp(u'download URL not found in metadata')
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
331 else:
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
332 self.disp(_(u'URL to retrieve the file:'),1)
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
333 # XXX: url is display alone on a line to make parsing easier
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
334 self.disp(url)
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
335
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
336 def onProgressError(self, error_msg):
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
337 self.disp(_(u'Error while uploading file: {}').format(error_msg),error=True)
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
338
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
339 def gotId(self, data, file_):
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
340 """Called when a progress id has been received
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
341
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
342 @param pid(unicode): progress id
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
343 @param file_(str): file path
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
344 """
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
345 try:
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
346 self.progress_id = data['progress']
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
347 except KeyError:
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
348 # TODO: if 'xmlui' key is present, manage xmlui message display
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
349 self.disp(_(u"Can't upload file"), error=True)
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
350 self.host.quit(2)
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
351
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
352 def error(self, failure):
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
353 self.disp(_("Error while trying to upload a file: {reason}").format(reason=failure), error=True)
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
354 self.host.quit(1)
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
355
1864
96ba685162f6 jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents: 1824
diff changeset
356 def start(self):
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
357 file_ = self.args.file
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
358 if not os.path.exists(file_):
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
359 self.disp(_(u"file [{}] doesn't exist !").format(file_), error=True)
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
360 self.host.quit(1)
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
361 if os.path.isdir(file_):
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
362 self.disp(_(u"[{}] is a dir! Can't upload a dir").format(file_))
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
363 self.host.quit(1)
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
364
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
365 self.full_dest_jid = self.host.get_full_jid(self.args.jid) if self.args.jid is not None else ''
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
366 options = {}
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
367 if self.args.ignore_tls_errors:
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
368 options['ignore_tls_errors'] = C.BOOL_TRUE
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
369
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
370 path = os.path.abspath(file_)
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
371 self.host.bridge.fileUpload(path, '', self.full_dest_jid, options, self.profile, callback=lambda pid, file_=file_: self.gotId(pid, file_), errback=self.error)
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
372
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
373
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
374 class ShareList(base.CommandBase):
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
375
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
376 def __init__(self, host):
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
377 extra_outputs = {'default': self.default_output}
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
378 super(ShareList, self).__init__(host, 'list', use_output=C.OUTPUT_LIST_DICT, extra_outputs=extra_outputs, help=_(u'retrieve files shared by an entity'), use_verbose=True)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
379 self.need_loop=True
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
380
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
381 def add_parser_options(self):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
382 self.parser.add_argument("-d", "--path", default=u'', help=_(u"path to the directory containing the files"))
2592
c0401a72cbb4 jp (file/share/list): fixed default value for jid
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
383 self.parser.add_argument("jid", type=base.unicode_decoder, nargs='?', default='', help=_("jid of sharing entity (nothing to check our own jid)"))
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
384
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
385 def file_gen(self, files_data):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
386 for file_data in files_data:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
387 yield file_data[u'name']
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
388 yield file_data.get(u'size', '')
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
389 yield file_data.get(u'hash','')
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
390
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
391 def _name_filter(self, name, row):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
392 if row.type == C.FILE_TYPE_DIRECTORY:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
393 return A.color(C.A_DIRECTORY, name)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
394 elif row.type == C.FILE_TYPE_FILE:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
395 return A.color(C.A_FILE, name)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
396 else:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
397 self.disp(_(u'unknown file type: {type}').format(type=row.type), error=True)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
398 return name
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
399
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
400 def _size_filter(self, size, row):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
401 if not size:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
402 return u''
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
403 size = int(size)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
404 # cf. https://stackoverflow.com/a/1094933 (thanks)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
405 suffix = u'o'
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
406 for unit in [u'', u'Ki', u'Mi', u'Gi', u'Ti', u'Pi', u'Ei', u'Zi']:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
407 if abs(size) < 1024.0:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
408 return A.color(A.BOLD, u"{:.2f}".format(size), unit, suffix)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
409 size /= 1024.0
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
410
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
411 return A.color(A.BOLD, u"{:.2f}".format(size), u'Yi', suffix)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
412
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
413 def default_output(self, files_data):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
414 """display files a way similar to ls"""
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
415 files_data.sort(key=lambda d: d['name'].lower())
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
416 show_header = False
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
417 if self.verbosity == 0:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
418 headers = (u'name', u'type')
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
419 elif self.verbosity == 1:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
420 headers = (u'name', u'type', u'size')
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
421 elif self.verbosity > 1:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
422 show_header = True
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
423 headers = (u'name', u'type', u'size', u'hash')
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
424 table = common.Table.fromDict(self.host,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
425 files_data,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
426 headers,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
427 filters={u'name': self._name_filter,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
428 u'size': self._size_filter},
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
429 defaults={u'size': u'',
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
430 u'hash': u''},
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
431 )
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
432 table.display_blank(show_header=show_header, hide_cols=['type'])
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
433
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
434 def _FISListCb(self, files_data):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
435 self.output(files_data)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
436 self.host.quit()
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
437
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
438 def start(self):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
439 self.host.bridge.FISList(
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
440 self.args.jid,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
441 self.args.path,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
442 {},
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
443 self.profile,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
444 callback=self._FISListCb,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
445 errback=partial(self.errback,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
446 msg=_(u"can't retrieve shared files: {}"),
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
447 exit_code=C.EXIT_BRIDGE_ERRBACK))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
448
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
449
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
450 class SharePath(base.CommandBase):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
451
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
452 def __init__(self, host):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
453 super(SharePath, self).__init__(host, 'path', help=_(u'share a file or directory'), use_verbose=True)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
454 self.need_loop=True
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
455
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
456 def add_parser_options(self):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
457 self.parser.add_argument("-n", "--name", type=base.unicode_decoder, default=u'', help=_(u"virtual name to use (default: use directory/file name)"))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
458 perm_group = self.parser.add_mutually_exclusive_group()
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
459 perm_group.add_argument("-j", "--jid", type=base.unicode_decoder, action='append', dest="jids", default=[], help=_(u"jid of contacts allowed to retrieve the files"))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
460 perm_group.add_argument("--public", action='store_true', help=_(u"share publicly the file(s) (/!\\ *everybody* will be able to access them)"))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
461 self.parser.add_argument("path", type=base.unicode_decoder, help=_(u"path to a file or directory to share"))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
462
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
463
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
464 def _FISSharePathCb(self, name):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
465 self.disp(_(u'{path} shared under the name "{name}"').format(
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
466 path = self.path,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
467 name = name))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
468 self.host.quit()
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
469
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
470 def start(self):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
471 self.path = os.path.abspath(self.args.path)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
472 if self.args.public:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
473 access = {u'read': {u'type': u'public'}}
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
474 else:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
475 jids = self.args.jids
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
476 if jids:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
477 access = {u'read': {u'type': 'whitelist',
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
478 u'jids': jids}}
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
479 else:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
480 access = {}
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
481 self.host.bridge.FISSharePath(
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
482 self.args.name,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
483 self.path,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
484 json.dumps(access, ensure_ascii=False),
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
485 self.profile,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
486 callback=self._FISSharePathCb,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
487 errback=partial(self.errback,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
488 msg=_(u"can't share path: {}"),
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
489 exit_code=C.EXIT_BRIDGE_ERRBACK))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
490
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
491
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
492 class Share(base.CommandBase):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
493 subcommands = (ShareList, SharePath)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
494
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
495 def __init__(self, host):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
496 super(Share, self).__init__(host, 'share', use_profile=False, help=_(u'files sharing management'))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
497
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
498
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
499 class File(base.CommandBase):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
500 subcommands = (Send, Request, Receive, Upload, Share)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
501
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
502 def __init__(self, host):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
503 super(File, self).__init__(host, 'file', use_profile=False, help=_(u'files sending/receiving/management'))