Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: use `DequeProgress` for sorting

NTFSvolume0707384ce00c
diff --git a/cyberdrop_dl/ui/progress/sort_progress.py b/cyberdrop_dl/ui/progress/sort_progress.py
index 590b921d6..60d5f6f07 100644
--- a/cyberdrop_dl/ui/progress/sort_progress.py
+++ b/cyberdrop_dl/ui/progress/sort_progress.py
@@ -2,10 +2,12 @@ from __future__ import annotations
from typing import TYPE_CHECKING
-from rich.console import Group
+from rich.markup import escape
from rich.panel import Panel
from rich.progress import BarColumn, Progress, SpinnerColumn, TaskID
+from cyberdrop_dl.ui.progress.deque_progress import DequeProgress
+
if TYPE_CHECKING:
from cyberdrop_dl.managers.manager import Manager
@@ -15,12 +17,16 @@ def adjust_title(s: str, length: int = 40, placeholder: str = "...") -> str:
return f"{s[:length - len(placeholder)]}{placeholder}" if len(s) >= length else s.ljust(length)
-class SortProgress:
+class SortProgress(DequeProgress):
"""Class that keeps track of sorted files."""
- def __init__(self, visible_task_limit: int, manager: Manager) -> None:
+ type_str = "Folders"
+
+ def __init__(self, visible_tasks_limit: int, manager: Manager) -> None:
+ """Sorter to track the progress of folders being sorted.
+
+ Should work similar to the file_progress but for folders, with a percentage and progress bar for the files within the folders"""
self.manager = manager
- # Sorter to track the progress of folders being sorted, should work similar to the file_progress but for folders, with a percentage and progress bar for the files within the folders
self.progress = Progress(
SpinnerColumn(),
"[progress.description]{task.description}",
@@ -29,127 +35,35 @@ class SortProgress:
"━",
"{task.completed}/{task.total} files",
)
- self.overflow = Progress("[progress.description]{task.description}")
- self.queue = Progress("[progress.description]{task.description}")
- self.progress_group = Group(self.progress, self.overflow, self.queue)
-
- self.color = "plum3"
- self.type_str = "Folders"
- self.progress_str = "[{color}]{description}"
- self.overflow_str = "[{color}]... And {number} Other Folders"
- self.queue_length = 0
- self.queue_str = "[{color}]... And {number} Folders In Sort Queue"
- self.overflow_task_id = self.overflow.add_task(
- self.overflow_str.format(color=self.color, number=0, type_str=self.type_str),
- visible=False,
- )
- self.queue_task_id = self.queue.add_task(
- self.queue_str.format(color=self.color, number=0, type_str=self.type_str),
- visible=False,
- )
+ super().__init__("Sort", visible_tasks_limit)
+
+ # counts
+ self.queue_length = self.audio_count = self.video_count = self.image_count = self.other_count = 0
+
+ def get_queue_length(self) -> int:
+ return self.queue_length
- self.visible_tasks: list[TaskID] = []
- self.invisible_tasks: list[TaskID] = []
- self.completed_tasks: list[TaskID] = []
- self.uninitiated_tasks: list[TaskID] = []
- self.tasks_visibility_limit = visible_task_limit
- self.panel = Panel(
+ def get_progress(self) -> Panel:
+ """Returns the progress bar."""
+ return Panel(
self.progress_group,
title=f"Sorting Downloads ━ Config: {self.manager.config_manager.loaded_config}",
border_style="green",
padding=(1, 1),
)
- # counts
- self.audio_count = 0
- self.video_count = 0
- self.image_count = 0
- self.other_count = 0
-
- def get_progress(self) -> Panel:
- """Returns the progress bar."""
- return self.panel
-
def set_queue_length(self, length: int) -> None:
self.queue_length = length
- def redraw(self) -> None:
- """Redraws the progress bar."""
- while len(self.visible_tasks) > self.tasks_visibility_limit:
- task_id = self.visible_tasks.pop(0)
- self.invisible_tasks.append(task_id)
- self.progress.update(task_id, visible=False)
- while len(self.invisible_tasks) > 0 and len(self.visible_tasks) < self.tasks_visibility_limit:
- task_id = self.invisible_tasks.pop(0)
- self.visible_tasks.append(task_id)
- self.progress.update(task_id, visible=True)
-
- if len(self.invisible_tasks) > 0:
- self.overflow.update(
- self.overflow_task_id,
- description=self.overflow_str.format(
- color=self.color,
- number=len(self.invisible_tasks),
- type_str=self.type_str,
- ),
- visible=True,
- )
- else:
- self.overflow.update(self.overflow_task_id, visible=False)
-
- queue_length = self.queue_length
- if queue_length > 0:
- self.queue.update(
- self.queue_task_id,
- description=self.queue_str.format(color=self.color, number=queue_length, type_str=self.type_str),
- visible=True,
- )
- else:
- self.queue.update(self.queue_task_id, visible=False)
-
def add_task(self, folder: str, expected_size: int | None) -> TaskID:
"""Adds a new task to the progress bar."""
# description = f'Sorting {folder}'
- description = folder
- description = description.encode("ascii", "ignore").decode().strip()
- description = adjust_title(description)
-
- if len(self.visible_tasks) >= self.tasks_visibility_limit:
- task_id = self.progress.add_task(
- self.progress_str.format(color=self.color, description=description),
- total=expected_size,
- visible=False,
- )
- self.invisible_tasks.append(task_id)
- else:
- task_id = self.progress.add_task(
- self.progress_str.format(color=self.color, description=description),
- total=expected_size,
- )
- self.visible_tasks.append(task_id)
- self.redraw()
- return task_id
-
- def remove_folder(self, task_id: TaskID) -> None:
- """Removes the given task from the progress bar."""
- if task_id in self.visible_tasks:
- self.visible_tasks.remove(task_id)
- self.progress.update(task_id, visible=False)
- elif task_id in self.invisible_tasks:
- self.invisible_tasks.remove(task_id)
- elif task_id == self.overflow_task_id:
- self.overflow.update(task_id, visible=False)
- else:
- msg = "Task ID not found"
- raise ValueError(msg)
- self.redraw()
+ description = folder.encode("ascii", "ignore").decode().strip()
+ description = escape(adjust_title(description))
+ return super().add_task(description, expected_size)
def advance_folder(self, task_id: TaskID, amount: int = 1) -> None:
"""Advances the progress of the given task by the given amount."""
- if task_id in self.uninitiated_tasks:
- self.uninitiated_tasks.remove(task_id)
- self.invisible_tasks.append(task_id)
- self.redraw()
self.progress.advance(task_id, amount)
def increment_audio(self) -> None:
diff --git a/cyberdrop_dl/utils/sorting.py b/cyberdrop_dl/utils/sorting.py
index 647ba6514..3c00024a1 100644
--- a/cyberdrop_dl/utils/sorting.py
+++ b/cyberdrop_dl/utils/sorting.py
@@ -106,7 +106,7 @@ class Sorter:
self.sort_other(file, folder_name)
self.manager.progress_manager.sort_progress.advance_folder(task_id)
- self.manager.progress_manager.sort_progress.remove_folder(task_id)
+ self.manager.progress_manager.sort_progress.remove_task(task_id)
queue_length -= 1
self.manager.progress_manager.sort_progress.set_queue_length(queue_length)
await asyncio.sleep(1) # required to update the UI