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 __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 __init__(self): err.set_file_name(self.FILE_NAME) err.set_note(self.USAGE_NOTE) err.set_example(self.USAGE_EXAMPLE) err.set_usage_message(self.DEFAULT_USAGE) self.arg_list = sys.argv[1:] # Skip script name, already stored err.set_script_call_string( f"{self.FILE_NAME} {' '.join(self.arg_list)}") self.tool_name = self.get_tool_name() self.tool_list = Automator.get_tool_list_from_tools_dir()
def validate_arguments(self, name_list, value_name_list, optional_name_list, optional_value_list): # Make all names lower case (do not change values) for local_list in (name_list, value_name_list, optional_name_list, optional_value_list): index = 0 while index < len(local_list): local_list[index] = local_list[index].lower() index += 1 for name in name_list: err.assert_abort(name in self.arg_names, f"ERROR: '{name}' is a required argument", True) for name in value_name_list: err.assert_abort(name in self.arg_pairs, f"ERROR: '{name}' requires a value", True) # Optional name list is a bit trickier. Need to remove all required names first then compare # each optional name to the optional list. I.e. are the non-required names in the list 'allowed' # by the optional list? remaining_names = [] for name in self.arg_names: if name not in name_list: remaining_names.append(name) for name in remaining_names: err.assert_abort(name in optional_name_list, f"ERROR: '{name}' is not a valid argument", True) # Is a valid optional arg. If also in optional_value_list then it must have a value if name in optional_value_list: err.assert_abort(name in self.arg_pairs, f"ERROR: '{name}' requires a value", True)
def __set_err_usage(class_name, required_arg_names, required_arg_values, optional_arg_names, optional_arg_values): usage_msg = f"-tool {class_name} " for arg in required_arg_names: if arg in required_arg_values: usage_msg += f"-{arg} <value> " else: usage_msg += f"-{arg} " for optional_arg in optional_arg_names: if optional_arg in optional_arg_values: usage_msg += f"[-{optional_arg} <value>] " else: usage_msg += f"[-{optional_arg}] " err.set_usage_message(usage_msg.rstrip())
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 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