changeset 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 791c45ed8659
children a3d0cfa5b7a6
files frontends/src/jp/base.py frontends/src/jp/cmd_param.py
diffstat 2 files changed, 60 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/jp/base.py	Sat Nov 14 19:18:10 2015 +0100
+++ b/frontends/src/jp/base.py	Sat Nov 14 19:18:10 2015 +0100
@@ -109,10 +109,21 @@
     def _make_parents(self):
         self.parents = {}
 
-        profile_parent = self.parents['profile'] = argparse.ArgumentParser(add_help=False)
-        profile_parent.add_argument("-p", "--profile", action="store", type=str, default='@DEFAULT@', help=_("Use PROFILE profile key (default: %(default)s)"))
-        profile_parent.add_argument("--pwd", action="store", type=unicode, default='', metavar='PASSWORD', help=_("Password used to connect profile, if necessary"))
-        profile_parent.add_argument("-c", "--connect", action="store_true", help=_("Connect the profile before doing anything else"))
+        # we have a special case here as the start-session option is present only if connection is not needed,
+        # so we create two similar parents, one with the option, the other one without it
+        for parent_name in ('profile', 'profile_session'):
+            parent = self.parents[parent_name] = argparse.ArgumentParser(add_help=False)
+            parent.add_argument("-p", "--profile", action="store", type=str, default='@DEFAULT@', help=_("Use PROFILE profile key (default: %(default)s)"))
+            parent.add_argument("--pwd", action="store", type=unicode, default='', metavar='PASSWORD', help=_("Password used to connect profile, if necessary"))
+
+        profile_parent, profile_session_parent = self.parents['profile'], self.parents['profile_session']
+
+        connect_short, connect_long, connect_action, connect_help = "-c", "--connect", "store_true", _(u"Connect the profile before doing anything else")
+        profile_parent.add_argument(connect_short, connect_long, action=connect_action, help=connect_help)
+
+        profile_session_connect_group = profile_session_parent.add_mutually_exclusive_group()
+        profile_session_connect_group.add_argument(connect_short, connect_long, action=connect_action, help=connect_help)
+        profile_session_connect_group.add_argument("--start-session", action="store_true", help=_("Start a profile session without connecting"))
 
         progress_parent = self.parents['progress'] = argparse.ArgumentParser(add_help=False)
         if progressbar:
@@ -231,15 +242,37 @@
         # FIXME: need better exit codes
 
         def cant_connect(failure):
-            error(_(u"Can't connect profile [%s]") % failure)
+            error(_(u"Can't connect profile: {reason}").format(reason=failure))
+            self.quit(1)
+
+        def cant_start_session(failure):
+            error(_(u"Can't start {profile}'s session: {reason}").format(profile=self.profile, reason=failure))
             self.quit(1)
 
         self.profile = self.bridge.getProfileName(self.args.profile)
 
         if not self.profile:
-            error(_("The profile [%s] doesn't exist") % self.args.profile)
+            error(_("The profile [{profile}] doesn't exist").format(profile=self.args.profile))
             self.quit(1)
 
+        try:
+            start_session = self.args.start_session
+        except AttributeError:
+            pass
+        else:
+            if start_session:
+                self.bridge.profileStartSession(self.args.pwd, self.profile, lambda dummy: callback(), cant_start_session)
+                self._auto_loop = True
+                return
+            elif not self.bridge.profileIsSessionStarted(self.profile):
+                if not self.args.connect:
+                    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))
+                    self.quit(1)
+            else:
+                callback()
+                return
+
+
         if not hasattr(self.args, 'connect'):
             # a profile can be present without connect option (e.g. on profile creation/deletion)
             return
@@ -247,9 +280,10 @@
             self.bridge.asyncConnect(self.profile, self.args.pwd, lambda dummy: callback(), cant_connect)
             self._auto_loop = True
             return
-        elif not self.bridge.isConnected(self.profile):
-            error(_(u"Profile [%(profile)s] is not connected, please connect it before using jp, or use --connect option") % { "profile": self.profile })
-            self.quit(1)
+        else:
+            if not self.bridge.isConnected(self.profile):
+                error(_(u"Profile [{profile}] is not connected, please connect it before using jp, or use --connect option").format(profile=self.profile))
+                self.quit(1)
 
         callback()
 
@@ -298,13 +332,17 @@
 
 class CommandBase(object):
 
-    def __init__(self, host, name, use_profile=True, use_progress=False, help=None, **kwargs):
+    def __init__(self, host, name, use_profile=True, use_progress=False, need_connect=None, help=None, **kwargs):
         """ Initialise CommandBase
         @param host: Jp instance
-        @param name: name of the new command
-        @param use_profile: if True, add profile selection/connection commands
-        @param use_progress: if True, add progress bar activation commands
-        @param help: help message to display
+        @param name(unicode): name of the new command
+        @param use_profile(bool): if True, add profile selection/connection commands
+        @param use_progress(bool): if True, add progress bar activation commands
+        @param need_connect(bool, None): True if profile connection is needed
+            False else (profile session must still be started)
+            None to set auto value (i.e. True if use_profile is set)
+            Can't be set if use_profile is False
+        @param help(unicode): help message to display
         @param **kwargs: args passed to ArgumentParser
 
         """
@@ -316,7 +354,12 @@
         parents = kwargs.setdefault('parents', set())
         if use_profile:
             #self.host.parents['profile'] is an ArgumentParser with profile connection arguments
-            parents.add(self.host.parents['profile'])
+            if need_connect is None:
+                need_connect = True
+            parents.add(self.host.parents['profile' if need_connect else 'profile_session'])
+        else:
+            assert need_connect is None
+
         if use_progress:
             parents.add(self.host.parents['progress'])
 
--- a/frontends/src/jp/cmd_param.py	Sat Nov 14 19:18:10 2015 +0100
+++ b/frontends/src/jp/cmd_param.py	Sat Nov 14 19:18:10 2015 +0100
@@ -26,7 +26,7 @@
 
 class Get(base.CommandBase):
     def __init__(self, host):
-        super(Get, self).__init__(host, 'get', help=_('Get a parameter value'))
+        super(Get, self).__init__(host, 'get', need_connect=False, help=_('Get a parameter value'))
 
     def add_parser_options(self):
         self.parser.add_argument("category", nargs='?', type=base.unicode_decoder, help=_(u"Category of the parameter"))
@@ -59,7 +59,7 @@
 
 class Set(base.CommandBase):
     def __init__(self, host):
-        super(Set, self).__init__(host, 'set', help=_('Set a parameter value'))
+        super(Set, self).__init__(host, 'set', need_connect=False, help=_('Set a parameter value'))
 
     def add_parser_options(self):
         self.parser.add_argument("category", type=base.unicode_decoder, help=_(u"Category of the parameter"))