comparison frontends/src/jp/cmd_profile.py @ 1596:b7ee113183fc

jp: better profile commands: - new "profile/default" command - info doesn't show password anymore by default, need to be explicitly requested - info and modify don't need to connect anymore - modify can now set default profile. As use_profile is set, at least a profile session need to be started when it would not be mandatory technicaly (if just setting the profile as default is needed). But this option should not be used often, and it's not a big side effect, so I don't feel the need to create a new dedicated command, or to do complicated checks to avoid the session start.
author Goffi <goffi@goffi.org>
date Sat, 14 Nov 2015 19:18:10 +0100
parents e4e960d285b0
children 91a605feed8c
comparison
equal deleted inserted replaced
1595:a3d0cfa5b7a6 1596:b7ee113183fc
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 """This module permits to manage profiles. It can list, create, delete 20 """This module permits to manage profiles. It can list, create, delete
21 and retrieve information about a profile.""" 21 and retrieve information about a profile."""
22 22
23 from logging import debug, info, error, warning 23 import logging as log
24 from sat.core.i18n import _ 24 from sat.core.i18n import _
25 from sat_frontends.jp import base 25 from sat_frontends.jp import base
26 26
27 __commands__ = ["Profile"] 27 __commands__ = ["Profile"]
28 28
29 PROFILE_HELP = _('The name of the profile') 29 PROFILE_HELP = _('The name of the profile')
30 30
31 31
32 class ProfileDefault(base.CommandBase):
33 def __init__(self, host):
34 super(ProfileDefault, self).__init__(host, 'default', use_profile=False, help=_('print default profile'))
35
36 def add_parser_options(self):
37 pass
38
39 def run(self):
40 print self.host.bridge.getProfileName('@DEFAULT@')
41
42
32 class ProfileDelete(base.CommandBase): 43 class ProfileDelete(base.CommandBase):
33 def __init__(self, host): 44 def __init__(self, host):
34 super(ProfileDelete, self).__init__(host, 'delete', use_profile=False, help=_('Delete a profile')) 45 super(ProfileDelete, self).__init__(host, 'delete', use_profile=False, help=_('delete a profile'))
35 46
36 def add_parser_options(self): 47 def add_parser_options(self):
37 self.parser.add_argument('profile', type=str, help=PROFILE_HELP) 48 self.parser.add_argument('profile', type=str, help=PROFILE_HELP)
38 49
39 def run(self): 50 def run(self):
40 super(ProfileDelete, self).run() 51 super(ProfileDelete, self).run()
41 if self.args.profile not in self.host.bridge.getProfilesList(): 52 if self.args.profile not in self.host.bridge.getProfilesList():
42 error("Profile %s doesn't exist." % self.args.profile) 53 log.error("Profile %s doesn't exist." % self.args.profile)
43 self.host.quit(1) 54 self.host.quit(1)
44 self.host.bridge.asyncDeleteProfile(self.args.profile, callback=lambda dummy: None) 55 self.host.bridge.asyncDeleteProfile(self.args.profile, callback=lambda dummy: None)
45 56
46 57
47 class ProfileInfo(base.CommandBase): 58 class ProfileInfo(base.CommandBase):
48 def __init__(self, host): 59 def __init__(self, host):
49 super(ProfileInfo, self).__init__(host, 'info', help=_('Get information about a profile')) 60 super(ProfileInfo, self).__init__(host, 'info', need_connect=False, help=_('get information about a profile'))
50 self.to_show = [(_(u"jid"), "Connection", "JabberID"), 61 self.to_show = [(_(u"jid"), "Connection", "JabberID"),]
51 (_(u"XMPP password"), "Connection", "Password"),
52 ]
53 self.largest = max([len(item[0]) for item in self.to_show]) 62 self.largest = max([len(item[0]) for item in self.to_show])
54 63
55 64
56 def add_parser_options(self): 65 def add_parser_options(self):
57 pass 66 self.parser.add_argument('--show-password', action='store_true', help=_(u'show the XMPP password IN CLEAR TEXT'))
58 67
59 def showNextValue(self, label=None, category=None, value=None): 68 def showNextValue(self, label=None, category=None, value=None):
60 """Show next value from self.to_show and quit on last one""" 69 """Show next value from self.to_show and quit on last one"""
61 if label is not None: 70 if label is not None:
62 print((u"{label:<"+unicode(self.largest+2)+"}{value}").format(label=label+": ", value=value)) 71 print((u"{label:<"+unicode(self.largest+2)+"}{value}").format(label=label+": ", value=value))
69 callback=lambda value: self.showNextValue(label, category, value)) 78 callback=lambda value: self.showNextValue(label, category, value))
70 79
71 def connected(self): 80 def connected(self):
72 self.need_loop = True 81 self.need_loop = True
73 super(ProfileInfo, self).connected() 82 super(ProfileInfo, self).connected()
83 if self.args.show_password:
84 self.to_show.append((_(u"XMPP password"), "Connection", "Password"))
74 self.showNextValue() 85 self.showNextValue()
75 86
76 87
77 class ProfileList(base.CommandBase): 88 class ProfileList(base.CommandBase):
78 def __init__(self, host): 89 def __init__(self, host):
79 super(ProfileList, self).__init__(host, 'list', use_profile=False, help=_('List profiles')) 90 super(ProfileList, self).__init__(host, 'list', use_profile=False, help=_('list profiles'))
80 91
81 def add_parser_options(self): 92 def add_parser_options(self):
82 pass 93 pass
83 94
84 def run(self): 95 def run(self):
108 119
109 def run(self): 120 def run(self):
110 """Create a new profile""" 121 """Create a new profile"""
111 self.need_loop = True 122 self.need_loop = True
112 if self.args.profile in self.host.bridge.getProfilesList(): 123 if self.args.profile in self.host.bridge.getProfilesList():
113 error("Profile %s already exists." % self.args.profile) 124 log.error("Profile %s already exists." % self.args.profile)
114 self.host.quit(1) 125 self.host.quit(1)
115 self.host.bridge.asyncCreateProfile(self.args.profile, self.args.password, callback=self._profile_created, errback=None) 126 self.host.bridge.asyncCreateProfile(self.args.profile, self.args.password, callback=self._profile_created, errback=None)
116 127
117 128
118 class ProfileModify(base.CommandBase): 129 class ProfileModify(base.CommandBase):
119 def __init__(self, host): 130 def __init__(self, host):
120 super(ProfileModify, self).__init__(host, 'modify', help=_('Modify an existing profile')) 131 super(ProfileModify, self).__init__(host, 'modify', need_connect=False, help=_('Modify an existing profile'))
121 132
122 def add_parser_options(self): 133 def add_parser_options(self):
123 self.parser.add_argument('-w', '--password', type=str, default='', help=_('the password of the profile')) 134 self.parser.add_argument('-w', '--password', type=str, default='', help=_('the password of the profile'))
124 self.parser.add_argument('-j', '--jid', type=str, help=_('the jid of the profile')) 135 self.parser.add_argument('-j', '--jid', type=str, help=_('the jid of the profile'))
125 self.parser.add_argument('-x', '--xmpp-password', type=str, help=_('the password of the XMPP account'), 136 self.parser.add_argument('-x', '--xmpp-password', type=str, help=_('the password of the XMPP account'),
126 metavar='PASSWORD') 137 metavar='PASSWORD')
138 self.parser.add_argument('-D', '--default', action='store_true', help=_(u'set as default profile'))
127 139
128 def _profile_created(self): 140 def _profile_created(self):
129 if self.args.jid: 141 if self.args.jid:
130 self.host.bridge.setParam("JabberID", self.args.jid, "Connection" ,profile_key=self.args.profile) 142 self.host.bridge.setParam("JabberID", self.args.jid, "Connection" ,profile_key=self.args.profile)
131 xmpp_pwd = self.args.password or self.args.xmpp_password 143 xmpp_pwd = self.args.password or self.args.xmpp_password
139 self.host.bridge.setParam("Password", self.args.password, "General", profile_key=self.host.profile) 151 self.host.bridge.setParam("Password", self.args.password, "General", profile_key=self.host.profile)
140 if self.args.jid is not None: 152 if self.args.jid is not None:
141 self.host.bridge.setParam("JabberID", self.args.jid, "Connection", profile_key=self.host.profile) 153 self.host.bridge.setParam("JabberID", self.args.jid, "Connection", profile_key=self.host.profile)
142 if self.args.xmpp_password is not None: 154 if self.args.xmpp_password is not None:
143 self.host.bridge.setParam("Password", self.args.xmpp_password, "Connection", profile_key=self.host.profile) 155 self.host.bridge.setParam("Password", self.args.xmpp_password, "Connection", profile_key=self.host.profile)
156 if self.args.default:
157 self.host.bridge.profileSetDefault(self.host.profile)
144 158
145 159
146 class Profile(base.CommandBase): 160 class Profile(base.CommandBase):
147 subcommands = (ProfileDelete, ProfileInfo, ProfileList, ProfileCreate, ProfileModify) 161 subcommands = (ProfileCreate, ProfileDefault, ProfileDelete, ProfileInfo, ProfileList, ProfileModify)
148 162
149 def __init__(self, host): 163 def __init__(self, host):
150 super(Profile, self).__init__(host, 'profile', use_profile=False, help=_('Profile commands')) 164 super(Profile, self).__init__(host, 'profile', use_profile=False, help=_('Profile commands'))