diff --git a/pysorta.py b/pysorta.py index ed4d34d..5445da1 100644 --- a/pysorta.py +++ b/pysorta.py @@ -8,6 +8,7 @@ import shutil from pathlib import Path from datetime import datetime +import sys, os FILE_CATEGORIES = { "Images": [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".svg"], @@ -26,6 +27,27 @@ } + + +def store_preview_info(preview_dict, folder_name, item_dest_path): + if folder_name not in preview_dict.keys(): + preview_dict[folder_name] = [] + preview_dict[folder_name].append(str(item_dest_path).replace('\\','/')) + return preview_dict + +def run_preview(source_directory, organized_dict): + print(f"""{'\u2501'*100}\n{'SOURCE PATH':<60}{'\u2192'} DESTINATION PATH\n{'\u2501'*100}""") + for key in organized_dict.keys(): + for modified_item_path in organized_dict[key]: + item_name = os.path.basename(modified_item_path).split('/')[-1] + source_path = str(source_directory/item_name).replace('\\','/') + item_str = f"""{source_path:<60}{'\u2192'}{modified_item_path}""" + print(item_str) + print('-'*100) + + + + def get_category(extension): """ Return the file category based on its extension. @@ -53,15 +75,19 @@ def organize_by_type(directory): Side Effects: Moves files into folders named after their categories (e.g., 'Images', 'Documents'). """ + global DRY_RUN, DRY_RUN_DICT print("🔤 Organizing by file type...") for item in directory.iterdir(): if item.is_file(): category = get_category(item.suffix) dest_folder = directory / category - dest_folder.mkdir(exist_ok=True) - shutil.move(str(item), dest_folder / item.name) - + if DRY_RUN: + updated_dict = store_preview_info(DRY_RUN_DICT, category, item_dest_path = dest_folder/item.name) + DRY_RUN_DICT = updated_dict + else: + dest_folder.mkdir(exist_ok=True) + shutil.move(str(item), dest_folder / item.name) def organize_by_date(directory): """ @@ -73,15 +99,19 @@ def organize_by_date(directory): Side Effects: Moves files into folders named by year and month (e.g., '2024-06'). """ - + global DRY_RUN, DRY_RUN_DICT print("📅 Organizing by date modified (YYYY-MM)...") for item in directory.iterdir(): if item.is_file(): timestamp = item.stat().st_mtime folder_name = datetime.fromtimestamp(timestamp).strftime('%Y-%m') dest_folder = directory / folder_name - dest_folder.mkdir(exist_ok=True) - shutil.move(str(item), dest_folder / item.name) + if DRY_RUN: + updated_dict = store_preview_info(DRY_RUN_DICT, folder_name, item_dest_path = dest_folder/item.name) + DRY_RUN_DICT = updated_dict + else: + dest_folder.mkdir(exist_ok=True) + shutil.move(str(item), dest_folder / item.name) def organize_by_size(directory): @@ -99,7 +129,7 @@ def organize_by_size(directory): Side Effects: Moves files into folders named after size categories. """ - + global DRY_RUN, DRY_RUN_DICT print("📏 Organizing by file size...") for item in directory.iterdir(): if item.is_file(): @@ -111,8 +141,12 @@ def organize_by_size(directory): else: size_folder = "Large (over 10MB)" dest_folder = directory / size_folder - dest_folder.mkdir(exist_ok=True) - shutil.move(str(item), dest_folder / item.name) + if DRY_RUN: + updated_dict = store_preview_info(DRY_RUN_DICT, size_folder, item_dest_path = dest_folder/item.name) + DRY_RUN_DICT = updated_dict + else: + dest_folder.mkdir(exist_ok=True) + shutil.move(str(item), dest_folder / item.name) def main(): @@ -132,6 +166,11 @@ def main(): Prints messages to the console and moves files on the file system. """ + + global DRY_RUN, DRY_RUN_DICT + DRY_RUN, DRY_RUN_DICT = False, {} + + print("📂 Welcome to PySorta – Sorta the Best!") path_input = input( @@ -151,6 +190,10 @@ def main(): choice = input("👉 Enter choice [1/2/3]: ").strip() + + if '--dry-run' in sys.argv: + DRY_RUN = True + if choice == "1": organize_by_type(directory) elif choice == "2": @@ -160,6 +203,10 @@ def main(): else: print("❌ Invalid choice. Exiting.") return + + if DRY_RUN: + run_preview(directory, DRY_RUN_DICT) + print("✅ Done organizing!")