o
    &zh6                     @  s  U d dl mZ d dlZd dlZd dlmZ d dlZd dlZd dl	Z	d dl
Z
d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dl
mZ d dlmZmZmZmZ d dlmZ d d	lmZ d d
lm Z  ersd dlm!Z! d dlm"Z" e#e$Z%de	j&v Z'de(d< e'reefnee
j)fZ*de(d< G dd dej+Z,e, Z-de(d< G dd dZ.G dd dZ/G dd de/Z0G dd de/Z1G dd de/Z2G dd  d e/Z3G d!d" d"e/Z4G d#d$ d$e/Z5G d%d& d&Z6G d'd( d(Z7d/d*d+Z8d0d-d.Z9dS )1    )annotationsN)abstractmethod)BytesIO)Path)_TemporaryFileWrapper)TYPE_CHECKINGAnyCallableClassVar)request)unquote)
httpConfig)HTTPResponse)SplitResultzgoogle.appengineboolGAEztuple[type, Any]
STRATEGIESc                   @  s.   e Zd ZU g Zded< d
ddZd
ddZd	S )TmpFilesz,ClassVar[list[_TemporaryFileWrapper[bytes]]]filesreturnNonec                 C  s   | j | d S N)r   appendselffile r   J/var/www/html/kangema/venv/lib/python3.10/site-packages/xhtml2pdf/files.pyr   %   s   zTmpFiles.appendc                 C  s"   | j D ]}|  q| j   d S r   )r   closeclearr   r   r   r   
cleanFiles(   s   

zTmpFiles.cleanFilesNr   r   )__name__
__module____qualname__r   __annotations__r   r    r   r   r   r   r   "   s   
 
r   	files_tmpc                   @  sn   e Zd ZU dZeZdZded< defd ddZd!ddZd"ddZ	d#ddZ
d$ddZd%ddZd&ddZdS )'pisaTempFileaX  
    A temporary file implementation that uses memory unless
    either capacity is breached or fileno is requested, at which
    point a real temporary file will be created and the relevant
    details returned
    If capacity is -1 the second strategy will never be used.
    Inspired by:
    http://code.activestate.com/recipes/496744/.
    i (  intCAPACITY bufferstrcapacityr   r   c                 C  sn   d| _ || _tt|| jk| _z
| j| j  | _W n ty*   | jd  | _Y nw | | | 	d dS )z
        Creates a TempFile object containing the specified buffer.
        If capacity is specified, we use a real temporary file once the
        file gets larger than that size.  Otherwise, the data is stored
        in memory.
        Nr   )
namer-   r(   lenstrategyr   	_delegate
IndexErrorwriteseek)r   r+   r-   r   r   r   __init__@   s   
zpisaTempFile.__init__c                 C  sd   | j dkr0z| jd  }||   || _d| _ td| j W dS  ty/   d| _	Y dS w dS )zf
        Switch to next strategy. If an error occurred,
        stay with the first strategy.
        r      zCreated temporary file %sN)
r0   r   r3   getvaluer1   logwarningr.   	Exceptionr-   )r   new_delegater   r   r   makeTempFileS   s   
zpisaTempFile.makeTempFile
str | Nonec                 C  s   |    | jS )zGet a named temporary file.)r=   r.   r   r   r   r   getFileNameb   s   zpisaTempFile.getFileNamec                 C  s   |    | j S )z
        Forces this buffer to use a temporary file as the underlying.
        object and returns the fileno associated with it.
        )r=   r1   filenor?   r   r   r   rA   g   s   
zpisaTempFile.filenobytesc                 C  sL   | j dkr
| j S | j  | jd | j }t|ts$|d}|S )zc
        Get value of file. Work around for second strategy.
        Always returns bytes.
        r   utf-8)	r0   r1   r8   flushr4   read
isinstancerB   encode)r   valuer   r   r   r8   o   s   





zpisaTempFile.getvaluerH   bytes | strc                 C  sz   | j dkr+| jdkr+t|}|| j krd}n| dd |  | | j k}|r+|   t|ts5|d}| j	
| dS )zEIf capacity != -1 and length of file > capacity it is time to switch.r   T   rC   N)r-   r0   r/   r4   tellr=   rF   rB   rG   r1   r3   )r   rH   	len_valueneeds_new_strategyr   r   r   r3   }   s   


zpisaTempFile.writer.   r   c              
   C  sJ   zt | j|W S  ty$ } zdt| j d| d}t||d }~ww )Nzobject 'z' has no attribute '')getattrr1   AttributeErrortyper"   )r   r.   emsgr   r   r   __getattr__   s   
zpisaTempFile.__getattr__N)r+   r,   r-   r(   r   r   r!   r   r>   )r   r(   )r   rB   )rH   rI   )r.   r,   r   r   )r"   r#   r$   __doc__r   r)   r%   r5   r=   r@   rA   r8   r3   rT   r   r   r   r   r'   1   s   
 





