Skip to content

Bulk asynchronous downloader for multiple file hosts

feat: add report subcommand (#1978)

NTFSvolume37bf0d454690
diff --git a/cyberdrop_dl/cli/__init__.py b/cyberdrop_dl/cli/__init__.py
index 8af050f0c..33ac60a77 100644
--- a/cyberdrop_dl/cli/__init__.py
+++ b/cyberdrop_dl/cli/__init__.py
@@ -58,12 +58,14 @@ def register_commands() -> None:
from cyberdrop_dl.cli.clean_up import app as cleanup
from cyberdrop_dl.cli.database import app as database
from cyberdrop_dl.cli.download import download
+ from cyberdrop_dl.cli.report import report
from cyberdrop_dl.cli.show import show
+ app.default(download)
app.command(database)
app.command(show)
- app.default(download)
app.command(cleanup)
+ app.command(report)
register_commands()
diff --git a/cyberdrop_dl/cli/report.py b/cyberdrop_dl/cli/report.py
new file mode 100644
index 000000000..56ad353b8
--- /dev/null
+++ b/cyberdrop_dl/cli/report.py
@@ -0,0 +1,54 @@
+from __future__ import annotations
+
+from typing import TYPE_CHECKING, Any
+
+from cyberdrop_dl import ALL_DEPENDENCIES, __version__, ffmpeg
+from cyberdrop_dl.utils import get_system_information
+from cyberdrop_dl.utils.markdown import Row, markdown_table
+
+if TYPE_CHECKING:
+ from collections.abc import Generator, Iterable, Sequence
+
+
+def _unpack(data: Iterable[tuple[str, Any]]) -> Generator[tuple[str, str]]:
+ for name, value in data:
+ yield name, str(value)
+
+
+def _table(title: str, headers: Sequence[str], rows: Iterable[Row]) -> str:
+ return f"## {title}\n\n" + markdown_table(headers, *rows) + "\n"
+
+
+def generate_report() -> str:
+ return "\n".join(
+ [
+ _table(
+ "Version",
+ ["Name", "Value"],
+ [
+ ("cyberdrop-dl", __version__),
+ ("ffmpeg", str(ffmpeg.version())),
+ ("ffprobe", str(ffmpeg.ffprobe_version())),
+ ],
+ ),
+ _table(
+ "System",
+ ["Name", "Value"],
+ _unpack(get_system_information().items()),
+ ),
+ _table(
+ "Dependencies",
+ ["Package", "Version"],
+ _unpack(sorted(ALL_DEPENDENCIES.items())),
+ ),
+ ]
+ )
+
+
+def report() -> None:
+ """Generate and display information about the system"""
+ print(generate_report()) # noqa: T201
+
+
+if __name__ == "__main__":
+ report()
diff --git a/cyberdrop_dl/utils/markdown.py b/cyberdrop_dl/utils/markdown.py
new file mode 100644
index 000000000..342c65e9c
--- /dev/null
+++ b/cyberdrop_dl/utils/markdown.py
@@ -0,0 +1,57 @@
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+if TYPE_CHECKING:
+ from collections.abc import Generator, Iterable, Sequence
+
+type Row = list[str] | tuple[str, ...]
+type _RowLike = Iterable[str]
+
+
+def markdown_table(headers: Sequence[str], *rows: Row) -> str:
+ if len(headers) == 0:
+ raise ValueError("At least 1 header is required")
+
+ if len(rows) == 0:
+ raise ValueError("At least 1 row is required")
+
+ _sanity_check(headers)
+ return "\n".join(_md_table_lines([headers, *rows]))
+
+
+def _md_table_lines(rows: Sequence[_RowLike]) -> Generator[str]:
+ column_widths = _get_columns_widths(rows)
+
+ def justify(row: _RowLike) -> _RowLike:
+ for value, width in zip(row, column_widths, strict=True):
+ yield value.strip().ljust(width)
+
+ lines = (_compose_md_line(justify(row)) for row in rows)
+ yield next(lines)
+ yield _compose_md_line("-" * w for w in column_widths)
+ yield from lines
+
+
+def _compose_md_line(row: _RowLike) -> str:
+ return "| " + " | ".join(value.replace("|", r"\|") for value in row) + " |"
+
+
+def _sanity_check[T: _RowLike](row: T) -> T:
+ if isinstance(row, str):
+ raise TypeError("strings are not valid rows", row)
+ return row
+
+
+def _get_columns_widths(rows: Sequence[_RowLike]) -> tuple[int, ...]:
+ row_widths = (map(len, _sanity_check(row)) for row in rows if row)
+ return tuple(map(max, zip(*row_widths, strict=True)))
+
+
+if __name__ == "__main__":
+ print( # noqa: T201
+ markdown_table(
+ ["col 1", "col 2", " col 3"],
+ ["val 1", "val 2 long", "val 3"],
+ )
+ )
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 90ebef3c7..129a29a41 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -1,6 +1,8 @@
import pytest
+from cyberdrop_dl import __version__
from cyberdrop_dl.__main__ import run_cdl
+from cyberdrop_dl.cli.report import generate_report
@pytest.mark.parametrize(
@@ -17,3 +19,10 @@ def test_command_by_console_output(capsys: pytest.CaptureFixture[str], command:
pass
output = capsys.readouterr().out
assert text in output
+
+
+def test_report() -> None:
+ report = generate_report()
+
+ for value in "cyberdrop-dl", __version__, "aiohttp", "GIL enabled":
+ assert value in report
diff --git a/tests/test_markdown.py b/tests/test_markdown.py
new file mode 100644
index 000000000..719f396f9
--- /dev/null
+++ b/tests/test_markdown.py
@@ -0,0 +1,69 @@
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+import pytest
+
+from cyberdrop_dl.utils import markdown
+
+if TYPE_CHECKING:
+ from collections.abc import Sequence
+
+
+def test_empty_headers_raise_value_error() -> None:
+ with pytest.raises(ValueError, match="header"):
+ _ = markdown.markdown_table((), ("a", "b"))
+
+
+def test_empty_rows_raise_value_error() -> None:
+ with pytest.raises(ValueError, match="row"):
+ _ = markdown.markdown_table(("a", "b"))
+
+
+ ("headers", "rows"),
+ [
+ ("headers", [("a", "b")]),
+ (("a", "b"), ["row1", "row2"]),
+ ],
+)
+def test_strings_are_not_valid_rows(headers: Sequence[str], rows: tuple[markdown.Row]) -> None:
+ with pytest.raises(TypeError, match="rows"):
+ _ = markdown.markdown_table(headers, *rows)
+
+
+def test_get_columns_widths() -> None:
+ rows = ["a", "b", "c"], ["aa", "b", "c"], ["a", "bbbb", "c"]
+ assert markdown._get_columns_widths(rows) == (2, 4, 1)
+
+
+ ("values", "expected"),
+ [
+ (("1", "2", "3"), "| 1 | 2 | 3 |"),
+ (("a", "example", "and", "another"), "| a | example | and | another |"),
+ (("name",), "| name |"),
+ (("a|b", "c", "name|"), r"| a\|b | c | name\| |"),
+ ],
+)
+def test_compose_md_line(values: tuple[str, ...], expected: str) -> None:
+ assert markdown._compose_md_line(values) == expected
+
+
+def test_md_table_lines() -> None:
+ rows = [
+ ("Index", "Name", "Description", "Price"),
+ ("1", "Compact Printer", "Air Advanced | Digital", "100.0"),
+ ("2", "Tablet", "Discussion loss politics free one thousand", "22.3"),
+ ("3", "Smart Blender Cooker", "discontinued", "0"),
+ ]
+ md = list(markdown._md_table_lines(rows))
+ expected = [
+ "| Index | Name | Description | Price |",
+ "| ----- | -------------------- | ------------------------------------------ | ----- |",
+ "| 1 | Compact Printer | Air Advanced \\| Digital | 100.0 |",
+ "| 2 | Tablet | Discussion loss politics free one thousand | 22.3 |",
+ "| 3 | Smart Blender Cooker | discontinued | 0 |",
+ ]
+ for line, expected_line in zip(md, expected, strict=True):
+ assert line == expected_line