Mercurial > libervia-backend
annotate sat_frontends/jp/cmd_avatar.py @ 3208:4f00fdf11445
bridge (constructor/dbus): typo
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 06 Mar 2020 18:19:03 +0100 |
parents | 559a625a236b |
children | 6cf4bd6972c2 |
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 |
3136 | 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 | 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 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 |
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 __commands__ = ["Avatar"] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
31 DISPLAY_CMD = ["xv", "display", "gwenview", "showtell"] |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 |
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 class Get(base.CommandBase): |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
36 super(Get, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
37 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
|
38 ) |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 def add_parser_options(self): |
3028 | 41 self.parser.add_argument("jid", help=_("entity")) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
42 self.parser.add_argument( |
3028 | 43 "-s", "--show", action="store_true", help=_("show avatar") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
44 ) |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 |
3040 | 46 async def showImage(self, path): |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 sat_conf = config.parseMainConf() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
48 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
|
49 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
|
50 for cmd in cmds: |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 try: |
3040 | 52 process = await asyncio.create_subprocess_exec(cmd, path) |
53 ret = await process.wait() | |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 except OSError: |
3040 | 55 continue |
56 | |
57 if ret in (0, 2): | |
58 # we can get exit code 2 with display when stopping it with C-c | |
59 break | |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 else: |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 # didn't worked with commands, we try our luck with webbrowser |
3040 | 62 # in some cases, webbrowser can actually open the associated display program. |
63 # Note that this may be possibly blocking, depending on the platform and | |
64 # available browser | |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 import webbrowser |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
66 |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 webbrowser.open(path) |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 |
3040 | 69 async def start(self): |
70 try: | |
71 avatar_path = await self.host.bridge.avatarGet( | |
72 self.args.jid, | |
73 False, | |
74 False, | |
75 self.profile, | |
76 ) | |
77 except Exception as e: | |
78 self.disp(f"can't retrieve avatar: {e}", error=True) | |
79 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
80 | |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 if not avatar_path: |
3028 | 82 self.disp(_("No avatar found."), 1) |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 self.host.quit(C.EXIT_NOT_FOUND) |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 self.disp(avatar_path) |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 if self.args.show: |
3040 | 87 await self.showImage(avatar_path) |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 self.host.quit() |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 |
3040 | 91 |
92 class Set(base.CommandBase): | |
93 def __init__(self, host): | |
94 super(Set, self).__init__( | |
95 host, "set", use_verbose=True, help=_("set avatar of the profile") | |
96 ) | |
97 | |
98 def add_parser_options(self): | |
99 self.parser.add_argument( | |
100 "image_path", type=str, help=_("path to the image to upload") | |
101 ) | |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 |
3040 | 103 async def start(self): |
104 path = self.args.image_path | |
105 if not os.path.exists(path): | |
106 self.disp(_(f"file {path!r} doesn't exist!"), error=True) | |
107 self.host.quit(C.EXIT_BAD_ARG) | |
108 path = os.path.abspath(path) | |
109 try: | |
110 await self.host.bridge.avatarSet(path, self.profile) | |
111 except Exception as e: | |
112 self.disp(f"can't set avatar: {e}", error=True) | |
113 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
114 else: | |
115 self.disp(_("avatar has been set"), 1) | |
116 self.host.quit() | |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 class Avatar(base.CommandBase): |
3040 | 120 subcommands = (Get, Set) |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
123 super(Avatar, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
124 host, "avatar", use_profile=False, help=_("avatar uploading/retrieving") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
125 ) |