def main(): config = Config() parser = ArgumentParser() set_parser_for_trash(parser) set_parser_with_common_flags(parser) args = parser.parse_args() config.update(get_config_dict_from_trash_parser_namespace(args)) if args.config_file_path: config.update( get_config_from_file(get_correct_path(args.config_file_path))) trash = init_trash_by_config(config) log_file = args.log_file_path if args.imitation or args.view_trash_content: args.verbose = True if args.silent_mode: trash.confirm_removal = return_true set_logger(write_to_stderr=False) elif args.verbose or not (args.restore_from_trash or args.clear_trash): set_logger(log_level="info", logfile_path=log_file) else: set_logger(logfile_path=log_file) if args.restore_from_trash: report_about_result(trash.restore(args.restore_from_trash)) elif args.clear_trash: report_about_result(trash.clean()) else: report_about_result(trash.view())
def __init__(self, path, trash_location): """Init TrashInfo object. The __init__ accept 'path' and 'trash_location' and setup initial_path as correct 'path'. Defaults to: 'deletion_date' - today, 'sha256_value' - 0, size - 0, errors - empty list. Args: path (str): Path to object in file system. trash_location (str): Full path to trash. """ self.initial_path = get_correct_path(path) self.path_in_trash = get_path_in_trash(path, trash_location) self.deletion_date = datetime.datetime.today() self.sha256_value = 0 self.size = 0 self.errors = []
def main(): config = Config() parser = ArgumentParser() set_parser_for_remove(parser) set_parser_with_common_flags(parser) args = parser.parse_args() config.update(get_config_dict_from_remove_parser_namespace(args)) if args.config_file_path: config.update( get_config_from_file(get_correct_path(args.config_file_path))) if args.regex: config.remove["aim"] = const.REMOVE_TREE_MODE trash = init_trash_by_config(config) log_file = args.log_file_path if args.imitation: args.verbose = True if args.silent_mode: trash.confirm_removal = return_true set_logger(write_to_stderr=False) elif args.verbose: set_logger(log_level="info", logfile_path=log_file) else: set_logger(logfile_path=log_file) report_about_result(trash.remove(args.path, regex=args.regex))
def test_get_trash_files_and_info_paths(self): for path in TestGetMethods.paths: correct_path = get_correct_path(path) self.assertTupleEqual( get_trash_files_and_info_paths(correct_path), (os.path.join(correct_path, const.TRASH_FILES_DIRECTORY), os.path.join(correct_path, const.TRASH_INFO_DIRECTORY)))
def __init__( self, trash_location=const.TRASH_LOCATION, remove_mode=const.REMOVE_FILE_MODE, dry_run=False, confirm_removal=return_true, clean_policy=const.DEFAULT_CLEAN_POLICY, clean_parametr=None, solve_name_conflict_policy=const.SOLVE_NAME_CONFLICT_POLICY, max_size=const.MAX_TRASH_SIZE_IN_BYTES, max_time_in_trash=const.MAX_TIME_IN_TRASH ): """Can override all trash attributes. Init parametr's names matches with trash attributes. Trash location is set uo with correct path. If remove_mode is uncorrect string then will be used remove "file" mode as default. If confirm_removal is not callable object then always will return true. """ self.trash_location = get_correct_path(trash_location) if (remove_mode == const.REMOVE_FILE_MODE or remove_mode == const.REMOVE_EMPTY_DIRECTORY_MODE or remove_mode == const.REMOVE_TREE_MODE): self.remove_mode = remove_mode else: self.remove_mode = const.REMOVE_FILE_MODE if callable(confirm_removal): self.confirm_removal = confirm_removal else: self.confirm_removal = return_true self.dry_run = dry_run self.clean_policy = clean_policy self.clean_parametr = clean_parametr self.conflict_policy = solve_name_conflict_policy self.max_size = max_size self.max_time_in_trash = max_time_in_trash self._trashinfo_config = ConfigParser.ConfigParser() self._trashinfo_config.add_section(const.INFO_SECTION)
def __init__(self): self.remove = { "aim": const.REMOVE_FILE_MODE, "mode": const.ATTENTION_IF_NOT_WRITE_ACCESS_MODE } self.path_to = { "trash": get_correct_path(const.TRASH_LOCATION), "log_file": None } self.settings = { "dry_run": False, "silent": False, "check_hash": False } self.policies = { "clean": const.DEFAULT_CLEAN_POLICY, "clean_parametr": None, "solve_name_conflict": const.SOLVE_NAME_CONFLICT_POLICY, "max_size": const.MAX_TRASH_SIZE_IN_BYTES, "max_time_in_trash": const.MAX_TIME_IN_TRASH }
def test_trash_init(self): trash_location = "trash_loc" tr = Trash(trash_location=trash_location) self.assertEqual(tr.trash_location, get_correct_path(trash_location)) tr = Trash(remove_mode="abracadabra") self.assertEqual(tr.remove_mode, const.REMOVE_FILE_MODE) for mode in ("file", "directory", "tree"): tr = Trash(remove_mode=mode) self.assertEqual(tr.remove_mode, mode) for dry_run in (True, False): tr = Trash(dry_run=dry_run) self.assertEqual(tr.dry_run, dry_run) confirm_removal = "str" tr = Trash(confirm_removal=confirm_removal) self.assertEqual(tr.confirm_removal, return_true) callable_object = callable confirm_removal = callable_object tr = Trash(confirm_removal=confirm_removal) self.assertEqual(tr.confirm_removal, confirm_removal)
def test_get_correct_path(self): for path in TestGetMethods.paths: correct_path = os.path.abspath(os.path.expanduser(path)) self.assertEqual(get_correct_path(path), correct_path)