Improve grouping controls and cleanup

This commit is contained in:
ajp_anton
2026-06-09 15:07:46 +00:00
parent b6024d8b65
commit f50f465465
2 changed files with 56 additions and 15 deletions
+18 -1
View File
@@ -17,7 +17,13 @@ from pathlib import Path
from tools.console import pause_if_interactive
from tools.filenames import group_stem
from tools.filesystem import collect_files, determine_working_dir, unique_path
from tools.filesystem import (
cleanup_roots_from_args,
collect_files,
determine_working_dir,
remove_empty_dirs,
unique_path,
)
def collect_group_files(args: list[str]) -> list[Path]:
@@ -41,6 +47,14 @@ def group_files(files: list[Path], working_dir: Path) -> Path | None:
return target_dir
def print_removed_dirs(removed_dirs: list[Path]) -> None:
if not removed_dirs:
return
print("Removed empty director" + ("y:" if len(removed_dirs) == 1 else "ies:"))
for directory in removed_dirs:
print(f" - {directory}")
def run(argv: list[str]) -> int:
if len(argv) < 2:
print("Usage: python3 grouping.py path/to/file/or/directory [...]")
@@ -52,6 +66,7 @@ def run(argv: list[str]) -> int:
print(exc)
return 1
cleanup_roots = cleanup_roots_from_args(argv[1:])
files = collect_group_files(argv[1:])
if not files:
print("No files to group.")
@@ -60,6 +75,8 @@ def run(argv: list[str]) -> int:
target_dir = group_files(files, working_dir)
if target_dir is not None:
print(f"Moved {len(files)} file(s) into {target_dir}")
removed_dirs = remove_empty_dirs(cleanup_roots, keep_dirs={working_dir.resolve()})
print_removed_dirs(removed_dirs)
return 0