Exclude unset optional cmd groups
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Mon, 8 Jul 2013 11:21:35 +0000 (14:21 +0300)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Mon, 8 Jul 2013 11:21:35 +0000 (14:21 +0300)
Refs: #4114

E.g. the cyclades spec package contains the non-optional group "server" and
the optional group "floatingip". Now kamaki can load only the "server"
group, except if the user requests the loading of "floatingip" group too.

kamaki/cli/__init__.py
kamaki/cli/argument.py
kamaki/cli/command_shell.py
kamaki/cli/command_tree.py
kamaki/cli/config.py

index 093d332..d5bf8ec 100644 (file)
@@ -340,13 +340,15 @@ def _groups_help(arguments):
     global _debug
     global kloger
     descriptions = {}
+    acceptable_groups = arguments['config'].get_groups()
     for cmd_group, spec in arguments['config'].get_cli_specs():
         pkg = _load_spec_module(spec, arguments, '_commands')
         if pkg:
             cmds = getattr(pkg, '_commands')
             try:
                 for cmd in cmds:
-                    descriptions[cmd.name] = cmd.description
+                    if cmd.name in acceptable_groups:
+                        descriptions[cmd.name] = cmd.description
             except TypeError:
                 if _debug:
                     kloger.warning(
index 7d4a8db..19a6fd4 100644 (file)
@@ -442,8 +442,7 @@ class ArgumentParseManager(object):
             the parsers arguments specification
         """
         self.parser = ArgumentParser(
-            add_help=False,
-            formatter_class=RawDescriptionHelpFormatter)
+            add_help=False, formatter_class=RawDescriptionHelpFormatter)
         self.syntax = '%s <cmd_group> [<cmd_subbroup> ...] <cmd>' % exe
         if arguments:
             self.arguments = arguments
@@ -513,14 +512,11 @@ class ArgumentParseManager(object):
             self.update_parser()
 
     def parse(self, new_args=None):
-        """Do parse user input"""
+        """Parse user input"""
         try:
-            if new_args:
-                self._parsed, unparsed = self.parser.parse_known_args(new_args)
-            else:
-                self._parsed, unparsed = self.parser.parse_known_args()
+            pkargs = (new_args,) if new_args else ()
+            self._parsed, unparsed = self.parser.parse_known_args(*pkargs)
         except SystemExit:
-            # deal with the fact that argparse error system is STUPID
             raiseCLIError(CLISyntaxError('Argument Syntax Error'))
         for name, arg in self.arguments.items():
             arg.value = getattr(self._parsed, name, arg.default)
index 002f687..5f6dd83 100644 (file)
@@ -310,6 +310,10 @@ class Shell(Cmd):
         else:
             intro = self.cmd_tree.name
 
+        acceptable = parser.arguments['config'].get_groups()
+        total = self.cmd_tree.get_group_names()
+        self.cmd_tree.exclude(set(total).difference(acceptable))
+
         for subcmd in self.cmd_tree.get_subcommands(path):
             self._register_command(subcmd.path)
 
index 6814592..218f84f 100644 (file)
@@ -137,6 +137,10 @@ class CommandTree(object):
         self.name = name
         self.description = description
 
+    def exclude(self, groups_to_exclude=[]):
+        for group in groups_to_exclude:
+            self.groups.pop(group, None)
+
     def add_command(self, command_path, description=None, cmd_class=None):
         terms = command_path.split('_')
         try:
index 39d70ab..e609231 100644 (file)
@@ -88,6 +88,7 @@ DEFAULTS = {
         #  Optional command specs:
         #  'livetest_cli': 'livetest',
         #  'astakos_cli': 'snf-astakos'
+        #  'floating_cli': 'cyclades'
     },
     CLOUD_PREFIX:
     {