
    e@d2                     D   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
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 dej        v Z ej        d          ZereefZn	ee	j        fZ G d	 d
e
j                  Z e            Z  G d de!          Z" G d d          Z# G d de#          Z$ G d de#          Z% G d de#          Z& G d de#          Z' G d de#          Z( G d de#          Z) G d d          Z* G d d          Z+d Z,d  Z-dS )!    N)BytesIO)Path)request)unquote)
httpConfigzgoogle.appengine	xhtml2pdfc                       e Zd Zg Zd Zd ZdS )TmpFilesc                 :    | j                             |           d S N)filesappendselffiles     /./xhtml2pdf/files.pyr   zTmpFiles.append"   s    
$    c                 t    | j         D ]}|                                 | j                                          d S r   )r   closeclearr   s     r   
cleanFileszTmpFiles.cleanFiles%   s<    J 	 	DJJLLLL
r   N)__name__
__module____qualname__r   r   r    r   r   r
   r
      s7        E         r   r
   c                   J    e Zd ZdZeZdZdefdZd Zd Zd Z	d Z
d	 Zd
 ZdS )pisaTempFileaW  
    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 (   c                 `   d| _         || _        t          t          |          | j        k              | _        	  | j        | j                             | _        n*# t          $ r  | j        d                     | _        Y nw xY w|                     |           | 	                    d           dS )zCreates 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   )
namecapacityintlenstrategy
STRATEGIES	_delegate
IndexErrorwriteseek)r   bufferr!   s      r   __init__zpisaTempFile.__init__=   s     	 CKK$-788	2;T_T];==DNN 	2 	2 	2/T_Q/11DNNN	2 	

6		!s   A $B Bc                    | j         dk    r|	  | j        d                     }|                    |                                            || _        d| _         t
                              d| j                   dS #  d| _        Y dS xY wdS )zd
        Switch to next startegy. If an error occured,
        stay with the first strategy
        r      zCreated temporary file %sN)	r$   r%   r(   getvaluer&   logwarningr    r!   )r   new_delegates     r   makeTempFilezpisaTempFile.makeTempFileO   s     =A$1tq133""4==??333!- !7CCCCC$ # s   A*A9 9	Bc                 8    |                                   | j        S )z,
        Get a named temporary file
        )r3   r    r   s    r   getFileNamezpisaTempFile.getFileName_   s    
 	yr   c                 \    |                                   | j                                        S )z
        Forces this buffer to use a temporary file as the underlying.
        object and returns the fileno associated with it.
        )r3   r&   filenor5   s    r   r8   zpisaTempFile.filenog   s*    
 	~$$&&&r   c                 :   | j         dk    r| j                                        S | j                                         | j                            d           | j                                        }t          |t                    s|                    d          }|S )zb
        Get value of file. Work around for second strategy.
        Always returns bytes
        r   utf-8)	r$   r&   r/   flushr)   read
isinstancebytesencode)r   values     r   r/   zpisaTempFile.getvalueo   s     =A>**,,,A##%%%'' 	*LL))Er   c                    | j         dk    rt| j        dk    rit          |          }|| j         k    rd}n6|                     dd           |                                 |z   | j         k    }|r|                                  t          |t                    s|                    d          }| j	        
                    |           dS )zV
        If capacity != -1 and length of file > capacity it is time to switch
        r   T   r:   N)r!   r$   r#   r)   tellr3   r=   r>   r?   r&   r(   )r   r@   	len_valueneeds_new_strategys       r   r(   zpisaTempFile.write~   s    
 =1!!3!3E

IDM))%)""		!QYY[[9,> #! $!!###%'' 	*LL))EU#####r   c                     	 t          | j        |          S # t          $ r# d| j        j        d|d}t          |          w xY w)Nzobject 'z' has no attribute '')getattrr&   AttributeError	__class__r   )r   r    es      r   __getattr__zpisaTempFile.__getattr__   sd    	$4>4000 	$ 	$ 	$ 	$ >***DDD2A ###		$s	    -AN)r   r   r   __doc__r%   CAPACITYr+   r3   r6   r8   r/   r(   rL   r   r   r   r   r   .   s          JH 8    $$ $ $   ' ' '  $ $ $*$ $ $ $ $r   r   c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )BaseFilec                 L    || _         || _        d | _        d | _        d | _        d S r   pathbasepathmimetypesuffixurir   rS   rT   s      r   r+   zBaseFile.__init__   s)    	 r   c                     | j         S r   )rW   r5   s    r   get_urizBaseFile.get_uri   s	    xr   c                     | j         S r   )rU   r5   s    r   get_mimetypezBaseFile.get_mimetype   s
    }r   c                    |                                  }t          j        | j                  }|rC|                    |           |                                 t                              |           | j        |j	        | _        |S )N)rV   )
get_datatempfileNamedTemporaryFilerV   r(   r;   	files_tmpr   rS   r    )r   datatmp_files      r   get_named_tmp_filezBaseFile.get_named_tmp_file   sx    }}.dkBBB 	'NN4   NNX&&&9 DIr   c                 P    |                                  }|rt          |          S d S r   )r^   r   )r   rb   s     r   get_BytesIOzBaseFile.get_BytesIO   s-    }} 	!4== 	! 	!r   N)r   r   r   r+   rZ   r\   rd   rf   r   r   r   rP   rP      s_              
 
 
! ! ! ! !r   rP   c                   V    e Zd Z ej        dej        ej        z            Zd Zd Z	dS )B64InlineURIz2^data:(?P<mime>[a-z]+/[a-z]+);base64,(?P<data>.*)$c                     	 |                                  S # t          $ r%}t                              d           Y d }~d S d }~ww xY w)NzExtract data form data: in tagextract_data	Exceptionr0   errorr   rK   s     r   r^   zB64InlineURI.get_data   s]    	8$$&&& 	8 	8 	8II6777777777	8    
AA  Ac                    | j                             | j                  }|                    d          | _        t          |                    d                    }|                    d                              d                                          }t          j	        dd|          }t          j	        dd|          }|dd	t          |          d	z  z
  d	z  z  z  }t          j        |          S )
Nmimerb   zb'rG   s   \nr   s   [^A-Za-z0-9\+\/]+   =   )_rx_dataurimatchrS   grouprU   urllib_unquotestripr?   resubr#   base64	b64decode)r   mb64s      r   rk   zB64InlineURI.extract_data   s    ""49--QWWV__-- iioo##C((//11fVS#&&f+S#66 	tCHHqL(A-..$$$r   N)
r   r   r   ry   compileMDOTALLrt   r^   rk   r   r   r   rh   rh      sU        "*<bdRY>NP PK8 8 8% % % % %r   rh   c                       e Zd Zd Zd ZdS )LocalProtocolURIc                     	 |                                  S # t          $ r%}t                              d           Y d }~d S d }~ww xY w)Nz.Extract data form local file based on protocolrj   rn   s     r   r^   zLocalProtocolURI.get_data   sb    	H$$&&& 	H 	H 	HIIFGGGGGGGGG	Hro   c                 v   | j         r| j                            d          rt          j        | j         | j        dd                    }t          j        |          }|                                                    dd          	                    d          d         | _
        |                                S d S d S )N/r-   Content-Typer   ;r   )rT   rS   
startswithurlparseurljoinr   urlopeninfogetsplitrU   r<   )r   rW   urlResponses      r   rk   zLocalProtocolURI.extract_data   s    = 	&TY11#66 	&"4=$)ABB-@@C!/#..K',,..22$ $$)E#JJq2DM##%%%	& 	& 	& 	&r   N)r   r   r   r^   rk   r   r   r   r   r      s5        H H H& & & & &r   r   c                   0     e Zd Z fdZd Zd Zd Z xZS )NetworkFileUric                 h    t                                          ||           d| _        d| _        d S )N   r   )superr+   attemptsactual_attempts)r   rS   rT   rJ   s      r   r+   zNetworkFileUri.__init__   s2    x((( r   c                    d }| j         | j        k    rs|q| xj        dz  c_        	 |                                 }n9# t          $ r,}t                              d| j        z             Y d }~nd }~ww xY w| j         | j        k    r|q|S )Nr-   zExtract data remote trying %d)r   r   rk   rl   r0   rm   )r   rb   rK   s      r   r^   zNetworkFileUri.get_data   s    md222t|  A%  R((** R R R		9D<PPQQQQQQQQR	 md222t| s   ; 
A1"A,,A1c                    t                               d| d           d\  }}t          j        |          }|d         }|d         }||d         rd|d         z   ndz  }|                    d	          rt          j        |fi t          }nt          j        |          }|	                    d
|           |
                                }|j        |j        fdk    rd|                    dd                              d          d         | _        |                                }|                    d          dk    rd}n*t                               d|j         d|j                    ||fS )NzSending request for z with httplib)NFr-   rB   r   ?r   zhttps://GET)   OKr   r   r   zcontent-encodinggzipTzReceived non-200 status:  )r0   debugr   urlsplitr   httplibHTTPSConnectionr   HTTPConnectionr   getresponsestatusreason	getheaderr   rU   r<   )	r   rW   rb   is_gzipurl_splittedserverrS   connr1s	            r   get_httplibzNetworkFileUri.get_httplib   sk   		;;;;<<<#g(--aAa@l1o%%b@>>*%% 	2*6@@Z@@DD)&11DUD!!!Iry![00LL$ $$)E#JJq2DM7799D||.//699III")IIbiIIJJJW}r   c                 x   | j         r:| j                            d          s t          j        | j         | j                  }n| j        }|| _        |                     |          \  }}|r#t          j        dt          |                    }t                              d                    |                     |S )Nhttprb)modefileobjzUri parsed: {})rT   rS   r   r   r   rW   r   r   GzipFiler   r0   r   format)r   rW   rb   r   s       r   rk   zNetworkFileUri.extract_data  s    = 	!5!5f!=!= 	"4=$)<<CC)C((--g 	C=dGDMMBBBD		"))#..///r   )r   r   r   r+   r^   r   rk   __classcell__rJ   s   @r   r   r      se        ! ! ! ! !
	 	 	  0      r   r   c                        e Zd Zd Zd Zd ZdS )LocalFileURIc                     	 |                                  S # t          $ r%}t                              d           Y d }~d S d }~ww xY w)NzExtract data form local filerj   rn   s     r   r^   zLocalFileURI.get_data"  s]    	6$$&&& 	6 	6 	6II4555555555	6ro   c                     t          j        t          |                    d         }|@t          j        t          |                    d                             d          d         }|S )z Guess the mime type r   Nr   )	mimetypes
guess_typestrr   )r   r    rU   s      r   guess_mimetypezLocalFileURI.guess_mimetype(  sV    'D		2215 +CII66q9??DDQGHr   c                    d }t                               d           t          | j                  }d }| j        t          | j                  |z  }nt          d          |z  }|                                r|                                s|}|                                r|| _        |j        | _        | 	                    |          | _
        | j
        rW| j
                            d          r=t          |d          5 }|                                }d d d            n# 1 swxY w Y   n<t          |d          5 }|                                }d d d            n# 1 swxY w Y   |S )Nz-Unrecognized scheme, assuming local file path.textrr   )r0   r   r   rS   rT   existsis_filerW   rV   r   rU   r   openr<   )r   rb   rS   rW   file_handlers        r   rk   zLocalFileURI.extract_data/  s   		ABBBDI=$t}%%,CCs))d"C;;== 	 	C;;== 	/DH*DK //44DM} /!9!9&!A!A /#s^^ /|',,..D/ / / / / / / / / / / / / / / #t__ /',,..D/ / / / / / / / / / / / / / / s$    D!!D%(D%=EE"%E"N)r   r   r   r^   r   rk   r   r   r   r   r   !  sA        6 6 6      r   r   c                       e Zd Zd ZdS )BytesFileUric                     | j         S r   )rS   r5   s    r   r^   zBytesFileUri.get_dataJ  s
    yr   N)r   r   r   r^   r   r   r   r   r   I  s#            r   r   c                   *     e Zd Zd Z fdZd Z xZS )LocalTmpFilec                 L    || _         d | _        || _        d | _        d | _        d S r   rR   rX   s      r   r+   zLocalTmpFile.__init__P  s)    	 r   c                 n    t                                                      }| j        |j        | _        |S r   )r   rd   rS   r    )r   rc   rJ   s     r   rd   zLocalTmpFile.get_named_tmp_fileW  s.    77--//9 DIr   c                     | j         d S t          | j         d          5 }|                                cd d d            S # 1 swxY w Y   d S )Nr   )rS   r   r<   )r   archs     r   r^   zLocalTmpFile.get_data]  s    9F$)T"" 	d99;;	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A  AA)r   r   r   r+   rd   r^   r   r   s   @r   r   r   N  sV                    r   r   c                   &    e Zd Zedd            ZdS )FileNetworkManagerNc                 B   | t          | |          }|S t          | t                    rt          | |          }n|                     d          rt          | |          }n|r.t          j        |           j        st          j        |          }nt          j        |           }t          	                    d
                    ||j        f                     |j        dk    rt          | |          }n*|j        dv rt          | |          }nt          | |          }|S )Nzdata:zURLParts: {}r   )r   https)r   r=   r>   r   r   rh   r   schemer0   r   r   r   r   r   )rW   rT   instanceurlPartss       r   get_managerzFileNetworkManager.get_managere  s   ;#C22HOc5!! 	7#C22HH^^G$$ 	7#C22HH 2 1# 6 6 = 2#,X66#,S11IIn++Xx,GHHIII&((+C::$555)#x88'X66r   r   )r   r   r   staticmethodr   r   r   r   r   r   d  s2           \  r   r   c                   F    e Zd ZddZd Zd Zd Zd Zd Zd Z	d	 Z
d
 ZdS )pisaFileObjectNc                     || _         d }| |||          }|
d | _        |}n|| _        t                              d||           t                              || j                  | _        d S )NzFileObject %r, Basepath: %r)rT   )rW   rT   r0   r   r   r   r   )r   rW   rT   callbackbasepathrets        r   r+   zpisaFileObject.__init__  sy    "(311K" DMCC$DM 			/h???*66sT]6SSr   c                 4    | j                                         S r   r   r^   r5   s    r   getFileContentzpisaFileObject.getFileContent      }%%'''r   c                 J    | j                                         }|r|j        S d S r   )r   rd   r    )r   fs     r   getNamedFilezpisaFileObject.getNamedFile  s.    M,,.. 	6M	 	r   c                 4    | j                                         S r   r   r5   s    r   getDatazpisaFileObject.getData  r   r   c                 t    t           r| j                                        S | j                                        S r   )GAEr   rf   rd   r5   s    r   getFilezpisaFileObject.getFile  s2     	/=,,...}//111r   c                 4    | j                                         S r   )r   r\   r5   s    r   getMimeTypezpisaFileObject.getMimeType  s    }))+++r   c                 .    |                                  d u S r   )r   r5   s    r   notFoundzpisaFileObject.notFound  s    ||~~%%r   c                 4    | j                                         S r   )r   rZ   r5   s    r   
getAbsPathzpisaFileObject.getAbsPath  s    }$$&&&r   c                 4    | j                                         S r   )r   rf   r5   s    r   
getBytesIOzpisaFileObject.getBytesIO  s    }((***r   )NN)r   r   r   r+   r   r   r   r   r   r   r   r   r   r   r   r   r   ~  s        T T T T"( ( (  
( ( (2 2 2
, , ,& & &' ' '+ + + + +r   r   c                      t          | i |S r   )r   )akws     r   r   r     s    1####r   c                  8    t                                            d S r   )ra   r   r   r   r   r   r     s    r   ).r{   r   http.clientclientr   loggingr   ry   sysr_   	threadingurllib.parseparser   ior   pathlibr   urllibr   r   rw   xhtml2pdf.config.httpconfigr   modulesr   	getLoggerr0   r%   r`   localr
   ra   objectr   rP   rh   r   r   r   r   r   r   r   r   r   r   r   r   <module>r     s$                				 



                              2 2 2 2 2 2 2 2 2 2 2 2CK'g$$ %JJ
 	#%J

 
 
 
 
y 
 
 
 HJJ	l$ l$ l$ l$ l$6 l$ l$ l$^! ! ! ! ! ! ! !@% % % % %8 % % %8& & & & &x & & &"4 4 4 4 4X 4 4 4n% % % % %8 % % %P    8   
    8   ,       4,+ ,+ ,+ ,+ ,+ ,+ ,+ ,+^$ $ $    r   