o
    +ca~I                     @   s  U 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 ej	
dZej	
do/dejv Zdejv o=dejdd	v Zej	
doIe oIe ZdZejejejgejf  ed
< edZdefddZ		dtdejdeje deje dededejfddZdedefddZdejdefddZ G dd dej!Z"G dd dZ#dudejdedefd d!Z$dudejdedefd"d#Z%dejdejej fd$d%Z&dejdejej fd&d'Z'dejdefd(d)Z(dejd*ed+eje defd,d-Z)dejdeje deje defd.d/Z*		dtd0ejdeje deje d1ejejegef d2ejejgejej f dededejfd3d4Z+	dud5ejdeje deje dedejf
d6d7Z,	dud8ejdeje deje dedejf
d9d:Z-dejfd;d<Z.dejfd=d>Z/dejfd?d@Z0	dvdeje deje dejfdAdBZ1	dvdeje deje dejfdCdDZ2	dvdeje deje dejfdEdFZ3dGej4eej5e6f dHedeje deje dejf
dIdJZ7	K		L	dwdMedHedeje deje dNedej8ejef fdOdPZ9G dQdR dRZ:d+edefdSdTZ;dejdefdUdVZ<	dvdejej dWeje defdXdYZ=ej	
drieridZd[l>m?Z? defd\d]Z@e ZAejBejejf ed^< 	dxdejdWeje dejfd_d
Zndefd`d]Z@daejdeje deje dejej fdbdcZ?ddede6fdedfZCdejdefdgdhZDdiejg ejf djejg ejf dejg ejf fdkdlZEeEdmdn e1ZFeEdodn e2ZGeEdpdn e3ZHe.e/e0dqZIejJeejg ejf f edr< e1e2e3dqZKejJeejeje eje gejf f eds< dS )y    N)WeakKeyDictionarycygwinwinGCCAPPENGINE_RUNTIMEzDevelopment/SERVER_SOFTWARE auto_wrap_for_ansiz\033\[[;?0-9]*[a-zA-Z]returnc                   C   s   t  pt  S N)sysgetfilesystemencodinggetdefaultencoding r   r   //usr/lib/python3/dist-packages/click/_compat.pyget_filesystem_encoding   s   r   Fstreamencodingerrorsforce_readableforce_writablec                 C   s0   |d u rt | }|d u rd}t| ||d||dS )NreplaceT)line_bufferingr   r   )get_best_encoding_NonClosingTextIOWrapper)r   r   r   r   r   r   r   r   _make_text_stream   s   r   c                 C   s(   z	t | jdkW S  ty   Y dS w )z$Checks if a given encoding is ascii.asciiF)codecslookupnameLookupError)r   r   r   r   is_ascii_encoding-   s
   r!   c                 C   s$   t | ddp	t }t|rdS |S )z1Returns the default stream encoding if not found.r   Nzutf-8)getattrr   r   r!   r   rvr   r   r   r   5   s   r   c                       sh   e Zd Z		ddejdeje deje dededejdd	f fd
dZ	dddZ
defddZ  ZS )r   Fr   r   r   r   r   extrar
   Nc                    s8   t t jt||| | _}t j|||fi | d S r   )tcastBinaryIO_FixupStream_streamsuper__init__)selfr   r   r   r   r   r%   	__class__r   r   r,   >   s   	
z!_NonClosingTextIOWrapper.__init__c                 C   s$   z|    W d S  ty   Y d S w r   )detach	Exceptionr-   r   r   r   __del__L   s
   z _NonClosingTextIOWrapper.__del__c                 C   s
   | j  S r   )r*   isattyr2   r   r   r   r4   R   s   
z_NonClosingTextIOWrapper.isattyFF)r
   N)__name__
__module____qualname__r&   r(   OptionalstrboolAnyr,   r3   r4   __classcell__r   r   r.   r   r   =   s(    
r   c                   @   s~   e Zd ZdZ		ddejdedefddZded	ej	fd
dZ
ded	efddZd	efddZd	efddZd	efddZdS )r)   aQ  The new io interface needs more from streams than streams
    traditionally implement.  As such, this fix-up code is necessary in
    some circumstances.

    The forcing of readable and writable flags are there because some tools
    put badly patched objects on sys (one such offender are certain version
    of jupyter notebook).
    Fr   r   r   c                 C   s   || _ || _|| _d S r   )r*   _force_readable_force_writable)r-   r   r   r   r   r   r   r,   a   s   
z_FixupStream.__init__r   r
   c                 C      t | j|S r   )r"   r*   r-   r   r   r   r   __getattr__k      z_FixupStream.__getattr__sizec                 C   s2   t | jdd }|d urtt||S | j|S )Nread1)r"   r*   r&   r'   bytesread)r-   rD   fr   r   r   rE   n   s   z_FixupStream.read1c                 C   sV   | j rdS t| jdd }|d urtt| S z	| jd W dS  ty*   Y dS w )NTreadabler   F)r>   r"   r*   r&   r'   r;   rG   r1   r-   xr   r   r   rI   v   s   z_FixupStream.readablec                 C   s|   | j rdS t| jdd }|d urtt| S z	| jd W dS  ty=   z
| jd W Y dS  ty<   Y Y dS w w )NTwritabler       F)r?   r"   r*   r&   r'   r;   writer1   rJ   r   r   r   rL      s    z_FixupStream.writablec                 C   sR   t | jdd }|d urtt| S z| j| j  W dS  ty(   Y dS w )NseekableFT)r"   r*   r&   r'   r;   seektellr1   rJ   r   r   r   rO      s   z_FixupStream.seekableNr5   )r6   r7   r8   __doc__r&   r(   r;   r,   r:   r<   rB   intrF   rE   rI   rL   rO   r   r   r   r   r)   W   s     

r)   defaultc                 C   s*   z	t | dtW S  ty   | Y S w Nr   )
isinstancerG   rF   r1   r   rT   r   r   r   _is_binary_reader   s
   rX   c                 C   sL   z|  d W dS  ty%   z	|  d W Y dS  ty$   Y | Y S w w )NrM   r   FT)rN   r1   rW   r   r   r   _is_binary_writer   s   
rY   c                 C   H   t | drttj| S t| dd }|d ur"t |dr"ttj|S d S NFbufferT)rX   r&   r'   r(   r"   r   bufr   r   r   _find_binary_reader      
r_   c                 C   rZ   r[   )rY   r&   r'   r(   r"   r]   r   r   r   _find_binary_writer   r`   ra   c                 C   s   t t| ddpdS )z3A stream is misconfigured if its encoding is ASCII.r   Nr   )r!   r"   r   r   r   r   _stream_is_misconfigured   s   rc   attrvaluec                 C   s$   t | |d}||kp|du o|duS )zA stream attribute is compatible if it is equal to the
    desired value or the desired value is unset and the attribute
    has a value.
    N)r"   )r   rd   re   stream_valuer   r   r   _is_compat_stream_attr   s   rg   c                 C   s   t | d|ot | d|S )zcCheck if a stream's encoding and errors attributes are
    compatible with the desired values.
    r   r   )rg   )r   r   r   r   r   r   _is_compatible_text_stream   s
   
