comparison sat_frontends/jp/cmd_profile.py @ 3122:4486d72658b9

jp (profile): added --autoconnect argument in `modify` and `create` + use output in `info`
author Goffi <goffi@goffi.org>
date Sat, 25 Jan 2020 21:08:39 +0100
parents fee60f17ebac
children 9d0df638c8b4
comparison
equal deleted inserted replaced
3121:040ca99e25fe 3122:4486d72658b9
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 sat_frontends.jp.constants import Const as C 23 from sat_frontends.jp.constants import Const as C
24 from sat.core.log import getLogger 24 from sat.core.log import getLogger
25 log = getLogger(__name__)
26 from sat.core.i18n import _ 25 from sat.core.i18n import _
27 from sat_frontends.jp import base 26 from sat_frontends.jp import base
27
28 log = getLogger(__name__)
29
28 30
29 __commands__ = ["Profile"] 31 __commands__ = ["Profile"]
30 32
31 PROFILE_HELP = _('The name of the profile') 33 PROFILE_HELP = _('The name of the profile')
32 34
67 self.host.quit() 69 self.host.quit()
68 70
69 71
70 class ProfileCreate(base.CommandBase): 72 class ProfileCreate(base.CommandBase):
71 def __init__(self, host): 73 def __init__(self, host):
72 super(ProfileCreate, self).__init__(host, 'create', use_profile=False, help=('create a new profile')) 74 super(ProfileCreate, self).__init__(
75 host, 'create', use_profile=False, help=('create a new profile'))
73 76
74 def add_parser_options(self): 77 def add_parser_options(self):
75 self.parser.add_argument('profile', type=str, help=_('the name of the profile')) 78 self.parser.add_argument('profile', type=str, help=_('the name of the profile'))
76 self.parser.add_argument('-p', '--password', type=str, default='', help=_('the password of the profile')) 79 self.parser.add_argument(
77 self.parser.add_argument('-j', '--jid', type=str, help=_('the jid of the profile')) 80 '-p', '--password', type=str, default='',
78 self.parser.add_argument('-x', '--xmpp-password', type=str, help=_('the password of the XMPP account (use profile password if not specified)'), 81 help=_('the password of the profile'))
79 metavar='PASSWORD') 82 self.parser.add_argument(
80 self.parser.add_argument('-C', '--component', default='', 83 '-j', '--jid', type=str, help=_('the jid of the profile'))
81 help=_('set to component import name (entry point) if this is a component')) 84 self.parser.add_argument(
85 '-x', '--xmpp-password', type=str,
86 help=_(
87 'the password of the XMPP account (use profile password if not specified)'
88 ),
89 metavar='PASSWORD')
90 self.parser.add_argument(
91 '-A', '--autoconnect', choices=[C.BOOL_TRUE, C.BOOL_FALSE], nargs='?',
92 const=C.BOOL_TRUE,
93 help=_('connect this profile automatically when backend starts')
94 )
95 self.parser.add_argument(
96 '-C', '--component', default='',
97 help=_('set to component import name (entry point) if this is a component'))
82 98
83 async def start(self): 99 async def start(self):
84 """Create a new profile""" 100 """Create a new profile"""
85 if self.args.profile in await self.host.bridge.profilesListGet(): 101 if self.args.profile in await self.host.bridge.profilesListGet():
86 self.disp(f"Profile {self.args.profile} already exists.", error=True) 102 self.disp(f"Profile {self.args.profile} already exists.", error=True)
105 xmpp_pwd = self.args.password or self.args.xmpp_password 121 xmpp_pwd = self.args.password or self.args.xmpp_password
106 if xmpp_pwd: 122 if xmpp_pwd:
107 await self.host.bridge.setParam( 123 await self.host.bridge.setParam(
108 "Password", xmpp_pwd, "Connection", profile_key=self.args.profile) 124 "Password", xmpp_pwd, "Connection", profile_key=self.args.profile)
109 125
126 if self.args.autoconnect is not None:
127 await self.host.bridge.setParam(
128 "autoconnect_backend", self.args.autoconnect, "Connection",
129 profile_key=self.args.profile)
130
110 self.disp(f'profile {self.args.profile} created successfully', 1) 131 self.disp(f'profile {self.args.profile} created successfully', 1)
111 self.host.quit() 132 self.host.quit()
112 133
113 134
114 class ProfileDefault(base.CommandBase): 135 class ProfileDefault(base.CommandBase):
115 def __init__(self, host): 136 def __init__(self, host):
116 super(ProfileDefault, self).__init__(host, 'default', use_profile=False, help=('print default profile')) 137 super(ProfileDefault, self).__init__(
138 host, 'default', use_profile=False, help=('print default profile'))
117 139
118 def add_parser_options(self): 140 def add_parser_options(self):
119 pass 141 pass
120 142
121 async def start(self): 143 async def start(self):
143 await self.host.bridge.asyncDeleteProfile(self.args.profile) 165 await self.host.bridge.asyncDeleteProfile(self.args.profile)
144 self.host.quit() 166 self.host.quit()
145 167
146 168
147 class ProfileInfo(base.CommandBase): 169 class ProfileInfo(base.CommandBase):
148 def __init__(self, host): 170
149 super(ProfileInfo, self).__init__(host, 'info', need_connect=False, help=_('get information about a profile')) 171 def __init__(self, host):
172 super(ProfileInfo, self).__init__(
173 host, 'info', need_connect=False, use_output=C.OUTPUT_DICT,
174 help=_('get information about a profile'))
150 self.to_show = [(_("jid"), "Connection", "JabberID"),] 175 self.to_show = [(_("jid"), "Connection", "JabberID"),]
151 self.largest = max([len(item[0]) for item in self.to_show]) 176
152 177 def add_parser_options(self):
153 def add_parser_options(self): 178 self.parser.add_argument(
154 self.parser.add_argument('--show-password', action='store_true', help=_('show the XMPP password IN CLEAR TEXT')) 179 '--show-password', action='store_true',
155 180 help=_('show the XMPP password IN CLEAR TEXT'))
156 async def showNextValue(self, label=None, category=None, value=None): 181
157 """Show next value from self.to_show and quit on last one""" 182 async def start(self):
158 if label is not None: 183 if self.args.show_password:
159 print((("{label:<"+str(self.largest+2)+"}{value}").format( 184 self.to_show.append((_("XMPP password"), "Connection", "Password"))
160 label=label+": ", value=value))) 185 self.to_show.append((_("autoconnect (backend)"), "Connection",
161 try: 186 "autoconnect_backend"))
162 label, category, name = self.to_show.pop(0) 187 data = {}
163 except IndexError: 188 for label, category, name in self.to_show:
164 self.host.quit()
165 else:
166 try: 189 try:
167 value = await self.host.bridge.asyncGetParamA( 190 value = await self.host.bridge.asyncGetParamA(
168 name, category, profile_key=self.host.profile) 191 name, category, profile_key=self.host.profile)
169 except Exception as e: 192 except Exception as e:
170 self.disp(f"can't get {name}/{category} param: {e}", error=True) 193 self.disp(f"can't get {name}/{category} param: {e}", error=True)
171 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
172 else: 194 else:
173 await self.showNextValue(label, category, value) 195 data[label] = value
174 196
175 async def start(self): 197 await self.output(data)
176 if self.args.show_password: 198 self.host.quit()
177 self.to_show.append((_("XMPP password"), "Connection", "Password"))
178 await self.showNextValue()
179 199
180 200
181 class ProfileList(base.CommandBase): 201 class ProfileList(base.CommandBase):
182 def __init__(self, host): 202 def __init__(self, host):
183 super(ProfileList, self).__init__(host, 'list', use_profile=False, use_output='list', help=('list profiles')) 203 super(ProfileList, self).__init__(
204 host, 'list', use_profile=False, use_output='list', help=('list profiles'))
184 205
185 def add_parser_options(self): 206 def add_parser_options(self):
186 group = self.parser.add_mutually_exclusive_group() 207 group = self.parser.add_mutually_exclusive_group()
187 group.add_argument('-c', '--clients', action='store_true', help=_('get clients profiles only')) 208 group.add_argument(
188 group.add_argument('-C', '--components', action='store_true', help=('get components profiles only')) 209 '-c', '--clients', action='store_true', help=_('get clients profiles only'))
189 210 group.add_argument(
211 '-C', '--components', action='store_true',
212 help=('get components profiles only'))
190 213
191 async def start(self): 214 async def start(self):
192 if self.args.clients: 215 if self.args.clients:
193 clients, components = True, False 216 clients, components = True, False
194 elif self.args.components: 217 elif self.args.components:
200 223
201 224
202 class ProfileModify(base.CommandBase): 225 class ProfileModify(base.CommandBase):
203 226
204 def __init__(self, host): 227 def __init__(self, host):
205 super(ProfileModify, self).__init__(host, 'modify', need_connect=False, help=_('modify an existing profile')) 228 super(ProfileModify, self).__init__(
229 host, 'modify', need_connect=False, help=_('modify an existing profile'))
206 230
207 def add_parser_options(self): 231 def add_parser_options(self):
208 profile_pwd_group = self.parser.add_mutually_exclusive_group() 232 profile_pwd_group = self.parser.add_mutually_exclusive_group()
209 profile_pwd_group.add_argument('-w', '--password', help=_('change the password of the profile')) 233 profile_pwd_group.add_argument(
210 profile_pwd_group.add_argument('--disable-password', action='store_true', help=_('disable profile password (dangerous!)')) 234 '-w', '--password', help=_('change the password of the profile'))
235 profile_pwd_group.add_argument(
236 '--disable-password', action='store_true',
237 help=_('disable profile password (dangerous!)'))
211 self.parser.add_argument('-j', '--jid', help=_('the jid of the profile')) 238 self.parser.add_argument('-j', '--jid', help=_('the jid of the profile'))
212 self.parser.add_argument('-x', '--xmpp-password', help=_('change the password of the XMPP account'), 239 self.parser.add_argument(
213 metavar='PASSWORD') 240 '-x', '--xmpp-password', help=_('change the password of the XMPP account'),
214 self.parser.add_argument('-D', '--default', action='store_true', help=_('set as default profile')) 241 metavar='PASSWORD')
242 self.parser.add_argument(
243 '-D', '--default', action='store_true', help=_('set as default profile'))
244 self.parser.add_argument(
245 '-A', '--autoconnect', choices=[C.BOOL_TRUE, C.BOOL_FALSE], nargs='?',
246 const=C.BOOL_TRUE,
247 help=_('connect this profile automatically when backend starts')
248 )
215 249
216 async def start(self): 250 async def start(self):
217 if self.args.disable_password: 251 if self.args.disable_password:
218 self.args.password = '' 252 self.args.password = ''
219 if self.args.password is not None: 253 if self.args.password is not None:
222 if self.args.jid is not None: 256 if self.args.jid is not None:
223 await self.host.bridge.setParam( 257 await self.host.bridge.setParam(
224 "JabberID", self.args.jid, "Connection", profile_key=self.host.profile) 258 "JabberID", self.args.jid, "Connection", profile_key=self.host.profile)
225 if self.args.xmpp_password is not None: 259 if self.args.xmpp_password is not None:
226 await self.host.bridge.setParam( 260 await self.host.bridge.setParam(
227 "Password", self.args.xmpp_password, "Connection", profile_key=self.host.profile) 261 "Password", self.args.xmpp_password, "Connection",
262 profile_key=self.host.profile)
228 if self.args.default: 263 if self.args.default:
229 await self.host.bridge.profileSetDefault(self.host.profile) 264 await self.host.bridge.profileSetDefault(self.host.profile)
265 if self.args.autoconnect is not None:
266 await self.host.bridge.setParam(
267 "autoconnect_backend", self.args.autoconnect, "Connection",
268 profile_key=self.host.profile)
230 269
231 self.host.quit() 270 self.host.quit()
232 271
233 272
234 class Profile(base.CommandBase): 273 class Profile(base.CommandBase):
235 subcommands = (ProfileConnect, ProfileDisconnect, ProfileCreate, ProfileDefault, ProfileDelete, ProfileInfo, ProfileList, ProfileModify) 274 subcommands = (
236 275 ProfileConnect, ProfileDisconnect, ProfileCreate, ProfileDefault, ProfileDelete,
237 def __init__(self, host): 276 ProfileInfo, ProfileList, ProfileModify)
238 super(Profile, self).__init__(host, 'profile', use_profile=False, help=_('profile commands')) 277
278 def __init__(self, host):
279 super(Profile, self).__init__(
280 host, 'profile', use_profile=False, help=_('profile commands'))