ó
    /j-jSM  ã                  ó¦  • S r SSKJr  SSKrSSKrSSKrSSKJr  SSK	J
r
  SSKJr  SSKJr  SSKJr  SSKrSS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJ r   SSK!J"r"  SSK!J#r#  SSK!J$r$  SSK!J%r%  SSK!J&r&  \\ \\\S.r'\(\-  \)-  \*-  \+-  \\)   -  r,S!S jr-S"S jr.S#S jr/ " S S 5      r0g)$z0Functions to handle all operations on the PDF's.é    )ÚannotationsN)Úpartial)Úchain)ÚPath)ÚIO)ÚAny)ÚPDFPasswordIncorrect)ÚPDFTextExtractionNotAllowed)ÚLTChar)ÚLTImage)ÚLTTextLineHorizontal)ÚLTTextLineVerticalé   )Ú	TableList)ÚHybrid)ÚLattice)ÚMachineLearning)ÚNetwork)ÚStream)Údownload_url)Úget_image_char_and_text_objects)Úget_page_layout)Úget_rotation)Úis_url)ÚlatticeÚstreamÚnetworkÚhybridÚmlc                óž   • [         R                  " SSSS9 nUR                  U 5        UR                  sSSS5        $ ! , (       d  f       g= f)u¿  Write `data` to a NamedTemporaryFile and return its path.

