Mercurial > libervia-backend
annotate sat_frontends/jp/cmd_avatar.py @ 2886:b06cb71079fa
core (xmpp): new networkEnabled() and networkDisabled() methods:
those methods can be called by platform specific plugins when network is known to be (un)available. This way, connection attempts can be cancelled when no network is available, saving resources (notably battery on mobile devices), or attempts can be restarted immediately when network is known to be available again.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 06 Apr 2019 19:05:57 +0200 |
parents | 7e8ca7c1429a |
children | ab2696e34d29 |
rev | line source |
---|---|
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python2 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
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 |
2771 | 5 # Copyright (C) 2009-2019 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 base |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 import os |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 import os.path |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from sat.core.i18n import _ |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 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
|
26 from sat.tools import config |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 import subprocess |
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 Set(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(Set, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
37 host, "set", use_verbose=True, help=_("set avatar of the profile") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
38 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
39 self.need_loop = True |
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): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
42 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
43 "image_path", type=str, help=_("path to the image to upload") |
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 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 def start(self): |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 """Send files to jabber contact""" |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 path = self.args.image_path |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 if not os.path.exists(path): |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 self.disp(_(u"file [{}] doesn't exist !").format(path), error=True) |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 self.host.quit(1) |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 path = os.path.abspath(path) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
53 self.host.bridge.avatarSet( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
54 path, self.profile, callback=self._avatarCb, errback=self._avatarEb |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
55 ) |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 def _avatarCb(self): |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 self.disp(_("avatar has been set"), 1) |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 self.host.quit() |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 def _avatarEb(self, failure_): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
62 self.disp( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
63 _("error while uploading avatar: {msg}").format(msg=failure_), error=True |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
64 ) |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 self.host.quit(C.EXIT_ERROR) |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 class Get(base.CommandBase): |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
70 super(Get, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
71 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
|
72 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
73 self.need_loop = True |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 def add_parser_options(self): |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 self.parser.add_argument("jid", type=base.unicode_decoder, help=_("entity")) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
77 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
78 "-s", "--show", action="store_true", help=_(u"show avatar") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
79 ) |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 def showImage(self, path): |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 sat_conf = config.parseMainConf() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
83 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
|
84 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
|
85 for cmd in cmds: |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 try: |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 ret = subprocess.call([cmd] + [path]) |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 except OSError: |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 pass |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 else: |
2883
7e8ca7c1429a
jp (avatar/get): fixed avatar being displayed 2 times in a corner case:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
91 if ret in (0, 2): |
7e8ca7c1429a
jp (avatar/get): fixed avatar being displayed 2 times in a corner case:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
92 # we can get exit code 2 with display when stopping it with C-c |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 break |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 else: |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 # didn't worked with commands, we try our luck with webbrowser |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 # in some cases, webbrowser can actually open the associated display program |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 import webbrowser |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
98 |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 webbrowser.open(path) |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2074
diff
changeset
|
101 def _avatarGetCb(self, avatar_path): |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 if not avatar_path: |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 self.disp(_(u"No avatar found."), 1) |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 self.host.quit(C.EXIT_NOT_FOUND) |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 self.disp(avatar_path) |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 if self.args.show: |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 self.showImage(avatar_path) |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 self.host.quit() |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2074
diff
changeset
|
112 def _avatarGetEb(self, failure_): |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 self.disp(_("error while getting avatar: {msg}").format(msg=failure_), error=True) |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 self.host.quit(C.EXIT_ERROR) |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 def start(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
117 self.host.bridge.avatarGet( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
118 self.args.jid, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
119 False, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
120 False, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
121 self.profile, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
122 callback=self._avatarGetCb, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
123 errback=self._avatarGetEb, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
124 ) |
2074
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 class Avatar(base.CommandBase): |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 subcommands = (Set, Get) |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 |
db5cda61740f
jp (avatar): avatar get/set implementations, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
131 super(Avatar, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
132 host, "avatar", use_profile=False, help=_("avatar uploading/retrieving") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
133 ) |