diff 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
line wrap: on
line diff
--- a/sat_frontends/jp/cmd_profile.py	Sat Jan 25 21:08:37 2020 +0100
+++ b/sat_frontends/jp/cmd_profile.py	Sat Jan 25 21:08:39 2020 +0100
@@ -22,10 +22,12 @@
 
 from sat_frontends.jp.constants import Const as C
 from sat.core.log import getLogger
-log = getLogger(__name__)
 from sat.core.i18n import _
 from sat_frontends.jp import base
 
+log = getLogger(__name__)
+
+
 __commands__ = ["Profile"]
 
 PROFILE_HELP = _('The name of the profile')
@@ -69,16 +71,30 @@
 
 class ProfileCreate(base.CommandBase):
     def __init__(self, host):
-        super(ProfileCreate, self).__init__(host, 'create', use_profile=False, help=('create a new profile'))
+        super(ProfileCreate, self).__init__(
+            host, 'create', use_profile=False, help=('create a new profile'))
 
     def add_parser_options(self):
         self.parser.add_argument('profile', type=str, help=_('the name of the profile'))
-        self.parser.add_argument('-p', '--password', type=str, default='', help=_('the password of the profile'))
-        self.parser.add_argument('-j', '--jid', type=str, help=_('the jid of the profile'))
-        self.parser.add_argument('-x', '--xmpp-password', type=str, help=_('the password of the XMPP account (use profile password if not specified)'),
-                                 metavar='PASSWORD')
-        self.parser.add_argument('-C', '--component', default='',
-                                 help=_('set to component import name (entry point) if this is a component'))
+        self.parser.add_argument(
+            '-p', '--password', type=str, default='',
+            help=_('the password of the profile'))
+        self.parser.add_argument(
+            '-j', '--jid', type=str, help=_('the jid of the profile'))
+        self.parser.add_argument(
+            '-x', '--xmpp-password', type=str,
+            help=_(
+                'the password of the XMPP account (use profile password if not specified)'
+            ),
+            metavar='PASSWORD')
+        self.parser.add_argument(
+            '-A', '--autoconnect', choices=[C.BOOL_TRUE, C.BOOL_FALSE], nargs='?',
+            const=C.BOOL_TRUE,
+            help=_('connect this profile automatically when backend starts')
+        )
+        self.parser.add_argument(
+            '-C', '--component', default='',
+            help=_('set to component import name (entry point) if this is a component'))
 
     async def start(self):
         """Create a new profile"""
@@ -107,13 +123,19 @@
             await self.host.bridge.setParam(
                 "Password", xmpp_pwd, "Connection", profile_key=self.args.profile)
 
+        if self.args.autoconnect is not None:
+            await self.host.bridge.setParam(
+                "autoconnect_backend", self.args.autoconnect, "Connection",
+                profile_key=self.args.profile)
+
         self.disp(f'profile {self.args.profile} created successfully', 1)
         self.host.quit()
 
 
 class ProfileDefault(base.CommandBase):
     def __init__(self, host):
-        super(ProfileDefault, self).__init__(host, 'default', use_profile=False, help=('print default profile'))
+        super(ProfileDefault, self).__init__(
+            host, 'default', use_profile=False, help=('print default profile'))
 
     def add_parser_options(self):
         pass
@@ -145,48 +167,49 @@
 
 
 class ProfileInfo(base.CommandBase):
+
     def __init__(self, host):
-        super(ProfileInfo, self).__init__(host, 'info', need_connect=False, help=_('get information about a profile'))
+        super(ProfileInfo, self).__init__(
+            host, 'info', need_connect=False, use_output=C.OUTPUT_DICT,
+            help=_('get information about a profile'))
         self.to_show = [(_("jid"), "Connection", "JabberID"),]
-        self.largest = max([len(item[0]) for item in self.to_show])
 
     def add_parser_options(self):
-        self.parser.add_argument('--show-password', action='store_true', help=_('show the XMPP password IN CLEAR TEXT'))
+        self.parser.add_argument(
+            '--show-password', action='store_true',
+            help=_('show the XMPP password IN CLEAR TEXT'))
 
-    async def showNextValue(self, label=None, category=None, value=None):
-        """Show next value from self.to_show and quit on last one"""
-        if label is not None:
-            print((("{label:<"+str(self.largest+2)+"}{value}").format(
-                label=label+": ", value=value)))
-        try:
-            label, category, name = self.to_show.pop(0)
-        except IndexError:
-            self.host.quit()
-        else:
+    async def start(self):
+        if self.args.show_password:
+            self.to_show.append((_("XMPP password"), "Connection", "Password"))
+        self.to_show.append((_("autoconnect (backend)"), "Connection",
+                                "autoconnect_backend"))
+        data = {}
+        for label, category, name in self.to_show:
             try:
                 value = await self.host.bridge.asyncGetParamA(
                     name, category, profile_key=self.host.profile)
             except Exception as e:
                 self.disp(f"can't get {name}/{category} param: {e}", error=True)
