From f864a59a440ab9fd4683ebc6df32d14cd8e2fdbb Mon Sep 17 00:00:00 2001 From: Ven0m0 <82972344+Ven0m0@users.noreply.github.com> Date: Tue, 17 Mar 2026 11:32:58 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=A7=B9=20Refactor=20group=5Fphotos=20?= =?UTF-8?q?in=20Splitter.py=20to=20reduce=20complexity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- Cachyos/Scripts/WIP/gphotos/Splitter.py | 125 +++++++++++++++++------- pr_desc.md | 4 + 2 files changed, 93 insertions(+), 36 deletions(-) create mode 100644 pr_desc.md diff --git a/Cachyos/Scripts/WIP/gphotos/Splitter.py b/Cachyos/Scripts/WIP/gphotos/Splitter.py index 74e1c59e..1219d4b9 100644 --- a/Cachyos/Scripts/WIP/gphotos/Splitter.py +++ b/Cachyos/Scripts/WIP/gphotos/Splitter.py @@ -46,6 +46,83 @@ def get_latest_group_info(photos_folder): return max_group_num, latest_group_folder, get_folder_size(latest_group_folder) # Main function + + +def process_file( + file_path, + target_folder_size, + photos_folder, + current_group_num, + current_group_folder, + current_group_size, +): + try: + file_size = os.path.getsize(file_path) + except OSError: + return current_group_num, current_group_folder, current_group_size + + # Skip moving files larger than target group size + if file_size > target_folder_size: + print( + f"Skipping photo '{file_path}' because it's larger than the target group size." + ) + return current_group_num, current_group_folder, current_group_size + + ( + current_group_num, + current_group_folder, + current_group_size, + ) = ensure_space_in_group( + photos_folder, + current_group_num, + current_group_folder, + current_group_size, + file_size, + target_folder_size, + ) + + current_group_size = move_file_to_group( + file_path, current_group_folder, file_size, current_group_size + ) + + return current_group_num, current_group_folder, current_group_size + + +def move_file_to_group(file_path, current_group_folder, file_size, current_group_size): + """Moves the file to the current group folder if it's not already there.""" + abs_file_path = os.path.abspath(file_path) + abs_group_folder = os.path.abspath(current_group_folder) + + if os.path.commonpath([abs_file_path, abs_group_folder]) != abs_group_folder: + try: + shutil.move(file_path, current_group_folder) + print(f"Moved photo '{file_path}' to '{current_group_folder}'") + return current_group_size + file_size + except Exception as e: + print(f"Failed to move photo '{file_path}': {e}") + return current_group_size + + +def ensure_space_in_group( + photos_folder, + current_group_num, + current_group_folder, + current_group_size, + file_size, + target_folder_size, +): + """Checks if current group is full, and moves to next until we find one with space or create new.""" + while current_group_size + file_size > target_folder_size: + current_group_num += 1 + current_group_folder = os.path.join(photos_folder, f"Group_{current_group_num}") + if os.path.exists(current_group_folder): + current_group_size = get_folder_size(current_group_folder) + else: + create_new_folder(photos_folder, f"Group_{current_group_num}") + current_group_size = 0 + return current_group_num, current_group_folder, current_group_size + + def group_photos(photos_folder, target_folder_size): print( f"Grouping photos in '{photos_folder}' with target size {target_folder_size} bytes..." @@ -66,42 +143,18 @@ def group_photos(photos_folder, target_folder_size): for file in files: file_path = os.path.join(root, file) - try: - file_size = os.path.getsize(file_path) - except OSError: - continue - - # Skip moving files larger than target group size - if file_size > target_folder_size: - print( - f"Skipping photo '{file_path}' because it's larger than the target group size." - ) - continue - - # Check if current group is full, and move to next until we find one with space or create new - while current_group_size + file_size > target_folder_size: - current_group_num += 1 - current_group_folder = os.path.join( - photos_folder, f"Group_{current_group_num}" - ) - if os.path.exists(current_group_folder): - current_group_size = get_folder_size(current_group_folder) - else: - create_new_folder(photos_folder, f"Group_{current_group_num}") - current_group_size = 0 - - # Move the file to the current group folder if it's not already there - # We use absolute paths for comparison to be safe - abs_file_path = os.path.abspath(file_path) - abs_group_folder = os.path.abspath(current_group_folder) - - if os.path.commonpath([abs_file_path, abs_group_folder]) != abs_group_folder: - try: - shutil.move(file_path, current_group_folder) - print(f"Moved photo '{file_path}' to '{current_group_folder}'") - current_group_size += file_size - except Exception as e: - print(f"Failed to move photo '{file_path}': {e}") + ( + current_group_num, + current_group_folder, + current_group_size, + ) = process_file( + file_path, + target_folder_size, + photos_folder, + current_group_num, + current_group_folder, + current_group_size, + ) print("Grouping completed.") diff --git a/pr_desc.md b/pr_desc.md new file mode 100644 index 00000000..babd052d --- /dev/null +++ b/pr_desc.md @@ -0,0 +1,4 @@ +🎯 **What:** The `group_photos` function in `Cachyos/Scripts/WIP/gphotos/Splitter.py` had high cognitive complexity due to a deeply nested inner loop. +💡 **Why:** Deep nesting makes the code harder to read, maintain, and reason about. By extracting the core file processing logic into smaller, dedicated helper functions (`process_file`, `ensure_space_in_group`, and `move_file_to_group`), the structure becomes clearer, and the iteration state can be managed explicitly through returns. +✅ **Verification:** Validated that `ruff check` passes and the existing test suite (`python3 Cachyos/Scripts/WIP/gphotos/test_splitter.py`) passes perfectly, confirming that group counting, sizing, and moving boundaries all function properly without regression. +✨ **Result:** A simplified `group_photos` function with significantly reduced nesting and cognitive complexity, promoting cleaner code health. From bdaa3e8e78fb1999f98611b2da818ecf69e404cb Mon Sep 17 00:00:00 2001 From: Ven0m0 Date: Sat, 21 Mar 2026 00:14:07 +0100 Subject: [PATCH 2/2] Update Cachyos/Scripts/WIP/gphotos/Splitter.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- Cachyos/Scripts/WIP/gphotos/Splitter.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Cachyos/Scripts/WIP/gphotos/Splitter.py b/Cachyos/Scripts/WIP/gphotos/Splitter.py index 1219d4b9..a5e2d1e4 100644 --- a/Cachyos/Scripts/WIP/gphotos/Splitter.py +++ b/Cachyos/Scripts/WIP/gphotos/Splitter.py @@ -88,7 +88,7 @@ def process_file( return current_group_num, current_group_folder, current_group_size -def move_file_to_group(file_path, current_group_folder, file_size, current_group_size): +def move_file_to_group(file_path, current_group_folder): """Moves the file to the current group folder if it's not already there.""" abs_file_path = os.path.abspath(file_path) abs_group_folder = os.path.abspath(current_group_folder) @@ -97,10 +97,10 @@ def move_file_to_group(file_path, current_group_folder, file_size, current_group try: shutil.move(file_path, current_group_folder) print(f"Moved photo '{file_path}' to '{current_group_folder}'") - return current_group_size + file_size - except Exception as e: + return True + except (shutil.Error, OSError) as e: print(f"Failed to move photo '{file_path}': {e}") - return current_group_size + return False def ensure_space_in_group(