annotate sat_frontends/jp/cmd_file.py @ 3080:16925f494820

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