annotate libervia/cli/cmd_avatar.py @ 4307:6a0155f410bd

test (unit): add test for plugin XEP-0033: those replace the legacy XEP-0033 test from libervia/backend/test/test_plugin_xep_0033.py. rel 450
author Goffi <goffi@goffi.org>
date Thu, 26 Sep 2024 16:12:01 +0200
parents 0d7bb4df2343
children
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
4075
47401850dec6 refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents: 4074
diff changeset
4 # Libervia CLI
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
5 # Copyright (C) 2009-2021 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
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
25 from libervia.backend.core.i18n import _
4075
47401850dec6 refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents: 4074
diff changeset
26 from libervia.cli.constants import Const as C
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
27 from libervia.backend.tools import config
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
28 from libervia.backend.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 )
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
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
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3626
diff changeset
50 async def show_image(self, path):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3626
diff changeset
51 sat_conf = config.parse_main_conf()
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3626
diff changeset
52 cmd = config.config_get(sat_conf, C.CONFIG_SECTION, "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:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3626
diff changeset
75 avatar_data_raw = await self.host.bridge.avatar_get(
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
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
90 avatar_path = avatar_data["path"]
3254
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:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3626
diff changeset
94 await self.show_image(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__(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
102 host,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
103 "set",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
104 use_verbose=True,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
105 help=_("set avatar of the profile or an entity"),
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
106 )
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
107
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
108 def add_parser_options(self):
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
109 self.parser.add_argument(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
110 "-j", "--jid", default="", help=_("entity whose avatar must be changed")
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
111 )
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
112 self.parser.add_argument(
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
113 "image_path", type=str, help=_("path to the image to upload")
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
114 )
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
115
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
116 async def start(self):
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
117 path = self.args.image_path
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
118 if not os.path.exists(path):
3568
04283582966f core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
119 self.disp(_("file {path} doesn't exist!").format(path=repr(path)), error=True)
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
120 self.host.quit(C.EXIT_BAD_ARG)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
121 path = os.path.abspath(path)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
122 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3626
diff changeset
123 await self.host.bridge.avatar_set(path, self.args.jid, self.profile)
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
124 except Exception as e:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
125 self.disp(f"can't set avatar: {e}", error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
126 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
127 else:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
128 self.disp(_("avatar has been set"), 1)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
129 self.host.quit()
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
130
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 class Avatar(base.CommandBase):
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
133 subcommands = (Get, Set)
2074
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
134
db5cda61740f jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
135 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
136 super(Avatar, self).__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
137 host, "avatar", use_profile=False, help=_("avatar uploading/retrieving")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
138 )