def __init__(self, script_call_string, arg_list): # ###################### DO NOT CHANGE ###################### # super().set_err_usage( type(self).__name__, self.REQUIRED_ARG_NAMES, self.REQUIRED_ARG_VALUES, self.OPTIONAL_ARG_NAMES, self.OPTIONAL_ARG_VALUES) super().__init__(arg_list) self.validate_arguments(self.REQUIRED_ARG_NAMES, self.REQUIRED_ARG_VALUES, self.OPTIONAL_ARG_NAMES, self.OPTIONAL_ARG_VALUES) # ########################################################### # # load argument information into object variables try: # TODO ###################### CHANGE ###################### # self.basedir = self.get_value('basedir') self.filename = self.get_value('filename') self.version = self.get_value('version') self.start = self.get_value('start') self.end = self.get_value('end') if self.optional_arg_set('r'): self.remove = True else: self.remove = False if self.optional_arg_set('foo'): self.foo = self.get_value('foo') else: self.foo = None # TODO ################### END CHANGE #################### # except Exception as e: err.error_abort(e, True) return
def get_tool_name(self): num_args = len(self.arg_list) err.assert_abort(num_args >= 2, f"ERROR: minimum # of args is 2, got '{num_args}'", True) first_arg = 0 first_value = 1 if self.arg_list[first_arg] == "-tool": # Check next arg and see if it is a value or another arg name if self.arg_list[first_value][ 0] != '-': # A '-' would indicate another argument name, not a value name = self.arg_list[first_value] # Remove the tool flag and value from the arg_list (tool creation won't need it) self.arg_list.pop(0) self.arg_list.pop( 0) # 0 again because you just popped the previous 0 item. return name else: err.error_abort( f"ERROR: Tool name has '-' in it '{self.arg_list[first_value]}'", True) else: err.error_abort( f"ERROR: Incorrect syntax: '{err.get_script_call_string()}'", True)
def __init__(self, arg_list): self.arg_pairs = {} self.arg_names = [] index = 0 num_args = len(arg_list) try: while index < num_args: # Format of arg list should be [-<arg_name> [<arg_value>] .. -<arg_name> [<arg_value>]] # Every arg name must start with '-'. Arg values are optional. Arg values cannot start with '-'. # Arg values must follow an arg name. # NOTE: All arg names and arg values will be stored as lower case to make them case independent if arg_list[index][0] == "-": arg_name_without_dash = arg_list[index][1:].lower() # [1:] notation means char 1 forward (0 based) # All arg names are stored in arg_names list self.arg_names.append(arg_name_without_dash) # If there are more args, do a quick look ahead to see if next item is another arg name (will start # with '-') or if it's an arg value (no '-'). Store arg value and arg name in arg_pairs. next_arg_index = index + 1 if next_arg_index < num_args and arg_list[next_arg_index][0] != "-": self.arg_pairs[arg_name_without_dash] = arg_list[next_arg_index].lower() index += 1 # Skip the arg value in the iteration of arg names and arg values index += 1 # Move to the next arg name else: # Reaching this condition means either the list started with an arg value or the list # had two arg values in a row. Both conditions are incorrect. # index + 3 is used because 0 = script name, 1 = '-tool', 2 = tool name. err.error_abort(f"ERROR: Arg value #{index+3} '{arg_list[index]}' not preceded by arg name.", True) except IndexError: err.error_abort(f"ERROR: Incorrect argument list.\n{err.get_call_script_string()}", True)
def run(self): # Check that the requested tool (tool_name) is in the Tools directory (tool_list). # Load the tool's module, get its class (same name as tool name), instantiate the class, and run it. if self.tool_name in self.tool_list: try: module = importlib.import_module(f"Tools.{self.tool_name}") except (NameError, SyntaxError) as ex: err.error_abort( f"ERROR: Failed trying to load module '{self.tool_name}'.\n{ex}" ) try: tool_class = getattr(module, self.tool_name) except AttributeError as ex: err.error_abort( f"ERROR: Failed attempting to extract class '{self.tool_name}' from module.\n{ex}" ) tool_object = tool_class(self.arg_list) tool_object.run() else: err.error_abort(f"ERROR: '{self.tool_name}' invalid tool", True)
def __init__(self, arg_list): self.__set_err_usage(type(self).__name__, self.REQUIRED_ARG_NAMES, self.REQUIRED_ARG_VALUES, self.OPTIONAL_ARG_NAMES, self.OPTIONAL_ARG_VALUES) self.arg_pairs = {} self.arg_names = [] index = 0 num_args = len(arg_list) try: while index < num_args: # Format of arg list should be [-<arg_name> [<arg_value>] .. -<arg_name> [<arg_value>]] # Every arg name must start with '-'. Arg values are optional. Arg values cannot start with '-'. # Arg values must follow an arg name. # NOTE: All arg names and arg values will be stored as lower case to make them case independent if arg_list[index][0] == "-": # [1:] notation means char 1 forward (0 based) arg_name_without_dash = arg_list[index][1:].lower() # All arg names are stored in arg_names list self.arg_names.append(arg_name_without_dash) # If there are more args, do a quick look ahead to see if next item is another arg name (will start # with '-') or if it's an arg value (no '-'). Store arg value and arg name in arg_pairs. next_arg_index = index + 1 if next_arg_index < num_args and arg_list[next_arg_index][0] != "-": self.arg_pairs[arg_name_without_dash] = arg_list[next_arg_index].lower( ) index += 1 # Skip the arg value in the iteration of arg names and arg values index += 1 # Move to the next arg name else: # Reaching this condition means either the list started with an arg value or the list # had two arg values in a row. Both conditions are incorrect. # index + 3 is used because 0 = script name, 1 = '-tool', 2 = tool name. err.error_abort(f"ERROR: Arg value #{index+3} '{arg_list[index]}' not preceded by arg name.", True) except IndexError: err.error_abort( f"ERROR: Incorrect argument list.\n{err.get_call_script_string()}", True) self.validate_arguments(self.REQUIRED_ARG_NAMES, self.REQUIRED_ARG_VALUES, self.OPTIONAL_ARG_NAMES, self.OPTIONAL_ARG_VALUES) self.arguments = {} # load argument information into object variables try: for arg in self.REQUIRED_ARG_NAMES: if arg in self.REQUIRED_ARG_VALUES: self.arguments[arg] = self.__get_value(arg) else: self.arguments[arg] = True if self.__optional_arg_set( arg) else False for arg in self.OPTIONAL_ARG_NAMES: if arg in self.OPTIONAL_ARG_VALUES: if self.__optional_arg_set(arg): self.arguments[arg] = self.__get_value(arg) else: self.arguments[arg] = None else: self.arguments[arg] = True if self.__optional_arg_set( arg) else False except Exception as e: err.error_abort(e, True) return