_protocols.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """Helpers for working with PDF types."""
  2. from abc import abstractmethod
  3. from pathlib import Path
  4. from typing import IO, Any, Optional, Protocol, Union
  5. from ._utils import StrByteType, StreamType
  6. class PdfObjectProtocol(Protocol):
  7. indirect_reference: Any
  8. def clone(
  9. self,
  10. pdf_dest: Any,
  11. force_duplicate: bool = False,
  12. ignore_fields: Union[tuple[str, ...], list[str], None] = (),
  13. ) -> Any:
  14. ... # pragma: no cover
  15. def _reference_clone(self, clone: Any, pdf_dest: Any) -> Any:
  16. ... # pragma: no cover
  17. def get_object(self) -> Optional["PdfObjectProtocol"]:
  18. ... # pragma: no cover
  19. def hash_value(self) -> bytes:
  20. ... # pragma: no cover
  21. def write_to_stream(
  22. self, stream: StreamType, encryption_key: Union[None, str, bytes] = None
  23. ) -> None:
  24. ... # pragma: no cover
  25. class XmpInformationProtocol(PdfObjectProtocol):
  26. pass
  27. class PdfCommonDocProtocol(Protocol):
  28. @property
  29. def pdf_header(self) -> str:
  30. ... # pragma: no cover
  31. @property
  32. def pages(self) -> list[Any]:
  33. ... # pragma: no cover
  34. @property
  35. def root_object(self) -> PdfObjectProtocol:
  36. ... # pragma: no cover
  37. def get_object(self, indirect_reference: Any) -> Optional[PdfObjectProtocol]:
  38. ... # pragma: no cover
  39. @property
  40. def strict(self) -> bool:
  41. ... # pragma: no cover
  42. class PdfReaderProtocol(PdfCommonDocProtocol, Protocol):
  43. @property
  44. @abstractmethod
  45. def xref(self) -> dict[int, dict[int, Any]]:
  46. ... # pragma: no cover
  47. @property
  48. @abstractmethod
  49. def trailer(self) -> dict[str, Any]:
  50. ... # pragma: no cover
  51. class PdfWriterProtocol(PdfCommonDocProtocol, Protocol):
  52. _objects: list[Any]
  53. _id_translated: dict[int, dict[int, int]]
  54. incremental: bool
  55. _reader: Any # PdfReader
  56. @abstractmethod
  57. def write(self, stream: Union[Path, StrByteType]) -> tuple[bool, IO[Any]]:
  58. ... # pragma: no cover
  59. @abstractmethod
  60. def _add_object(self, obj: Any) -> Any:
  61. ... # pragma: no cover