def handle_archive_argument(configuration: LogmergeConfig, args: argparse.Namespace) -> LogmergeConfig: archive_path = Path(configuration.archive_folder) do_archive = bool(configuration.archive) if isinstance(args.archive, str): do_archive = True archive_path = Path(args.archive) elif args.archive is True: do_archive = True elif args.archive is False: do_archive = False if archive_path.is_dir(): archive_path = Path(archive_path, configuration.name_date_component) elif archive_path.exists(): raise (FileExistsError( f"The file {archive_path} already exists and will not be overwritten." )) # TODO: Update the tests so that this check doesn't cause them to fail. # The normal default is a specific user directory. During tests, that directory might reaosnably not exist. # Update the testing framework to change the default configuration file during testing so the locations are # inside the test directories. # elif not archive_path.exists(): # raise FileNotFoundError( # f"Archive folder must be a directory that exists, the directory {archive_path} does not exist.") configuration.archive = do_archive configuration.archive_folder = archive_path return configuration
def handle_input_directory_argument( configuration: LogmergeConfig, args: argparse.Namespace) -> LogmergeConfig: if args.input_directory is DEFAULT_OBJECT: configuration.input_directory = Path.cwd() else: user_supplied_path = Path(args.input_directory) if not user_supplied_path.is_dir(): raise argparse.ArgumentTypeError( f"--input-directory {user_supplied_path} is not a directory." ) configuration.input_directory = user_supplied_path return configuration
def logmerge_config_object(): """ A fixture for supplying a default LogmergeConfig object to test functions. """ # TODO: Update the default configuration for tests to locations in the test directories. config_parser = create_default_config() return LogmergeConfig(create_default_config())
def test_handle_output_location_argument_custom_file_exists( self, arg_parser, logmerge_config_object, argparse_test_dir): configuration = LogmergeConfig(create_default_config()) argument_list = ["-o", str(Path(argparse_test_dir, "exists.csv"))] argument_namespace = arg_parser.parse_args(argument_list) with pytest.raises(FileExistsError): configuration = update_configuration_from_args( configuration, argument_namespace)
def test_defaults(self): cfg = configparser.ConfigParser() lmc = LogmergeConfig(cfg) assert lmc.header == default_header assert lmc.recursive is False assert lmc.archive_folder == default_archive_location assert lmc.archive is True assert lmc.output_location == default_output_location assert lmc.log_level == "WARNING"
def test_handle_output_location_argument_custom_directory( self, arg_parser, logmerge_config_object, argparse_test_dir): configuration = LogmergeConfig(create_default_config()) argument_list = ["-o", str(argparse_test_dir)] args_namespace = arg_parser.parse_args(argument_list) configuration = update_configuration_from_args(configuration, args_namespace) assert configuration.output_location.parent == argparse_test_dir assert configuration.output_location.name.endswith(".csv")
def handle_verbosity_argument(configuration: LogmergeConfig, args: argparse.Namespace) -> LogmergeConfig: idx = log_levels.index("WARNING") idx += args.verbose idx -= args.silent # Force idx to be a valid index into log_levels. # It won't be less than 0 and it won't be greater than the last valid index in log_levels idx = max(0, min(len(log_levels) - 1, idx)) configuration.log_level = log_levels[idx] return configuration
def test_handle_recursive_argument_default(self, arg_parser, logmerge_config_object, argparse_test_dir): config_file = create_default_config() configuration = LogmergeConfig(config_file) assert configuration.recursive is False argument_list = [] args_namespace = arg_parser.parse_args(argument_list) configuration = update_configuration_from_args(configuration, args_namespace) assert configuration.recursive is False
def test_handle_archive_argument_exists(self, arg_parser, logmerge_config_object, argparse_test_dir): config_file = create_default_config() config_file["ARCHIVE"]["AutoArchive"] = str(False) configuration = LogmergeConfig(config_file) assert configuration.archive is False argument_list = ["-a", str(Path(argparse_test_dir, "exists.csv"))] args_namespace = arg_parser.parse_args(argument_list) with pytest.raises(FileExistsError): configuration = update_configuration_from_args( configuration, args_namespace)
def test_handle_archive_argument_default(self, arg_parser, logmerge_config_object, argparse_test_dir): config_file = create_default_config() config_file["ARCHIVE"]["Folder"] = str(argparse_test_dir) configuration = LogmergeConfig(config_file) argument_list = [] args_namespace = arg_parser.parse_args(argument_list) configuration = update_configuration_from_args(configuration, args_namespace) assert configuration.archive_folder.parent == argparse_test_dir assert configuration.archive is True
def test_handle_header_argument_true(self, arg_parser, logmerge_config_object, argparse_test_dir): # TODO: Allow this to be false by default? config_file = create_default_config() configuration = LogmergeConfig(config_file) assert configuration.header == default_header argument_list = ["-t"] args_namespace = arg_parser.parse_args(argument_list) configuration = update_configuration_from_args(configuration, args_namespace) assert configuration.header == default_header
def test_handle_recursive_argument_false(self, arg_parser, logmerge_config_object, argparse_test_dir): config_file = create_default_config() config_file["SEARCH"]["AutoRecursive"] = str(True) configuration = LogmergeConfig(config_file) assert configuration.recursive is True argument_list = ["-R"] args_namespace = arg_parser.parse_args(argument_list) configuration = update_configuration_from_args(configuration, args_namespace) assert configuration.recursive is False
def test_handle_header_argument_false(self, arg_parser, logmerge_config_object, argparse_test_dir): # TODO: Is None how this should be expressed? config_file = create_default_config() configuration = LogmergeConfig(config_file) assert configuration.header == default_header argument_list = ["-T"] args_namespace = arg_parser.parse_args(argument_list) configuration = update_configuration_from_args(configuration, args_namespace) assert configuration.header is None
def test_handle_header_argument_bad(self, arg_parser, logmerge_config_object, argparse_test_dir): config_file = create_default_config() configuration = LogmergeConfig(config_file) custom_header = "[\\fnord" assert configuration.header == default_header argument_list = ["-t", repr(custom_header)] args_namespace = arg_parser.parse_args(argument_list) with pytest.raises(ArgumentTypeError): configuration = update_configuration_from_args( configuration, args_namespace)
def test_handle_header_argument_custom(self, arg_parser, logmerge_config_object, argparse_test_dir): config_file = create_default_config() configuration = LogmergeConfig(config_file) custom_header = ["one", "two", "three"] assert configuration.header == default_header argument_list = ["-t", repr(custom_header)] args_namespace = arg_parser.parse_args(argument_list) configuration = update_configuration_from_args(configuration, args_namespace) assert configuration.header == custom_header
def test_handle_output_location_argument_default(self, arg_parser, logmerge_config_object, argparse_test_dir): configfile = create_default_config() configfile["OUTPUT"]["Folder"] = str(argparse_test_dir) configuration = LogmergeConfig(configfile) assert configuration is not None argument_list = [] args_namespace = arg_parser.parse_args(argument_list) configuration = update_configuration_from_args(configuration, args_namespace) assert configuration.output_location.parent == argparse_test_dir assert configuration.output_location.name.endswith(".csv")
def handle_header_argument(configuration: LogmergeConfig, args: argparse.Namespace) -> LogmergeConfig: header_arg = args.header file_header = configuration.header if header_arg is False: file_header = None if isinstance(header_arg, str): file_header = literal_eval(header_arg) if not isinstance(file_header, (list, tuple)): raise argparse.ArgumentTypeError( f"{file_header} is not a valid header row.") configuration.header = file_header return configuration
def test_handle_archive_argument_custom(self, arg_parser, logmerge_config_object, argparse_test_dir): config_file = create_default_config() config_file["ARCHIVE"]["AutoArchive"] = str(False) configuration = LogmergeConfig(config_file) assert configuration.archive is False argument_list = ["-a", str(Path(argparse_test_dir, "subdirectory"))] args_namespace = arg_parser.parse_args(argument_list) configuration = update_configuration_from_args(configuration, args_namespace) assert configuration.archive_folder.parent == Path( argparse_test_dir, "subdirectory") assert configuration.archive is True
def handle_output_location_argument( configuration: LogmergeConfig, args: argparse.Namespace) -> LogmergeConfig: res = Path(args.output_location ) if args.output_location is not DEFAULT_OBJECT else ( configuration.output_location) if res.exists(): if res.is_dir(): res = Path(res, f"{configuration.name_date_component}.csv") else: raise (FileExistsError( f"The file {res} already exists and will not be overwritten." )) configuration.output_location = res return configuration
def handle_recursive_argument(configuration: LogmergeConfig, args: argparse.Namespace) -> LogmergeConfig: configuration.recursive = configuration.recursive if args.recursive is DEFAULT_OBJECT else bool( args.recursive) return configuration