refactor(main.py): improve error handling and logging for file operations

This commit is contained in:
2025-08-19 08:09:25 +02:00
parent 0b08869483
commit adc468bbc2

13
main.py
View File

@@ -98,7 +98,8 @@ class MediaGrader:
def is_video(self, file_path: Path) -> bool: def is_video(self, file_path: Path) -> bool:
"""Check if file is a video""" """Check if file is a video"""
return file_path.suffix.lower() in self.extensions video_extensions = [".mp4", ".avi", ".mov", ".mkv", ".gif"]
return file_path.suffix.lower() in video_extensions
def calculate_frame_delay(self) -> int: def calculate_frame_delay(self) -> int:
"""Calculate frame delay in milliseconds based on playback speed""" """Calculate frame delay in milliseconds based on playback speed"""
@@ -122,8 +123,10 @@ class MediaGrader:
self.current_cap.release() self.current_cap.release()
if self.is_video(file_path): if self.is_video(file_path):
# Suppress OpenCV error messages for unsupported codecs
self.current_cap = cv2.VideoCapture(str(file_path)) self.current_cap = cv2.VideoCapture(str(file_path))
if not self.current_cap.isOpened(): if not self.current_cap.isOpened():
print(f"Warning: Could not open video file {file_path.name} (unsupported codec)")
return False return False
self.total_frames = int(self.current_cap.get(cv2.CAP_PROP_FRAME_COUNT)) self.total_frames = int(self.current_cap.get(cv2.CAP_PROP_FRAME_COUNT))
self.current_frame = 0 self.current_frame = 0
@@ -364,13 +367,13 @@ class MediaGrader:
destination = grade_dir / f"{stem}_{counter}{suffix}" destination = grade_dir / f"{stem}_{counter}{suffix}"
counter += 1 counter += 1
# Track this move for undo functionality BEFORE making changes
self.undo_history.append((str(destination), str(current_file), self.current_index))
try: try:
shutil.move(str(current_file), str(destination)) shutil.move(str(current_file), str(destination))
print(f"Moved {current_file.name} to grade {grade}") print(f"Moved {current_file.name} to grade {grade}")
# Track this move for undo functionality
self.undo_history.append((str(destination), str(current_file), self.current_index))
self.media_files.pop(self.current_index) self.media_files.pop(self.current_index)
if self.current_index >= len(self.media_files): if self.current_index >= len(self.media_files):
@@ -382,6 +385,8 @@ class MediaGrader:
except Exception as e: except Exception as e:
print(f"Error moving file: {e}") print(f"Error moving file: {e}")
# Remove the undo entry since the move failed
self.undo_history.pop()
return True return True