r'   c                   @  sV   e Zd ZdddZedd
dZdddZdddZdddZdddZ	dddZ
dS ) BaseFilepathr,   basepathr>   r   r   c                 C  s"   || _ || _d | _d | _d | _d S r   rX   rY   mimetypesuffixurir   rX   rY   r   r   r   r5      
   
zBaseFile.__init__bytes | Nonec                 C  s   t r   )NotImplementedErrorr?   r   r   r   extract_data   s   zBaseFile.extract_datac              
   C  sR   z|   W S  ty( } ztdt|j|t| j| j W Y d }~d S d }~ww )Nz(%s: %s while extracting data from %s: %r)rb   r;   r9   errorrQ   r"   r]   )r   rR   r   r   r   get_data   s   
zBaseFile.get_datastr | Path | Nonec                 C     | j S r   )r]   r?   r   r   r   get_uri      zBaseFile.get_uric                 C  rf   r   )r[   r?   r   r   r   get_mimetype   rh   zBaseFile.get_mimetype_TemporaryFileWrapper[bytes]c                 C  sL   |   }tj| jd}|r|| |  t| | jd u r$|j	| _|S )N)r\   )
rd   tempfileNamedTemporaryFiler\   r3   rD   r&   r   rX   r.   )r   datatmp_filer   r   r   get_named_tmp_file   s   


zBaseFile.get_named_tmp_fileBytesIO | Nonec                 C  s   |   }|r
t|S d S r   )rd   r   )r   rm   r   r   r   get_BytesIO   s   zBaseFile.get_BytesIONrX   r,   rY   r>   r   r   r   r`   )r   re   rU   )r   rj   )r   rp   )r"   r#   r$   r5   r   rb   rd   rg   ri   ro   rq   r   r   r   r   rW      s    




rW   c                   @  s    e Zd ZU ded< dddZdS )	B64InlineURIlistmime_paramsr   r`   c                 C  s   | j d}| j drd| j vst|dkrd}t||d }|d tdd  d}|d |dd  | _| _t|d}t	
|S )	Nzbase64,data:rJ   z$Base64-encoded data URI is malformedr6   r   ;rC   )rX   split
startswithr/   RuntimeErrorr[   rv   urllib_unquoterG   base64	b64decode)r   partsrS   rm   mimeb64r   r   r   rb      s   


zB64InlineURI.extract_dataNrs   )r"   r#   r$   r%   rb   r   r   r   r   rt      s   
 rt   c                   @     e Zd ZdddZdS )LocalProtocolURIr   r`   c                 C  s`   | j r.| jdr.t| j | jdd  | _t| j}| 	dd
dd | _| S d S )N/r6   Content-Typer*   rx   r   )rY   rX   rz   urlparseurljoinr]   r   urlopeninfogetry   r[   rE   )r   urlResponser   r   r   rb      s   zLocalProtocolURI.extract_dataNrs   r"   r#   r$   rb   r   r   r   r   r          r   c                      s<   e Zd Zd fddZdd
dZdddZdddZ  ZS )NetworkFileUrirX   r,   rY   r>   r   r   c                   s   t  || d| _d| _d S )N   r   )superr5   attemptsactual_attemptsr^   	__class__r   r   r5      s   
zNetworkFileUri.__init__r`   c                 C  s   d }| j | jkrH|d u rH|  jd7  _z|  }W n$ ty= } ztdt|j|t| j| j| j W Y d }~nd }~ww | j | jkrH|d u s|S )Nr6   z6%s: %s while extracting data from %s: %r on attempt %d)	r   r   rb   r;   r9   rc   rQ   r"   r]   )r   rm   rR   r   r   r   rd      s$   zNetworkFileUri.get_datatuple[bytes | None, bool]c           	      C  s   t d| d }d}t|}|d }|d }||d r#d|d  nd7 }d }|dr7tj|fi t}nt|}|	d	| |
 }|j|jfd
krk|dddd | _| }|ddkrgd}||fS t d|j|j ||fS )Nz#Sending request for %r with httplibFr6   rJ   r   ?r*   zhttps://GET)   OKr   rx   r   zcontent-encodinggzipTzReceived non-200 status: %d %s)r9   debugr   urlsplitrz   httplibHTTPSConnectionr   HTTPConnectionr   getresponsestatusreason	getheaderry   r[   rE   )	r   r]   rm   is_gzipurl_splittedserverrX   connr1r   r   r   get_httplib  s*   


zNetworkFileUri.get_httplibc                 C  sl   | j r| jdst| j | j}n| j}|| _| |\}}|r.|r.tjdt	|d
 }td| |S )Nhttprb)modefileobjzUri parsed: %r)rY   rX   rz   r   r   r]   r   r   GzipFiler   rE   r9   r   )r   r]   rm   r   r   r   r   rb     s   zNetworkFileUri.extract_datarr   rs   )r   r   )r"   r#   r$   r5   rd   r   rb   __classcell__r   r   r   r   r      s
    

r   c                   @  s$   e Zd Zed	ddZd
ddZdS )LocalFileURIr   r>   c                 C  s,   t t| d }|dur|dd }|S )zGuess the mime type.r   Nrx   )	mimetypes
guess_typer,   ry   )r.   r[   r   r   r   guess_mimetype(  s   zLocalFileURI.guess_mimetyper`   c                 C  s   d }t d t| j}d }| jd urt| j| nt | }| r)| s)|}| r}|| _|j| _| 	|| _
| j
rc| j
drct|dd}| d}W d    |S 1 s\w   Y  |S t|d}| }W d    |S 1 sxw   Y  |S )Nz-Unrecognized scheme, assuming local file pathtextrC   )encodingr   )r9   r   r   rX   rY   existsis_filer]   r\   r   r[   rz   openrE   rG   )r   rm   rX   r]   file_handlerr   r   r   rb   0  s0   

