From 580d2775665d17698b5fb3cfd3ecf3fde464b29e Mon Sep 17 00:00:00 2001 From: Sahil Singh Date: Sun, 31 Jan 2016 03:25:10 +0530 Subject: [PATCH] Refactored functionality, created Classifier class --- classifier/classifier.py | 136 ++++++++++------------------ classifier/format_specifications.py | 20 ++++ classifier/main.py | 29 ++++++ setup.py | 2 +- 4 files changed, 99 insertions(+), 88 deletions(-) create mode 100644 classifier/format_specifications.py create mode 100644 classifier/main.py diff --git a/classifier/classifier.py b/classifier/classifier.py index 2f95851..e1f8294 100644 --- a/classifier/classifier.py +++ b/classifier/classifier.py @@ -1,105 +1,67 @@ -#!/usr/bin/env python import os import sys -import argparse import arrow +import format_specifications -""" -All format lists were taken from wikipedia, not all of them were added due to extensions -not being exclusive to one format such as webm, or raw -Audio - https://en.wikipedia.org/wiki/Audio_file_format -Images - https://en.wikipedia.org/wiki/Image_file_formats -Video - https://en.wikipedia.org/wiki/Video_file_format -Documents - https://en.wikipedia.org/wiki/List_of_Microsoft_Office_filename_extensions -""" +class Classifier(object): + formats = format_specifications.formats -def moveto(file, from_folder, to_folder): - from_file = os.path.join(from_folder, file) - to_file = os.path.join(to_folder, file) + def __init__(self, args): - # to move only files, not folders - if os.path.isfile(from_file): - if not os.path.exists(to_folder): - os.makedirs(to_folder) - os.rename(from_file, to_file) - - -def classify(formats, output): - print("Scanning Files") - - directory = os.getcwd() - - for file in os.listdir(directory): - filename, file_ext = os.path.splitext(file) - file_ext = file_ext.lower() - - for folder, ext_list in list(formats.items()): - folder = os.path.join(output, folder) - - if file_ext in ext_list: - moveto(file, directory, folder) - - print("Done!") + if not self.validate_args(args): + sys.exit() + self.args = args + self.date_format = 'DD-MM-YYYY' + self.output = os.getcwd() if self.args.output is None else self.args.output + self.current_dir = os.getcwd() -def classify_by_date(date_format, output_dir): - print("Scanning Files") + # filter out paths that are not files. + self.files = filter(os.path.isfile, os.listdir(self.current_dir)) - directory = os.getcwd() - files = os.listdir(directory) - creation_dates = map(lambda x: (x, arrow.get(os.path.getctime(x))), files) + if self.args.date: + self.classify_by_date() + else: + self.classify() - for file, creation_date in creation_dates: - folder = creation_date.format(date_format) - moveto(file, directory, folder) + def classify(self): + print ("Scanning Files") - print("Done!") + for file in self.files: + filename, file_ext = os.path.splitext(file) + file_ext = file_ext.lower() + for folder, ext_list in list(self.formats.items()): + folder = os.path.join(self.output, folder) + if file_ext in ext_list: + self.moveto(file, self.current_dir, folder) + print ("Done!") -def main(): - description = "Organize files in your directory instantly,by classifying them into different folders" - parser = argparse.ArgumentParser(description=description) + def classify_by_date(self): + print ("Scanning Files") - parser.add_argument("-st", "--specific-types", type=str, nargs='+', - help="Move all file extensions, given in the args list, in the current directory into the Specific Folder") + creation_dates = \ + map(lambda x: (x, arrow.get(os.path.getctime(x))), self.files) - parser.add_argument("-sf", "--specific-folder", type=str, - help="Folder to move Specific File Type") + for file, creation_date in creation_dates: + folder = creation_date.format(self.date_format) + self.moveto(file, self.current_dir, folder) + print ("Done!") - parser.add_argument("-o", "--output", type=str, - help="Main directory to put organized folders") - - parser.add_argument("-dt", "--date", action='store_true', - help="Organize files by creation date") - - args = parser.parse_args() - - formats = { - 'Music' : ['.mp3', '.aac', '.flac', '.ogg', '.wma', '.m4a', '.aiff'], - 'Videos': ['.flv', '.ogv', '.avi', '.mp4', '.mpg', '.mpeg', '.3gp', '.mkv', '.ts'], - 'Pictures': ['.png', '.jpeg', '.gif', '.jpg', '.bmp', '.svg', '.webp', '.psd'], - 'Archives': ['.rar', '.zip', '.7z', '.gz', '.bz2', '.tar', '.dmg', '.tgz'], - 'Documents': ['.txt', '.pdf', '.doc', '.docx', '.xls', '.xlsv', '.xlsx', - '.ppt', '.pptx', '.ppsx', '.odp', '.odt', '.ods', '.md', '.json', '.csv'], - 'Books': ['.mobi', '.epub'], - 'RPMPackages': ['.rpm'] - } - - if bool(args.specific_folder) ^ bool(args.specific_types): - print( - 'Specific Folder and Specific Types need to be specified together') - sys.exit() - - if args.specific_folder and args.specific_types: - formats = {args.specific_folder: args.specific_types} - - if args.output is None: - args.output = os.getcwd() - - if args.date: - classify_by_date('DD-MM-YYYY', args.output) - else: - classify(formats, args.output) + def moveto(self, file, from_folder, to_folder): + from_file = os.path.join(from_folder, file) + to_file = os.path.join(to_folder, file) + if not os.path.exists(to_folder): + os.makedirs(to_folder) + os.rename(from_file, to_file) - sys.exit() + def validate_args(self, args): + if bool(args.specific_folder) ^ bool(args.specific_types): + print('Specific Folder and Specific Types need to be specified together') + return False + elif args.specific_folder and args.specific_types: + self.formats = {args.specific_folder: args.specific_types} + return True + else: + return True diff --git a/classifier/format_specifications.py b/classifier/format_specifications.py new file mode 100644 index 0000000..1cba92a --- /dev/null +++ b/classifier/format_specifications.py @@ -0,0 +1,20 @@ + +""" +All format lists were taken from wikipedia, not all of them were added due to extensions +not being exclusive to one format such as webm, or raw +Audio - https://en.wikipedia.org/wiki/Audio_file_format +Images - https://en.wikipedia.org/wiki/Image_file_formats +Video - https://en.wikipedia.org/wiki/Video_file_format +Documents - https://en.wikipedia.org/wiki/List_of_Microsoft_Office_filename_extensions +""" + +formats = { + 'Music' : ['.mp3', '.aac', '.flac', '.ogg', '.wma', '.m4a', '.aiff'], + 'Videos': ['.flv', '.ogv', '.avi', '.mp4', '.mpg', '.mpeg', '.3gp', '.mkv', '.ts'], + 'Pictures': ['.png', '.jpeg', '.gif', '.jpg', '.bmp', '.svg', '.webp', '.psd'], + 'Archives': ['.rar', '.zip', '.7z', '.gz', '.bz2', '.tar', '.dmg', '.tgz'], + 'Documents': ['.txt', '.pdf', '.doc', '.docx', '.xls', '.xlsv', '.xlsx', + '.ppt', '.pptx', '.ppsx', '.odp', '.odt', '.ods', '.md', '.json', '.csv'], + 'Books': ['.mobi', '.epub'], + 'RPMPackages': ['.rpm'] +} diff --git a/classifier/main.py b/classifier/main.py new file mode 100644 index 0000000..152ec90 --- /dev/null +++ b/classifier/main.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python +import sys +import argparse +from classifier import Classifier + +def main(): + description = "Organize files in your directory instantly,by classifying them into different folders" + parser = argparse.ArgumentParser(description=description) + + parser.add_argument("-st", "--specific-types", type=str, nargs='+', + help="Move all file extensions, given in the args list, in the current directory into the Specific Folder") + + parser.add_argument("-sf", "--specific-folder", type=str, + help="Folder to move Specific File Type") + + parser.add_argument("-o", "--output", type=str, + help="Main directory to put organized folders") + + parser.add_argument("-dt", "--date", action='store_true', + help="Organize files by creation date") + + args = parser.parse_args() + + Classifier(args) + + sys.exit() + +if __name__ == '__main__': + main() diff --git a/setup.py b/setup.py index 77c7768..0722a60 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ packages=["classifier"], entry_points=""" [console_scripts] - classifier = classifier.classifier:main + classifier = classifier.main:main """, install_requires=[ 'arrow',