Use CLIErrors in shell
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Wed, 14 Nov 2012 15:26:40 +0000 (17:26 +0200)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Wed, 14 Nov 2012 15:26:40 +0000 (17:26 +0200)
kamaki/cli/command_shell.py
kamaki/cli/commands/history_cli.py
kamaki/cli/utils.py

index fb3c5d0..49e1e2c 100644 (file)
@@ -36,10 +36,11 @@ from os import popen
 from sys import stdout
 from argparse import ArgumentParser
 
-from kamaki.cli import _exec_cmd
+from kamaki.cli import _exec_cmd, _print_error_message
 from kamaki.cli.argument import _arguments, update_arguments
 from kamaki.cli.utils import print_dict
 from kamaki.cli.history import History
+from kamaki.cli.errors import CLIError
 
 
 def _fix_arguments():
@@ -137,7 +138,10 @@ class Shell(Cmd):
 
                 for name, arg in instance.arguments.items():
                     arg.value = getattr(parsed, name, arg.default)
-                _exec_cmd(instance, unparsed, cmd_parser.print_help)
+                try:
+                    _exec_cmd(instance, unparsed, cmd_parser.print_help)
+                except CLIError as err:
+                    _print_error_message(err)
             elif ('-h' in cmd_args or '--help' in cmd_args) \
             or len(cmd_args):  # print options
                 print('%s: %s' % (subname, subcmd.help))
@@ -190,7 +194,7 @@ class Shell(Cmd):
 
     def run(self, path=''):
         self._history = History(_arguments['config'].get('history', 'file'))
-        if len(path):
+        if path:
             cmd = self.cmd_tree.get_command(path)
             intro = cmd.path.replace('_', ' ')
         else:
@@ -200,4 +204,5 @@ class Shell(Cmd):
             self._push_in_command(subcmd.path)
 
         self.set_prompt(intro)
+
         self.cmdloop()
index 5111452..fb4d146 100644 (file)
@@ -41,6 +41,7 @@ from kamaki.cli.commands import _command_init
 
 
 history_cmds = CommandTree('history', 'Command history')
+_commands = [history_cmds]
 
 
 class _init_history(_command_init):
index 43c0358..b1ea4f2 100644 (file)
@@ -73,8 +73,9 @@ def print_dict(d, exclude=(), ident=0):
     if not isinstance(d, dict):
         raise CLIError(message='Cannot dict_print a non-dict object')
 
-    margin = max(len(unicode(key).strip())\
-        for key in d.keys() if key not in exclude)
+    if d:
+        margin = max(len(unicode(key).strip())\
+            for key in d.keys() if key not in exclude)
 
     for key, val in sorted(d.items()):
         if key in exclude:
@@ -97,8 +98,9 @@ def print_list(l, exclude=(), ident=0):
     if not isinstance(l, list):
         raise CLIError(message='Cannot list_print a non-list object')
 
-    margin = max(len(unicode(item).strip())\
-        for item in l if item not in exclude)
+    if l:
+        margin = max(len(unicode(item).strip())\
+            for item in l if item not in exclude)
 
     for item in sorted(l):
         if item in exclude: