Most of the code is done, only testing to do

This commit is contained in:
Maximilien Rigaut 2022-02-07 05:55:52 +01:00
parent c718c083c3
commit 9c48a586bf
4 changed files with 191 additions and 32 deletions

1
.gitignore vendored
View File

@ -150,3 +150,4 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/
cancel*

View File

@ -17,6 +17,7 @@ operations:
operation1
operation2
operation3
operation4
operation1:
conditions : |
has_artist
@ -29,4 +30,10 @@ operations:
operation3:
conditions : |
is_audio
destination : audios/{obj.name}
destination : audios/{obj.name}
operation4:
conditions : |
is_document
destination : others/{obj.name}
default_destination:
destination : default/{obj.name}

View File

@ -3,37 +3,14 @@ import sys
import yaml
import argparse
import logging
import os
import time
import rich
from rich.logging import RichHandler
from fileops import DirectoryHandler, FileHandler, ArtistHandler
if __name__ == '__main__':
if len(sys.argv) == 1:
sys.argv.append('--help')
parser = argparse.ArgumentParser(description='A simple scrip to sort files according to a plethora of criteria.')
parser.add_argument('folder', help='The folder to sort.')
parser.add_argument("-o", '--output', help='The output folder.', default=None)
parser.add_argument('-c', '--operations', help='The specific batch of operations to draw from.', default='default_operations')
parser.add_argument('--dry-run', help='Dry run.', action='store_true')
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)
# parser.add_argument('-r', '--recursive', help='Recursive.', action='store_true')
# parser.add_argument('-f', '--force', help='Force.', action='store_true')
# parser.add_argument('-e', '--exclude', help='Exclude.', type=str, default='', nargs='+')
args = parser.parse_args()
# Set up logging
_LOG_FORMAT = ' > [%(levelname)s:%(filename)s] - %(message)s'
if args.verbose == 0:
logging.basicConfig(level=logging.ERROR, format=_LOG_FORMAT)
elif args.verbose == 1:
logging.basicConfig(level=logging.WARNING, format=_LOG_FORMAT)
elif args.verbose == 2:
logging.basicConfig(level=logging.INFO, format=_LOG_FORMAT)
else:
logging.basicConfig(level=logging.DEBUG, format=_LOG_FORMAT)
def action_sort(args):
# Load the config file
with open(args.config, 'r') as stream:
@ -67,7 +44,140 @@ if __name__ == '__main__':
# Create the DirectorySorter
directory_handler = DirectoryHandler(args.folder)
# Get the list of Handler from the directory
handler_list = []
for file in directory_handler.get_files():
logging.debug("Processing {}".format(file))
file_handler = Handler(file)
rich.inspect(file_handler)
try:
logging.debug("[Handler list] Processing {}".format(file))
file_handler = Handler(file)
handler_list.append(file_handler)
except Exception as e:
logging.error("[Handler list] Could not load file {}".format(file))
logging.error(e)
logging.debug(e, exc_info=True)
# sys.exit(1) # Unsure if this is the right thing to do
# Get the action to perform on each file from the handler and the operations
operation_list = []
for operation in operations_config["operation_order"].split("\n")[:-1]:
try:
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.debug(e, exc_info=True)
# sys.exit(1) # Unsure if this is the right thing to do
# 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)))
except Exception as e:
logging.error("[File sorting] Could not sort {}".format(handler.file_name))
logging.error(e)
logging.debug(e, exc_info=True)
# Move the files
list_of_operations = []
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))
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:
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:
f.write("{} : {}\n".format(origin, destination))
logging.info("Written "+args.cancel_file)
logging.info("Done")
def action_list(args):
logging.info("Listing operations")
with open(args.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:
rich.print(config["operations"])
else:
for line in config["operations"].keys():
print(line)
def action_help(args):
parser.parse_args(['-h'])
def action_cancel(args):
with open(os.path.join(args.cancel_file), "r") as f:
for line in f:
try:
origin, destination = line.split(" : ")
os.rename(destination, origin)
except Exception as e:
logging.error("Could not cancel {}".format(line))
logging.error(e)
logging.debug(e, exc_info=True)
_LIST_ACTIONS = {
"sort": action_sort,
"help": action_help,
"list": action_list,
"cancel": action_cancel,
}
if __name__ == '__main__':
if len(sys.argv) == 1:
sys.argv.append('--help')
# 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('--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)
# parser.add_argument('-r', '--recursive', help='Recursive.', action='store_true')
# parser.add_argument('-f', '--force', help='Force.', action='store_true')
# parser.add_argument('-e', '--exclude', help='Exclude.', type=str, default='', nargs='+')
parser.add_argument("--cancel_file", help="The file to cancel the operation.", default="cancel_{}.txt".format(time.time()))
subparsers = parser.add_subparsers(dest='action')
parser_sort = subparsers.add_parser('sort', help='Sort files according to criteria')
parser_list = subparsers.add_parser('list', help='List criteria')
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('-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')
args = parser.parse_args()
# Set up logging
# _LOG_FORMAT = ' > [%(levelname)s:%(filename)s] - %(message)s'
_LOG_FORMAT = '%(message)s'
if args.verbose == 0:
logging.basicConfig(level=logging.ERROR, format=_LOG_FORMAT, handlers=[RichHandler()])
elif args.verbose == 1:
logging.basicConfig(level=logging.WARNING, format=_LOG_FORMAT, handlers=[RichHandler()])
elif args.verbose == 2:
logging.basicConfig(level=logging.INFO, format=_LOG_FORMAT, handlers=[RichHandler()])
else:
logging.basicConfig(level=logging.DEBUG, format=_LOG_FORMAT, handlers=[RichHandler()])
# Execute the action
_LIST_ACTIONS[args.action](args)

View File

@ -61,8 +61,10 @@ class FileHandler:
def __init__(self, file_path):
self.file_path = file_path
self.file_name = os.path.basename(file_path)
self.name, self.extension = os.path.splitext(self.file_name)
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
def get_file_path(self):
return self.file_path
@ -82,15 +84,54 @@ class FileHandler:
def is_image(self):
return self.guess_file_type() == 'image'
def is_picture(self):
return self.is_image()
def is_video(self):
return self.guess_file_type() == 'video'
def is_music(self):
return self.guess_file_type() == 'music'
def is_audio(self):
return self.is_music()
def is_document(self):
return self.guess_file_type() == 'document'
def fills_all_conditions(self, list_functions):
for function in list_functions:
if not self.__getattribute__(function)():
return False
return True
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
return default
def move(self, destination=None, dry_run=False):
if dry_run:
dry_run_message = ' > [dry] '
else:
dry_run_message = ''
if destination is None:
destination = self.destination.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)
if not dry_run:
os.makedirs(destination_dir)
logging.debug(dry_run_message+'Moving file: ' + self.file_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):