annotate sat_frontends/jp/cmd_file.py @ 3032:95e2fd14a761

plugin XEP-0329: fixed ShareNode item() and values after Python 3 port
author Goffi <goffi@goffi.org>
date Fri, 16 Aug 2019 17:06:44 +0200
parents ab2696e34d29
children fee60f17ebac
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
2771
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
5 # Copyright (C) 2009-2019 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
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
21 from . import base
814
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 _
2932
472fadadefe6 jp (file/invite): added a --thumbnail argument
Goffi <goffi@goffi.org>
parents: 2916
diff changeset
27 from sat.tools.common import data_format
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
28 from sat_frontends.jp.constants import Const as C
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
29 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
30 from sat_frontends.tools import jid
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
31 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
32 import tempfile
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
33 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
34 from functools import partial
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
35 import json
402
f03688bdb858 jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents: 401
diff changeset
36
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
37 __commands__ = ["File"]
0
goffi@necton2
parents:
diff changeset
38
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
39
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
40 class Send(base.CommandBase):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
41 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
42 super(Send, self).__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
43 host,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
44 "send",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
45 use_progress=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
46 use_verbose=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
47 help=_("send a file to a contact"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
48 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
49 self.need_loop = True
0
goffi@necton2
parents:
diff changeset
50
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
51 def add_parser_options(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
52 self.parser.add_argument(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
53 "files", type=str, nargs="+", metavar="file", help=_("a list of file")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
54 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
55 self.parser.add_argument(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
56 "jid", help=_("the destination jid")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
57 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
58 self.parser.add_argument(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
59 "-b", "--bz2", action="store_true", help=_("make a bzip2 tarball")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
60 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
61 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
62 "-d",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
63 "--path",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
64 help=("path to the directory where the file must be stored"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
65 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
66 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
67 "-N",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
68 "--namespace",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
69 help=("namespace of the file"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
70 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
71 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
72 "-n",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
73 "--name",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
74 default="",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
75 help=("name to use (DEFAULT: use source file name)"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
76 )
0
goffi@necton2
parents:
diff changeset
77
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
78 def start(self):
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
79 """Send files to jabber contact"""
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
80 self.send_files()
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
81
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
82 def onProgressStarted(self, metadata):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
83 self.disp(_("File copy started"), 2)
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
84
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
85 def onProgressFinished(self, metadata):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
86 self.disp(_("File sent successfully"), 2)
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
87
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
88 def onProgressError(self, error_msg):
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
89 if error_msg == C.PROGRESS_ERROR_DECLINED:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
90 self.disp(_("The file has been refused by your contact"))
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
91 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
92 self.disp(_("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
93
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
94 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
95 """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
96
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
97 @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
98 @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
99 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
100 # FIXME: this show progress only for last progress_id
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
101 self.disp(_("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
102 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
103 self.progress_id = data["progress"]
1621
a17a91531fbe jp (file): print a message and quit if progress_id is not received
Goffi <goffi@goffi.org>
parents: 1606
diff changeset
104 except KeyError:
a17a91531fbe jp (file): print a message and quit if progress_id is not received
Goffi <goffi@goffi.org>
parents: 1606
diff changeset
105 # TODO: if 'xmlui' key is present, manage xmlui message display
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
106 self.disp(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
107 _("Can't send file to {jid}".format(jid=self.full_dest_jid)), error=True
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
108 )
1621
a17a91531fbe jp (file): print a message and quit if progress_id is not received
Goffi <goffi@goffi.org>
parents: 1606
diff changeset
109 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
110
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
111 def error(self, failure):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
112 self.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
113 _("Error while trying to send a file: {reason}").format(reason=failure),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
114 error=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
115 )
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
116 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
117
0
goffi@necton2
parents:
diff changeset
118 def send_files(self):
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
119 for file_ in self.args.files:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
120 if not os.path.exists(file_):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
121 self.disp(_("file [{}] doesn't exist !").format(file_), error=True)
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
122 self.host.quit(1)
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
123 if not self.args.bz2 and os.path.isdir(file_):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
124 self.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
125 _(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
126 "[{}] is a dir ! Please send files inside or use compression"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
127 ).format(file_)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
128 )
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
129 self.host.quit(1)
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
130
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
131 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
132 extra = {}
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
133 if self.args.path:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
134 extra["path"] = self.args.path
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
135 if self.args.namespace:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
136 extra["namespace"] = self.args.namespace
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
137
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
138 if self.args.bz2:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
139 with tempfile.NamedTemporaryFile("wb", delete=False) as buf:
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
140 self.host.addOnQuitCallback(os.unlink, buf.name)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
141 self.disp(_("bz2 is an experimental option, use with caution"))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
142 # FIXME: check free space
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
143 self.disp(_("Starting compression, please wait..."))
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
144 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
145 bz2 = tarfile.open(mode="w:bz2", fileobj=buf)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
146 archive_name = "{}.tar.bz2".format(
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
147 os.path.basename(self.args.files[0]) or "compressed_files"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
148 )
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
149 for file_ in self.args.files:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
150 self.disp(_("Adding {}").format(file_), 1)
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
151 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
152 bz2.close()
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
153 self.disp(_("Done !"), 1)
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
154
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
155 self.host.bridge.fileSend(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
156 self.full_dest_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
157 buf.name,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
158 self.args.name or archive_name,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
159 "",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
160 extra,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
161 self.profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
162 callback=lambda pid, file_=buf.name: self.gotId(pid, file_),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
163 errback=self.error,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
164 )
0
goffi@necton2
parents:
diff changeset
165 else:
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
166 for file_ in self.args.files:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
167 path = os.path.abspath(file_)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
168 self.host.bridge.fileSend(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
169 self.full_dest_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
170 path,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
171 self.args.name,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
172 "",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
173 extra,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
174 self.profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
175 callback=lambda pid, file_=file_: self.gotId(pid, file_),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
176 errback=self.error,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
177 )
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
178
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
179
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
180 class Request(base.CommandBase):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
181 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
182 super(Request, self).__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
183 host,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
184 "request",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
185 use_progress=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
186 use_verbose=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
187 help=_("request a file from a contact"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
188 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
189 self.need_loop = True
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
190
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
191 @property
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
192 def filename(self):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
193 return self.args.name or self.args.hash or "output"
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
194
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
195 def add_parser_options(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
196 self.parser.add_argument(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
197 "jid", help=_("the destination jid")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
198 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
199 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
200 "-D",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
201 "--dest",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
202 help=_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
203 "destination path where the file will be saved (default: [current_dir]/[name|hash])"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
204 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
205 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
206 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
207 "-n",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
208 "--name",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
209 default="",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
210 help=_("name of the file"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
211 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
212 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
213 "-H",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
214 "--hash",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
215 default="",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
216 help=_("hash of the file"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
217 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
218 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
219 "-a",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
220 "--hash-algo",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
221 default="sha-256",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
222 help=_("hash algorithm use for --hash (default: sha-256)"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
223 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
224 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
225 "-d",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
226 "--path",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
227 help=("path to the directory containing the file"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
228 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
229 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
230 "-N",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
231 "--namespace",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
232 help=("namespace of the file"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
233 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
234 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
235 "-f",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
236 "--force",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
237 action="store_true",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
238 help=_("overwrite existing file without confirmation"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
239 )
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
240
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
241 def onProgressStarted(self, metadata):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
242 self.disp(_("File copy started"), 2)
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
243
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
244 def onProgressFinished(self, metadata):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
245 self.disp(_("File received successfully"), 2)
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
246
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
247 def onProgressError(self, error_msg):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
248 if error_msg == C.PROGRESS_ERROR_DECLINED:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
249 self.disp(_("The file request has been refused"))
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
250 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
251 self.disp(_("Error while requesting file: {}").format(error_msg), error=True)
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
252
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
253 def gotId(self, progress_id):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
254 """Called when a progress id has been received
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
255
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
256 @param progress_id(unicode): progress id
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
257 """
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
258 self.progress_id = progress_id
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
259
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
260 def error(self, failure):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
261 self.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
262 _("Error while trying to send a file: {reason}").format(reason=failure),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
263 error=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
264 )
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
265 self.host.quit(1)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
266
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
267 def start(self):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
268 if not self.args.name and not self.args.hash:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
269 self.parser.error(_("at least one of --name or --hash must be provided"))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
270 #  extra = dict(self.args.extra)
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
271 if self.args.dest:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
272 path = os.path.abspath(os.path.expanduser(self.args.dest))
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
273 if os.path.isdir(path):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
274 path = os.path.join(path, self.filename)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
275 else:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
276 path = os.path.abspath(self.filename)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
277
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
278 if os.path.exists(path) and not self.args.force:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
279 message = _("File {path} already exists! Do you want to overwrite?").format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
280 path=path
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
281 )
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
282 confirm = input("{} (y/N) ".format(message).encode("utf-8"))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
283 if confirm not in ("y", "Y"):
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
284 self.disp(_("file request cancelled"))
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
285 self.host.quit(2)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
286
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
287 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
288 extra = {}
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
289 if self.args.path:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
290 extra["path"] = self.args.path
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
291 if self.args.namespace:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
292 extra["namespace"] = self.args.namespace
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
293 self.host.bridge.fileJingleRequest(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
294 self.full_dest_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
295 path,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
296 self.args.name,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
297 self.args.hash,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
298 self.args.hash_algo if self.args.hash else "",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
299 extra,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
300 self.profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
301 callback=self.gotId,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
302 errback=partial(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
303 self.errback,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
304 msg=_("can't request file: {}"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
305 exit_code=C.EXIT_BRIDGE_ERRBACK,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
306 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
307 )
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
308
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
309
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
310 class Receive(base.CommandAnswering):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
311 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
312 super(Receive, self).__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
313 host,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
314 "receive",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
315 use_progress=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
316 use_verbose=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
317 help=_("wait for a file to be sent by a contact"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
318 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
319 self._overwrite_refused = False # True when one overwrite as already been refused
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
320 self.action_callbacks = {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
321 C.META_TYPE_FILE: self.onFileAction,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
322 C.META_TYPE_OVERWRITE: self.onOverwriteAction,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
323 }
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
324
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
325 def onProgressStarted(self, metadata):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
326 self.disp(_("File copy started"), 2)
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
327
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
328 def onProgressFinished(self, metadata):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
329 self.disp(_("File received successfully"), 2)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
330 if metadata.get("hash_verified", False):
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
331 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
332 self.disp(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
333 _("hash checked: {algo}:{checksum}").format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
334 algo=metadata["hash_algo"], checksum=metadata["hash"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
335 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
336 1,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
337 )
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
338 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
339 self.disp(_("hash is checked but hash value is missing", 1), 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
340 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
341 self.disp(_("hash can't be verified"), 1)
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
342
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
343 def onProgressError(self, error_msg):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
344 self.disp(_("Error while receiving 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
345
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
346 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
347 # 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
348 # 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
349 # 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
350 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
351 xml_ui = action_data["xmlui"]
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
352 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
353 self.disp(_("Action has no XMLUI"), 1)
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
354 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
355 ui = ET.fromstring(xml_ui.encode("utf-8"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
356 xmlui_id = ui.get("submit")
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
357 if not xmlui_id:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
358 self.disp(_("Invalid XMLUI received"), error=True)
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
359 return xmlui_id
0
goffi@necton2
parents:
diff changeset
360
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
361 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
362 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
363 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
364 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
365 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
366 from_jid = jid.JID(action_data["meta_from_jid"])
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
367 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
368 self.disp(_("Ignoring action without from_jid data"), 1)
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
369 return
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
370 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
371 progress_id = action_data["meta_progress_id"]
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
372 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
373 self.disp(_("ignoring action without progress id"), 1)
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
374 return
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
375
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
376 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
377 if self._overwrite_refused:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
378 self.disp(_("File refused because overwrite is needed"), error=True)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
379 self.host.bridge.launchAction(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
380 xmlui_id, {"cancelled": C.BOOL_TRUE}, profile_key=profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
381 )
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
382 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
383 self.progress_id = progress_id
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
384 xmlui_data = {"path": self.path}
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
385 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
386
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
387 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
388 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
389 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
390 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
391 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
392 progress_id = action_data["meta_progress_id"]
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
393 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
394 self.disp(_("ignoring action without progress id"), 1)
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
395 return
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
396 self.disp(_("Overwriting needed"), 1)
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
397
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
398 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
399 if self.args.force:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
400 self.disp(_("Overwrite accepted"), 2)
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
401 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
402 self.disp(_("Refused to overwrite"), 2)
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
403 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
404
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
405 xmlui_data = {"answer": C.boolConst(self.args.force)}
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
406 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
407
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
408 def add_parser_options(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
409 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
410 "jids",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
411 nargs="*",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
412 help=_("jids accepted (accept everything if none is specified)"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
413 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
414 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
415 "-m",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
416 "--multiple",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
417 action="store_true",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
418 help=_("accept multiple files (you'll have to stop manually)"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
419 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
420 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
421 "-f",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
422 "--force",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
423 action="store_true",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
424 help=_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
425 "force overwritting of existing files (/!\\ name is choosed by sender)"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
426 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
427 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
428 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
429 "--path",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
430 default=".",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
431 metavar="DIR",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
432 help=_("destination path (default: working directory)"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
433 )
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
434
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
435 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
436 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
437 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
438 if not os.path.isdir(self.path):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
439 self.disp(_("Given path is not a directory !", error=True))
1880
68944e270c40 jp (file): fixed bad quit call
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
440 self.host.quit(2)
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
441 if self.args.multiple:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
442 self.host.quit_on_progress_end = False
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
443 self.disp(_("waiting for incoming file request"), 2)
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
444
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
445
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
446 class Upload(base.CommandBase):
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
447 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
448 super(Upload, self).__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
449 host, "upload", use_progress=True, use_verbose=True, help=_("upload a file")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
450 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
451 self.need_loop = True
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
452
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
453 def add_parser_options(self):
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
454 self.parser.add_argument("file", type=str, help=_("file to upload"))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
455 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
456 "jid",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
457 nargs="?",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
458 help=_("jid of upload component (nothing to autodetect)"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
459 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
460 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
461 "--ignore-tls-errors",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
462 action="store_true",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
463 help=_("ignore invalide TLS certificate"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
464 )
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
465
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
466 def onProgressStarted(self, metadata):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
467 self.disp(_("File upload started"), 2)
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
468
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
469 def onProgressFinished(self, metadata):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
470 self.disp(_("File uploaded successfully"), 2)
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
471 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
472 url = metadata["url"]
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
473 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
474 self.disp("download URL not found in metadata")
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
475 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
476 self.disp(_("URL to retrieve the file:"), 1)
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
477 # 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
478 self.disp(url)
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
479
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
480 def onProgressError(self, error_msg):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
481 self.disp(_("Error while uploading file: {}").format(error_msg), error=True)
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
482
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
483 def gotId(self, data, file_):
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
484 """Called when a progress id has been received
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
485
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
486 @param pid(unicode): progress id
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
487 @param file_(str): file path
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
488 """
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
489 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
490 self.progress_id = data["progress"]
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
491 except KeyError:
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
492 # TODO: if 'xmlui' key is present, manage xmlui message display
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
493 self.disp(_("Can't upload file"), error=True)
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
494 self.host.quit(2)
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
495
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
496 def error(self, failure):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
497 self.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
498 _("Error while trying to upload a file: {reason}").format(reason=failure),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
499 error=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
500 )
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
501 self.host.quit(1)
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
502
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
503 def start(self):
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
504 file_ = self.args.file
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
505 if not os.path.exists(file_):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
506 self.disp(_("file [{}] doesn't exist !").format(file_), error=True)
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
507 self.host.quit(1)
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
508 if os.path.isdir(file_):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
509 self.disp(_("[{}] is a dir! Can't upload a dir").format(file_))
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
510 self.host.quit(1)
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
511
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
512 self.full_dest_jid = (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
513 self.host.get_full_jid(self.args.jid) if self.args.jid is not None else ""
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
514 )
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
515 options = {}
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
516 if self.args.ignore_tls_errors:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
517 options["ignore_tls_errors"] = C.BOOL_TRUE
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
518
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
519 path = os.path.abspath(file_)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
520 self.host.bridge.fileUpload(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
521 path,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
522 "",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
523 self.full_dest_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
524 options,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
525 self.profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
526 callback=lambda pid, file_=file_: self.gotId(pid, file_),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
527 errback=self.error,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
528 )
1643
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
529
17f9b911899a jp (file): new file/upload command
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
530
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
531 class ShareList(base.CommandBase):
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
532 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
533 extra_outputs = {"default": self.default_output}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
534 super(ShareList, self).__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
535 host,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
536 "list",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
537 use_output=C.OUTPUT_LIST_DICT,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
538 extra_outputs=extra_outputs,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
539 help=_("retrieve files shared by an entity"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
540 use_verbose=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
541 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
542 self.need_loop = True
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
543
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
544 def add_parser_options(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
545 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
546 "-d",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
547 "--path",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
548 default="",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
549 help=_("path to the directory containing the files"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
550 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
551 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
552 "jid",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
553 nargs="?",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
554 default="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
555 help=_("jid of sharing entity (nothing to check our own jid)"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
556 )
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
557
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
558 def file_gen(self, files_data):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
559 for file_data in files_data:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
560 yield file_data["name"]
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
561 yield file_data.get("size", "")
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
562 yield file_data.get("hash", "")
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
563
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
564 def _name_filter(self, name, row):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
565 if row.type == C.FILE_TYPE_DIRECTORY:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
566 return A.color(C.A_DIRECTORY, name)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
567 elif row.type == C.FILE_TYPE_FILE:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
568 return A.color(C.A_FILE, name)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
569 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
570 self.disp(_("unknown file type: {type}").format(type=row.type), error=True)
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
571 return name
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
572
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
573 def _size_filter(self, size, row):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
574 if not size:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
575 return ""
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
576 size = int(size)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
577 #  cf. https://stackoverflow.com/a/1094933 (thanks)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
578 suffix = "o"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
579 for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]:
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
580 if abs(size) < 1024.0:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
581 return A.color(A.BOLD, "{:.2f}".format(size), unit, suffix)
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
582 size /= 1024.0
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
583
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
584 return A.color(A.BOLD, "{:.2f}".format(size), "Yi", suffix)
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
585
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
586 def default_output(self, files_data):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
587 """display files a way similar to ls"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
588 files_data.sort(key=lambda d: d["name"].lower())
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
589 show_header = False
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
590 if self.verbosity == 0:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
591 headers = ("name", "type")
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
592 elif self.verbosity == 1:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
593 headers = ("name", "type", "size")
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
594 elif self.verbosity > 1:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
595 show_header = True
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
596 headers = ("name", "type", "size", "hash")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
597 table = common.Table.fromDict(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
598 self.host,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
599 files_data,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
600 headers,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
601 filters={"name": self._name_filter, "size": self._size_filter},
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
602 defaults={"size": "", "hash": ""},
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
603 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
604 table.display_blank(show_header=show_header, hide_cols=["type"])
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
605
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
606 def _FISListCb(self, files_data):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
607 self.output(files_data)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
608 self.host.quit()
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
609
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
610 def start(self):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
611 self.host.bridge.FISList(
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
612 self.args.jid,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
613 self.args.path,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
614 {},
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
615 self.profile,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
616 callback=self._FISListCb,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
617 errback=partial(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
618 self.errback,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
619 msg=_("can't retrieve shared files: {}"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
620 exit_code=C.EXIT_BRIDGE_ERRBACK,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
621 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
622 )
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
623
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
624
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
625 class SharePath(base.CommandBase):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
626 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
627 super(SharePath, self).__init__(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
628 host, "path", help=_("share a file or directory"), use_verbose=True
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
629 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
630 self.need_loop = True
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
631
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
632 def add_parser_options(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
633 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
634 "-n",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
635 "--name",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
636 default="",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
637 help=_("virtual name to use (default: use directory/file name)"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
638 )
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
639 perm_group = self.parser.add_mutually_exclusive_group()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
640 perm_group.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
641 "-j",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
642 "--jid",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
643 action="append",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
644 dest="jids",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
645 default=[],
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
646 help=_("jid of contacts allowed to retrieve the files"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
647 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
648 perm_group.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
649 "--public",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
650 action="store_true",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
651 help=_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
652 "share publicly the file(s) (/!\\ *everybody* will be able to access them)"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
653 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
654 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
655 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
656 "path",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
657 help=_("path to a file or directory to share"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
658 )
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
659
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
660 def _FISSharePathCb(self, name):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
661 self.disp(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
662 _('{path} shared under the name "{name}"').format(path=self.path, name=name)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
663 )
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
664 self.host.quit()
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
665
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
666 def start(self):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
667 self.path = os.path.abspath(self.args.path)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
668 if self.args.public:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
669 access = {"read": {"type": "public"}}
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
670 else:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
671 jids = self.args.jids
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
672 if jids:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
673 access = {"read": {"type": "whitelist", "jids": jids}}
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
674 else:
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
675 access = {}
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
676 self.host.bridge.FISSharePath(
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
677 self.args.name,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
678 self.path,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
679 json.dumps(access, ensure_ascii=False),
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
680 self.profile,
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
681 callback=self._FISSharePathCb,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
682 errback=partial(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
683 self.errback,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
684 msg=_("can't share path: {}"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
685 exit_code=C.EXIT_BRIDGE_ERRBACK,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
686 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
687 )
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
688
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
689
2916
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
690 class ShareInvite(base.CommandBase):
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
691 def __init__(self, host):
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
692 super(ShareInvite, self).__init__(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
693 host, "invite", help=_("send invitation for a shared repository")
2916
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
694 )
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
695 self.need_loop = True
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
696
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
697 def add_parser_options(self):
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
698 self.parser.add_argument(
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
699 "-n",
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
700 "--name",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
701 default="",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
702 help=_("name of the repository"),
2916
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
703 )
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
704 self.parser.add_argument(
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
705 "-N",
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
706 "--namespace",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
707 default="",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
708 help=_("namespace of the repository"),
2916
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
709 )
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
710 self.parser.add_argument(
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
711 "-P",
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
712 "--path",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
713 help=_("path to the repository"),
2916
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
714 )
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
715 self.parser.add_argument(
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
716 "-t",
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
717 "--type",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
718 choices=["files", "photos"],
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
719 default="files",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
720 help=_("type of the repository"),
2916
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
721 )
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
722 self.parser.add_argument(
2932
472fadadefe6 jp (file/invite): added a --thumbnail argument
Goffi <goffi@goffi.org>
parents: 2916
diff changeset
723 "-T",
472fadadefe6 jp (file/invite): added a --thumbnail argument
Goffi <goffi@goffi.org>
parents: 2916
diff changeset
724 "--thumbnail",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
725 help=_("https URL of a image to use as thumbnail"),
2932
472fadadefe6 jp (file/invite): added a --thumbnail argument
Goffi <goffi@goffi.org>
parents: 2916
diff changeset
726 )
472fadadefe6 jp (file/invite): added a --thumbnail argument
Goffi <goffi@goffi.org>
parents: 2916
diff changeset
727 self.parser.add_argument(
2916
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
728 "service",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
729 help=_("jid of the file sharing service hosting the repository"),
2916
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
730 )
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
731 self.parser.add_argument(
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
732 "jid",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
733 help=_("jid of the person to invite"),
2916
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
734 )
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
735
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
736 def _FISInviteCb(self):
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
737 self.disp(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
738 _('invitation sent to {entity}').format(entity=self.args.jid)
2916
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
739 )
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
740 self.host.quit()
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
741
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
742 def start(self):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
743 self.path = os.path.normpath(self.args.path) if self.args.path else ""
2932
472fadadefe6 jp (file/invite): added a --thumbnail argument
Goffi <goffi@goffi.org>
parents: 2916
diff changeset
744 extra = {}
472fadadefe6 jp (file/invite): added a --thumbnail argument
Goffi <goffi@goffi.org>
parents: 2916
diff changeset
745 if self.args.thumbnail is not None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
746 if not self.args.thumbnail.startswith('http'):
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
747 self.parser.error(_("only http(s) links are allowed with --thumbnail"))
2932
472fadadefe6 jp (file/invite): added a --thumbnail argument
Goffi <goffi@goffi.org>
parents: 2916
diff changeset
748 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
749 extra['thumb_url'] = self.args.thumbnail
2916
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
750 self.host.bridge.FISInvite(
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
751 self.args.jid,
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
752 self.args.service,
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
753 self.args.type,
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
754 self.args.namespace,
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
755 self.path,
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
756 self.args.name,
2932
472fadadefe6 jp (file/invite): added a --thumbnail argument
Goffi <goffi@goffi.org>
parents: 2916
diff changeset
757 data_format.serialise(extra),
2916
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
758 self.profile,
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
759 callback=self._FISInviteCb,
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
760 errback=partial(
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
761 self.errback,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
762 msg=_("can't send invitation: {}"),
2916
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
763 exit_code=C.EXIT_BRIDGE_ERRBACK,
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
764 ),
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
765 )
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
766
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
767
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
768 class Share(base.CommandBase):
2916
0b9faea5cb58 jp (file/share): added invite command to invite an entity to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
769 subcommands = (ShareList, SharePath, ShareInvite)
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
770
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
771 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
772 super(Share, self).__init__(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
773 host, "share", use_profile=False, help=_("files sharing management")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
774 )
2505
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
775
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
776
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
777 class File(base.CommandBase):
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
778 subcommands = (Send, Request, Receive, Upload, Share)
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
779
8e770ac05b0c jp (file): file sharing + improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
780 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
781 super(File, self).__init__(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2939
diff changeset
782 host, "file", use_profile=False, help=_("files sending/receiving/management")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2592
diff changeset
783 )