#!/usr/bin/env python3
# Timestamp: 2026-08-10
# File: src/scitex_cv/_document/_types.py
"""The shape a document reading is reported in — the contract itself.
Four page states, not three
---------------------------
``text`` / ``blank`` / ``unreadable`` / ``error``. The consuming layer asked
for three (text / unreadable / error) with the explicit requirement that "a
blank page is a legitimate result and must not look like a failure, and vice
versa". Satisfying that requirement IS the fourth state: a genuinely empty
page and a page the model refused to read are different facts, and collapsing
them re-creates the ambiguity the requirement exists to remove.
``text`` is the ONLY state carrying characters. The others set ``text`` to
``None`` rather than ``""``, because an empty string is precisely the value
that cannot distinguish "nothing was written here" from "nothing was
recovered".
``unreadable`` vs ``error``
---------------------------
``unreadable`` means every rung was tried and each returned layout-only: a
fact about the PAGE. ``error`` means processing could not complete —
unreachable endpoint, corrupt page, decrypt failure: a fact about the RUN,
where the page may be perfectly readable. ``attempts`` records how many rungs
actually ran, so "fell off the end of the ladder" and "died on rung one" are
never confused.
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import Optional, Tuple
STATUS_TEXT = "text"
STATUS_BLANK = "blank"
STATUS_UNREADABLE = "unreadable"
STATUS_ERROR = "error"
PAGE_STATUSES = (STATUS_TEXT, STATUS_BLANK, STATUS_UNREADABLE, STATUS_ERROR)
SOURCE_TEXT_LAYER = "pdf-text-layer"
SOURCE_OCR = "ocr"
[docs]
@dataclass(frozen=True)
class DocumentReading:
"""Ordered per-page results for one input file.
Page-level rather than document-level by request: filing rules key off
page 1, and a 20-page register extract must not collapse into one blob.
"""
path: str
page_count: int
pages: Tuple[PageReading, ...]
ladder: Tuple[Tuple[int, str], ...]
def __post_init__(self) -> None:
if len(self.pages) != self.page_count:
raise ValueError(
f"page_count is {self.page_count} but {len(self.pages)} pages "
"were supplied — a partial result must not claim to be whole"
)
for position, page in enumerate(self.pages):
if page.index != position:
raise ValueError(
"pages must be ordered and contiguous: position "
f"{position} holds page index {page.index}"
)
@property
def unread_pages(self) -> Tuple[int, ...]:
"""Indices that produced no characters, for any reason."""
return tuple(page.index for page in self.pages if not page.read)
# EOF