comparison libervia/cli/cmd_avatar.py @ 4075:47401850dec6

refactoring: rename `libervia.frontends.jp` to `libervia.cli`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 14:54:26 +0200
parents libervia/frontends/jp/cmd_avatar.py@26b7ed2817da
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4074:26b7ed2817da 4075:47401850dec6
1 #!/usr/bin/env python3
2
3
4 # Libervia CLI
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 import os
22 import os.path
23 import asyncio
24 from . import base
25 from libervia.backend.core.i18n import _
26 from libervia.cli.constants import Const as C
27 from libervia.backend.tools import config
28 from libervia.backend.tools.common import data_format
29
30
31 __commands__ = ["Avatar"]
32 DISPLAY_CMD = ["xdg-open", "xv", "display", "gwenview", "showtell"]
33
34
35 class Get(base.CommandBase):
36 def __init__(self, host):
37 super(Get, self).__init__(
38 host, "get", use_verbose=True, help=_("retrieve avatar of an entity")
39 )
40
41 def add_parser_options(self):
42 self.parser.add_argument(
43 "--no-cache", action="store_true", help=_("do no use cached values")
44 )
45 self.parser.add_argument(
46 "-s", "--show", action="store_true", help=_("show avatar")
47 )
48 self.parser.add_argument("jid", nargs='?', default='', help=_("entity"))
49
50 async def show_image(self, path):
51 sat_conf = config.parse_main_conf()
52 cmd = config.config_get(sat_conf, C.CONFIG_SECTION, "image_cmd")
53 cmds = [cmd] + DISPLAY_CMD if cmd else DISPLAY_CMD
54 for cmd in cmds:
55 try:
56 process = await asyncio.create_subprocess_exec(cmd, path)
57 ret = await process.wait()
58 except OSError:
59 continue
60
61 if ret in (0, 2):
62 # we can get exit code 2 with display when stopping it with C-c
63 break
64 else:
65 # didn't worked with commands, we try our luck with webbrowser
66 # in some cases, webbrowser can actually open the associated display program.
67 # Note that this may be possibly blocking, depending on the platform and
68 # available browser
69 import webbrowser
70
71 webbrowser.open(path)
72
73 async def start(self):
74 try:
75 avatar_data_raw = await self.host.bridge.avatar_get(
76 self.args.jid,
77 not self.args.no_cache,
78 self.profile,
79 )
80 except Exception as e:
81 self.disp(f"can't retrieve avatar: {e}", error=True)
82 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
83
84 avatar_data = data_format.deserialise(avatar_data_raw, type_check=None)
85
86 if not avatar_data:
87 self.disp(_("No avatar found."), 1)
88 self.host.quit(C.EXIT_NOT_FOUND)
89
90 avatar_path = avatar_data['path']
91
92 self.disp(avatar_path)
93 if self.args.show:
94 await self.show_image(avatar_path)
95
96 self.host.quit()
97
98
99 class Set(base.CommandBase):
100 def __init__(self, host):
101 super(Set, self).__init__(
102 host, "set", use_verbose=True,
103 help=_("set avatar of the profile or an entity")
104 )
105
106 def add_parser_options(self):
107 self.parser.add_argument(
108 "-j", "--jid", default='', help=_("entity whose avatar must be changed"))
109 self.parser.add_argument(
110 "image_path", type=str, help=_("path to the image to upload")
111 )
112
113 async def start(self):
114 path = self.args.image_path
115 if not os.path.exists(path):
116 self.disp(_("file {path} doesn't exist!").format(path=repr(path)), error=True)
117 self.host.quit(C.EXIT_BAD_ARG)
118 path = os.path.abspath(path)
119 try:
120 await self.host.bridge.avatar_set(path, self.args.jid, self.profile)
121 except Exception as e:
122 self.disp(f"can't set avatar: {e}", error=True)
123 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
124 else:
125 self.disp(_("avatar has been set"), 1)
126 self.host.quit()
127
128
129 class Avatar(base.CommandBase):
130 subcommands = (Get, Set)
131
132 def __init__(self, host):
133 super(Avatar, self).__init__(
134 host, "avatar", use_profile=False, help=_("avatar uploading/retrieving")
135 )