B
    u9aVh                 @   s  d ddddddddd	d
ddg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ZddlZejZejejB ejB ZeedreejO ZeZeedreejO ZeedrejZndZdZe Zeedrej Z!needrej"Z!ndd Z!dd Z#dd Z$dd Z%G dd dZ&d d! Z'd"d# Z(da)d$d% Z*d&d' Z+d(d Z,d)d Z-da.d*d
 Z/d+d Z0d?d-dZ1d@d.dZ2d/edfd0dZ3G d1d2 d2Z4G d3d4 d4Z5dAd8d Z6ej7d9ksej8j9d:kre6Z:need;a;dBd<dZ:G d=d dZ<G d>d de=Z>dS )CNamedTemporaryFileTemporaryFileSpooledTemporaryFileTemporaryDirectorymkstempmkdtempmktempTMP_MAXgettempprefixtempdir
gettempdirgettempprefixbgettempdirb    N)Random
O_NOFOLLOWO_BINARYi'  Ztmplstatstatc             C   s   t | t j}t | d S )N)_osopenO_RDONLYclose)fnfd r   /usr/lib/python3.7/tempfile.py_statP   s    r   c             C   s*   yt |  W n tk
r    dS X dS d S )NFT)r   OSError)r   r   r   r   _existsT   s
    r   c              G   s`   d }xJ| D ]B}|d krq
t |tr8|tkr2tdt}q
|tkrHtdt}q
W |d kr\tS |S )Nz1Can't mix bytes and non-bytes in path components.)
isinstancebytesstr	TypeError)argsZreturn_typeargr   r   r   _infer_return_type]   s    

r%   c             C   sd   t | ||}|d kr| }| d kr:|tkr0t} n
tt} |d krX|tkrRt }nt }| |||fS )N)r%   r!   templater   fsencoder   r   )prefixsuffixdiroutput_typer   r   r   _sanitize_paramsr   s    
r,   c               @   s,   e Zd ZdZedd Zdd Zdd ZdS )	_RandomNameSequenceZ%abcdefghijklmnopqrstuvwxyz0123456789_c             C   s,   t  }|t| dd kr&t | _|| _| jS )N_rng_pid)r   getpidgetattr_RandomZ_rngr.   )selfZcur_pidr   r   r   rng   s
    z_RandomNameSequence.rngc             C   s   | S )Nr   )r2   r   r   r   __iter__   s    z_RandomNameSequence.__iter__c                s0   | j  | jj fddtdD }d|S )Nc                s   g | ]} qS r   r   ).0Zdummy)cchooser   r   
<listcomp>   s    z0_RandomNameSequence.__next__.<locals>.<listcomp>    )
charactersr3   choicerangejoin)r2   Zlettersr   )r6   r7   r   __next__   s    z_RandomNameSequence.__next__N)__name__
__module____qualname__r;   propertyr3   r4   r?   r   r   r   r   r-      s   r-   c           	   C   s   g } x$dD ]}t |}|r
| | q
W t jdkr\| t jdt jdddddg n| d	d
dg y| t   W n$ t	t
fk
r   | t j Y nX | S )N)ZTMPDIRZTEMPZTMPntz~\AppData\Local\Tempz%SYSTEMROOT%\Tempzc:\tempzc:\tmpz\tempz\tmpz/tmpz/var/tmpz/usr/tmp)r   getenvappendnameextendpath
expanduser
expandvarsgetcwdAttributeErrorr   curdir)dirlistZenvnamedirnamer   r   r   _candidate_tempdir_list   s    

 

rQ   c              C   s4  t  } t }x|D ]}|tjkr0tj|}xtdD ]}t| }tj||}y\t	|t
d}z<z*tj	|ddd}|d W d Q R X W d t| X W d t| X |S  tk
r   Y q: tk
r    tjdkrtj|rt|tjrw:P Y q: tk
r   P Y q:X q:W qW ttjd| d S )	Nd   i  wbF)closefds   blatrD   z)No usable temporary directory found in %s)r-   rQ   r   rN   rI   abspathr=   nextr>   r   _bin_openflags_iowriter   unlinkFileExistsErrorPermissionErrorrG   isdiraccessW_OKr   FileNotFoundError_errnoENOENT)ZnamerrO   r*   seqrG   filenamer   fpr   r   r   _get_default_tempdir   s:    	
rf   c               C   s2   t d kr.t  zt d kr t a W d t  X t S )N)_name_sequence
_once_lockacquirer-   releaser   r   r   r   _get_candidate_names   s    

rk   c       
   	   C   s   t  }|tkrttj|}xttD ]}t|}tj	| || | }yt
||d}	W nR tk
rn   w$Y n> tk
r   tjdkrtj| rt| tjrw$n Y nX |	tj|fS W ttjdd S )Ni  rD   z#No usable temporary file name found)rk   r    mapr   r'   r=   r   rV   rI   r>   r   r[   r\   rG   r]   r^   r_   rU   ra   EEXIST)
r*   ZpreZsufflagsr+   namesrc   rG   filer   r   r   r   _mkstemp_inner   s$    rq   c               C   s   t S )N)r&   r   r   r   r   r	     s    c               C   s   t t S )N)r   r'   r	   r   r   r   r   r     s    c               C   s2   t d kr.t  zt d kr t a W d t  X t S )N)r
   rh   ri   rf   rj   r   r   r   r   r     s    

c               C   s   t t S )N)r   r'   r   r   r   r   r   r   +  s    Fc             C   s2   t || |\}} }}|rt}nt}t||| ||S )N)r,   _text_openflagsrW   rq   )r)   r(   r*   textr+   rn   r   r   r   r   /  s
    c          	   C   s   t || |\}} }}t }|tkr.ttj|}xttD ]}t|}tj	
||| |  }yt|d W nR tk
r   w8Y n> tk
r   tjdkrtj	|rt|tjrw8n Y nX |S W ttjdd S )Ni  rD   z(No usable temporary directory name found)r,   rk   r    rl   r   r'   r=   r   rV   rI   r>   mkdirr[   r\   rG   r]   r^   r_   ra   rm   )r)   r(   r*   r+   ro   rc   rG   rp   r   r   r   r   W  s&    r:   c             C   s`   |d krt  }t }x:ttD ].}t|}tj||| |  }t|s|S qW t	t
jdd S )Nz"No usable temporary filename found)r   rk   r=   r   rV   r   rI   r>   r   r[   ra   rm   )r)   r(   r*   ro   rc   rG   rp   r   r   r   r   ~  s    c               @   sH   e Zd ZdZdZdddZejdkr<ejfddZ	d	d
 Z
