def get_keywords(self, menu_split): """ Gets all the keystrings of a given command Example: - "replace subsequence 1 3 5 with 4 5" will return ["replace subsequence", "with"] - "delete index 1" will return ["delete index"] Input: -menu_split - The command string Returns: An array containing all identified keystrings """ key_list = [] key_string = '' for i in range(0, len(menu_split)): try: num = Validator.validate_int(get_index(menu_split,i), -10000, 10000) if(key_string != ''): key_string = key_string[:len(key_string) - 1] key_list.append(key_string) key_string = '' except ValueError as e: if(get_index(e.args, 0) == "Number is invalid"): key_string += get_index(menu_split,i) + ' ' if(key_string != ''): key_string = key_string[:len(key_string) - 1] key_list.append(key_string) return key_list
def display_Menu(self): """ Displays the menu, waits for and processes commands """ menu_split = [' '] self._print_sequence(self.Command_Handler.menu_dictionary['display menu'](None), "") while(menu_split[0] != 'quit'): menu_code = input("Input your command: ") menu_split = menu_code.split(sep=" ") try: sequence = self.Command_Handler.process_command(menu_split) if(isinstance(sequence, int)): print(sequence) else: try: Validator.validate_int(get_index(sequence, 0), -10000, 10000) end_character = " " except IndexError: pass except ValueError: end_character = "" self._print_sequence(sequence, end_character) except TypeError as e: if(get_index(e.args[0].split(sep = " "), 1) == 'takes'): print('Invalid command') else: print(e.args[0]) except ValueError as e: print (e.args[0]) except IndexError as e: print (e.args[0])
def process_command(self, menu_split): """ Process a given command in order to determine the called function and it's parameters If the command is valid, the function is called Input: -menu_split - The command string, split into an array of strings by the ' ' character -sequence - The base sequence Returns: The sequence, modified by the function called by the command string if the command is executed successfully The unmodified sequence, if the command string is invalid, or the function call fails """ keystring_list = self.get_keywords(menu_split) num_list = self.get_numbers(menu_split, get_word_count(keystring_list[0])) try : f = self.menu_dictionary[get_index(keystring_list, 0)] except : raise ValueError("Invalid command") if(len(keystring_list) == 2 and get_index(keystring_list, 1) != 'with'): # If we find the keyword with, we have a command of the form [ keystring number_list with numbar_list ] raise ValueError('Invalid input') elif(len(keystring_list) == 2): num_list = [num_list, self.get_numbers(menu_split, get_word_count(get_index(keystring_list, 0)) + 1 + len(num_list))] # the list num_list becomes an array of two number lists temp_sequence = f(self.sequence_handler, *num_list) return temp_sequence