Files
2026-05-15 10:44:57 -04:00

34 lines
1.2 KiB
Python

"""Page range parsing for partial downloads."""
from __future__ import annotations
def parse_page_selection(selection: str, total_pages: int) -> list[int]:
"""Return zero-based page indices from strings like 'all', '3', or '1-10'."""
selection = selection.strip().lower()
if not selection or selection == "all":
return list(range(total_pages))
indices: set[int] = set()
for part in selection.split(","):
part = part.strip()
if not part:
continue
if "-" in part:
start_s, end_s = part.split("-", 1)
start, end = int(start_s), int(end_s)
if start < 1 or end > total_pages or start > end:
raise ValueError(
f"Range {start}-{end} is outside document bounds (1-{total_pages})."
)
indices.update(range(start - 1, end))
else:
page_num = int(part)
if page_num < 1 or page_num > total_pages:
raise ValueError(
f"Page {page_num} is outside document bounds (1-{total_pages})."
)
indices.add(page_num - 1)
return sorted(indices)