ndd Z	dS )_TemporaryFileCloserNFTc             C   s   || _ || _|| _d S )N)rp   rG   delete)r2   rp   rG   rv   r   r   r   __init__  s    z_TemporaryFileCloser.__init__rD   c             C   s<   | j s8| jd k	r8d| _ z| j  W d | jr6|| j X d S )NT)close_calledrp   r   rv   rG   )r2   rZ   r   r   r   r     s    z_TemporaryFileCloser.closec             C   s   |    d S )N)r   )r2   r   r   r   __del__  s    z_TemporaryFileCloser.__del__c             C   s   | j sd| _ | j  d S )NT)rx   rp   r   )r2   r   r   r   r     s    )T)r@   rA   rB   rp   rx   rw   r   rG   rZ   r   ry   r   r   r   r   ru     s   



ru   c               @   s>   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd ZdS )_TemporaryFileWrapperTc             C   s$   || _ || _|| _t|||| _d S )N)rp   rG   rv   ru   _closer)r2   rp   rG   rv   r   r   r   rw     s    z_TemporaryFileWrapper.__init__c                s^   | j d }t||}t|drD| t  fdd}| j|_|}t|tsZt| || |S )Nrp   __call__c                 s
    | |S )Nr   )r#   kwargs)funcr   r   func_wrapper  s    z7_TemporaryFileWrapper.__getattr__.<locals>.func_wrapper)	__dict__r0   hasattr
_functoolswrapsr{   r   intsetattr)r2   rG   rp   ar   r   )r~   r   __getattr__  s    