Used by ``PDFHandler`` when the caller passes a ``bytes``-like object
or a file-like stream rather than a filesystem path â€” the Lattice
flavor's OpenCV image-conversion backend needs a real on-disk file,
so the simplest contract is "always spill once, treat as a path from
here on, clean up on close()". The file is created with ``delete=False``
and reaped in :meth:`PDFHandler.close`.
zcamelot-z.pdfF)ÚprefixÚsuffixÚdeleteN)ÚtempfileÚNamedTemporaryFileÚwriteÚname)ÚdataÚfs     ÚM/home/mande/repo/quber/.venv/lib/python3.13/site-packages/camelot/handlers.pyÚ_spill_bytes_to_tempfiler+   .   s>   € ô 
×	$Ò	$Ø &°ò
à	
Ø	�‰�ŒØ�v‰v÷	
÷ 
÷ 
ús	   —>¾
Ac                óâ  • [        U SS5      n[        U SS5      n[        U5      (       a  U" 5       OSn[        U5      (       a	   U" S5        U R	                  5       nUb  [        U5      (       a	   U" U5        [        U[        [        [        45      (       d!  [        S[        U5      R                   35      e[        U5      $ ! [        [        4 a     NŠf = f! [        [        4 a     Ntf = f)u%  Read a binary stream from the start, preserve the caller's cursor.

Always reads from position 0 â€” a PDF mid-stream isn't useful â€” and
seeks back to whatever position the caller had before the call so
they can keep using the same handle. Non-seekable streams (sockets,
pipes) silently fall through: ``read()`` returns whatever's left,
and the seek-back is best-effort.

Raises ``TypeError`` when ``read()`` returns text rather than bytes,
which catches accidental text-mode opens before the bad data reaches
pdfium and produces a clearer error.
ÚtellNÚseekr   z9file-like 'filepath' must return bytes from .read(), got )ÚgetattrÚcallableÚOSErrorÚ
ValueErrorÚreadÚ
isinstanceÚbytesÚ	bytearrayÚ
memoryviewÚ	TypeErrorÚtypeÚ__name__)Úfilepathr-   r.   Úposr(   s        r*   Ú_consume_filelike_to_bytesr=   ?   sæ   € ô �8˜V TÓ*€DÜ�8˜V TÓ*€DÜ˜T—N‘N‰$Œ&¨€CÜ�‡~�~ð	Ù�ŒGð �=‰=‹?€DØ
�œ8 DŸ>™>ð	Ù�ŒIô �dœU¤I¬zÐ:×;Ñ;ÜðÜ˜“J×'Ñ'Ð(ð*ó
ð 	
ô �‹;Ðøô œÐ$ó 	Ùð	ûô œÐ$ó 	Ùð	ús$   ÁC Á1C ÃCÃCÃC.Ã-C.c                óH  • [        U [        [        [        45      (       a  [	        [        U 5      5      S4$ [        U S5      (       a2  [        U R                  5      (       a  [        U 5      n[	        U5      S4$ [        U 5      (       a  [        [        U 5      5      S4$ U S4$ )u"  Normalise read_pdf's polymorphic filepath argument to a real path.

Returns ``(path, is_temp)``. ``is_temp`` is True when the returned
path points to a tempfile we created (and therefore own â€” the
caller's :meth:`PDFHandler.close` should reap it).

Three branches:

* ``bytes`` / ``bytearray`` / ``memoryview``: spill to a tempfile.
* binary file-like (``.read()`` returning bytes): read once,
  preserve the caller's cursor, spill to a tempfile.
* everything else (path / URL): URL â†’ download to tempfile; path
  â†’ pass through untouched.
Tr3   F)r4   r5   r6   r7   r+   Úhasattrr0   r3   r=   r   r   Ústr)r;   r(   s     r*   Ú_resolve_filepathrA   b   sŒ   € ô �(œU¤I¬zÐ:×;Ñ;Ü'¬¨h«Ó8¸$Ð>Ð>Üˆx˜× Ñ ¤X¨h¯m©m×%<Ñ%<Ü)¨(Ó3ˆÜ'¨Ó-¨tÐ3Ð3Üˆh×ÑÜœC ›MÓ*¨DÐ0Ð0ð �Uˆ?Ðó    c                  óø   • \ rS rSrSr   S SS jjr\SS j5       rSSS jjr\	SS j5       r
SS jrSS	 jrSS
 jrS r    SS jr        S               SS jjr   S       SS jjrSrg)Ú
PDFHandleré}   u÷  Handles all operations on the PDF's.

Handles all operations like temp directory creation, splitting
file into single page PDFs, parsing each PDF and then removing the
temp directory.

Parameters
----------
filepath : str, Path, bytes, or binary file-like
    Source PDF. Accepts a filesystem path / URL, or â€” since #270 â€”
    a ``bytes``-like object or any binary stream with a ``.read()``
    method (``io.BytesIO``, an open ``"rb"`` file, ``requests``
    response ``.raw``, etc). In the in-memory cases the bytes are
    spilled to a temporary file once and cleaned up when the handler
    is closed; this keeps the rest of the pipeline (in particular
    the Lattice OpenCV image-conversion backend) unchanged.
pages : str, optional (default: '1')
    Comma-separated page numbers.
    Example: '1,3,4' or '1,4-end' or 'all'.
password : str, optional (default: None)
    Password for decryption.
debug : bool, optional (default: False)
    Whether the parser should store debug information during parsing.
Nc                óš   • X@l         [        U5      u  oPl        XPl        Uc  SU l        OX0l        X l        US:X  a	  S/U l        g S U l        g )NÚ Ú1r   )ÚdebugrA   Úis_temp_filer;   ÚpasswordÚ_pages_specÚ_pages_cache)Úselfr;   ÚpagesrK   rI   Úresolveds         r*   Ú__init__ÚPDFHandler.__init__—   sO   € ð Œ
Ü&7¸Ó&AÑ#ˆÔ#Ø$,ŒàÑØˆD�Mà$ŒMð !ÔØ5:¸c³\¨q¨cˆÕÀtˆÕrB   c                ót   • U R                   c   U R                  U R                  5      U l         U R                   $ )z§Resolved 1-based page numbers, sorted and de-duplicated.

Lazy: only opens the PDF if the spec is something other than the
default ``"1"``. Cached after first access.
)rM   Ú_resolve_pagesrL   ©rN   s    r*   rO   ÚPDFHandler.pages¬   s5   € ð ×ÑÑ$Ø $× 3Ñ 3°D×4DÑ4DÓ EˆDÔØ× Ñ Ð rB   c                ó  • US:X  a  S/$ UcE  [         R                  " U R                  SU R                  S9 nU R	                  X5      sSSS5        $ U R                  U[        UR                  5      5      $ ! , (       d  f       N3= f)zÂConvert the ``pages`` spec to a sorted, de-duplicated list of ints.

Pass an already-open ``pdf`` to avoid the playa.open() round-trip
that would otherwise be needed to read ``len(pdf.pages)``.
rH   r   NÚpage©ÚspacerK   )ÚplayaÚopenr;   rK   rT   Ú_expand_pages_specÚlenrO   )rN   rO   Úpdfs      r*   rT   ÚPDFHandler._resolve_pages·   sn   € ð �C‹<Ø�3ˆJØ‰;Ü—’˜DŸM™M°À$Ç-Á-ÒPÐTWØ×*Ñ*¨5Ó6÷ QÑPà×&Ñ& u¬c°#·)±)«nÓ=Ð=÷ QÕPús   ·A7Á7
Bc                óÔ  • / nU S:X  a  UR                  SUS.5        O‰U R                  S5       Ht  nSU;   aE  UR                  S5      u  pEUS:X  a  UO
[        U5      nUR                  [        U5      US.5        MN  UR                  [        U5      [        U5      S.5        Mv     / nU H'  nUR                  [	        US   US   S-   5      5        M)     [        [        U5      5      $ )zCExpand a pages spec string (``"all"``, ``"1,3,4"``, ``"1,4-end"``).Úallr   ©ÚstartÚendÚ,Ú-re   rd   )ÚappendÚsplitÚintÚextendÚrangeÚsortedÚset)	rO   Ú
page_countÚpage_numbersÚrÚaÚbÚb_intÚresultÚps	            r*   r]   ÚPDFHandler._expand_pages_specÄ   sÎ   € ð .0ˆØ�E‹>Ø×Ñ¨!°JÑ ?Õ@à—[‘[ Ö%�Ø˜!“8ØŸ7™7 3›<‘D�AØ*+¨u«*™J¼#¸a»&�EØ ×'Ñ'´#°a³&ÀÑ(GÖHà ×'Ñ'´#°a³&ÄÀQÃÑ(HÖIñ &ð ˆÛˆAØ�M‰Mœ%  '¡
¨A¨e©H°q©LÓ9Ö:ñ ä”c˜&“kÓ"Ð"rB   c                ó   • U $ )u¶   Allow ``PDFHandler`` to be used as a context manager.

On ``__exit__`` any temp file created by :func:`download_url` (when
the caller passed a URL) is removed â€” see :meth:`close`.
© rU   s    r*   Ú	__enter__ÚPDFHandler.__enter__×   s	   € ð ˆrB   c                ó$   • U R                  5         g)z.Clean up the URL-downloaded temp file, if any.N)Úclose)rN   Úexc_typeÚexc_valÚexc_tbs       r*   Ú__exit__ÚPDFHandler.__exit__ß   s   € à�
‰
�rB   c                ó  • U R                   (       d  gU R                  n[        U[        [        45      (       aD  [
        R                  R                  U5      (       a   [
        R                  " U5        SU l         ggg! [         a     Nf = f)zåDelete the URL-downloaded temp file, if any.

Idempotent; safe to call from both ``__exit__`` and an explicit
``handler.close()`` call. No-op when ``filepath`` was a user-owned
path (we never delete a file the caller passed in).
NF)
rJ   r;   r4   r@   r   ÚosÚpathÚexistsÚremover1   )rN   r…   s     r*   r}   ÚPDFHandler.closeã   su   € ð × × ØØ�}‰}ˆÜ�dœS¤$˜K×(Ñ(¬R¯W©W¯^©^¸D×-AÑ-Að	Ü—	’	˜$”ð !&ˆDÕð .BÐ(øô ó ñ ðús   ÁA? Á?
BÂBc                ó¸  • / nUS:X  a  UR                  SSS.5        Oê[        R                  " U R                  SU R                  S9 n[        UR                  5      nSSS5        US:X  a  UR                  SWS.5        O‡UR                  S5       Hr  nS	U;   aC  UR                  S	5      u  pgUS
:X  a  WnUR                  [        U5      [        U5      S.5        ML  UR                  [        U5      [        U5      S.5        Mt     / nU H'  n	UR                  [        U	S   U	S
   S-   5      5        M)     [        [        U5      5      $ ! , (       d  f       Nó= f)a¯  Convert pages string to list of integers.

.. deprecated::
    Use the :attr:`pages` property; this method exists only for
    backwards-compat with callers that imported it directly.

Parameters
----------
filepath : str
    Filepath or URL of the PDF file.
pages : str, optional (default: '1')
    Comma-separated page numbers.
    Example: '1,3,4' or '1,4-end' or 'all'.

Returns
-------
P : list
    List of int page numbers.

rH   r   rc   rX   rY   Nrb   rf   rg   re   rd   )rh   r[   r\   r;   rK   r^   rO   ri   rj   rk   rl   rm   rn   )
rN   rO   rp   r_   ro   rq   rr   rs   ru   rv   s
             r*   Ú
_get_pagesÚPDFHandler._get_pagesþ   s  € ð* ˆà�C‹<Ø×Ñ¨!°AÑ 6Õ7ä—’˜DŸM™M°À$Ç-Á-ÒPÐTWÜ  §¡›^�
÷ Qà˜‹~Ø×#Ñ#¨a¸
Ñ$CÕDàŸ™ SÖ)�AØ˜a“xØ Ÿw™w s›|™˜Ø ›:Ø *˜AØ$×+Ñ+´c¸!³fÄSÈÃVÑ,LÖMà$×+Ñ+´c¸!³fÄSÈÃVÑ,LÖMñ *ð ˆÛˆAØ�M‰Mœ%  '¡
¨A¨e©H°q©LÓ9Ö:ñ ä”c˜&“kÓ"Ð"÷! QÕPús   ÁEÅ
Ec                ó†  • [        U40 UD6u  p4[        U5      u  pVpx[        XgU5      n	U	(       aŠ  U	S:X  a*  UR                  UR                  UR
                  S-
  5        O>U	S:X  a*  UR                  UR                  UR
                  S-   5        O[        SU	 35      e[        U40 UD6u  p4[        U5      u  pVpxUUUUUUU	4$ )a  Get layout from a page.

Parameters
----------
page : playa.Page
    Page in the document.


Returns
-------
layout : object

dimensions : tuple
    The dimensions of the pdf page

filepath : str
    The path of the single page PDF - either the original, or a
    normalized version.

Ú	clockwiseéZ   Úanticlockwisez2rotation should be clockwise or anticlockwise, is )r   r   r   Úset_initial_ctmrZ   ÚrotateÚAssertionError)
rN   rX   Úlayout_kwargsÚlayoutÚ
dimensionsÚimagesÚcharsÚhorizontal_textÚvertical_textÚrotations
             r*   Ú_get_layoutÚPDFHandler._get_layout*  sà   € ô> -¨TÑC°]ÑCÑˆä8WØó9
Ñ5ˆ�ô   ¸ÓFˆÞà˜;Ó&à×$Ñ$ T§Z¡Z°·±¸rÑ1AÕBØ˜_Ó,à×$Ñ$ T§Z¡Z°·±¸rÑ1AÕBä$ØHÈÈ
ÐSóð ô "1°Ñ!G¸Ñ!GÑˆFä/°Ó7ñ :ˆF˜?ð ØØØØØØð
ð 	
rB   c	                ó¨  • Uc  0 nU=(       d    0 n[         U   n
U
" S
SU R                  0U	D6nU(       a  [        US5      (       a  X‹l        [        R
                  " 5       nU(       a!  US:”  a  Uc  UO[        S[        XL5      5      nOSn [        R                  " U R                  U R                  SUS9 nUR                  (       d  [        SU R                   35      eUc:  U R                  c!  U R                  U R                   U5      U l        U R                  nU Vs/ s H  oîS-
  PM	     nnUR"                  U   R%                  ['        U R(                  UUUU	US95      nSSS5        [/        [1        [2        R4                  " W5      5      5      $ s  snf ! , (       d  f       N;= f! [*         a  n[-        S	5      UeSnAf[         a    e f = f)a¦  Extract tables by calling parser.get_tables on all single page PDFs.

Parameters
----------
flavor : str (default: 'lattice')
    The parsing method to use.
    Lattice is used by default.
suppress_stdout : bool (default: False)
    Suppress logs and warnings.
parallel : bool (default: False)
    Process pages in parallel using all available cpu cores.
cpu_count : int, optional (default: None)
    Maximum number of worker processes to use when ``parallel`` is
    True. ``None`` (default) uses all available cores. Values are
    clamped to ``[1, multiprocessing.cpu_count()]``. Ignored when
    ``parallel`` is False.
layout_kwargs : dict, optional (default: {})
    A dict of `pdfminer.layout.LAParams
    <https://pdfminersix.readthedocs.io/en/latest/reference/composable.html#laparams>`_ kwargs.
kwargs : dict
    See camelot.read_pdf kwargs.

Returns
-------
tables : camelot.core.TableList
    List of tables found in PDF.

NrI   Ú_render_cacher   rX   )rK   rZ   Úmax_workersz Text extraction is not allowed: )Úparserr“   ÚflavorÚbase_kwargsÚper_pagezFile has not been decryptedry   )ÚPARSERSrI   r?   rž   ÚmpÚ	cpu_countÚmaxÚminr[   r\   r;   rK   Úis_extractabler
   rM   rT   rL   rO   Úmapr   Ú_parse_pager	   ÚRuntimeErrorr   rm   r   Úfrom_iterable)rN   r¡   Úsuppress_stdoutÚparallelr¦   r“   r£   rO   Úrender_cacheÚkwargsÚ
parser_objr    Úmax_cpusr_   ÚxÚtablesÚes                    r*   ÚparseÚPDFHandler.parsej  s¾  € ðP Ñ ØˆMØ—>˜rˆô ˜V‘_ˆ
ÙÑ7 $§*¡*Ð7°Ñ7ˆÞœG F¨O×<Ñ<ð $0Ô ô —<’<“>ˆÞ˜ 1›à%Ñ-‘´3°q¼#¸iÓ:RÓ3Sñ ð ˆIð!	Ü—’Ø—‘ØŸ™ØØ%ò	ð
 Ø×)×)Ü5Ø:¸4¿=¹=¸/ÐJóð ð ‘=Ø×(Ñ(Ñ0Ø,0×,?Ñ,?À×@PÑ@PÐRUÓ,V˜Ô)Ø ×-Ñ-�EÙ(-Ó.ª 1˜Qœ©�Ð.ØŸ™ 5Ñ)×-Ñ-ÜØ×(Ñ(Ø%Ø&3Ø%Ø$*Ø!)ñó	�÷'ôB œ¤× 3Ò 3°FÓ ;Ó<Ó=Ð=ùò /÷%õ ûô: $ó 	EÜÐ<Ó=À1ÐDûÜ*ó 	Øð	úsI   Â+F, Â7A+FÄ"FÄ06FÅ&F, ÆFÆ
F)Æ%F, Æ)F, Æ,
GÆ6GÇGc                óè  • UR                   S-   nU=(       d    0 R                  U5      nU(       ag  UR                  SU5      n	[        U=(       d    0 5      n
UR                  5        H  u  p¼US:w  d  M  XÊU'   M     [        U	   " SSU R
                  0U
D6nOUnU R                  " U40 UD6u  pïnnnnnUR                  U R                  UUUR                   S-   UUUUUS9	  UR                  5       nU$ )ar  Extract tables by calling parser.get_tables on a single page PDF.

Parameters
----------
page : playa.Page
    Page to parse
parser : Lattice, Stream, Network or Hybrid
    The default parser to use when no per-page override applies.
layout_kwargs : dict, optional (default: {})
    A dict of `pdfminer.layout.LAParams
    <https://pdfminersix.readthedocs.io/en/latest/reference/composable.html#laparams>`_ kwargs.
flavor : str, optional
    The global flavor; used as the fallback when a per-page override
    doesn't itself supply ``flavor=``.
base_kwargs : dict, optional
    The global (already-cleaned) parser kwargs. Merged with any
    per-page override to construct a fresh parser for that page.
per_page : dict, optional
    Page-number-keyed kwargs overrides (already validated upstream).

Returns
-------
tables : camelot.core.TableList
    List of tables found in PDF.

r   r¡   rI   )r“   ry   )
Úpage_idxÚgetÚdictÚitemsr¤   rI   r›   Úprepare_page_parser;   Úextract_tables)rN   rX   r    r“   r¡   r¢   r£   Úpage_noÚ	overridesÚpage_flavorÚmergedÚkÚvÚpage_parserr”   r•   r–   r—   r˜   r™   rš   rµ   s                         r*   r«   ÚPDFHandler._parse_pageÐ  s  € ðH —-‘- !Ñ#ˆØ—^ ×(Ñ(¨Ó1ˆ	ÞØ#Ÿ-™-¨°&Ó9ˆKÜ˜+×+¨Ó,ˆFØ!Ÿ™Ö)‘�Ø˜•=Ø !˜1“Iñ *ô " +Ò.ÑJ°T·Z±ZÐJÀ6ÑJ‰Kà ˆKà×Ò˜TÑ3 ]Ñ3ñ 	Tˆ˜F E¨?¸MÈ8ð 	×&Ñ&Ø�M‰MØØØ�M‰M˜AÑØØØØØ'ð 	'ñ 
	
ð ×+Ñ+Ó-ˆØˆrB   )rM   rL   rI   r;   rJ   rK   )rH   NF)r;   ÚFilepathOrBuffer)Úreturnú	list[int])N)rO   r@   r_   z
Any | NonerÉ   rÊ   )rO   r@   ro   rj   rÉ   rÊ   )rÉ   rD   )rÉ   ÚNone)rX   ú
playa.PagerÉ   zwtuple[Any, tuple[float, float], list[LTImage], list[LTChar], list[LTTextLineHorizontal], list[LTTextLineVertical], str])r   FFNNNNN)r¡   r@   r®   Úboolr¯   rÍ   r¦   z
int | Noner“   údict[str, Any] | Noner£   ú dict[int, dict[str, Any]] | NonerO   zlist[int] | Noner°   zdict[int, str] | None)r   NN)rX   rÌ   r¡   r@   r¢   rÎ   r£   rÏ   )r:   Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__rQ   ÚpropertyrO   rT   Ústaticmethodr]   rz   r�   r}   rŠ   r›   r·   r«   Ú__static_attributes__ry   rB   r*   rD   rD   }   s-  † ñð8 ØØðLà"õLð* ó!ó ð!ö>ð ó#ó ð#ô$ôô&ò6*#ðX>
Øð>
ð
ô>
ðD  Ø %ØØ $Ø/3Ø59Ø"&Ø.2ðd>àðd>ð ðd>ð ð	d>ð
 ðd>ð -ðd>ð 3ðd>ð  ðd>ð ,õd>ðV  Ø-1Ø59ð>àð>ð
 ð>ð +ð>ð 3÷>ð >rB   rD   )r(   r5   rÉ   r@   )r;   z	IO[bytes]rÉ   r5   )r;   rÈ   rÉ   ztuple[str | Path, bool])1rÓ   Ú
__future__r   Úmultiprocessingr¥   r„   r$   Ú	functoolsr   Ú	itertoolsr   Úpathlibr   Útypingr   r   r[   Úplaya.exceptionsr	   r
   Úplaya.minerr   r   r   r   Úcorer   Úparsersr   r   r   r   r   Úutilsr   r   r   r   r   r¤   r@   r5   r6   r7   rÈ   r+   r=   rA   rD   ry   rB   r*   Ú<module>râ      s±   ðÙ 6å "ã Û 	Û Ý Ý Ý Ý Ý ã Ý 1Ý 8Ý Ý Ý ,Ý *å Ý Ý Ý $Ý Ý Ý Ý 2Ý "Ý Ý ð ØØØØ
ñ€ð ˜‘: Ñ%¨	Ñ1°JÑ>ÀÀEÁÑJÐ ôô" ôF÷6Qò QrB   