""" A simple test to make sure seash's parser is catching wrong commands correctly. """ #pragma error Command not understood import seash_dictionary # Parser should recognize these commands as valid seash_dictionary.parse_command("loadkeys notakey") seash_dictionary.parse_command("show ip to example_file.txt") seash_dictionary.parse_command("run example.1.1.repy example arguments") # Parser should throw an error for following command seash_dictionary.parse_command("incorrect command")
def command_loop(test_command_list): # If a test command list is passed, filter the tab completion warning if test_command_list: warnings.filterwarnings("ignore", "Auto tab completion is off, because it is not available on your operating system.", ImportWarning) # Things that may be set herein and used in later commands. # Contains the local variables of the original command loop. # Keeps track of the user's state in seash. Referenced # during command executions by the command_parser. environment_dict = { 'host': None, 'port': None, 'expnum': None, 'filename': None, 'cmdargs': None, 'defaulttarget': None, 'defaultkeyname': None, 'currenttarget': None, 'currentkeyname': None, 'autosave': False, 'handleinfo': {}, 'showparse': True, } # Set up the tab completion environment (Added by Danny Y. Huang) if tabcompletion: # Initializes seash's tab completer completer = tab_completer.Completer() readline.parse_and_bind("tab: complete") # Determines when a new tab complete instance should be initialized, # which, in this case, is never, so the tab completer will always take # the entire user's string into account readline.set_completer_delims("") # Sets the completer function that readline will utilize readline.set_completer(completer.complete) else: warnings.warn("Auto tab completion is off, because it is not available on your operating system.",ImportWarning) # If passed a list of commands, do not prompt for user input if test_command_list: seash_helper.update_time() # Iterates through test_command_list in sequential order for command_strings in test_command_list: # Saving state after each command? (Added by Danny Y. Huang) if environment_dict['autosave'] and environment_dict['defaultkeyname']: try: # State is saved in file "autosave_username", so that user knows which # RSA private key to use to reload the state. autosavefn = "autosave_" + str(environment_dict['defaultkeyname']) seash_helper.savestate(autosavefn, environment_dict['handleinfo'], environment_dict['host'], environment_dict['port'], environment_dict['expnum'], environment_dict['filename'], environment_dict['cmdargs'], environment_dict['defaulttarget'], environment_dict['defaultkeyname'], environment_dict['autosave'], environment_dict['defaultkeyname']) except Exception, error: raise seash_exceptions.UserError("There is an error in autosave: '" + str(error) + "'. You can turn off autosave using the command 'set autosave off'.") # Returns the dictionary of dictionaries that correspond to the # command string cmd_input = seash_dictionary.parse_command(command_strings, display_parsed_result=environment_dict['showparse']) # by default, use the target specified in the prompt environment_dict['currenttarget'] = environment_dict['defaulttarget'] # by default, use the identity specified in the prompt environment_dict['currentkeyname'] = environment_dict['defaultkeyname'] # calls the command_dispatch method of seash_dictionary to execute the callback # method associated with the command the user inputed seash_dictionary.command_dispatch(cmd_input, environment_dict)
""" Passes a series of commands to the parser in seash_dictionary and make sures the parser returns the correct input dictionary. Make sure to update this if the following command's dictionaries changes: browse show ownerinfo set autosave upload """ import seash_dictionary import command_callbacks resulting_dictionary = seash_dictionary.parse_command("browse") expected_dictionary = {'browse':{'name':'browse', 'summary': 'Find vessels I can control', 'callback':command_callbacks.browse, 'help_text':""" browse [advertisetype] This command will use the default identity to search for vessels that can be controlled. Any vessel with the advertise flag set will be advertised in at least one advertise service. browse will look into these services and add any vessels it can contact. Setting advertisetype will restrict the advertise lookup to only use that service. Some permitted values for advertisetype are central, DHT, and DOR. Example: exampleuser@ !> show targets %all (empty) exampleuser@ !> browse
comment_index = userinput.find('#') # throw away anything after (and including) the '#' if comment_index != -1: userinput = userinput[0:comment_index] # drop any leading or trailing whitespace userinput = userinput.strip() # if it's an empty line, continue... if len(userinput)==0: continue # Returns the dictionary of dictionaries that correspond to the # command the user inputted cmd_input = seash_dictionary.parse_command(userinput, display_parsed_result=environment_dict['showparse']) # by default, use the target specified in the prompt environment_dict['currenttarget'] = environment_dict['defaulttarget'] # by default, use the identity specified in the prompt environment_dict['currentkeyname'] = environment_dict['defaultkeyname'] # calls the command_dispatch method of seash_dictionary to execute the callback # method associated with the command the user inputed seash_dictionary.command_dispatch(cmd_input, environment_dict)
# display the thing they are acting on in their prompt (if applicable) if environment_dict['defaulttarget']: prompt = prompt + seash_helper.fit_string(environment_dict['defaulttarget'],20) prompt = prompt + " !> " # the prompt should look like: justin@good !> # get the user input userinput = raw_input(prompt) if len(userinput)==0: continue # Returns the dictionary of dictionaries that correspond to the # command the user inputted cmd_input = seash_dictionary.parse_command(userinput) # by default, use the target specified in the prompt environment_dict['currenttarget'] = environment_dict['defaulttarget'] # by default, use the identity specified in the prompt environment_dict['currentkeyname'] = environment_dict['defaultkeyname'] # calls the command_dispatch method of seash_dictionary to execute the callback # method associated with the command the user inputed seash_dictionary.command_dispatch(cmd_input, environment_dict)