z!_TemporaryFileWrapper.__getattr__c             C   s   | j   | S )N)rp   	__enter__)r2   r   r   r   r     s    
z_TemporaryFileWrapper.__enter__c             C   s   | j |||}|   |S )N)rp   __exit__r   )r2   excvaluetbresultr   r   r   r     s    z_TemporaryFileWrapper.__exit__c             C   s   | j   d S )N)r{   r   )r2   r   r   r   r     s    z_TemporaryFileWrapper.closec             c   s   x| j D ]
}|V  qW d S )N)rp   )r2   liner   r   r   r4     s    z_TemporaryFileWrapper.__iter__N)T)	r@   rA   rB   rw   r   r   r   r   r4   r   r   r   r   rz     s   
rz   w+bTc             C   s   t |||\}}}}t}	tjdkr0|r0|	tjO }	t||||	|\}
}y tj|
| |||d}t|||S  t	k
r   t
| t|
  Y nX d S )NrD   )	bufferingnewlineencoding)r,   rW   r   rG   ZO_TEMPORARYrq   rX   r   rz   BaseExceptionrZ   r   )moder   r   r   r)   r(   r*   rv   r+   rn   r   rG   rp   r   r   r   r     s    




posixcygwin	O_TMPFILEc             C   s   t |||\}}}}t}try$|tjB tj @ }	t||	d}
W n* tk
rX   daY nB tk
rj   Y n0X yt	j|
| |||dS    t
|
  Y nX t|||||\}
}yt| t	j|
| |||dS    t
|
  Y nX d S )Ni  F)r   r   r   )r,   rW   _O_TMPFILE_WORKSr   r   O_CREATr   IsADirectoryErrorr   rX   r   rq   rZ   )r   r   r   r   r)   r(   r*   r+   rn   Zflags2r   rG   r   r   r   r   9  s0    






c               @   s   e Zd ZdZd6ddZdd	 Zd
d Zdd Zdd Zdd Z	dd Z
edd Zedd Zdd Zdd Zdd Zedd Zed d! Zed"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zed,d- Zd.d/ Zd7d0d1Zd2d3 Zd4d5 ZdS )8r   Fr   w+br   Nc	       	      C   sH   d|krt  | _nt jdd| _|| _d| _|||||||d| _d S )Nb
)r   F)r   r   r)   r(   r   r   r*   )rX   BytesIO_fileStringIO	_max_size_rolled_TemporaryFileArgs)	r2   max_sizer   r   r   r   r)   r(   r*   r   r   r   rw   z  s    zSpooledTemporaryFile.__init__c             C   s,   | j r
d S | j}|r(| |kr(|   d S )N)r   r   tellrollover)r2   rp   r   r   r   r   _check  s
     zSpooledTemporaryFile._checkc             C   sN   | j r
