Spot and remove unused Command methods
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Fri, 12 Jul 2013 11:50:44 +0000 (14:50 +0300)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Fri, 12 Jul 2013 11:50:44 +0000 (14:50 +0300)
kamaki/cli/__init__.py
kamaki/cli/command_shell.py
kamaki/cli/command_tree/__init__.py
kamaki/cli/commands/history.py
kamaki/cli/one_command.py

index d5bf8ec..c056814 100644 (file)
@@ -348,11 +348,11 @@ def _groups_help(arguments):
             try:
                 for cmd in cmds:
                     if cmd.name in acceptable_groups:
-                        descriptions[cmd.name] = cmd.description
+                        descriptions[cmd.name] = cmd.help
             except TypeError:
                 if _debug:
                     kloger.warning(
-                        'No cmd description for module %s' % cmd_group)
+                        'No cmd description (help) for module %s' % cmd_group)
         elif _debug:
             kloger.warning('Loading of %s cmd spec failed' % cmd_group)
     print('\nOptions:\n - - - -')
@@ -381,9 +381,9 @@ def _load_all_commands(cmd_tree, arguments):
 
 def print_subcommands_help(cmd):
     printout = {}
-    for subcmd in cmd.get_subcommands():
+    for subcmd in cmd.subcommands.values():
         spec, sep, print_path = subcmd.path.partition('_')
-        printout[print_path.replace('_', ' ')] = subcmd.description
+        printout[print_path.replace('_', ' ')] = subcmd.help
     if printout:
         print('\nOptions:\n - - - -')
         print_dict(printout)
@@ -396,18 +396,14 @@ def update_parser_help(parser, cmd):
 
     description = ''
     if cmd.is_command:
-        cls = cmd.get_class()
+        cls = cmd.cmd_class
         parser.syntax += ' ' + cls.syntax
         parser.update_arguments(cls().arguments)
-        description = getattr(cls, 'long_description', '')
-        description = description.strip()
+        description = getattr(cls, 'long_description', '').strip()
     else:
         parser.syntax += ' <...>'
-    if cmd.has_description:
-        parser.parser.description = cmd.help + (
-            ('\n%s' % description) if description else '')
-    else:
-        parser.parser.description = description
+    parser.parser.description = (
+        cmd.help + ('\n' if description else '')) if cmd.help else description
 
 
 def print_error_message(cli_err):
index 5f6dd83..a1a7265 100644 (file)
@@ -197,7 +197,7 @@ class Shell(Cmd):
             # exec command or change context
             if subcmd.is_command:  # exec command
                 try:
-                    cls = subcmd.get_class()
+                    cls = subcmd.cmd_class
                     ldescr = getattr(cls, 'long_description', '')
                     if subcmd.path == 'history_run':
                         instance = cls(
@@ -241,7 +241,7 @@ class Shell(Cmd):
                 old_prompt = self.prompt
                 new_context._roll_command(cmd.parent_path)
                 new_context.set_prompt(subcmd.path.replace('_', ' '))
-                newcmds = [subcmd for subcmd in subcmd.get_subcommands()]
+                newcmds = [subcmd for subcmd in subcmd.subcommands.values()]
                 for subcmd in newcmds:
                     new_context._register_command(subcmd.path)
                 new_context.cmdloop()
@@ -253,7 +253,7 @@ class Shell(Cmd):
         def help_method(self):
             print('%s (%s -h for more options)' % (cmd.help, cmd.name))
             if cmd.is_command:
-                cls = cmd.get_class()
+                cls = cmd.cmd_class
                 ldescr = getattr(cls, 'long_description', '')
                 #_construct_command_syntax(cls)
                 plist = self.prompt[len(self._prefix):-len(self._suffix)]
@@ -277,12 +277,12 @@ class Shell(Cmd):
         def complete_method(self, text, line, begidx, endidx):
             subcmd, cmd_args = cmd.parse_out(split_input(line)[1:])
             if subcmd.is_command:
-                cls = subcmd.get_class()
+                cls = subcmd.cmd_class
                 instance = cls(dict(arguments))
                 empty, sep, subname = subcmd.path.partition(cmd.path)
                 cmd_name = '%s %s' % (cmd.name, subname.replace('_', ' '))
                 print('\n%s\nSyntax:\t%s %s' % (
-                    cls.description, cmd_name, cls.syntax))
+                    cls.help, cmd_name, cls.syntax))
                 cmd_args = {}
                 for arg in instance.arguments.values():
                     cmd_args[','.join(arg.parsed_name)] = arg.help
