
    }-j                        d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	 d dl
mZmZ d dlZddlmZmZmZ ddlmZmZ ddd	d
dedededee         dedefdZdd	ddededededee         f
dZdd	ddededededee         f
dZdededee         defdZdedefdZdededdfdZdedededdfdZdeeef         dee	eef                  fdZ dedefdZ!dedefd Z"d!eddfd"Z#dS )#    N)Path)DictIterableListOptionalTuple)unquoteurlparse   )InvalidRequestErrorNetworkErrorRequestTimeoutError)DocParsingResult	OCRResultFg     r@)	overwritefilenametimeoutresource_urldestinationr   r   r   returnc                   | st          d          |st          d          t          |           }|j        dvs|j        st          d|            t	          |j        ||          }t          ||           	 t          j        | |          }nO# t          j	        $ r}t          d|           |d }~wt          j        $ r}t          d|           |d }~ww xY w	 |                                 n*# t          j        $ r}t          d|           |d }~ww xY wt          ||j        |           t#          |          S )	Nzresource_url is required.zdestination is required.)httphttpszInvalid resource URL: )r   zRequest timed out: zConnection failed: zFailed to download resource: )r   r
   schemenetloc_resolve_destinationpath_require_writable_targetrequestsgetTimeoutr   ConnectionErrorr   raise_for_statusRequestException_atomic_writecontentstr)	r   r   r   r   r   
parsed_urltargetresponsees	            `/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddleocr/_api_client/_resources.pysave_resourcer-      s     ?!"=>>> >!"<===,''J 1119J1!"I<"I"IJJJ!*/;IIFVY///=<g>>> D D D!";";";<<!C# = = =444551<=G!!####$ G G G>1>>??QFG &(*I666v;;s<   ;B C!B44CCC"C7 7DDDr   r   resultc          
      <   | t          d          t          |          }g }t          | j                  D ]d\  }}|j        sd|dz    t          |j                   }|                    t          |j        t          ||z            ||                     e|S )NzOCR result is required.z	ocr-page-r   r.   )	r   _require_existing_directory	enumeratepagesocr_image_url_safe_resource_extensionappendr-   r'   )	r/   r   r   r   dest_dirsaved_pathsindexpager   s	            r,   save_ocr_result_resourcesr;   ?   s     ~!";<<<*;77HK .. 
 
t! 	XuqyX*B4CU*V*VXX"Hx'((#	  	
 	
 	
 	
     c                   | t          d          t          |          }g }| j        D ]}t          |j                  D ];\  }}|                    t          |t          ||z            ||                     <t          |j                  D ];\  }}|                    t          |t          ||z            ||                     <|S )Nz$document parsing result is required.r.   )	r   r1   r3   _iter_named_resourcesmarkdown_imagesr6   r-   r'   output_images)	r/   r   r   r   r7   r8   r:   r   r   s	            r,   &save_document_parsing_result_resourcesrA   Y   s    ~!"HIII*;77HK  &;D<P&Q&Q 	 	"Hl 8+,,'#	      '<D<N&O&O 	 	"Hl 8+,,'#	     	 r<   url_pathc                    t          |          }|t          |           ||z  }n=|                                r'|                                r|t	          |           z  }n|}|j        }|                                st          t          |                    |                                st          d|           |S )Nz(Destination parent must be a directory: )	r   "_validate_result_resource_filenameexistsis_dir_safe_url_basenameparentFileNotFoundErrorr'   r   )rB   r   r   destination_pathr)   rH   s         r,   r   r   z   s     K((*8444!H,		 	 	"	" "'7'>'>'@'@ "!$6x$@$@@!]F==?? -F,,,==?? W!"UV"U"UVVVMr<   c                     t          |           }|                                st          |           |                                st	          d|            |S )Nz+Destination must be an existing directory: )r   rE   rI   rF   r   )r   r7   s     r,   r1   r1      se    K  H?? -,,,?? 
!G+GG
 
 	
 Or<   r)   c                 Z    |                                  r|st          d|            d S d S )NDestination already exists: )rE   r   )r)   r   s     r,   r   r      sJ    }} Ky K!"I"I"IJJJK K K Kr<   r&   c                    t          j        d| j         dt          | j                            \  }}	 t          j        |d          5 }|                    |           d d d            n# 1 swxY w Y   |rt          j        ||            n)t          j	        ||            t          j
        |           n%# t          $ r}t          d|            |d }~ww xY wt
          j                            |          rt          j
        |           d S d S # t
          j                            |          rt          j
        |           w w xY w)N.z.tmp-)prefixdirwbrM   )tempfilemkstempnamer'   rH   osfdopenwritereplacelinkremoveFileExistsErrorr   r   rE   )r)   r&   r   fd	temp_path	temp_filer+   s          r,   r%   r%      s   $%6;%%%  MB	!Yr4   	%IOOG$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	!Jy&))))GIv&&&Ii    R R R!"I"I"IJJPQQR 7>>)$$ 	!Ii     	! 	!27>>)$$ 	!Ii    	!sN   B: A-!B: -A11B: 4A15AB: 9D :
CCCD 6E	resourcesc              #   n   K   t          |           D ]"}| |         }|st          |           ||fV  #d S )N)sortedrD   )r`   keyr   s      r,   r>   r>      s]      i       ~ 	*3///<   r<   c                 v    t          t          |                     j        }|r|dv rdS t          |           |S )NrO   z..resource)r   r	   rU   rD   )rB   rU   s     r,   rG   rG      sD    !!""'D 4;&&z&t,,,Kr<   c                     t          |           }t          t          |j                            j        }|sdS 	 t          d|            n# t          $ r Y dS w xY w|S )N rf   )r
   r   r	   r   suffixrD   r   )r   parsedri   s      r,   r5   r5      s|    l##F'&+&&''.F r*+>f+>+>????   rrMs   A 
AArU   c                     | st          d          t          |           }|j        | k    sd| v sd| v s| dv rt          d|            d S )Nz$Resource filename must not be empty./\re   zUnsafe resource filename: )r   r   rU   )rU   r   s     r,   rD   rD      sl     J!"HIII::DyDC4KK44<<4;;N;N!"Et"E"EFFF <O;Nr<   )$rV   rS   pathlibr   typingr   r   r   r   r   urllib.parser	   r
   r   errorsr   r   r   resultsr   r   r'   boolfloatr-   r;   rA   r   r1   r   bytesr%   r>   rG   r5   rD    r<   r,   <module>rw      s,   
			        8 8 8 8 8 8 8 8 8 8 8 8 8 8 * * * * * * * *  J J J J J J J J J J 0 0 0 0 0 0 0 0 "! ! !!! 	!
 sm! ! 	! ! ! !P    	
  
#Y   <    	
  
#Y   B #/7}	   (S T    KT Kd Kt K K K K
!$ ! !4 !D ! ! ! !( T#s(^  sCx8Q             	3 	3 	 	 	 	GS GT G G G G G Gr<   