d S | j}tf | j }| _| `||  || d d| _ d S )Nr   T)r   r   r   r   rY   getvalueseekr   )r2   rp   Znewfiler   r   r   r     s     zSpooledTemporaryFile.rolloverc             C   s   | j jrtd| S )Nz%Cannot enter context with closed file)r   closed
ValueError)r2   r   r   r   r     s    zSpooledTemporaryFile.__enter__c             C   s   | j   d S )N)r   r   )r2   r   r   r   r   r   r   r     s    zSpooledTemporaryFile.__exit__c             C   s
   | j  S )N)r   r4   )r2   r   r   r   r4     s    zSpooledTemporaryFile.__iter__c             C   s   | j   d S )N)r   r   )r2   r   r   r   r     s    zSpooledTemporaryFile.closec             C   s   | j jS )N)r   r   )r2   r   r   r   r     s    zSpooledTemporaryFile.closedc             C   s8   y| j jS  tk
r2   d| jd kr( | jd S X d S )Nr   r   r   )r   r   rM   r   )r2   r   r   r   r     s    zSpooledTemporaryFile.encodingc             C   s   |    | j S )N)r   r   fileno)r2   r   r   r   r     s    zSpooledTemporaryFile.filenoc             C   s   | j   d S )N)r   flush)r2   r   r   r   r     s    zSpooledTemporaryFile.flushc             C   s
   | j  S )N)r   isatty)r2   r   r   r   r     s    zSpooledTemporaryFile.isattyc             C   s(   y| j jS  tk
r"   | jd S X d S )Nr   )r   r   rM   r   )r2   r   r   r   r     s    zSpooledTemporaryFile.modec             C   s"   y| j jS  tk
r   d S X d S )N)r   rG   rM   )r2   r   r   r   rG     s    zSpooledTemporaryFile.namec             C   s8   y| j jS  tk
r2   d| jd kr( | jd S X d S )Nr   r   r   )r   newlinesrM   r   )r2   r   r   r   r     s    zSpooledTemporaryFile.newlinesc             G   s   | j j| S )N)r   read)r2   r#   r   r   r   r     s    zSpooledTemporaryFile.readc             G   s   | j j| S )N)r   readline)r2   r#   r   r   r   r     s    zSpooledTemporaryFile.readlinec             G   s   | j j| S )N)r   	readlines)r2   r#   r   r   r   r     s    zSpooledTemporaryFile.readlinesc             G   s   | j j|  d S )N)r   r   )r2   r#   r   r   r   r     s    zSpooledTemporaryFile.seekc             C   s   | j jS )N)r   	softspace)r2   r   r   r   r     s    zSpooledTemporaryFile.softspacec             C   s
   | j  S )N)r   r   )r2   r   r   r   r     s    zSpooledTemporaryFile.tellc             C   s6   |d kr| j   n|| jkr&|   | j | d S )N)r   truncater   r   )r2   sizer   r   r   r     s
    
zSpooledTemporaryFile.truncatec             C   s   | j }||}| | |S )N)r   rY   r   )r2   srp   rvr   r   r   rY     s    

zSpooledTemporaryFile.writec             C   s   | j }||}| | |S )N)r   
writelinesr   )r2   iterablerp   r   r   r   r   r      s    

zSpooledTemporaryFile.writelines)r   r   r   NNNNN)N)r@   rA   rB   r   rw   r   r   r   r   r4   r   rC   r   r   r   r   r   r   rG   r   r   r   r   r   r   r   r   rY   r   r   r   r   r   r   s  s6     
		
c               @   sB   e Zd ZdddZedd Zdd Zdd	 Zd
d Zdd Z	dS )r   Nc             C   s0   t |||| _tj| | j| jd| d| _d S )NzImplicitly cleaning up {!r})warn_message)r   rG   _weakrefZfinalize_cleanupformat
_finalizer)r2   r)   r(   r*   r   r   r   rw     s    
zTemporaryDirectory.__init__c             C   s   t | t|t d S )N)_shutilrmtree	_warningswarnResourceWarning)clsrG   r   r   r   r   r     s    
zTemporaryDirectory._cleanupc             C   s   d | jj| jS )Nz	<{} {!r}>)r   	__class__r@   rG   )r2   r   r   r   __repr__  s    zTemporaryDirectory.__repr__c             C   s   | j S )N)rG   )r2   r   r   r   r   !  s    zTemporaryDirectory.__enter__c             C   s   |    d S )N)cleanup)r2   r   r   r   r   r   r   r   $  s    zTemporaryDirectory.__exit__c             C   s   | j  rt| j d S )N)r   detachr   r   rG   )r2   r   r   r   r   '  s    
zTemporaryDirectory.cleanup)NNN)
r@   rA   rB   rw   classmethodr   r   r   r   r   r   r   r   r   r     s   
)NNNF)NNN)r   r   NNNNNT)r   r   NNNNN)?__all__	functoolsr   warningsr   iorX   osr   Zshutilr   errnora   Zrandomr   r1   weakrefr   _threadallocate_lockZ_allocate_lockO_RDWRr   O_EXCLrr   r   r   rW   r   r   r&   rh   r   r   r   r   r%   r,   r-   rQ   rf   rg   rk   rq   r	   r   r
   r   r   r   r   r   ru   rz   r   rG   sysplatformr   r   r   objectr   r   r   r   r   <module>   s~   






	-
(
' +?  
$
  
8 