43 lines
1.5 KiB
Python
43 lines
1.5 KiB
Python
"""Locate tablet documents, including the parts of a large checked copy."""
|
|
from pathlib import Path
|
|
|
|
from copienator import configuration
|
|
from copienator.json_io import read_json
|
|
|
|
|
|
def annotation_limits() -> tuple[int, int]:
|
|
values = (configuration.ANNOTATION_MAX_HEIGHT_PX, configuration.ANNOTATION_MAX_PIXELS)
|
|
for name, value in zip(("ANNOTATION_MAX_HEIGHT_PX", "ANNOTATION_MAX_PIXELS"), values):
|
|
if type(value) is not int or value <= 0:
|
|
raise ValueError(f"{name} must be a positive integer (pixels)")
|
|
return values
|
|
|
|
|
|
# Validate global settings at import, as before.
|
|
annotation_limits()
|
|
|
|
|
|
def document_fits(width: int, height: int, max_height: int, max_pixels: int) -> bool:
|
|
return height <= max_height and width * height <= max_pixels
|
|
|
|
|
|
def copy_documents(directory: Path) -> list[Path]:
|
|
try:
|
|
metadata = read_json(directory / "bnote.json", default={})
|
|
except (OSError, ValueError):
|
|
# Ordinary documents need no manifest. Let the reader report bad metadata.
|
|
return [directory]
|
|
parts = metadata.get("parts", []) if isinstance(metadata, dict) else []
|
|
return [directory / name for name in parts] if parts else [directory]
|
|
|
|
|
|
def tablet_documents(root: Path) -> list[tuple[str, Path]]:
|
|
documents = []
|
|
for directory in sorted(root.iterdir()):
|
|
if not directory.is_dir():
|
|
continue
|
|
for part in copy_documents(directory):
|
|
name = directory.name if part == directory else f"{directory.name} {part.name}"
|
|
documents.append((name, part))
|
|
return documents
|