changeset 2409:d2ff5ff3de77

jp (ad-hoc): new "list" and "run" commands: - list just display commands available at ad-hoc entry point. --verbose allows to display values names, which is useful for automation. - run can either run interactively a command, or execute a workflow to automate them.
author Goffi <goffi@goffi.org>
date Tue, 31 Oct 2017 23:20:04 +0100
parents a870daeab15e
children 40e6e779a253
files frontends/src/jp/base.py frontends/src/jp/cmd_adhoc.py
diffstat 2 files changed, 81 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/jp/base.py	Tue Oct 31 23:17:37 2017 +0100
+++ b/frontends/src/jp/base.py	Tue Oct 31 23:20:04 2017 +0100
@@ -859,7 +859,7 @@
 
     to manage action_types answer,
     """
-    action_callbacks = {} # XXX: set managed action types in an dict here:
+    action_callbacks = {} # XXX: set managed action types in a dict here:
                           # key is the action_type, value is the callable
                           # which will manage the answer. profile filtering is
                           # already managed when callback is called
--- a/frontends/src/jp/cmd_adhoc.py	Tue Oct 31 23:17:37 2017 +0100
+++ b/frontends/src/jp/cmd_adhoc.py	Tue Oct 31 23:20:04 2017 +0100
@@ -19,23 +19,27 @@
 
 import base
 from sat.core.i18n import _
+from functools import partial
+from sat_frontends.jp.constants import Const as C
+from sat_frontends.jp import xmlui_manager
 
 __commands__ = ["AdHoc"]
 
 FLAG_LOOP = 'LOOP'
 MAGIC_BAREJID = '@PROFILE_BAREJID@'
 
+
 class Remote(base.CommandBase):
     def __init__(self, host):
-        super(Remote, self).__init__(host, 'remote', use_verbose=True, help=_('Remote control a software'))
+        super(Remote, self).__init__(host, 'remote', use_verbose=True, help=_(u'remote control a software'))
 
     def add_parser_options(self):
-        self.parser.add_argument("software", type=str, help=_("Software name"))
-        self.parser.add_argument("-j", "--jids", type=base.unicode_decoder, nargs='*', default=[], help=_("Jids allowed to use the command"))
-        self.parser.add_argument("-g", "--groups", type=base.unicode_decoder, nargs='*', default=[], help=_("Groups allowed to use the command"))
-        self.parser.add_argument("--forbidden-groups", type=base.unicode_decoder, nargs='*', default=[], help=_("Groups that are *NOT* allowed to use the command"))
-        self.parser.add_argument("--forbidden-jids", type=base.unicode_decoder, nargs='*', default=[], help=_("Jids that are *NOT* allowed to use the command"))
-        self.parser.add_argument("-l", "--loop", action="store_true", help=_("Loop on the commands"))
+        self.parser.add_argument("software", type=str, help=_(u"software name"))
+        self.parser.add_argument("-j", "--jids", type=base.unicode_decoder, nargs='*', default=[], help=_(u"jids allowed to use the command"))
+        self.parser.add_argument("-g", "--groups", type=base.unicode_decoder, nargs='*', default=[], help=_(u"groups allowed to use the command"))
+        self.parser.add_argument("--forbidden-groups", type=base.unicode_decoder, nargs='*', default=[], help=_(u"groups that are *NOT* allowed to use the command"))
+        self.parser.add_argument("--forbidden-jids", type=base.unicode_decoder, nargs='*', default=[], help=_(u"jids that are *NOT* allowed to use the command"))
+        self.parser.add_argument("-l", "--loop", action="store_true", help=_(u"loop on the commands"))
 
     def start(self):
         name = self.args.software.lower()
@@ -59,8 +63,76 @@
                                                                                          'command': command
                                                                                          }),1)
 
+
+class Run(base.CommandBase):
+    """Run an Ad-Hoc command"""
+
+    def __init__(self, host):
+        super(Run, self).__init__(host, 'run', use_verbose=True, help=_(u'run an Ad-Hoc command'))
+        self.need_loop=True
+
+    def add_parser_options(self):
+        self.parser.add_argument('-n', '--node', type=base.unicode_decoder, default=u'', help=_(u"node of the command (default: list commands)"))
+        self.parser.add_argument('-j', '--jid', type=base.unicode_decoder, default=u'', help=_(u"jid of the service (default: profile's server"))
+        self.parser.add_argument("-S", "--submit", action='append_const', const=xmlui_manager.SUBMIT, dest='workflow', help=_(u"submit form/page"))
+        self.parser.add_argument("-f",
+            "--field",
+            type=base.unicode_decoder,
+            action='append',
+            nargs=2,
+            dest='workflow',
+            metavar=(u"KEY", u"VALUE"),
+            help=_(u"field value"))
+
+    def adHocRunCb(self, xmlui_raw):
+        xmlui = xmlui_manager.create(self.host, xmlui_raw)
+        workflow = self.args.workflow
+        xmlui.show(workflow)
+        if not workflow:
+            if xmlui.type == 'form':
+                xmlui.submitForm()
+            else:
+                self.host.quit()
+
+    def start(self):
+        self.host.bridge.adHocRun(
+            self.args.jid,
+            self.args.node,
+            self.profile,
+            callback=self.adHocRunCb,
+            errback=partial(self.errback,
+                            msg=_(u"can't get ad-hoc commands list: {}"),
+                            exit_code=C.EXIT_BRIDGE_ERRBACK))
+
+
+class List(base.CommandBase):
+    """Run an Ad-Hoc command"""
+
+    def __init__(self, host):
+        super(List, self).__init__(host, 'list', use_verbose=True, help=_(u'list Ad-Hoc commands of a service'))
+        self.need_loop=True
+
+    def add_parser_options(self):
+        self.parser.add_argument('-j', '--jid', type=base.unicode_decoder, default=u'', help=_(u"jid of the service (default: profile's server"))
+
+    def adHocListCb(self, xmlui_raw):
+        xmlui = xmlui_manager.create(self.host, xmlui_raw)
+        xmlui.readonly = True
+        xmlui.show()
+        self.host.quit()
+
+    def start(self):
+        self.host.bridge.adHocList(
+            self.args.jid,
+            self.profile,
+            callback=self.adHocListCb,
+            errback=partial(self.errback,
+                            msg=_(u"can't get ad-hoc commands list: {}"),
+                            exit_code=C.EXIT_BRIDGE_ERRBACK))
+
+
 class AdHoc(base.CommandBase):
-    subcommands = (Remote,)
+    subcommands = (Run, List, Remote)
 
     def __init__(self, host):
         super(AdHoc, self).__init__(host, 'ad-hoc', use_profile=False, help=_('Ad-hoc commands'))