annotate sat_frontends/jp/cmd_avatar.py @ 3410:55f9a38864af

jp (file/receive): display or answer to C.META_TYPE_NOT_IN_ROSTER_LEAK dialog: when a file is proposed by an entity not in roster, the dialog asking for confirmation is displayed. If the bare jid of the sender is explicitely waited (i.e. it's in arguments), then the dialog is automatically confirmed.
author Goffi <goffi@goffi.org>
date Thu, 12 Nov 2020 14:53:16 +0100
parents 6cf4bd6972c2
children be6d91572633
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
1 #!/usr/bin/env python3
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # jp: a SAT command line tool
3136
9d0df638c8b4 dates update
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
5 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org)
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21 import os
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 import os.path
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
23 import asyncio
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
24 from . import base
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core.i18n import _
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from sat_frontends.jp.constants import Const as C
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from sat.tools import config
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
28 from sat.tools.common import data_format
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 __commands__ = ["Avatar"]
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
32 DISPLAY_CMD = ["xdg-open", "xv", "display", "gwenview", "showtell"]
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35 class Get(base.CommandBase):
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
37 super(Get, self).__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
38 host, "get", use_verbose=True, help=_("retrieve avatar of an entity")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
39 )
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41 def add_parser_options(self):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
42 self.parser.add_argument(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
43 "--no-cache", action="store_true", help=_("do no use cached values")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
44 )
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
45 self.parser.add_argument(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2883
diff changeset
46 "-s", "--show", action="store_true", help=_("show avatar")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
47 )
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
48 self.parser.add_argument("jid", nargs='?', default='', help=_("entity"))
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
50 async def showImage(self, path):
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 sat_conf = config.parseMainConf()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
52 cmd = config.getConfig(sat_conf, "jp", "image_cmd")
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 cmds = [cmd] + DISPLAY_CMD if cmd else DISPLAY_CMD
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 for cmd in cmds:
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 try:
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
56 process = await asyncio.create_subprocess_exec(cmd, path)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
57 ret = await process.wait()
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 except OSError:
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
59 continue
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
60
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
61 if ret in (0, 2):
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
62 # we can get exit code 2 with display when stopping it with C-c
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
63 break
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64 else:
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 # didn't worked with commands, we try our luck with webbrowser
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
66 # in some cases, webbrowser can actually open the associated display program.
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
67 # Note that this may be possibly blocking, depending on the platform and
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
68 # available browser
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69 import webbrowser
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
70
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71 webbrowser.open(path)
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
73 async def start(self):
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
74 try:
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
75 avatar_data_raw = await self.host.bridge.avatarGet(
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
76 self.args.jid,
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
77 not self.args.no_cache,
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
78 self.profile,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
79 )
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
80 except Exception as e:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
81 self.disp(f"can't retrieve avatar: {e}", error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
82 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
83
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
84 avatar_data = data_format.deserialise(avatar_data_raw, type_check=None)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
85
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
86 if not avatar_data:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2883
diff changeset
87 self.disp(_("No avatar found."), 1)
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
88 self.host.quit(C.EXIT_NOT_FOUND)
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
89
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
90 avatar_path = avatar_data['path']
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
91
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92 self.disp(avatar_path)
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
93 if self.args.show:
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
94 await self.showImage(avatar_path)
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
95
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96 self.host.quit()
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
98
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
99 class Set(base.CommandBase):
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
100 def __init__(self, host):
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
101 super(Set, self).__init__(
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
102 host, "set", use_verbose=True,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
103 help=_("set avatar of the profile or an entity")
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
104 )
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
105
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
106 def add_parser_options(self):
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
107 self.parser.add_argument(
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
108 "-j", "--jid", default='', help=_("entity whose avatar must be changed"))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
109 self.parser.add_argument(
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
110 "image_path", type=str, help=_("path to the image to upload")
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
111 )
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
112
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
113 async def start(self):
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
114 path = self.args.image_path
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
115 if not os.path.exists(path):
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
116 self.disp(_(f"file {path!r} doesn't exist!"), error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
117 self.host.quit(C.EXIT_BAD_ARG)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
118 path = os.path.abspath(path)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
119 try:
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
120 await self.host.bridge.avatarSet(path, self.args.jid, self.profile)
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
121 except Exception as e:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
122 self.disp(f"can't set avatar: {e}", error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
123 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
124 else:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
125 self.disp(_("avatar has been set"), 1)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
126 self.host.quit()
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
127
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
128
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
129 class Avatar(base.CommandBase):
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
130 subcommands = (Get, Set)
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
131
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
132 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
133 super(Avatar, self).__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
134 host, "avatar", use_profile=False, help=_("avatar uploading/retrieving")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
135 )