B
    ÓùÓ`´  ã               @   sð  U 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 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 dlmZ d dlmZ ddlmZ ejdksät‚d dlZd dlmZ d dlmZ ee
ƒZejZejZejZej Z ej!Z!ej"Z"eeƒdejfƒZ#eeeƒeee	ƒƒdej$fƒZ%eeeƒdejfƒZ&edƒZ'edƒZ(edƒZ)d Z*dZ+d Z,dZ-dZ.d Z/dZ0dZ1dZ2dZ3yd d lm4Z4 W n e5k
�rÞ   dZ6Y n(X G d!d"„ d"eƒZ7e4j8Z8e4j9Z9d=d$d%„Z6G d&d'„ d'e j:ƒZ;G d(d)„ d)e;ƒZ<G d*d+„ d+e;ƒZ=G d,d-„ d-ƒZ>ej?ej@d.œd/d0„ZAej?ej@d.œd1d2„ZBej?ej@d.œd3d4„ZCeAeBeCd5œZDejEeFejGej?gej@f f eHd6< ej@eId7œd8d9„ZJej@ejKeL ejKeL ejKej@ d:œd;d<„ZMdS )>é    N)Úbyref)Úc_char)Úc_char_p)Úc_int)Ú	c_ssize_t)Úc_ulong)Úc_void_p)ÚPOINTER)Ú	py_object)Ú	Structure)ÚDWORD)ÚHANDLE)ÚLPCWSTR)ÚLPWSTRé   )Ú_NonClosingTextIOWrapperÚwin32)Úwindll)ÚWINFUNCTYPEÚGetCommandLineWÚCommandLineToArgvWÚ	LocalFreeiöÿÿÿiõÿÿÿiôÿÿÿé   iã  é   ó   iÿ  )Ú	pythonapic               @   sR   e Zd Zdefdefdefdefdefdefdefdefd	efd
efdefgZ	dS )Ú	Py_bufferÚbufÚobjÚlenÚitemsizeÚreadonlyÚndimÚformatÚshapeÚstridesÚ
suboffsetsZinternalN)
Ú__name__Ú
__module__Ú__qualname__r   r
   r   r   r   Ú	c_ssize_pÚ_fields_© r,   r,   ú5/tmp/pip-unpacked-wheel-zz7esowa/click/_winconsole.pyr   K   s   r   Fc          	   C   sP   t ƒ }|rtnt}tt| ƒt|ƒ|ƒ zt|j }| |j	¡S t
t|ƒƒ X d S )N)r   ÚPyBUF_WRITABLEÚPyBUF_SIMPLEÚPyObject_GetBufferr
   r   r   r   Úfrom_addressr   ÚPyBuffer_Release)r   Úwritabler   ÚflagsZbuffer_typer,   r,   r-   Ú
get_buffer]   s    
r5   c                   s$   e Zd Zdd„ Z‡ fdd„Z‡  ZS )Ú_WindowsConsoleRawIOBasec             C   s
   || _ d S )N)Úhandle)Úselfr7   r,   r,   r-   Ú__init__j   s    z!_WindowsConsoleRawIOBase.__init__c                s   t ƒ  ¡  dS )NT)ÚsuperÚisatty)r8   )Ú	__class__r,   r-   r;   m   s    
z_WindowsConsoleRawIOBase.isatty)r'   r(   r)   r9   r;   Ú__classcell__r,   r,   )r<   r-   r6   i   s   r6   c               @   s   e Zd Zdd„ Zdd„ ZdS )Ú_WindowsConsoleReaderc             C   s   dS )NTr,   )r8   r,   r,   r-   Úreadables   s    z_WindowsConsoleReader.readablec             C   s–   t |ƒ}|sdS |d r tdƒ‚t|dd�}|d }tƒ }tt| jƒ||t|ƒd ƒ}tƒ t	krht
 d¡ |s|tdtƒ › �ƒ‚|d tkrŒdS d|j S )Nr   r   z>cannot read odd number of bytes from UTF-16-LE encoded consoleT)r3   gš™™™™™¹?zWindows error: )r   Ú
ValueErrorr5   r   ÚReadConsoleWr   r7   r   ÚGetLastErrorÚERROR_OPERATION_ABORTEDÚtimeÚsleepÚOSErrorÚEOFÚvalue)r8   ÚbZbytes_to_be_readÚbufferZcode_units_to_be_readZcode_units_readÚrvr,   r,   r-   Úreadintov   s,    

z_WindowsConsoleReader.readintoN)r'   r(   r)   r?   rL   r,   r,   r,   r-   r>   r   s   r>   c               @   s(   e Zd Zdd„ Zedd„ ƒZdd„ ZdS )Ú_WindowsConsoleWriterc             C   s   dS )NTr,   )r8   r,   r,   r-   r3   –   s    z_WindowsConsoleWriter.writablec             C   s"   | t krdS | tkrdS d| › �S )NÚERROR_SUCCESSÚERROR_NOT_ENOUGH_MEMORYzWindows error )rN   rO   )Úerrnor,   r,   r-   Ú_get_error_message™   s
    z(_WindowsConsoleWriter._get_error_messagec             C   sl   t |ƒ}t|ƒ}t|tƒd }tƒ }tt| jƒ||t|ƒd ƒ d|j	 }|dkrh|dkrht
|  tƒ ¡ƒ‚|S )Nr   r   )r   r5   ÚminÚMAX_BYTES_WRITTENr   ÚWriteConsoleWr   r7   r   rH   rF   rQ   rB   )r8   rI   Zbytes_to_be_writtenr   Zcode_units_to_be_writtenZcode_units_writtenZbytes_writtenr,   r,   r-   Úwrite¡   s    
z_WindowsConsoleWriter.writeN)r'   r(   r)   r3   ÚstaticmethodrQ   rU   r,   r,   r,   r-   rM   •   s   rM   c               @   s†   e Zd Zejejddœdd„Zeedœdd„ƒZ	ej
edœd	d
„Zejej
 ddœdd„Zeejdœdd„Zedœdd„Zdd„ ZdS )ÚConsoleStreamN)Útext_streamÚbyte_streamÚreturnc             C   s   || _ || _d S )N)Ú_text_streamrJ   )r8   rX   rY   r,   r,   r-   r9   ¶   s    zConsoleStream.__init__)rZ   c             C   s   | j jS )N)rJ   Úname)r8   r,   r,   r-   r\   º   s    zConsoleStream.name)ÚxrZ   c             C   sD   t |tƒr| j |¡S y|  ¡  W n tk
r6   Y nX | j |¡S )N)Ú
isinstanceÚstrr[   rU   ÚflushÚ	ExceptionrJ   )r8   r]   r,   r,   r-   rU   ¾   s    
zConsoleStream.write)ÚlinesrZ   c             C   s   x|D ]}|   |¡ qW d S )N)rU   )r8   rb   Úliner,   r,   r-   Ú
writelinesÇ   s    
zConsoleStream.writelines)r\   rZ   c             C   s   t | j|ƒS )N)Úgetattrr[   )r8   r\   r,   r,   r-   Ú__getattr__Ë   s    zConsoleStream.__getattr__c             C   s
   | j  ¡ S )N)rJ   r;   )r8   r,   r,   r-   r;   Î   s    zConsoleStream.isattyc             C   s   d| j ›d| j›d�S )Nz<ConsoleStream name=z
 encoding=ú>)r\   Úencoding)r8   r,   r,   r-   Ú__repr__Ñ   s    zConsoleStream.__repr__)r'   r(   r)   ÚtÚTextIOÚBinaryIOr9   Úpropertyr_   r\   ZAnyStrÚintrU   ÚIterablerd   ZAnyrf   Úboolr;   ri   r,   r,   r,   r-   rW   µ   s   	rW   )Úbuffer_streamrZ   c             C   s.   t t ttƒ¡dddd�}t tjt|| ƒ¡S )Nz	utf-16-leÚstrictT)Úline_buffering)	r   ÚioÚBufferedReaderr>   ÚSTDIN_HANDLErj   Úcastrk   rW   )rq   rX   r,   r,   r-   Ú_get_text_stdinÕ   s    rx   c             C   s.   t t ttƒ¡dddd�}t tjt|| ƒ¡S )Nz	utf-16-lerr   T)rs   )	r   rt   ÚBufferedWriterrM   ÚSTDOUT_HANDLErj   rw   rk   rW   )rq   rX   r,   r,   r-   Ú_get_text_stdoutß   s    r{   c             C   s.   t t ttƒ¡dddd�}t tjt|| ƒ¡S )Nz	utf-16-lerr   T)rs   )	r   rt   ry   rM   ÚSTDERR_HANDLErj   rw   rk   rW   )rq   rX   r,   r,   r-   Ú_get_text_stderré   s    r}   )r   r   r   Ú_stream_factories)ÚfrZ   c          	   C   sT   t | dƒsdS y|  ¡ }W n ttjfk
r4   dS X t |¡}tt|t	t
ƒ ƒƒƒS )NÚfilenoF)Úhasattrr€   rF   rt   ÚUnsupportedOperationÚmsvcrtZget_osfhandlerp   ÚGetConsoleModer   r   )r   r€   r7   r,   r,   r-   Ú_is_consoleú   s    

r…   )r   rh   ÚerrorsrZ   c             C   sZ   t d k	rV|dkrV|dkrVt| ƒrVt |  ¡ ¡}|d k	rVt| dd ƒ}|d krNd S ||ƒS d S )N>   ú	utf-16-leN>   Nrr   rJ   )r5   r…   r~   Úgetr€   re   )r   rh   r†   ÚfuncrI   r,   r,   r-   Ú_get_windows_console_stream  s    rŠ   )F)Nrt   ÚsysrD   Útypingrj   Úctypesr   r   r   r   r   r   r   r	   r
   r   Zctypes.wintypesr   r   r   r   Ú_compatr   ÚplatformÚAssertionErrorrƒ   r   r   r*   Úkernel32ZGetStdHandlerA   rT   r„   rB   r   Úshell32r   r   rv   rz   r|   r/   r.   rN   rO   rC   ZSTDIN_FILENOZSTDOUT_FILENOZSTDERR_FILENOrG   rS   r   ÚImportErrorr5   r   r0   r2   Ú	RawIOBaser6   r>   rM   rW   rl   rk   rx   r{   r}   r~   ÚMappingrn   ÚCallableÚ__annotations__rp   r…   ÚOptionalr_   rŠ   r,   r,   r,   r-   Ú<module>	   sˆ    

	#  

*