def postcmd(self, stop, line): ''' Hook method executed just after a command dispatch is finished line is the command line which was executed, stop is a variable which indicates whether execution will be terminated after the call to postcmd(); ''' new_prompt = self.prompt.split() if (vps_cli.get_current_prompt() != new_prompt[0]): if vps_cli.get_current_prompt() == None: vps_cli.set_current_prompt(new_prompt[0]) if not stop: self.prompt = vps_cli.create_enable_config_prompt() return stop
def postcmd(self, stop, line): """ Hook method executed just after a command dispatch is finished line is the command line which was executed, stop is a variable which indicates whether execution will be terminated after the call to postcmd(); """ new_prompt = self.prompt.split() if vps_cli.get_current_prompt() != new_prompt[0]: if vps_cli.get_current_prompt() == None: vps_cli.set_current_prompt(new_prompt[0]) if not stop: self.prompt = vps_cli.create_enable_config_prompt() return stop
def default(self, command): """ All the processing goes in here. This is the central point of the intrepreter.py """ xml_output = lib.output.XMLoutput() try: # Tokenization of the command if not vps_cli.vps_tokenized: tokens = vps_cli.cmd_tokenize(command, cmd_line_complete=True) else: tokens = [] for arg in command: tokens.append(lib.escape.unescape(arg)) # configure terminal is handled here. currentmode = vps_cli.get_current_prompt() for token in tokens: if token == 'terminal': modifier, cmd, command_name, tokens = \ command_ref.cmdToMode(command, tokens) command_name = "configure terminal" return self.__invokeConfig(modifier, cmd, command_name, tokens) # vadapter <vadapter-name> type commands handled here. if len(tokens) == 2 and tokens[0] in command_ref.NON_SUB_CMD_MOD: modifier, cmd, command_name, tokens = \ command_ref.cmdToMode(command, tokens) return self.__invokeConfig(modifier, cmd, command_name, tokens) # vadapter [ So when length is 1 then is an error] if len(tokens) == 1 and tokens[0] in command_ref.NON_SUB_CMD_MOD: raise NameError( " \n '%s' Not a valid command. \n Please type : 'help %s' \n " % (tokens[0], tokens[0])) return # # So when len is 1 and the modifier is "show" [now] .Later add [edit] # in here. if len(tokens) == 1 and command == "show" \ and "config" in currentmode : modifier, cmd, command_name = command_ref.cmdToModifier( tokens, currentmode) token = currentmode.split("-") mode = token[1].split(")") if mode[0] in command_ref.NON_SUB_CMD_MOD and command == "show": tokens = [] tokens.insert(0, vps_current_module) elif tokens[0] == "edit" and "config" in currentmode: token = currentmode.split("-") mode = token[1].split(")") command_name = "commands." + mode[0] cmd = mode[0] modifier = tokens[0] index = tokens.index('edit') tokens.pop(index) tokens.insert(0, vps_current_module) else: # Get the command from the command_ref. All other command handled here. modifier, cmd, command_name = \ command_ref.cmdToPlugin(tokens, currentmode) if command_name == 'exit': return self.__invokeExit(modifier, cmd, command_name, tokens) elif command_name == 'set': return self.__invokeSet(modifier, cmd, command_name, tokens) elif command_name == 'enable': return self.__invokeLoginExec() elif command_name == 'end': return self.__invokeDisable() # Invoke the actual command #print "modifier", modifier #print "cmd" , cmd #print "command_name", command_name #print "tokens", tokens xml_output = self.__invokeExecuteCommand(modifier, cmd, command_name, tokens) except (lib.errorhandler.StringUnspecial_characterException, command_ref.UnknownPluginException, lib.errorhandler.ModifierDoesntExistException, NameError), ex: xml_output.completeOutputError(ex)
def __invokeConfig(self, modifier, cmd, command_name, args): '''Calls the correct prompt function''' if vps_cli.get_current_prompt() == lib.constants._VFM_STAN \ or vps_cli.get_current_prompt() == None: print NameError("'%s' command can't be invoked in standard mode. " % \ (args)) return # # FIXME: Should check if the modifier is "add" then # args should contain an unique <object> # if modifier not in command_ref.COMMAND_MODIFIER: raise NameError("'%s' command can't be invoked by the action %s" % \ (cmd, modifier)) #else : # print 'Call the appropriate module to handle the modifier' # This is done due to changes made in the design. # FIXME: Make some changes according to the final design. # if vps_cli.check_mode_change(cmd) != 0: raise NameError("Incorrect command to change mode \ Usage : <mode> <object>") #print modifier , cmd , command_name , args cmd = command_name args_len = len(args) if cmd == "": return -1 if cmd != "": tokens = cmd.split() #if args_len == 0 and tokens[1] != "": # vps_cli.cli_runner(tokens[1]) if args_len > 0: if tokens[0] != 'configure': print 'This is modifier ', args else: if args_len < 3: command_extension = tokens[1] global vps_current_module vps_current_module = args[1] vps_cli.cli_runner(command_extension) elif args_len == 3: if args[2] == 'configure': print lib.errorhandler.InvalidArgumentCount() else: print 'This is modifier ', args elif args_len == 4: if args[2] == 'configure' and args[3] != "": command_extension = tokens[1] + ' ' + args[ 1] + ' ' + args[3] vps_cli.cli_runner(command_extension) elif args_len > 4: print args return 0
def default(self, command): """ All the processing goes in here. This is the central point of the intrepreter.py """ xml_output = lib.output.XMLoutput() try: # Tokenization of the command if not vps_cli.vps_tokenized: tokens = vps_cli.cmd_tokenize(command, cmd_line_complete=True) else: tokens = [] for arg in command: tokens.append(lib.escape.unescape(arg)) # configure terminal is handled here. currentmode = vps_cli.get_current_prompt() for token in tokens: if token == "terminal": modifier, cmd, command_name, tokens = command_ref.cmdToMode(command, tokens) command_name = "configure terminal" return self.__invokeConfig(modifier, cmd, command_name, tokens) # vadapter <vadapter-name> type commands handled here. if len(tokens) == 2 and tokens[0] in command_ref.NON_SUB_CMD_MOD: modifier, cmd, command_name, tokens = command_ref.cmdToMode(command, tokens) return self.__invokeConfig(modifier, cmd, command_name, tokens) # vadapter [ So when length is 1 then is an error] if len(tokens) == 1 and tokens[0] in command_ref.NON_SUB_CMD_MOD: raise NameError(" \n '%s' Not a valid command. \n Please type : 'help %s' \n " % (tokens[0], tokens[0])) return # # So when len is 1 and the modifier is "show" [now] .Later add [edit] # in here. if len(tokens) == 1 and command == "show" and "config" in currentmode: modifier, cmd, command_name = command_ref.cmdToModifier(tokens, currentmode) token = currentmode.split("-") mode = token[1].split(")") if mode[0] in command_ref.NON_SUB_CMD_MOD and command == "show": tokens = [] tokens.insert(0, vps_current_module) elif tokens[0] == "edit" and "config" in currentmode: token = currentmode.split("-") mode = token[1].split(")") command_name = "commands." + mode[0] cmd = mode[0] modifier = tokens[0] index = tokens.index("edit") tokens.pop(index) tokens.insert(0, vps_current_module) else: # Get the command from the command_ref. All other command handled here. modifier, cmd, command_name = command_ref.cmdToPlugin(tokens, currentmode) if command_name == "exit": return self.__invokeExit(modifier, cmd, command_name, tokens) elif command_name == "set": return self.__invokeSet(modifier, cmd, command_name, tokens) elif command_name == "enable": return self.__invokeLoginExec() elif command_name == "end": return self.__invokeDisable() # Invoke the actual command # print "modifier", modifier # print "cmd" , cmd # print "command_name", command_name # print "tokens", tokens xml_output = self.__invokeExecuteCommand(modifier, cmd, command_name, tokens) except ( lib.errorhandler.StringUnspecial_characterException, command_ref.UnknownPluginException, lib.errorhandler.ModifierDoesntExistException, NameError, ), ex: xml_output.completeOutputError(ex)
def __invokeConfig(self, modifier, cmd, command_name, args): """Calls the correct prompt function""" if vps_cli.get_current_prompt() == lib.constants._VFM_STAN or vps_cli.get_current_prompt() == None: print NameError("'%s' command can't be invoked in standard mode. " % (args)) return # # FIXME: Should check if the modifier is "add" then # args should contain an unique <object> # if modifier not in command_ref.COMMAND_MODIFIER: raise NameError("'%s' command can't be invoked by the action %s" % (cmd, modifier)) # else : # print 'Call the appropriate module to handle the modifier' # This is done due to changes made in the design. # FIXME: Make some changes according to the final design. # if vps_cli.check_mode_change(cmd) != 0: raise NameError( "Incorrect command to change mode \ Usage : <mode> <object>" ) # print modifier , cmd , command_name , args cmd = command_name args_len = len(args) if cmd == "": return -1 if cmd != "": tokens = cmd.split() # if args_len == 0 and tokens[1] != "": # vps_cli.cli_runner(tokens[1]) if args_len > 0: if tokens[0] != "configure": print "This is modifier ", args else: if args_len < 3: command_extension = tokens[1] global vps_current_module vps_current_module = args[1] vps_cli.cli_runner(command_extension) elif args_len == 3: if args[2] == "configure": print lib.errorhandler.InvalidArgumentCount() else: print "This is modifier ", args elif args_len == 4: if args[2] == "configure" and args[3] != "": command_extension = tokens[1] + " " + args[1] + " " + args[3] vps_cli.cli_runner(command_extension) elif args_len > 4: print args return 0