Mercurial > libervia-backend
annotate libervia/cli/cmd_avatar.py @ 4340:ea72364131d5 default tip @
doc (components): Update Email Gateway documentation:
A section has been added to explain how attachments are handled.
fix 453
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 03 Dec 2024 00:53:18 +0100 |
parents | 0d7bb4df2343 |
children |
rev | line source |
---|---|
3137 | 1 #!/usr/bin/env python3 |
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 | 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 | 23 import asyncio |
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 | 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 | 56 process = await asyncio.create_subprocess_exec(cmd, path) |
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 | 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 | |
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 | 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 | |
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 | 73 async def start(self): |
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 | 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 | 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 | |
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 | 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 | 98 |
99 class Set(base.CommandBase): | |
100 def __init__(self, host): | |
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 | 106 ) |
107 | |
108 def add_parser_options(self): | |
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 | 113 "image_path", type=str, help=_("path to the image to upload") |
114 ) | |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 |
3040 | 116 async def start(self): |
117 path = self.args.image_path | |
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 | 120 self.host.quit(C.EXIT_BAD_ARG) |
121 path = os.path.abspath(path) | |
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 | 124 except Exception as e: |
125 self.disp(f"can't set avatar: {e}", error=True) | |
126 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
127 else: | |
128 self.disp(_("avatar has been set"), 1) | |
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 | 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 ) |