"


zLocalFileURI.extract_dataNrU   rs   )r"   r#   r$   staticmethodr   rb   r   r   r   r   r   '  s    r   c                   @  r   )BytesFileUrir   r`   c                 C  s   | j | _| j dS )NrC   )rX   r]   rG   r?   r   r   r   rb   F  s   zBytesFileUri.extract_dataNrs   r   r   r   r   r   r   E  r   r   c                      s0   e Zd Zd
ddZ fddZddd	Z  ZS )LocalTmpFiler   r   c                 C  s"   || _ d | _|| _d | _d | _d S r   rZ   r^   r   r   r   r5   L  r_   zLocalTmpFile.__init__c                   s    t   }| jd u r|j| _|S r   )r   ro   rX   r.   )r   rn   r   r   r   ro   S  s   

zLocalTmpFile.get_named_tmp_filer`   c                 C  sN   | j d u rd S | j | _t| j d}| W  d    S 1 s w   Y  d S )Nr   )rX   r]   r   rE   )r   archr   r   r   rb   Y  s   
$zLocalTmpFile.extract_datar!   rs   )r"   r#   r$   r5   ro   rb   r   r   r   r   r   r   K  s    
r   c                   @  s   e Zd ZedddZdS )FileNetworkManagerNc                 C  s   | d u r	t | |S t| trt| |}|S | dr!t| |}|S |r/t| js/t|}nt| }t	d||j |jdkrHt
| |}|S |jdv rTt| |}|S t| |}|S )Nrw   zURLParts: %r, %rr   >   r   https)r   rF   rB   r   rz   rt   r   schemer9   r   r   r   r   )r]   rY   instanceurlPartsr   r   r   get_managerb  s(   










zFileNetworkManager.get_managerr   )r"   r#   r$   r   r   r   r   r   r   r   a  s    r   c                   @  sh   e Zd Z		dd d
dZd!ddZd"ddZd!ddZd#ddZd"ddZd$ddZ	dd Z
dd ZdS )%pisaFileObjectNr]   re   rY   r>   callbackCallable | Noner   r   c                 C  sT   || _ || _|r||| }r|| _ d | _td| j | j tj| j | jd| _d S )NzFileObject %r, Basepath: %r)rY   )r]   rY   r9   r   r   r   r   )r   r]   rY   r   newr   r   r   r5   {  s   zpisaFileObject.__init__r`   c                 C  
   | j  S r   r   rd   r?   r   r   r   getFileContent     
zpisaFileObject.getFileContentc                 C  s   | j  }|r
|jS d S r   )r   ro   r.   )r   fr   r   r   getNamedFile  s   
zpisaFileObject.getNamedFilec                 C  r   r   r   r?   r   r   r   getData  r   zpisaFileObject.getData&BytesIO | _TemporaryFileWrapper | Nonec                 C  s   t r| j S | j S r   )r   r   rq   ro   r?   r   r   r   getFile  s   

zpisaFileObject.getFilec                 C  r   r   )r   ri   r?   r   r   r   getMimeType  r   zpisaFileObject.getMimeTyper   c                 C  s   |   d u S r   )r   r?   r   r   r   notFound     zpisaFileObject.notFoundc                 C  r   r   )r   rg   r?   r   r   r   
getAbsPath  r   zpisaFileObject.getAbsPathc                 C  r   r   )r   rq   r?   r   r   r   
getBytesIO  r   zpisaFileObject.getBytesIO)NN)r]   re   rY   r>   r   r   r   r   rs   rU   )r   r   )r   r   )r"   r#   r$   r5   r   r   r   r   r   r   r   r   r   r   r   r   r   z  s    





r   r   c                  O  s   t | i |S r   )r   )akwr   r   r   r     s   r   r   c                   C  s   t   d S r   )r&   r    r   r   r   r   r      r   r    )r   r   r!   ):
__future__r   r}   r   http.clientclientr   loggingr   sysrk   	threadingurllib.parseparser   abcr   ior   pathlibr   r   typingr   r   r	   r
   urllibr   r   r|   xhtml2pdf.config.httpconfigr   r   r   	getLoggerr"   r9   modulesr   r%   rl   r   localr   r&   r'   rW   rt   r   r   r   r   r   r   r   r   r    r   r   r   r   <module>   sN    

e2
>
/