Skip to content

Bulk asynchronous downloader for multiple file hosts

fix: ruff fixes (#2)

NTFSvolumef3a120d8d7d1
diff --git a/cyberdrop_dl/database/tables/hash.py b/cyberdrop_dl/database/tables/hash.py
index cc7c7f346..2d318c8f2 100644
--- a/cyberdrop_dl/database/tables/hash.py
+++ b/cyberdrop_dl/database/tables/hash.py
@@ -17,6 +17,7 @@ if TYPE_CHECKING:
class HashTable:
def __init__(self, database: Database) -> None:
self._database = database
+ self.cwd: Path = Path.cwd().absolute()
@property
def db_conn(self) -> aiosqlite.Connection:
@@ -40,9 +41,7 @@ class HashTable:
"""
query = "SELECT hash FROM hash WHERE folder=? AND download_filename=? AND hash_type=? AND hash IS NOT NULL"
try:
- path = Path(path)
- if not path.is_absolute():
- path = path.absolute()
+ path = self.cwd / path
folder = str(path.parent)
filename = path.name
@@ -124,9 +123,7 @@ class HashTable:
"""
try:
- full_path = Path(file)
- if not full_path.is_absolute():
- full_path = full_path.absolute()
+ full_path = self.cwd / file
download_filename = full_path.name
folder = str(full_path.parent)
await self.db_conn.execute(query, (hash_value, hash_type, folder, download_filename, hash_value))
@@ -146,9 +143,7 @@ class HashTable:
"""
referer_ = str(referer) if referer else None
try:
- full_path = Path(file)
- if not full_path.is_absolute():
- full_path = full_path.absolute()
+ full_path = self.cwd / file
download_filename = full_path.name
folder = str(full_path.parent)
stat = full_path.stat()
diff --git a/cyberdrop_dl/ui/progress/deque_progress.py b/cyberdrop_dl/ui/progress/deque_progress.py
index 2a1ac37c5..ec1645501 100644
--- a/cyberdrop_dl/ui/progress/deque_progress.py
+++ b/cyberdrop_dl/ui/progress/deque_progress.py
@@ -41,7 +41,7 @@ class DequeProgress(ABC):
self.queue_str.format(color=self.color, number=0, type_str=self.type_str, title=self.title_lower),
visible=False,
)
- self._tasks: deque[TaskID] = deque([])
+ self._tasks: deque[TaskID] = deque()
self._tasks_visibility_limit = visible_tasks_limit
@abstractmethod
diff --git a/cyberdrop_dl/utils/sorting.py b/cyberdrop_dl/utils/sorting.py
index b99e4844c..f817f44f8 100644
--- a/cyberdrop_dl/utils/sorting.py
+++ b/cyberdrop_dl/utils/sorting.py
@@ -17,8 +17,6 @@ from cyberdrop_dl.utils.logger import log, log_with_color
from cyberdrop_dl.utils.utilities import purge_dir_tree
if TYPE_CHECKING:
- from collections.abc import AsyncGenerator
-
from cyberdrop_dl.managers.manager import Manager
@@ -41,17 +39,6 @@ class Sorter:
self.video_format: str | None = settings.sorted_video
self.other_format: str | None = settings.sorted_other
- async def _get_files(self, directory: Path) -> AsyncGenerator[Path]:
- """Finds all files in a directory and returns them in a list."""
-
- def resolve_if_file(path: Path) -> Path | None:
- if path.is_file():
- return path.resolve()
-
- for file in directory.rglob("*"):
- if file := await asyncio.to_thread(resolve_if_file, file):
- yield file
-
def _move_file(self, old_path: Path, new_path: Path) -> bool:
"""Moves a file to a destination folder."""
if new_path.is_symlink():
@@ -92,14 +79,13 @@ class Sorter:
files_to_sort: dict[str, list[Path]] = {}
with self.manager.live_manager.get_sort_live(stop=True):
- for subfolder in self.download_folder.iterdir():
- if not await asyncio.to_thread(subfolder.is_dir):
- continue
- files_to_sort[subfolder.name] = [file async for file in self._get_files(subfolder)]
+ for subfolder in await asyncio.to_thread(_subfolders, self.download_folder):
+ files_to_sort[subfolder.name] = await asyncio.to_thread(_get_files, subfolder)
+
await self._sort_files(files_to_sort)
log_with_color("DONE!", "green", 20)
- purge_dir_tree(self.download_folder)
+ _ = purge_dir_tree(self.download_folder)
async def _sort_files(self, files_to_sort: dict[str, list[Path]]) -> None:
queue_length = len(files_to_sort)
@@ -247,3 +233,23 @@ class Sorter:
new_file = Path(file_path)
return self._move_file(file, new_file)
+
+
+def _subfolders(directory: Path) -> list[Path]:
+ def walk():
+ for subfolder in directory.resolve().iterdir():
+ if subfolder.is_dir():
+ yield subfolder
+
+ return list(walk())
+
+
+def _get_files(directory: Path) -> list[Path]:
+ """Finds all files in a directory and returns them in a list."""
+
+ def walk():
+ for file in directory.resolve().rglob("*"):
+ if file.is_file():
+ yield file
+
+ return list(walk())
diff --git a/tests/test_storage.py b/tests/test_storage.py
index 97b5ec8f2..0e6badec1 100644
--- a/tests/test_storage.py
+++ b/tests/test_storage.py
@@ -1,3 +1,4 @@
+import asyncio
import dataclasses
from collections.abc import AsyncGenerator
from pathlib import Path
@@ -15,7 +16,7 @@ async def storage(running_manager: Manager) -> AsyncGenerator[StorageManager]:
async def test_unsupported_fs_should_not_return_zero(storage: StorageManager) -> None:
- cwd = Path().resolve()
+ cwd = await asyncio.to_thread(Path().resolve)
free_space = await storage._get_free_space(cwd)
assert free_space > 0
with mock.patch("psutil.disk_usage", side_effect=OSError(None, "operation not supported")):
@@ -28,7 +29,7 @@ async def test_unsupported_fs_should_not_return_zero(storage: StorageManager) ->
async def test_fuse_filesystem_should_not_return_zero(storage: StorageManager) -> None:
- cwd = Path().resolve()
+ cwd = await asyncio.to_thread(Path().resolve)
partition = storage._get_partition(cwd)
assert partition
storage._partitions = [dataclasses.replace(partition, fstype="fuse")]