comparison frontends/src/jp/cmd_avatar.py @ 2074:db5cda61740f

jp (avatar): avatar get/set implementations, first draft
author Goffi <goffi@goffi.org>
date Tue, 13 Sep 2016 22:19:47 +0200
parents
children c42aab22c2c0
comparison
equal deleted inserted replaced
2073:3d633458d962 2074:db5cda61740f
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # jp: a SAT command line tool
5 # Copyright (C) 2009-2016 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 base
22 import os
23 import os.path
24 from sat.core.i18n import _
25 from sat_frontends.jp.constants import Const as C
26 from sat.tools import config
27 import subprocess
28
29
30 __commands__ = ["Avatar"]
31 DISPLAY_CMD = ['xv', 'display', 'gwenview', 'showtell']
32
33
34 class Set(base.CommandBase):
35 def __init__(self, host):
36 super(Set, self).__init__(host, 'set', use_verbose=True, help=_('set avatar of the profile'))
37 self.need_loop=True
38
39 def add_parser_options(self):
40 self.parser.add_argument("image_path", type=str, help=_("path to the image to upload"))
41
42 def start(self):
43 """Send files to jabber contact"""
44 path = self.args.image_path
45 if not os.path.exists(path):
46 self.disp(_(u"file [{}] doesn't exist !").format(path), error=True)
47 self.host.quit(1)
48 path = os.path.abspath(path)
49 self.host.bridge.setAvatar(path, self.profile, callback=self._avatarCb, errback=self._avatarEb)
50
51 def _avatarCb(self):
52 self.disp(_("avatar has been set"), 1)
53 self.host.quit()
54
55 def _avatarEb(self, failure_):
56 self.disp(_("error while uploading avatar: {msg}").format(msg=failure_), error=True)
57 self.host.quit(C.EXIT_ERROR)
58
59
60 class Get(base.CommandBase):
61
62 def __init__(self, host):
63 super(Get, self).__init__(host, 'get', use_verbose=True, help=_('retrieve avatar of an entity'))
64 self.need_loop=True
65
66 def add_parser_options(self):
67 self.parser.add_argument("jid", type=base.unicode_decoder, help=_("entity"))
68 self.parser.add_argument("-s", "--show", action="store_true", help=_(u"show avatar"))
69
70 def showImage(self, path):
71 sat_conf = config.parseMainConf()
72 cmd = config.getConfig(sat_conf, 'jp', 'image_cmd')
73 cmds = [cmd] + DISPLAY_CMD if cmd else DISPLAY_CMD
74 for cmd in cmds:
75 try:
76 ret = subprocess.call([cmd] + [path])
77 except OSError:
78 pass
79 else:
80 if ret == 0:
81 break
82 else:
83 # didn't worked with commands, we try our luck with webbrowser
84 # in some cases, webbrowser can actually open the associated display program
85 import webbrowser
86 webbrowser.open(path)
87
88 def _getAvatarCb(self, avatar_path):
89 if not avatar_path:
90 self.disp(_(u"No avatar found."), 1)
91 self.host.quit(C.EXIT_NOT_FOUND)
92
93 self.disp(avatar_path)
94 if self.args.show:
95 self.showImage(avatar_path)
96
97 self.host.quit()
98
99 def _getAvatarEb(self, failure_):
100 self.disp(_("error while getting avatar: {msg}").format(msg=failure_), error=True)
101 self.host.quit(C.EXIT_ERROR)
102
103 def start(self):
104 self.host.bridge.getAvatar(self.args.jid, self.profile, callback=self._getAvatarCb, errback=self._getAvatarEb)
105
106
107 class Avatar(base.CommandBase):
108 subcommands = (Set, Get)
109
110 def __init__(self, host):
111 super(Avatar, self).__init__(host, 'avatar', use_profile=False, help=_('avatar uploading/retrieving'))