-                self.host.quit(C.EXIT_BRIDGE_ERRBACK)
             else:
-                await self.showNextValue(label, category, value)
+                data[label] = value
 
-    async def start(self):
-        if self.args.show_password:
-            self.to_show.append((_("XMPP password"), "Connection", "Password"))
-        await self.showNextValue()
+        await self.output(data)
+        self.host.quit()
 
 
 class ProfileList(base.CommandBase):
     def __init__(self, host):
-        super(ProfileList, self).__init__(host, 'list', use_profile=False, use_output='list', help=('list profiles'))
+        super(ProfileList, self).__init__(
+            host, 'list', use_profile=False, use_output='list', help=('list profiles'))
 
     def add_parser_options(self):
         group = self.parser.add_mutually_exclusive_group()
-        group.add_argument('-c', '--clients', action='store_true', help=_('get clients profiles only'))
-        group.add_argument('-C', '--components', action='store_true', help=('get components profiles only'))
-
+        group.add_argument(
+            '-c', '--clients', action='store_true', help=_('get clients profiles only'))
+        group.add_argument(
+            '-C', '--components', action='store_true',
+            help=('get components profiles only'))
 
     async def start(self):
         if self.args.clients:
@@ -202,16 +225,27 @@
 class ProfileModify(base.CommandBase):
 
     def __init__(self, host):
-        super(ProfileModify, self).__init__(host, 'modify', need_connect=False, help=_('modify an existing profile'))
+        super(ProfileModify, self).__init__(
+            host, 'modify', need_connect=False, help=_('modify an existing profile'))
 
     def add_parser_options(self):
         profile_pwd_group = self.parser.add_mutually_exclusive_group()
-        profile_pwd_group.add_argument('-w', '--password', help=_('change the password of the profile'))
-        profile_pwd_group.add_argument('--disable-password', action='store_true', help=_('disable profile password (dangerous!)'))
+        profile_pwd_group.add_argument(
+            '-w', '--password', help=_('change the password of the profile'))
+        profile_pwd_group.add_argument(
+            '--disable-password', action='store_true',
+            help=_('disable profile password (dangerous!)'))
         self.parser.add_argument('-j', '--jid', help=_('the jid of the profile'))
-        self.parser.add_argument('-x', '--xmpp-password', help=_('change the password of the XMPP account'),
-                                 metavar='PASSWORD')
-        self.parser.add_argument('-D', '--default', action='store_true', help=_('set as default profile'))
+        self.parser.add_argument(
+            '-x', '--xmpp-password', help=_('change the password of the XMPP account'),
+            metavar='PASSWORD')
+        self.parser.add_argument(
+            '-D', '--default', action='store_true', help=_('set as default profile'))
+        self.parser.add_argument(
+            '-A', '--autoconnect', choices=[C.BOOL_TRUE, C.BOOL_FALSE], nargs='?',
+            const=C.BOOL_TRUE,
+            help=_('connect this profile automatically when backend starts')
+        )
 
     async def start(self):
         if self.args.disable_password:
@@ -224,15 +258,23 @@
                 "JabberID", self.args.jid, "Connection", profile_key=self.host.profile)
         if self.args.xmpp_password is not None:
             await self.host.bridge.setParam(
-                "Password", self.args.xmpp_password, "Connection", profile_key=self.host.profile)
+                "Password", self.args.xmpp_password, "Connection",
+                profile_key=self.host.profile)
         if self.args.default:
             await self.host.bridge.profileSetDefault(self.host.profile)
+        if self.args.autoconnect is not None:
+            await self.host.bridge.setParam(
+                "autoconnect_backend", self.args.autoconnect, "Connection",
+                profile_key=self.host.profile)
 
         self.host.quit()
 
 
 class Profile(base.CommandBase):
-    subcommands = (ProfileConnect, ProfileDisconnect, ProfileCreate, ProfileDefault, ProfileDelete, ProfileInfo, ProfileList, ProfileModify)
+    subcommands = (
+        ProfileConnect, ProfileDisconnect, ProfileCreate, ProfileDefault, ProfileDelete,
+        ProfileInfo, ProfileList, ProfileModify)
 
     def __init__(self, host):
-        super(Profile, self).__init__(host, 'profile', use_profile=False, help=_('profile commands'))
+        super(Profile, self).__init__(
+            host, 'profile', use_profile=False, help=_('profile commands'))