comparison frontends/src/jp/base.py @ 1594:313f2bb7841b

jp: profile session can now be started without connection: - a new need_connect option is used when during BaseCommand init - if need_connect is True, profile connection is necessary, so use_profile must be True too - if need_connect if False, session can be started without connection, use_profile must be True - if need_connect is None, use_profile can be True (need_connect will be set to True) or False (need_connect is not used) - if need_connect is False (and so use_profile is True), a new --start-session option is usable - param get and set are the first commands to use this option, so parameters can be changed if connection is not working
author Goffi <goffi@goffi.org>
date Sat, 14 Nov 2015 19:18:10 +0100
parents 823a385235ef
children 8d41cd4da2f6
comparison
equal deleted inserted replaced
1593:791c45ed8659 1594:313f2bb7841b
107 self._progress_id = value 107 self._progress_id = value
108 108
109 def _make_parents(self): 109 def _make_parents(self):
110 self.parents = {} 110 self.parents = {}
111 111
112 profile_parent = self.parents['profile'] = argparse.ArgumentParser(add_help=False) 112 # we have a special case here as the start-session option is present only if connection is not needed,
113 profile_parent.add_argument("-p", "--profile", action="store", type=str, default='@DEFAULT@', help=_("Use PROFILE profile key (default: %(default)s)")) 113 # so we create two similar parents, one with the option, the other one without it
114 profile_parent.add_argument("--pwd", action="store", type=unicode, default='', metavar='PASSWORD', help=_("Password used to connect profile, if necessary")) 114 for parent_name in ('profile', 'profile_session'):
115 profile_parent.add_argument("-c", "--connect", action="store_true", help=_("Connect the profile before doing anything else")) 115 parent = self.parents[parent_name] = argparse.ArgumentParser(add_help=False)
116 parent.add_argument("-p", "--profile", action="store", type=str, default='@DEFAULT@', help=_("Use PROFILE profile key (default: %(default)s)"))
117 parent.add_argument("--pwd", action="store", type=unicode, default='', metavar='PASSWORD', help=_("Password used to connect profile, if necessary"))
118
119 profile_parent, profile_session_parent = self.parents['profile'], self.parents['profile_session']
120
121 connect_short, connect_long, connect_action, connect_help = "-c", "--connect", "store_true", _(u"Connect the profile before doing anything else")
122 profile_parent.add_argument(connect_short, connect_long, action=connect_action, help=connect_help)
123
124 profile_session_connect_group = profile_session_parent.add_mutually_exclusive_group()
125 profile_session_connect_group.add_argument(connect_short, connect_long, action=connect_action, help=connect_help)
126 profile_session_connect_group.add_argument("--start-session", action="store_true", help=_("Start a profile session without connecting"))
116 127
117 progress_parent = self.parents['progress'] = argparse.ArgumentParser(add_help=False) 128 progress_parent = self.parents['progress'] = argparse.ArgumentParser(add_help=False)
118 if progressbar: 129 if progressbar:
119 progress_parent.add_argument("-P", "--progress", action="store_true", help=_("Show progress bar")) 130 progress_parent.add_argument("-P", "--progress", action="store_true", help=_("Show progress bar"))
120 131
229 - 1 when there is a connection error 240 - 1 when there is a connection error
230 """ 241 """
231 # FIXME: need better exit codes 242 # FIXME: need better exit codes
232 243
233 def cant_connect(failure): 244 def cant_connect(failure):
234 error(_(u"Can't connect profile [%s]") % failure) 245 error(_(u"Can't connect profile: {reason}").format(reason=failure))
235 self.quit(1) 246 self.quit(1)
236 247
248 def cant_start_session(failure):
249 error(_(u"Can't start {profile}'s session: {reason}").format(profile=self.profile, reason=failure))
250 self.quit(1)
251
237 self.profile = self.bridge.getProfileName(self.args.profile) 252 self.profile = self.bridge.getProfileName(self.args.profile)
238 253
239 if not self.profile: 254 if not self.profile:
240 error(_("The profile [%s] doesn't exist") % self.args.profile) 255 error(_("The profile [{profile}] doesn't exist").format(profile=self.args.profile))
241 self.quit(1) 256 self.quit(1)
257
258 try:
259 start_session = self.args.start_session
260 except AttributeError:
261 pass
262 else:
263 if start_session:
264 self.bridge.profileStartSession(self.args.pwd, self.profile, lambda dummy: callback(), cant_start_session)
265 self._auto_loop = True
266 return
267 elif not self.bridge.profileIsSessionStarted(self.profile):
268 if not self.args.connect:
269 error(_(u"Session for [{profile}] is not started, please start it before using jp, or use either --start-session or --connect option").format(profile=self.profile))
270 self.quit(1)
271 else:
272 callback()
273 return
274
242 275
243 if not hasattr(self.args, 'connect'): 276 if not hasattr(self.args, 'connect'):
244 # a profile can be present without connect option (e.g. on profile creation/deletion) 277 # a profile can be present without connect option (e.g. on profile creation/deletion)
245 return 278 return
246 elif self.args.connect is True: # if connection is asked, we connect the profile 279 elif self.args.connect is True: # if connection is asked, we connect the profile
247 self.bridge.asyncConnect(self.profile, self.args.pwd, lambda dummy: callback(), cant_connect) 280 self.bridge.asyncConnect(self.profile, self.args.pwd, lambda dummy: callback(), cant_connect)
248 self._auto_loop = True 281 self._auto_loop = True
249 return 282 return
250 elif not self.bridge.isConnected(self.profile): 283 else:
251 error(_(u"Profile [%(profile)s] is not connected, please connect it before using jp, or use --connect option") % { "profile": self.profile }) 284 if not self.bridge.isConnected(self.profile):
252 self.quit(1) 285 error(_(u"Profile [{profile}] is not connected, please connect it before using jp, or use --connect option").format(profile=self.profile))
286 self.quit(1)
253 287
254 callback() 288 callback()
255 289
256 def get_full_jid(self, param_jid): 290 def get_full_jid(self, param_jid):
257 """Return the full jid if possible (add main resource when find a bare jid)""" 291 """Return the full jid if possible (add main resource when find a bare jid)"""
296 return True 330 return True
297 331
298 332
299 class CommandBase(object): 333 class CommandBase(object):
300 334
301 def __init__(self, host, name, use_profile=True, use_progress=False, help=None, **kwargs): 335 def __init__(self, host, name, use_profile=True, use_progress=False, need_connect=None, help=None, **kwargs):
302 """ Initialise CommandBase 336 """ Initialise CommandBase
303 @param host: Jp instance 337 @param host: Jp instance
304 @param name: name of the new command 338 @param name(unicode): name of the new command
305 @param use_profile: if True, add profile selection/connection commands 339 @param use_profile(bool): if True, add profile selection/connection commands
306 @param use_progress: if True, add progress bar activation commands 340 @param use_progress(bool): if True, add progress bar activation commands
307 @param help: help message to display 341 @param need_connect(bool, None): True if profile connection is needed
342 False else (profile session must still be started)
343 None to set auto value (i.e. True if use_profile is set)
344 Can't be set if use_profile is False
345 @param help(unicode): help message to display
308 @param **kwargs: args passed to ArgumentParser 346 @param **kwargs: args passed to ArgumentParser
309 347
310 """ 348 """
311 try: # If we have subcommands, host is a CommandBase and we need to use host.host 349 try: # If we have subcommands, host is a CommandBase and we need to use host.host
312 self.host = host.host 350 self.host = host.host
314 self.host = host 352 self.host = host
315 353
316 parents = kwargs.setdefault('parents', set()) 354 parents = kwargs.setdefault('parents', set())
317 if use_profile: 355 if use_profile:
318 #self.host.parents['profile'] is an ArgumentParser with profile connection arguments 356 #self.host.parents['profile'] is an ArgumentParser with profile connection arguments
319 parents.add(self.host.parents['profile']) 357 if need_connect is None:
358 need_connect = True
359 parents.add(self.host.parents['profile' if need_connect else 'profile_session'])
360 else:
361 assert need_connect is None
362
320 if use_progress: 363 if use_progress:
321 parents.add(self.host.parents['progress']) 364 parents.add(self.host.parents['progress'])
322 365
323 self.parser = host.subparsers.add_parser(name, help=help, **kwargs) 366 self.parser = host.subparsers.add_parser(name, help=help, **kwargs)
324 if hasattr(self, "subcommands"): 367 if hasattr(self, "subcommands"):