"""Shared low-level helpers used by every PDF-import family adapter.Declaracion, borrador, and justificante parsers all need the same twoprovenance operations: hash the source bytes for integrity and derive apersistable source reference that does not expose the operator's localfilename. Keeping those helpers here prevents per-format drift in thesecure-storage boundary."""from__future__importannotationsimportloggingfrompathlibimportPathfrom....core.hashingimportsha256_fileas_core_sha256_filefrom....domain.justificanteimportPdfModeloImportError_logger=logging.getLogger(__name__)_INPUT_PDF_SOURCE_LABEL="<input-pdf>"_SOURCE_REFERENCE_ROOT=Path(".secure-source")
[docs]defsha256_file(path:Path)->str:"""Return the lowercase hex SHA-256 of the bytes at ``path``. Delegates to the canonical chunked file digest, wrapping the ``OSError`` raised on an unreadable artefact in the shared PDF-import error so callers see the translated message rather than a raw OS failure. Error messages and logs use ``<input-pdf>`` instead of the concrete path to avoid leaking source filenames through diagnostics. """try:return_core_sha256_file(path)exceptOSErrorasexc:_logger.debug("sha256_file: source=%s failure=%s",_INPUT_PDF_SOURCE_LABEL,type(exc).__name__,)raisePdfModeloImportError(f"PDF file could not be hashed: {_INPUT_PDF_SOURCE_LABEL}",context={"path":_INPUT_PDF_SOURCE_LABEL},translated_message="adapters.inbound.pdf.errors.hash_failed",)fromNone
[docs]defsource_pdf_reference_path(source_pdf_sha256:str)->Path:"""Return the persisted source reference path for a parsed PDF digest. The returned value is ``.secure-source/<sha256>.pdf`` rather than the operator's local filesystem path. Parser records can persist it as provenance without disclosing the source directory or filename, while the companion digest remains the integrity coordinate for the original bytes. """return_SOURCE_REFERENCE_ROOT/f"{source_pdf_sha256}.pdf"