Files
Copies/copienator/annotation_documents.py
T

38 lines
1.5 KiB
Python

"""Locate tablet documents, including the parts of a large checked copy."""
from pathlib import Path
from copienator.configuration import ANNOTATION_MAX_HEIGHT_PX, ANNOTATION_MAX_PIXELS
from copienator.json_io import read_json
if type(ANNOTATION_MAX_HEIGHT_PX) is not int or ANNOTATION_MAX_HEIGHT_PX <= 0:
raise ValueError("ANNOTATION_MAX_HEIGHT_PX must be a positive integer (pixels)")
MAX_HEIGHT_PX = ANNOTATION_MAX_HEIGHT_PX
if type(ANNOTATION_MAX_PIXELS) is not int or ANNOTATION_MAX_PIXELS <= 0:
raise ValueError("ANNOTATION_MAX_PIXELS must be a positive integer (pixels)")
MAX_PIXELS = ANNOTATION_MAX_PIXELS
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