ó
    �®žjÑ-  ã            	       ó  • S 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	J
r
  SSKJr  SSKrSSKJrJrJr  SSKJr  SS	KJr  SS
KJr  SrSr " S S\5      rS\S\4S jrS\S\S-  4S jr S S\S\ \\4   S\4S jjr!  S!S\\-  \-  S\ \\4   S-  S\"S-  S\4S jjr#  S!S\\-  \-  S\ \\4   S-  S\S-  S\4S jjr$  S!S\\-  \-  S\ \\4   S-  S\S-  S\4S jjr%\" S5       S"S\\-  \-  S\ \\4   S-  S\4S jj5       r&g)#zFile-related utilities.é    N)ÚBytesIO)ÚPath)ÚOptionalÚUnion)Úurlparse)Ú
AnyHttpUrlÚTypeAdapterÚValidationError)Ú
deprecated)Úrelative_path)ÚDocumentStreamé   i   c                   ó<   ^ • \ rS rSrSrS\S\S\4U 4S jjrSrU =r	$ )ÚFileSizeLimitExceededErroré   zERaised when a remote file exceeds the configured download size limit.ÚfilenameÚsizeÚlimitc                 óV   >• Xl         X l        X0l        [        TU ]  SU SU S35        g )Nz.Remote file exceeds the maximum allowed size (z > z bytes).)r   r   r   ÚsuperÚ__init__)Úselfr   r   r   Ú	__class__s       €ÚT/home/mande/repo/quber/.venv/lib/python3.13/site-packages/docling_core/utils/file.pyr   Ú#FileSizeLimitExceededError.__init__   s2   ø€ Ø ŒØŒ	ØŒ
Ü‰ÑÐIÈ$ÈÈsÐSXÐRYÐYaÐbÕcó    )r   r   r   )
Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__ÚstrÚintr   Ú__static_attributes__Ú__classcell__)r   s   @r   r   r      s*   ø† ÙOðd ð d¨Cð d¸÷ dõ dr   r   ÚurlÚreturnc                 óT  •  [        U 5      nUR                  nU(       d  g [        R                  " U5      nUR                  =(       ap    UR                  =(       dX    UR                  =(       dE    UR                  =(       d2    UR                  =(       d    UR                  =(       d    UR                  (       + $ ! [         aR    SSKn UR                  U5      n[        R                  " U5      n N»! UR                  UR                  4 a      gf = ff = f! [          a     gf = f)z=Return whether a URL resolves to a globally routable address.Fr   N)r   ÚhostnameÚ	ipaddressÚ
ip_addressÚ
ValueErrorÚsocketÚgethostbynameÚgaierrorÚherrorÚ	is_globalÚ
is_privateÚis_loopbackÚis_link_localÚis_reservedÚis_multicastÚis_unspecifiedÚ	Exception)r&   Úparsedr)   Úipr-   Úip_strs         r   Ú_is_safe_urlr<   $   s  € ðÜ˜#“ˆØ—?‘?ˆæØð		Ü×%Ò% hÓ/ˆBð �|‰|÷ 
Ø�M‰M÷ !Ø�~‰~÷!à×Ñ÷!ð �~‰~÷!ð �‰÷	!ð
 × Ñ ô!
