From 06a4d17bf82f55a42776621d3857a1f499ed4147 Mon Sep 17 00:00:00 2001 From: qiu0130 <15545995928> Date: Sun, 21 Feb 2016 17:21:56 +0800 Subject: [PATCH 1/2] add redo function --- README.md | 7 +++++ classifier/classifier.py | 62 +++++++++++++++++++++++++++++++++++----- classifier/test.py | 7 +++++ 3 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 classifier/test.py diff --git a/README.md b/README.md index 9a5458b..a109805 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ Downloads -st --specific-types Move the specific file extensions into the Specific Folder -sf --specific-folder Folder to move files with Specific Type -o --output Main directory to put organized folders + -rd --redo Redo the last action ###Example ######Classify specific file types @@ -97,6 +98,12 @@ Workspace ######Classify by Date: `classifier -dt` +###Example +######Redo the last action +`classifier -rd` +or +`classifier -rd -o path` + ====== ## The MIT License diff --git a/classifier/classifier.py b/classifier/classifier.py index 206e932..0e1a8dc 100644 --- a/classifier/classifier.py +++ b/classifier/classifier.py @@ -4,7 +4,7 @@ import os import six import sys - +import json from six.moves import getcwd """ @@ -30,10 +30,10 @@ def moveto(file, from_folder, to_folder): def classify(formats, output): print("Scanning Files") - directory = getcwd() + obj = [] - for file in os.listdir(directory): + for file in [x for x in os.listdir(directory) if not x.startswith('.')]: filename, file_ext = os.path.splitext(file) file_ext = file_ext.lower() @@ -43,25 +43,69 @@ def classify(formats, output): if file_ext in ext_list: try: moveto(file, directory, folder) + obj.append({"file": file, "directory": directory, "folder": folder}) + except Exception as e: print('Cannot move file - {} - {}'.format(file, str(e))) - + # record info to json + dump_json(0, obj) print("Done!") def classify_by_date(date_format, output_dir): print("Scanning Files") - directory = getcwd() + obj = [] files = [x for x in os.listdir(directory) if not x.startswith('.')] creation_dates = map(lambda x: (x, arrow.get(os.path.getctime(x))), files) for file, creation_date in creation_dates: folder = creation_date.format(date_format) moveto(file, directory, folder) + obj.append({"file": file, "directory": directory, "folder": folder}) + # record info to json + dump_json(0, obj) + print("Done!") + +def classify_redo(output): + print("Scanning Files") + obj = load_json() + status, items = obj['status'], obj['obj_json'] + # check repeat + if int(status) == -1: + print("Repeat redo") + return + else: + dump_json(-1, items) + + for item in items: + file, directory, folder = item['file'], item['directory'], item['folder'] + dir_der = os.path.join(directory, folder) + try: + moveto(file, dir_der, directory) + except Exception as e: + print("Redo Files - {} - {}".format(file, str(e))) + # remove the (directory+folder) + if len([x for x in os.listdir(dir_der) if not x.startswith('.')]) == 0: + os.rmdir(dir_der) + + # remove --output + if len([x for x in os.listdir(output) if not x.startswith('.')]) == 0: + os.removedirs(output) print("Done!") +def dump_json(status, obj): + obj_json = {"status": status, "obj_json": obj} + with open(".redo.json", "wt") as fp: + json.dump(obj_json, fp, ensure_ascii=False, indent=4, sort_keys=True) + + +def load_json(): + with open(".redo.json", "rt") as fp: + obj = json.load(fp) + return obj + def _format_text_arg(arg): if not isinstance(arg, six.text_type): @@ -90,16 +134,18 @@ def main(): parser.add_argument("-dt", "--date", action='store_true', help="Organize files by creation date") + parser.add_argument("-rd", "--redo", action='store_true', + help="Redo the last action") args = parser.parse_args() formats = { - 'Music' : ['.mp3', '.aac', '.flac', '.ogg', '.wma', '.m4a', '.aiff', '.wav'], + 'Music': ['.mp3', '.aac', '.flac', '.ogg', '.wma', '.m4a', '.aiff', '.wav'], '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', '.xz'], 'Documents': ['.txt', '.pdf', '.doc', '.docx', '.xls', '.xlsv', '.xlsx', - '.ppt', '.pptx', '.ppsx', '.odp', '.odt', '.ods', '.md', '.json', '.csv'], + '.ppt', '.pptx', '.ppsx', '.odp', '.odt', '.ods', '.md', '.json', '.csv'], 'Books': ['.mobi', '.epub', '.chm'], 'DEBPackages': ['.deb'], 'RPMPackages': ['.rpm'] @@ -121,6 +167,8 @@ def main(): if args.date: classify_by_date('DD-MM-YYYY', output) + elif args.redo: + classify_redo(output) else: classify(formats, output) diff --git a/classifier/test.py b/classifier/test.py new file mode 100644 index 0000000..36144b4 --- /dev/null +++ b/classifier/test.py @@ -0,0 +1,7 @@ +# -*- coding = utf-8 -*- +# Created by qiu on 16-2-20 +# + +import os +import tempfile + From 9b416951eb8f73ab93c8d74b13458c4820027058 Mon Sep 17 00:00:00 2001 From: qiu0130 <15545995928> Date: Sun, 21 Feb 2016 17:30:14 +0800 Subject: [PATCH 2/2] add redo function --- classifier/test.py | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 classifier/test.py diff --git a/classifier/test.py b/classifier/test.py deleted file mode 100644 index 36144b4..0000000 --- a/classifier/test.py +++ /dev/null @@ -1,7 +0,0 @@ -# -*- coding = utf-8 -*- -# Created by qiu on 16-2-20 -# - -import os -import tempfile -