Mercurial > libervia-backend
annotate sat_frontends/jp/cmd_avatar.py @ 3916:40d47cc29ea4
plugin XEP-0300: type hints
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 06 Oct 2022 15:19:08 +0200 |
parents | 82e616b70a2a |
children | 524856bd7b19 |
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 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # jp: a SAT command line tool |
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 | |
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 | 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 | 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() |
3626
82e616b70a2a
CLI: fix section used for config following name change
Goffi <goffi@goffi.org>
parents:
3568
diff
changeset
|
52 cmd = config.getConfig(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: | |
3254
6cf4bd6972c2
core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
75 avatar_data_raw = await self.host.bridge.avatarGet( |
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 |
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 | 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 | 98 |
99 class Set(base.CommandBase): | |
100 def __init__(self, host): | |
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 | 104 ) |
105 | |
106 def add_parser_options(self): | |
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 | 110 "image_path", type=str, help=_("path to the image to upload") |
111 ) | |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 |
3040 | 113 async def start(self): |
114 path = self.args.image_path | |
115 if not os.path.exists(path): | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
116 self.disp(_("file {path} doesn't exist!").format(path=repr(path)), error=True) |
3040 | 117 self.host.quit(C.EXIT_BAD_ARG) |
118 path = os.path.abspath(path) | |
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 | 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() | |
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 | 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 ) |