comparison sat_frontends/jp/cmd_profile.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents frontends/src/jp/cmd_profile.py@fcf0ae8102b8
children 378188abe941
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # jp: a SAT command line tool
5 # Copyright (C) 2009-2018 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 """This module permits to manage profiles. It can list, create, delete
21 and retrieve information about a profile."""
22
23 from sat_frontends.jp.constants import Const as C
24 from sat.core.log import getLogger
25 log = getLogger(__name__)
26 from sat.core.i18n import _
27 from sat_frontends.jp import base
28 from functools import partial
29
30 __commands__ = ["Profile"]
31
32 PROFILE_HELP = _('The name of the profile')
33
34
35 class ProfileConnect(base.CommandBase):
36 """Dummy command to use profile_session parent, i.e. to be able to connect without doing anything else"""
37
38 def __init__(self, host):
39 # it's weird to have a command named "connect" with need_connect=False, but it can be handy to be able
40 # to launch just the session, so some paradoxes don't hurt
41 super(ProfileConnect, self).__init__(host, 'connect', need_connect=False, help=(u'connect a profile'))
42
43 def add_parser_options(self):
44 pass
45
46
47 class ProfileDisconnect(base.CommandBase):
48
49 def __init__(self, host):
50 super(ProfileDisconnect, self).__init__(host, 'disconnect', need_connect=False, help=(u'disconnect a profile'))
51 self.need_loop = True
52
53 def add_parser_options(self):
54 pass
55
56 def start(self):
57 self.host.bridge.disconnect(self.args.profile, callback=self.host.quit)
58
59
60 class ProfileDefault(base.CommandBase):
61 def __init__(self, host):
62 super(ProfileDefault, self).__init__(host, 'default', use_profile=False, help=(u'print default profile'))
63
64 def add_parser_options(self):
65 pass
66
67 def start(self):
68 print self.host.bridge.profileNameGet('@DEFAULT@')
69
70
71 class ProfileDelete(base.CommandBase):
72 def __init__(self, host):
73 super(ProfileDelete, self).__init__(host, 'delete', use_profile=False, help=(u'delete a profile'))
74
75 def add_parser_options(self):
76 self.parser.add_argument('profile', type=str, help=PROFILE_HELP)
77 self.parser.add_argument('-f', '--force', action='store_true', help=_(u'delete profile without confirmation'))
78
79 def start(self):
80 if self.args.profile not in self.host.bridge.profilesListGet():
81 log.error("Profile %s doesn't exist." % self.args.profile)
82 self.host.quit(1)
83 if not self.args.force:
84 message = u"Are you sure to delete profile [{}] ?".format(self.args.profile)
85 res = raw_input("{} (y/N)? ".format(message))
86 if res not in ("y", "Y"):
87 self.disp(_(u"Profile deletion cancelled"))
88 self.host.quit(2)
89
90 self.host.bridge.asyncDeleteProfile(self.args.profile, callback=lambda dummy: None)
91
92
93 class ProfileInfo(base.CommandBase):
94 def __init__(self, host):
95 super(ProfileInfo, self).__init__(host, 'info', need_connect=False, help=_(u'get information about a profile'))
96 self.need_loop = True
97 self.to_show = [(_(u"jid"), "Connection", "JabberID"),]
98 self.largest = max([len(item[0]) for item in self.to_show])
99
100
101 def add_parser_options(self):
102 self.parser.add_argument('--show-password', action='store_true', help=_(u'show the XMPP password IN CLEAR TEXT'))
103
104 def showNextValue(self, label=None, category=None, value=None):
105 """Show next value from self.to_show and quit on last one"""
106 if label is not None:
107 print((u"{label:<"+unicode(self.largest+2)+"}{value}").format(label=label+": ", value=value))
108 try:
109 label, category, name = self.to_show.pop(0)
110 except IndexError:
111 self.host.quit()
112 else:
113 self.host.bridge.asyncGetParamA(name, category, profile_key=self.host.profile,
114 callback=lambda value: self.showNextValue(label, category, value))
115
116 def start(self):
117 if self.args.show_password:
118 self.to_show.append((_(u"XMPP password"), "Connection", "Password"))
119 self.showNextValue()
120
121
122 class ProfileList(base.CommandBase):
123 def __init__(self, host):
124 super(ProfileList, self).__init__(host, 'list', use_profile=False, use_output='list', help=(u'list profiles'))
125
126 def add_parser_options(self):
127 group = self.parser.add_mutually_exclusive_group()
128 group.add_argument('-c', '--clients', action='store_true', help=_(u'get clients profiles only'))
129 group.add_argument('-C', '--components', action='store_true', help=(u'get components profiles only'))
130
131
132 def start(self):
133 if self.args.clients:
134 clients, components = True, False
135 elif self.args.components:
136 clients, components = False, True
137 else:
138 clients, components = True, True
139 self.output(self.host.bridge.profilesListGet(clients, components))
140
141
142 class ProfileCreate(base.CommandBase):
143 def __init__(self, host):
144 super(ProfileCreate, self).__init__(host, 'create', use_profile=False, help=(u'create a new profile'))
145 self.need_loop = True
146
147 def add_parser_options(self):
148 self.parser.add_argument('profile', type=str, help=_(u'the name of the profile'))
149 self.parser.add_argument('-p', '--password', type=str, default='', help=_(u'the password of the profile'))
150 self.parser.add_argument('-j', '--jid', type=str, help=_(u'the jid of the profile'))
151 self.parser.add_argument('-x', '--xmpp-password', type=str, help=_(u'the password of the XMPP account (use profile password if not specified)'),
152 metavar='PASSWORD')
153 self.parser.add_argument('-C', '--component', type=base.unicode_decoder, default='',
154 help=_(u'set to component import name (entry point) if this is a component'))
155
156 def _session_started(self, dummy):
157 if self.args.jid:
158 self.host.bridge.setParam("JabberID", self.args.jid, "Connection", profile_key=self.args.profile)
159 xmpp_pwd = self.args.password or self.args.xmpp_password
160 if xmpp_pwd:
161 self.host.bridge.setParam("Password", xmpp_pwd, "Connection", profile_key=self.args.profile)
162 self.host.quit()
163
164 def _profile_created(self):
165 self.host.bridge.profileStartSession(self.args.password, self.args.profile, callback=self._session_started, errback=None)
166
167 def start(self):
168 """Create a new profile"""
169 if self.args.profile in self.host.bridge.profilesListGet():
170 log.error("Profile %s already exists." % self.args.profile)
171 self.host.quit(1)
172 self.host.bridge.profileCreate(self.args.profile, self.args.password, self.args.component,
173 callback=self._profile_created,
174 errback=partial(self.errback,
175 msg=_(u"can't create profile: {}"),
176 exit_code=C.EXIT_BRIDGE_ERRBACK))
177
178
179 class ProfileModify(base.CommandBase):
180 def __init__(self, host):
181 super(ProfileModify, self).__init__(host, 'modify', need_connect=False, help=_(u'modify an existing profile'))
182
183 def add_parser_options(self):
184 profile_pwd_group = self.parser.add_mutually_exclusive_group()
185 profile_pwd_group.add_argument('-w', '--password', type=base.unicode_decoder, help=_(u'change the password of the profile'))
186 profile_pwd_group.add_argument('--disable-password', action='store_true', help=_(u'disable profile password (dangerous!)'))
187 self.parser.add_argument('-j', '--jid', type=base.unicode_decoder, help=_(u'the jid of the profile'))
188 self.parser.add_argument('-x', '--xmpp-password', type=base.unicode_decoder, help=_(u'change the password of the XMPP account'),
189 metavar='PASSWORD')
190 self.parser.add_argument('-D', '--default', action='store_true', help=_(u'set as default profile'))
191
192 def start(self):
193 if self.args.disable_password:
194 self.args.password = ''
195 if self.args.password is not None:
196 self.host.bridge.setParam("Password", self.args.password, "General", profile_key=self.host.profile)
197 if self.args.jid is not None:
198 self.host.bridge.setParam("JabberID", self.args.jid, "Connection", profile_key=self.host.profile)
199 if self.args.xmpp_password is not None:
200 self.host.bridge.setParam("Password", self.args.xmpp_password, "Connection", profile_key=self.host.profile)
201 if self.args.default:
202 self.host.bridge.profileSetDefault(self.host.profile)
203
204
205 class Profile(base.CommandBase):
206 subcommands = (ProfileConnect, ProfileDisconnect, ProfileCreate, ProfileDefault, ProfileDelete, ProfileInfo, ProfileList, ProfileModify)
207
208 def __init__(self, host):
209 super(Profile, self).__init__(host, 'profile', use_profile=False, help=_(u'profile commands'))