index 7d8d2d7..3140cf8 100644 (file)
@@ -71,38 +71,14 @@ class Command(object):
 
     @property
     def is_command(self):
-        return self.cmd_class is not None and len(self.subcommands) == 0
-
-    @property
-    def has_description(self):
-        return len(self.help.strip()) > 0
-
-    @property
-    def description(self):
-        return self.help
+        return len(self.subcommands) == 0 if self.cmd_class else False
 
     @property
     def parent_path(self):
-        parentpath, sep, name = self.path.rpartition('_')
-        return parentpath
-
-    def set_class(self, cmd_class):
-        self.cmd_class = cmd_class
-
-    def get_class(self):
-        return self.cmd_class
-
-    def has_subname(self, subname):
-        return subname in self.subcommands
-
-    def get_subnames(self):
-        return self.subcommands.keys()
-
-    def get_subcommands(self):
-        return self.subcommands.values()
-
-    def sublen(self):
-        return len(self.subcommands)
+        try:
+            return self.path[self.path.rindex('_') + 1:]
+        except ValueError:
+            return ''
 
     def parse_out(self, args):
         cmd = self
@@ -121,7 +97,7 @@ class Command(object):
             self.name,
             self.is_command,
             self.help))
-        for cmd in self.get_subcommands():
+        for cmd in self.subcommands.values():
             cmd.pretty_print(recursive)
 
 
@@ -159,7 +135,7 @@ class CommandTree(object):
                 cmd.add_subcmd(new_cmd)
                 cmd = new_cmd
         if cmd_class:
-            cmd.set_class(cmd_class)
+            cmd.cmd_class = cmd_class
         if description is not None:
             cmd.help = description
 
@@ -205,21 +181,14 @@ class CommandTree(object):
     def get_description(self, path):
         return self._all_commands[path].help
 
-    def set_class(self, path, cmd_class):
-        self._all_commands[path].set_class(cmd_class)
-
-    def get_class(self, path):
-        return self._all_commands[path].get_class()
-
     def get_subnames(self, path=None):
         if path in (None, ''):
             return self.get_group_names()
-        return self._all_commands[path].get_subnames()
+        return self._all_commands[path].subcommands.keys()
 
     def get_subcommands(self, path=None):
-        if path in (None, ''):
-            return self.get_groups()
-        return self._all_commands[path].get_subcommands()
+        return self._all_commands[path].subcommands.values() if (
+            path) else self.get_groups()
 
     def get_parent(self, path):
         if '_' not in path:
index 33fdb2c..189537a 100644 (file)
@@ -171,7 +171,7 @@ class history_run(_init_history):
         if not cmd.is_command:
             return
         try:
-            instance = cmd.get_class()(
+            instance = cmd.cmd_class(
                 self.arguments,
                 auth_base=getattr(self, 'auth_base', None))
             instance.config = self.config
@@ -180,7 +180,7 @@ class history_run(_init_history):
                 dict(instance.arguments))
             prs.syntax = '%s %s' % (
                 cmd.path.replace('_', ' '),
-                cmd.get_class().syntax)
+                cmd.cmd_class.syntax)
             prs.parse(args)
             exec_cmd(instance, prs.unparsed, prs.parser.print_help)
         except (CLIError, ClientError) as err:
index 2dca4ad..eed456f 100644 (file)
@@ -95,7 +95,7 @@ def run(auth_base, cloud, parser, _help):
         print_subcommands_help(cmd)
         exit(0)
 
-    cls = cmd.get_class()
+    cls = cmd.cmd_class
     executable = cls(parser.arguments, auth_base, cloud)
     parser.update_arguments(executable.arguments)
     #parsed, unparsed = parse_known_args(parser, executable.arguments)