rh   text_stream	is_binaryfind_binaryc           	      C   s~   || drt t j| }n#t t j| } t| ||r$|d u r"t| s$| S || }|d u r.| S |}|d u r6d}t|||||dS )NFr   )r   r   )r&   r'   r(   TextIOrh   rc   r   )	ri   r   r   rj   rk   r   r   binary_readerpossible_binary_readerr   r   r   _force_correct_text_stream   s,   
	ro   text_readerc                 C      t | ||tt|dS )Nr   )ro   rX   r_   )rp   r   r   r   r   r   r   _force_correct_text_reader!     rs   text_writerc                 C   rq   )Nr   )ro   rY   ra   )ru   r   r   r   r   r   r   _force_correct_text_writer1  rt   rw   c                  C      t tj} | d u rtd| S )Nz6Was not able to determine binary stream for sys.stdin.)r_   r   stdinRuntimeError)readerr   r   r   get_binary_stdinA     
r|   c                  C   rx   )Nz7Was not able to determine binary stream for sys.stdout.)ra   r   stdoutrz   writerr   r   r   get_binary_stdoutH  r}   r   c                  C   rx   )Nz7Was not able to determine binary stream for sys.stderr.)ra   r   stderrrz   r   r   r   r   get_binary_stderrO  r}   r   c                 C   ,   t tj| |}|d ur|S ttj| |ddS )NTrr   )_get_windows_console_streamr   ry   rs   r   r   r$   r   r   r   get_text_stdinV     r   c                 C   r   NTrv   )r   r   r~   rw   r   r   r   r   get_text_stdout_  r   r   c                 C   r   r   )r   r   r   rw   r   r   r   r   get_text_stderrh  r   r   filemodec                 C   s"   d|v r	t | |S t | |||dS )z?Handles not passing ``encoding`` and ``errors`` in binary mode.br   r   )open)r   r   r   r   r   r   r   _wrap_io_openq  s   
r   rstrictfilenameatomicc              
      s  d v }| dkr1t  fdddD r"|rt dfS t||ddfS |r)t dfS t||ddfS |s<t|  ||dfS d	 v rDtd
d v rLtdd vrTtddd l}dd l}zt	
| j}W n tyo   d }Y nw t	jt	jB t	jB }	|r|	tt	ddO }		 t	jt	j| d|dd}
zt	|
|	|d u rdn|}W n3 ty } z'|j|jkst	jdkr|j|jkrt	j|jrt	|jt	jrW Y d }~q d }~ww |d urt	|
| t| ||}t||
t	j| }t !t j"|dfS )Nr   -c                 3   s    | ]}| v V  qd S r   r   ).0mr   r   r   	<genexpr>  s    zopen_stream.<locals>.<genexpr>)warK   Fr   Tr   zAppending to an existing file is not supported, because that would involve an expensive `copy`-operation to a temporary file. Open the file in normal `w`-mode and copy explicitly if that's what you're after.rK   z&Use the `overwrite`-parameter instead.r   z,Atomic writes only make sense with `w`-mode.r   O_BINARYz.__atomic-writel        08xi  nt)#anyr   r   r|   r   r   
ValueErrorerrnorandomosstatst_modeOSErrorO_RDWRO_CREATO_EXCLr"   pathjoindirname	randranger   EEXISTr   EACCESisdirr   accessW_OKchmod_AtomicFilerealpathr&   r'   IO)r   r   r   r   r   binaryr   r   permflagstmp_filenamefderH   afr   r   r   open_stream~  sp   




