def evaluate_command(self, cmd, args, raw): args = args or "" if cmd in self._command_registry: cmd_instance = self._command_registry.find_command(cmd) else: suggestions = find_approx( cmd, self._command_registry.get_all_commands_map()) if self._options.auto_execute_single_suggestions and len( suggestions) == 1: print() cprint( "Auto-correcting '{}' to '{}'".format(cmd, suggestions[0]), "red", attrs=["bold"], ) cmd_instance = self._command_registry.find_command( suggestions[0]) else: print() cprint( "Unknown Command '{}',{} type `help` to see all " "available commands".format(cmd, suggestions_msg(suggestions)), "red", attrs=["bold"], ) cmd_instance = None if cmd_instance is not None: try: ret = self._blacklist.is_blacklisted(cmd) if ret: return ret except Exception as e: err_message = ("Blacklist executing failed, " "all commands are available.\n" "{}".format(str(e))) cprint(err_message, "red") logging.error(err_message) try: catchall(self._usagelogger.pre_exec) result = cmd_instance.run_interactive(cmd, args, raw) catchall(self._usagelogger.post_exec, cmd, args, result, False) self._status_bar.set_last_command_status(result) return result except NotImplementedError as e: cprint("[NOT IMPLEMENTED]: {}".format(str(e)), "yellow", attrs=["bold"]) # not implemented error code return 99
def test_catchall(self): def raise_generic_error(): raise RuntimeError() def raise_keyboard_interrupt(): raise KeyboardInterrupt() def raise_sysexit(): raise SystemExit() # expected catch all errors except keyboard, sysexit catchall(raise_generic_error) self.assertRaises(KeyboardInterrupt, catchall, raise_keyboard_interrupt) self.assertRaises(SystemExit, catchall, raise_sysexit)
def run_cli(self, args): catchall(self.usage_logger.pre_exec) try: ret = self._blacklist.is_blacklisted(args._cmd) if ret: return ret except Exception as e: err_message = ("Blacklist executing failed, " "all commands are available.\n" "{}".format(str(e))) cprint(err_message, "red") logging.error(err_message) self._ctx.on_cli(args._cmd, args) ret = self._registry.find_command(args._cmd).run_cli(args) return ret
def evaluate_command(self, stdout, cmd, args, raw): if cmd not in self._command_registry: print(file=stdout) cprint( "Unknown Command '{}',{} type :help to see all " "available commands".format( cmd, self._command_registry.find_approx(cmd) ), "magenta", attrs=["bold"], file=stdout, ) else: if args is None: args = "" cmd_instance = self._command_registry.find_command(cmd) try: ret = self._blacklist.is_blacklisted(cmd) if ret: return ret except Exception as e: err_message = ( "Blacklist executing failed, " "all commands are available.\n" "{}".format(str(e)) ) cprint(err_message, "red") logging.error(err_message) try: catchall(self._usagelogger.pre_exec) result = cmd_instance.run_interactive(cmd, args, raw) catchall(self._usagelogger.post_exec, cmd, args, result, False) self._status_bar.set_last_command_status(result) return result except NotImplementedError as e: cprint( "[NOT IMPLEMENTED]: {}".format(str(e)), "yellow", attrs=["bold"], file=stdout, ) # not implemented error code return 99
def run(self, cli_args=sys.argv, ipython=False): """ Runs nubia either in interactive or cli (or parsing commands from stdin) based on the cli_args supplied (defaults to sys.argv). This will block until the shell is done processing all the input and will return the exit code. """ args = self._pre_run(cli_args) if args._print_completion_model: from nubia.internal import registry_tools as regtools try: data = regtools.export_registry( self._plugin, args, self._opts_parser, self._registry ) print(data) return 0 except Exception as e: print("Failed to export model: {}".format(e), file=sys.stderr) traceback.print_exc() return 1 if ipython: return self.start_ipython(args) # by default, if no command is passed we will get 'connect' if args._cmd == "connect": return self.start_interactive(args) else: ret = self.run_cli(args) catchall( self.usage_logger.post_exec, args._cmd, cli_args, ret, True ) if type(ret) is int: return ret if type(ret) is bool: return int(not (ret)) if ret is None: return 0 else: return 1