diff --git a/config.yaml b/config.yaml index 131a012..aed339b 100644 --- a/config.yaml +++ b/config.yaml @@ -22,7 +22,7 @@ operations: conditions : | has_artist is_image - destination : artists/{obj.artist}/{obj.name} + destination : Artists/{obj.artist}/{obj.name} operation2: conditions : | is_video @@ -35,5 +35,3 @@ operations: conditions : | is_document destination : others/{obj.name} - default_destination: - destination : default/{obj.name} \ No newline at end of file diff --git a/criteriaSorter.py b/criteriaSorter.py index 4c657cb..1c4bc14 100644 --- a/criteriaSorter.py +++ b/criteriaSorter.py @@ -10,10 +10,9 @@ from rich.logging import RichHandler from fileops import DirectoryHandler, FileHandler, ArtistHandler -def action_sort(args): - +def action_sort(argsp): # Load the config file - with open(args.config, 'r') as stream: + with open(argsp.config, 'r') as stream: try: config = yaml.load(stream, Loader=yaml.FullLoader) except yaml.YAMLError as exc: @@ -22,11 +21,11 @@ def action_sort(args): # Load the operations try: - if args.operations == "default_operations": + if argsp.operations == "default_operations": logging.info("Loading default operations") operations_name = config["general"]['default_operations'] else: - operations_name = config[args.operation] + operations_name = config[argsp.operation] operations_config = config["operations"][operations_name] except Exception: logging.error("Could not load operations") @@ -42,7 +41,7 @@ def action_sort(args): sys.exit(1) # Create the DirectorySorter - directory_handler = DirectoryHandler(args.folder) + directory_handler = DirectoryHandler(argsp.folder) # Get the list of Handler from the directory handler_list = [] @@ -64,17 +63,23 @@ def action_sort(args): logging.debug("[Operation list] Processing operation {}".format(operation)) operation_list.append(operations_config[operation]) except Exception as e: - logging.error("[Operation list] Could not load operation {} : {}".format(operation,e)) + logging.error("[Operation list] Could not load operation {} : {}".format(operation, e)) logging.debug(e, exc_info=True) # sys.exit(1) # Unsure if this is the right thing to do + # Find the default destination + if "default_destination" in operations_config: + default_destination = operations_config["default_destination"]["destination"] + else: + default_destination = None + # Sort the files for handler in handler_list: try: # rich.inspect(operation_list) logging.debug("[File sorting] Processing {}".format(handler.file_name)) - destination = handler.sort(operation_list, default=operations_config["default_destination"]["destination"]) - logging.info("[File sorting] {} -> {}".format(handler.file_name, destination.format(obj=handler))) + destination = handler.sort(operation_list, default=default_destination) + logging.debug("[File sorting] {} -> {}".format(handler.file_name, destination)) except Exception as e: logging.error("[File sorting] Could not sort {}".format(handler.file_name)) logging.error(e) @@ -85,44 +90,47 @@ def action_sort(args): for handler in handler_list: try: logging.debug("[File moving] Processing {}".format(handler.file_name)) - list_of_operations.append(handler.move(dry_run=args.dry_run)) + operation = handler.move(destination=argsp.output, dry_run=argsp.dry_run) + if operation: + list_of_operations.append(operation) except Exception as e: logging.error("[File moving] Could not move {}".format(handler.file_name)) logging.error(e) logging.debug(e, exc_info=True) # Write a cancel file - if args.dry_run and len(list_of_operations) > 0: + if argsp.dry_run and len(list_of_operations) > 0: logging.info("Writing cancel file...") - with open(os.path.join(args.folder, args.cancel_file), "w") as f: - for origin,destination in list_of_operations: + with open(os.path.join(argsp.output, argsp.cancel_file), "w", encoding="utf-8") as f: + for origin, destination in list_of_operations: f.write("{} : {}\n".format(origin, destination)) - logging.info("Written "+args.cancel_file) + logging.info("Written " + argsp.cancel_file) logging.info("Done") -def action_list(args): +def action_list(argsp): logging.info("Listing operations") - with open(args.config, 'r') as stream: + with open(argsp.config, 'r') as stream: try: config = yaml.load(stream, Loader=yaml.FullLoader) except yaml.YAMLError as exc: logging.error(exc) sys.exit(1) - if args.verbose: + if argsp.verbose: rich.print(config["operations"]) else: for line in config["operations"].keys(): print(line) -def action_help(args): - parser.parse_args(['-h']) +def action_help(argsp): + if argsp: + parser.parse_args(['-h']) # This will print the help -def action_cancel(args): - with open(os.path.join(args.cancel_file), "r") as f: +def action_cancel(argsp): + with open(os.path.join(argsp.cancel_file), "r") as f: for line in f: try: origin, destination = line.split(" : ") @@ -146,7 +154,7 @@ if __name__ == '__main__': # Parse arguments parser = argparse.ArgumentParser(description='A simple scrip to sort files according to a plethora of criteria.') - parser.add_argument('-v','--verbose', help='Verbose.', action="count", default=0) + parser.add_argument('-v', '--verbose', help='Verbose.', action="count", default=0) parser.add_argument('--version', action='version', version='%(prog)s 1.0') parser.add_argument('--config', help='The config file.', default='config.yaml') # parser.add_argument('--log', help='The log file.', default=None) @@ -161,7 +169,7 @@ if __name__ == '__main__': parser_help = subparsers.add_parser('help', help='Show help') parser_cancel = subparsers.add_parser('cancel', help='Cancel') parser_sort.add_argument('folder', help='The folder to sort.') - parser_sort.add_argument("-o", '--output', help='The output folder.', default=None) + parser_sort.add_argument("-o", '--output', help='The output folder.', default=".") parser_sort.add_argument('-c', '--operations', help='The specific batch of operations to draw from.', default='default_operations') parser_sort.add_argument('--dry-run', help='Dry run.', action='store_true') @@ -180,4 +188,4 @@ if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG, format=_LOG_FORMAT, handlers=[RichHandler()]) # Execute the action - _LIST_ACTIONS[args.action](args) \ No newline at end of file + _LIST_ACTIONS[args.action](args) diff --git a/fileops.py b/fileops.py index 0efedc6..4b45192 100644 --- a/fileops.py +++ b/fileops.py @@ -40,8 +40,9 @@ class DirectoryHandler: def get_files(self): for file in self.get_file_list(): - if os.path.isfile(os.path.join(self.dir_path, file)): - yield file + fullname = os.path.join(self.dir_path, file) + if os.path.isfile(fullname): + yield fullname def get_directories(self): for file in self.get_file_list(): @@ -61,10 +62,11 @@ class FileHandler: def __init__(self, file_path): self.file_path = file_path self.file_name = os.path.basename(file_path) + self.dir_path = os.path.dirname(file_path) self.name = self.file_name self.base_name, self.extension = os.path.splitext(self.file_name) self.type = self.guess_file_type() - self.destination = None + self.future_name = None # Do a no operation def get_file_path(self): return self.file_path @@ -108,31 +110,34 @@ class FileHandler: def sort(self, list_of_condition_dicts, default=None): for condition in list_of_condition_dicts: if self.fills_all_conditions(condition['conditions'].split('\n')[:-1]): - self.destination = condition['destination'] - return self.destination - logging.debug('No destination found for file: ' + self.file_name) - self.destination = default + self.future_name = condition['destination'] + return self.future_name + logging.warning("The file {} doesn't meet any criteria, reverting to default".format(self.file_name)) + self.future_name = default return default - def move(self, destination=None, dry_run=False): + def move(self, destination=".", dry_run=False): if dry_run: dry_run_message = ' > [dry] ' else: dry_run_message = '' - if destination is None: - destination = self.destination.format(obj=self) + if self.future_name is None: + logging.debug('No future_name found for file: ' + self.file_name) # This is a no-op + return + destination = os.path.join(destination, self.future_name.format(obj=self)) destination_dir = os.path.dirname(destination) if not os.path.exists(destination_dir): - logging.debug(dry_run_message+'Creating directory: ' + destination_dir) + logging.info(dry_run_message+'Creating directory: ' + destination_dir) if not dry_run: os.makedirs(destination_dir) - logging.debug(dry_run_message+'Moving file: ' + self.file_name + ' to: ' + destination) + logging.info(dry_run_message+'Moving file: ' + self.name + ' to: ' + destination) if not dry_run: os.rename(self.file_path, destination) return self.file_path, destination + class ArtistHandler(FileHandler): def __init__(self, file_path): super().__init__(file_path) @@ -141,15 +146,15 @@ class ArtistHandler(FileHandler): def guess_artist_and_file_name(self): """Try to guess artist from file name""" regex = [ - r"(?P[^-]+)-(?P.+)$", r"(?P.+) by (?P.+)$", - r"(?P.+)_(?P.+)$", + r"(?P[^-]+)-(?P.+)$", + # r"(?P.+)_(?P.+)$", ] try: for r in regex: m = re.match(r, self.file_name) if m: - return m.group("artist"), m.group("file_name") + return m.group("artist").rstrip(" "), m.group("file_name").lstrip(" ") return None, self.file_name except re.error as e: logging.error("An error occured while processing regex on " + self.file_name)