From: Stavros Sachtouris Date: Thu, 3 Jan 2013 12:30:50 +0000 (+0200) Subject: Catch and handle KeyboardInterrupt in shell X-Git-Tag: v0.6.3~37 X-Git-Url: https://code.grnet.gr/git/kamaki/commitdiff_plain/af569ab91bc0fd3cdd542f4ae302a613ae9004be?hp=57ecec97b61ac85292af21645853da4fbb356588 Catch and handle KeyboardInterrupt in shell --- diff --git a/kamaki/cli/__init__.py b/kamaki/cli/__init__.py index 62b28e1..af47efb 100644 --- a/kamaki/cli/__init__.py +++ b/kamaki/cli/__init__.py @@ -38,7 +38,7 @@ from inspect import getargspec from kamaki.cli.argument import ArgumentParseManager from kamaki.cli.history import History -from kamaki.cli.utils import print_dict, print_list, red, magenta, yellow +from kamaki.cli.utils import print_dict, red, magenta, yellow from kamaki.cli.errors import CLIError _help = False diff --git a/kamaki/cli/command_shell.py b/kamaki/cli/command_shell.py index 609cdc9..bc94d5c 100644 --- a/kamaki/cli/command_shell.py +++ b/kamaki/cli/command_shell.py @@ -100,6 +100,15 @@ class Shell(Cmd): def set_prompt(self, new_prompt): self.prompt = '%s%s%s' % (self._prefix, new_prompt, self._suffix) + def cmdloop(self): + while True: + try: + Cmd.cmdloop(self) + except KeyboardInterrupt: + print(' - interrupted') + continue + break + def do_exit(self, line): print('') if self.prompt[len(self._prefix):-len(self._suffix)]\ @@ -281,6 +290,7 @@ class Shell(Cmd): try: self.cmdloop() - except Exception: + except Exception as e: + print('(%s)' % e) from traceback import print_stack print_stack()