ð 	
øô ó 	ÛðØ×-Ñ-¨hÓ7�Ü×)Ò)¨&Ó1’øØ—O‘O V§]¡]Ð3ó Úðúð	ûô" ó ÙðúsR   ‚D ¢B; ¸BD Â;DÃ
'C3Ã1D Ã3DÄDÄD ÄDÄDÄD Ä
D'Ä&D'r   c                 ó|   • U R                  SS5      n[        U5      R                  nU(       a  US;   d  SU;   a  gU$ )zGReturn a basename-safe filename, or None if no usable basename remains.Ú\Ú/)Ú.z..N)Úreplacer   Úname)r   Ú
normalizedÚbasenames      r   Ú_sanitize_filenamerE   D   s<   € à×!Ñ! $¨Ó,€JÜ�JÓ×$Ñ$€Hæ�x ;Ó.°#¸³/Øà€Or   Úhttp_urlÚresponse_headersc                 ó*  • SnUR                  S5      =n(       a“  UR                  5       R                  S5       Hp  nUR                  S5      =n(       d  M  US   R                  5       S:X  d  M6  SR                  USS 5      R                  5       R                  S5      =(       d    Sn  O   Uc1  [	        U R
                  =(       d    S	5      R                  =(       d    Un[        U5      =n(       a  U$ [        U5      =n(       a  U$ [        S
5      e)aF  Resolves the filename from a remote url and its response headers.

Args:
    source AnyHttpUrl: The source http url.
    response_headers Dict: Headers received while fetching the remote file.
    fallback_filename str: Filename to use in case none can be determined.

Returns:
    str: The actual filename of the remote url.
NzContent-DispositionÚ;Ú=r   r   é   z'"Ú z Could not derive a safe filename)	ÚgetÚstripÚsplitÚjoinr   ÚpathrB   rE   r,   )rF   rG   Úfallback_filenameÚ	raw_fnameÚ	cont_dispÚparrO   Úfnames           r   Úresolve_remote_filenamerW   O   sï   € ð €IØ$×(Ñ(Ð)>Ó?Ð?€yÕ?Ø—?‘?Ó$×*Ñ*¨3Ö/ˆCØŸ™ 3›Ð'�×'¨U°1©X¯^©^Ó-=ÀÕ-KØŸH™H U¨1¨2 YÓ/×5Ñ5Ó7×=Ñ=¸eÓD×LÈ�	Ùñ 0ð
 ÑÜ˜Ÿ™×,¨"Ó-×2Ñ2×GÐ6Gˆ	ä" 9Ó-Ð-€uÕ-Øˆä"Ð#4Ó5Ð5€uÕ5Øˆä
Ð7Ó
8Ð8r   ÚsourceÚheadersÚmax_file_sizec           	      ó”  •  [        [        5      R                  U 5      n[        U5      n[	        U5      (       d  [        SU 35      eU=(       d    0 nUR                  5        VVs0 s H  u  pgUR                  5       U_M     nnnSU;  a'   SSKn	SU	R                  R                  S5       3n
X¨S'   [        R                  " SU5      nU(       a�  UR                  S5      nUR                  S	5      nUS
:X  a  SU 3nO5US:X  a  SU S3nO(US:X  a  SU S3nOUS:X  a  SU S3nO[        SU 35      e[        [        5      R                  U5      nS n[        R                   " 5        n["        Ul        UR&                  S   R)                  U5        UR+                  USUSS9 nUR-                  5         [/        UR0                  5      n[3        UUS9nUb>  UR0                  R+                  S5      nUb    [5        U5      nUb  UU:”  a  [7        UUUS9e[9        5       nSnUR;                  [<        S9 H?  nU(       d  M  U[?        U5      -  nUb  UU:”  a  [7        UUUS9eURA                  U5        MA     URC                  S5        [E        UUS9nSSS5        SSS5        W$ s  snnf ! [         a    Sn
 GNf = f! [
         a    Sn NÊf = f! , (       d  f       NC= f! , (       d  f       W$ = f! [F         aÈ    [I        U [        5      (       a>  SU ;   a8  U RK                  SS5      S   R                  5       nUS ;  a  [        S!U S"35      e [        [L        5      R                  U 5      n[9        URO                  5       5      n[E        URP                  US9n U$ ! [F         a    [        S#[S        U 5       35      ef = ff = f)$a—  Resolves the source (URL, path) of a file to a binary stream.

Args:
    source: The file input source. Can be a path or URL.
    headers: Optional set of headers to use for fetching the remote URL.
    max_file_size: Optional maximum size, in bytes, for a remote download.
        When set, the download is rejected upfront if the declared
        ``Content-Length`` exceeds it, and aborted while streaming as soon as
        the received bytes exceed it.

Raises:
    ValueError: If source is of unexpected type.
    FileSizeLimitExceededError: If a remote download exceeds ``max_file_size``.

Returns:
    DocumentStream: The resolved file loaded as a stream.
zURL is not allowed: z
user-agentr   Nzdocling-core/zdocling-corezCgoogle\.com\/(file|document|spreadsheets|presentation)\/d\/([\w-]+)rK   é   Úfilez/https://drive.google.com/uc?export=download&id=Údocumentz#https://docs.google.com/document/d/z/export?format=docxÚspreadsheetsz'https://docs.google.com/spreadsheets/d/z/export?format=xlsxÚpresentationz'https://docs.google.com/presentation/d/z/export?format=pptxzUnexpected Google doc type: c                 ó*  • U R                   (       d  U R                  (       aq  U R                  R                  S5      nU(       aN  UR	                  S5      (       d  SSKJn  U" U R                  U5      n[        U5      (       d  [        SU 35      eggg)z2Validate each redirect target before following it.Úlocation)zhttp://zhttps://r   )Úurljoinz Redirect target is not allowed: N)
Úis_redirectÚis_permanent_redirectrY   rM   Ú
startswithÚurllib.parserc   r&   r<   r,   )ÚresponseÚargsÚkwargsÚredirect_urlrc   s        r   Ú_check_redirect_safetyÚ8resolve_source_to_stream.<locals>._check_redirect_safety®   s   € à×#×# x×'E×'EØ'×/Ñ/×3Ñ3°JÓ?�ÞØ'×2Ñ2Ð3J×KÑKÝ8á'.¨x¯|©|¸\Ó'J˜ä'¨×5Ñ5Ü(Ð+KÈLÈ>Ð)ZÓ[Ð[ð 6ð  ð (Fr   rh   T)ÚstreamrY   Úallow_redirects)rF   rG   zContent-Length)r   r   r   )Ú
chunk_size)rB   rn   z://)ÚhttpÚhttpszUnsupported URL scheme: 'z+'. Only http:// and https:// are supported.z$Unexpected source type encountered: )*r	   r   Úvalidate_pythonr"   r<   r,   ÚitemsÚlowerÚimportlib.metadataÚmetadataÚversionr8   ÚreÚsearchÚgroupÚrequestsÚSessionÚ_MAX_REDIRECTSÚmax_redirectsÚhooksÚappendrM   Úraise_for_statusÚdictrY   rW   r#   r   r   Úiter_contentÚ_DOWNLOAD_CHUNK_SIZEÚlenÚwriteÚseekr   r
   Ú
isinstancerO   r   Ú
read_bytesrB   Útype)rX   rY   rZ   rF   Úurl_strÚ_headersÚkÚvÚreq_headersÚ	importlibÚ
agent_nameÚgoogle_doc_idÚdoc_typeÚdoc_idrl   ÚsessionÚresrG   rV   Úcontent_lengthÚcontent_length_valuern   Ú
downloadedÚchunkÚ
doc_streamÚschemeÚ
local_paths                              r   Úresolve_source_to_streamrŸ   q   sü  € ð,nTÜ*¬:Ó6×FÑFÀvÓNˆÜ�h“-ˆä˜G×$Ñ$ÜÐ3°G°9Ð=Ó>Ð>à—=˜bˆØ08·±Ô0@ÔAÒ0@©¨�q—w‘w“y !’|Ñ0@ˆÑAØ˜{Ó*ð,Û)à,¨Y×-?Ñ-?×-GÑ-GÈÓ-WÐ,XÐY�
ð )3˜Ñ%äŸ	š	ØRØó
ˆö Ø$×*Ñ*¨1Ó-ˆHØ"×(Ñ(¨Ó+ˆFà˜6Ó!ØKÈFÈ8ÐT‘Ø˜ZÓ'Ø?À¸xÐGZÐ[‘Ø˜^Ó+ØCÀFÀ8ÐK^Ð_‘Ø˜^Ó+ØCÀFÀ8ÐK^Ð_‘ä Ð#?À¸zÐ!JÓKÐKä"¤:Ó.×>Ñ>¸wÓGˆHò	\ô  ×ÒÔ 7Ü$2ˆGÔ!Ø�M‰M˜*Ñ%×,Ñ,Ð-CÔDà—‘ØØØ#Ø $ð	 ñ ð
 Ø×$Ñ$Ô&ä#'¨¯©Ó#4Ð Ü/¸ÐTdÑe�à Ñ,Ø%(§[¡[§_¡_Ð5EÓ%F�NØ%Ñ1ð8Ü36°~Ó3FÐ0ð 0Ñ;Ð@TÐWdÓ@dÜ"<Ø).Ø%9Ø&3ñ#ð ô !›�Ø�
Ø ×-Ñ-Ô9MÐ-ÓN�EÞ Ù Ø¤# e£*Ñ,�JØ$Ñ0°ZÀ-Ó5OÜ8Ø%*Ø!+Ø"/ñð ð
 —L‘L Ö'ñ Oð —‘˜A”Ü+°¸vÑF�
÷Q÷	  ðp ÐùóO Bøô ó ,Ø+“
ð,ûôz  *ó 8Ø37Ò0ð8ú÷!õ ú÷	  Ôðp Ðûô ó 
TÜ�fœc×"Ñ" u°£Ø—\‘\ %¨Ó+¨AÑ.×4Ñ4Ó6ˆFØÐ.Ó.Ü Ð#<¸V¸HÐDoÐ!pÓqÐqð	TÜ$¤TÓ*×:Ñ:¸6ÓBˆJÜ˜Z×2Ñ2Ó4Ó5ˆFÜ'¨Z¯_©_ÀVÑL‰Jð Ðøô ó 	TÜÐCÄDÈÃLÀ>ÐRÓSÐSð	Túð
Tús³   ‚A%K5 Á'J'Â	K5 Â"J- Â0B;K5 Å+<K#Æ'AKÇ9K ÈBKÊK#ÊK5 Ê'K5 Ê-J=Ê9K5 Ê<J=Ê=K5 Ë KËKËKËKË
K 	ËK#Ë#
K2Ë-K5 Ë2K5 Ë5AOÍAN!Î!"OÏOÚworkdirc                 ó@  • [        XS9nUc  [        [        R                  " 5       5      nUR	                  SSS9  X#R
                  -  nUR                  S5       nUR                  UR                  R                  5       5        S S S 5        U$ ! , (       d  f       U$ = f)N©rX   rY   T)Úexist_okÚparentsÚwb)
rŸ   r   ÚtempfileÚmkdtempÚmkdirrB   Úopenr‡   rn   Úread)rX   rY   r    rœ   rž   Úfs         r   Ú_resolve_source_to_pathr¬   ù   s�   € ô
 *°ÑI€Jð �Ü”x×'Ò'Ó)Ó*ˆð ‡M�M˜4¨€MÑ.ð Ÿ?™?Ñ*€JØ	�‰˜Ô	 !Ø	�‰�
×!Ñ!×&Ñ&Ó(Ô)÷ 
ð Ð÷ 
Ô	ð Ðús   Á*BÂ
Bc                 ó   • [        U UUS9$ )aŽ  Resolves the source (URL, path) of a file to a local file path.

If a URL is provided, the content is first downloaded to a local file, located in
  the provided workdir or in a temporary directory if no workdir provided.

Args:
    source (Path | AnyHttpUrl | str): The file input source. Can be a path or URL.
    headers (Optional[dict[str, str]]): Optional set of headers to use for fetching
        the remote URL.
    workdir (Optional[Path]): If set, the work directory where the file will
        be downloaded, otherwise a temp dir will be used.

Raises:
    ValueError: If source is of unexpected type.

Returns:
    Path: The local file path.
©rX   rY   r    ©r¬   r®   s      r   Úresolve_source_to_pathr°     s   € ô. #ØØØñð r   zGUse `resolve_source_to_path()` or `resolve_source_to_stream()`  insteadc                 ó   • [        U US9$ )a»  Resolves the source (URL, path) of a file to a local file path.

If a URL is provided, the content is first downloaded to a temporary local file.

Args:
    source (Path | AnyHttpUrl | str): The file input source. Can be a path or URL.
    headers (Optional[dict[str, str]]): Optional set of headers to use for fetching
        the remote URL.

Raises:
    ValueError: If source is of unexpected type.

Returns:
    Path: The local file path.
r¢   r¯   r¢   s     r   Úresolve_file_sourcer²   -  s   € ô( #ØØñð r   )r]   )NN)N)'r!   r*   ry   r¦   Úior   Úpathlibr   Útypingr   r   rg   r   r|   Úpydanticr   r	   r
   Útyping_extensionsr   Údocling_core.types.doc.utilsr   Údocling_core.types.ior   r~   r…   r,   r   r"   Úboolr<   rE   rƒ   rW   r#   rŸ   r¬   r°   r²   © r   r   Ú<module>r¼      sÝ  ðÙ ã Û 	Û Ý Ý ß "Ý !ã ß =Ñ =Ý (å 6Ý 0à€ð
 "Ð ôd ô dð�cð ˜dô ð@ ð ¨¨t©ô ð ñ9Øð9à˜3 ˜8‘nð9ð 	õ	9ðH &*Ø $ñEØ�:Ñ Ñ#ðEà�#�s�(‰^˜dÑ"ðEð ˜‘:ðEð õ	EðT &*ØñØ�:Ñ Ñ#ðà�#�s�(‰^˜dÑ"ðð �D‰[ðð 
õ	ð0 &*ØñØ�:Ñ Ñ#ðà�#�s�(‰^˜dÑ"ðð �D‰[ðð 
õ	ñ< ÐUÓVð &*ñØ�:Ñ Ñ#ðà�#�s�(‰^˜dÑ"ðð 
ôó Wñr   