Improve cancellation handling and use generator expressions for memory efficiency

Co-authored-by: denizsafak <39929354+denizsafak@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-11-20 23:23:25 +00:00
co-authored by denizsafak
parent 23a89a618b
commit bf5dfddee6
+9 -6
View File
@@ -874,8 +874,11 @@ class ConversionThread(QThread):
self.log_updated.emit("\nDetected timestamps in text file") self.log_updated.emit("\nDetected timestamps in text file")
# Signal to ask user (-1 indicates timestamp detection) # Signal to ask user (-1 indicates timestamp detection)
self.chapters_detected.emit(-1) self.chapters_detected.emit(-1)
# Wait for user response using event instead of busy-wait loop # Wait for user response using event with timeout for responsive cancellation
self._timestamp_response_event.wait() while not self._timestamp_response_event.wait(timeout=0.1):
if self.cancel_requested:
self.conversion_finished.emit("Cancelled", None)
return
if self.cancel_requested: if self.cancel_requested:
self.conversion_finished.emit("Cancelled", None) self.conversion_finished.emit("Cancelled", None)
return return
@@ -1011,8 +1014,8 @@ class ConversionThread(QThread):
parent_dir, f"{sanitized_base_name}{suffix}_chapters" parent_dir, f"{sanitized_base_name}{suffix}_chapters"
) )
# Only check for files with allowed extensions (extension without dot, case-insensitive) # Only check for files with allowed extensions (extension without dot, case-insensitive)
# Optimize by pre-splitting paths to avoid repeated splitext calls # Use generator expression to avoid processing all files upfront
file_parts = [os.path.splitext(fname) for fname in os.listdir(parent_dir)] file_parts = (os.path.splitext(fname) for fname in os.listdir(parent_dir))
clash = any( clash = any(
name == f"{sanitized_base_name}{suffix}" name == f"{sanitized_base_name}{suffix}"
and ext[1:].lower() in allowed_exts and ext[1:].lower() in allowed_exts
@@ -1756,8 +1759,8 @@ class ConversionThread(QThread):
allowed_exts = set(SUPPORTED_SOUND_FORMATS + SUPPORTED_SUBTITLE_FORMATS) allowed_exts = set(SUPPORTED_SOUND_FORMATS + SUPPORTED_SUBTITLE_FORMATS)
while True: while True:
suffix = f"_{counter}" if counter > 1 else "" suffix = f"_{counter}" if counter > 1 else ""
# Optimize by pre-splitting paths to avoid repeated splitext calls # Use generator expression to avoid processing all files upfront
file_parts = [os.path.splitext(f) for f in os.listdir(parent_dir)] file_parts = (os.path.splitext(f) for f in os.listdir(parent_dir))
if not any( if not any(
name == f"{sanitized_base_name}{suffix}" name == f"{sanitized_base_name}{suffix}"
and ext[1:].lower() in allowed_exts and ext[1:].lower() in allowed_exts