r   c                   @   s   e Zd ZdejdededdfddZedefdd	Zdde	ddfddZ
dedejfddZdddZdd ZdefddZdS )r   rH   r   real_filenamer
   Nc                 C   s   || _ || _|| _d| _d S NF)_f_tmp_filename_real_filenameclosed)r-   rH   r   r   r   r   r   r,     s   
z_AtomicFile.__init__c                 C   s   | j S r   )r   r2   r   r   r   r     s   z_AtomicFile.nameFdeletec                 C   s.   | j rd S | j  t| j| j d| _ d S )NT)r   r   closer   r   r   r   )r-   r   r   r   r   r     s
   

z_AtomicFile.closer   c                 C   r@   r   )r"   r   rA   r   r   r   rB     rC   z_AtomicFile.__getattr__c                 C   s   | S r   r   r2   r   r   r   	__enter__  s   z_AtomicFile.__enter__c                 C   s   | j |d ud d S )N)r   )r   )r-   exc_type	exc_valuetbr   r   r   __exit__     z_AtomicFile.__exit__c                 C   s
   t | jS r   )reprr   r2   r   r   r   __repr__  s   
z_AtomicFile.__repr__F)r
   r   )r6   r7   r8   r&   r   r:   r,   propertyr   r;   r   r<   rB   r   r   r   r   r   r   r   r     s    
r   c                 C   s   t d| S )Nr   )_ansi_resub)re   r   r   r   
strip_ansi  rC   r   c                 C   s0   t | ttfr| j} t | ttfs| jjdS )Nz
ipykernel.)rV   r)   r   r*   r/   r7   
startswithrb   r   r   r   _is_jupyter_kernel_output  s   r   colorc                 C   s0   |d u r| d u rt j} t|  ot|  S | S r   )r   ry   r4   r   )r   r   r   r   r   should_strip_ansi  s
   r      )r   c                  C   s   dd l } |  S rU   )localegetpreferredencoding)r   r   r   r   _get_argv_encoding  s   r   _ansi_stream_wrappersc                    s   zt | }W n ty   d}Y nw |dur|S ddl}t| |}|j| |dttjj	}|j
  fdd}||_
z|t | < W |S  tyO   Y |S w )zbSupport ANSI color and style codes on Windows by wrapping a
        stream with colorama.
        Nr   )stripc                    s$   z | W S  t y      w r   )BaseException	reset_all)s_writeansi_wrapperr   r   _safe_write$  s   
z'auto_wrap_for_ansi.<locals>._safe_write)r   getr1   coloramar   AnsiToWin32r&   r'   rl   r   rN   )r   r   cachedr   r   r$   r   r   r   r   r	     s*   

c                   C   s   t tjdd p	t S )Nr   )r"   r   ry   r   r   r   r   r   r   7  r   rH   c                 C   s   d S r   r   )rH   r   r   r   r   r   r   :  s   r   rK   c                 C   s   t t| S r   )lenr   )rK   r   r   r   term_len@  rC   r   c                 C   s    z|   W S  ty   Y dS w r   )r4   r1   rb   r   r   r   r4   D  s
   
r4   src_funcwrapper_funcc                    s"   t   dtjf fdd}|S )Nr
   c                     sb    } z  | }W n ty   d }Y nw |d ur|S  }z| | < W |S  ty0   Y |S w r   )r   r1   r#   cacher   r   r   r   funcP  s    
z&_make_cached_stream_func.<locals>.func)r   r&   rl   )r   r   r   r   r   r   _make_cached_stream_funcK  s   r   c                   C      t jS r   )r   ry   r   r   r   r   <lambda>b      r   c                   C   r   r   )r   r~   r   r   r   r   r   c  r   c                   C   r   r   )r   r   r   r   r   r   r   d  r   )ry   r~   r   binary_streamstext_streamsr5   r   )NN)r   Nr   Fr   )Lr   ior   rer   typingr&   weakrefr   platformr   CYGWINversionMSYS2environr   
APP_ENGINEWINr	   r9   Callablerl   __annotations__compiler   r:   r   r(   r;   r   r!   r   r   TextIOWrapperr   r)   rX   rY   r_   ra   rc   rg   rh   ro   rs   rw   r|   r   r   r   r   r   UnionPathLikerS   r   Tupler   r   r   r   r   _winconsoler   r   r   MutableMappingr   r4   r   _default_text_stdin_default_text_stdout_default_text_stderrr   Mappingr   r   r   r   r   <module>   s  
 "

E	"		

2






	

P

(


$	"