� j f]� @s�dZdddddddgZdd lZeed �rRejd d g�nd dlmZd dlmZd dlmZd dlm Z d dlm Z d dlm Z d dl m Z d$ZGdd�de�ZGdd�de�Ze d d dd dedd��Ze d d dd dedd��Zeed �r�e d dd dedd ��Ze d dd dedd ��ZnGdd �d e j�ZGd!d�dee j�ZGd"d�d�ZGd#d�d�Zd S)%zStream-related things.� StreamReader� StreamWriter�StreamReaderProtocol�open_connection� start_server�IncompleteReadError�LimitOverrunError�NZAF_UNIX�open_unix_connection�start_unix_server�)� coroutines)�compat)�events)�futures)� protocols)� coroutine)�logger��cs(eZdZdZ�fdd�Z�S)rz� Incomplete read error. Attributes: - partial: read bytes string before the end of stream was reached - expected: total number of expected bytes (or None if unknown) cs6t�jdt|�|f�||_||_dS)Nz-%d bytes read on a total of %r expected bytes)�super�__init__�len�partial�expected)�selfrr)� __class__��4/opt/alt/python34/lib64/python3.4/asyncio/streams.pyr!s  zIncompleteReadError.__init__)�__name__� __module__� __qualname__�__doc__rrr)rrrs cs(eZdZdZ�fdd�Z�S)rz�Reached buffer limit while looking for the separator. Attributes: - message: error message - consumed: total number of bytes that should be consumed cs&t�j|�||_||_dS)N)rr�message�consumed)rr"r#)rrrr/s zLimitOverrunError.__init__)rrr r!rrr)rrr(s �loop�limitc +s�|dkrtj�}ntd|d|�}t|d|��|j�fdd�|||�DdH\}}t|�||�}||fS)a�A wrapper for create_connection() returning a (reader, writer) pair. The reader returned is a StreamReader instance; the writer is a StreamWriter instance. The arguments are all the usual arguments to create_connection() except protocol_factory; most common are positional host and port, with various optional keyword arguments following. Additional optional keyword arguments are loop (to set the event loop instance to use) and limit (to set the buffer limit passed to the StreamReader). (If you want to customize the StreamReader and/or StreamReaderProtocol classes, just copy the code -- there's really nothing special here except some convenience.) Nr%r$cs�S)Nrr)�protocolrr�<lambda>Nsz!open_connection.<locals>.<lambda>)r�get_event_looprrZcreate_connectionr) �host�portr$r%�kwds�reader� transport�_�writerr)r&rr5s )c+sN�dkrtj��n���fdd�}�j||||�DdHS)a�Start a socket server, call back for each client connected. The first parameter, `client_connected_cb`, takes two parameters: client_reader, client_writer. client_reader is a StreamReader object, while client_writer is a StreamWriter object. This parameter can either be a plain callback function or a coroutine; if it is a coroutine, it will be automatically converted into a Task. The rest of the arguments are all the usual arguments to loop.create_server() except protocol_factory; most common are positional host and port, with various optional keyword arguments following. The return value is the same as loop.create_server(). Additional optional keyword arguments are loop (to set the event loop instance to use) and limit (to set the buffer limit passed to the StreamReader). The return value is the same as loop.create_server(), i.e. a Server object which can be used to stop the service. Ncs.td�d��}t|�d��}|S)Nr%r$)rr)r,r&)�client_connected_cbr%r$rr�factoryns  zstart_server.<locals>.factory)rr(Z create_server)r0r)r*r$r%r+r1r)r0r%r$rrSs c+s�|dkrtj�}ntd|d|�}t|d|��|j�fdd�||�DdH\}}t|�||�}||fS)z@Similar to `open_connection` but works with UNIX Domain Sockets.Nr%r$cs�S)Nrr)r&rrr'�sz&open_unix_connection.<locals>.<lambda>)rr(rrZcreate_unix_connectionr)�pathr$r%r+r,r-r.r/r)r&rr zs &c+sK�dkrtj��n���fdd�}�j|||�DdHS)z=Similar to `start_server` but works with UNIX Domain Sockets.Ncs.td�d��}t|�d��}|S)Nr%r$)rr)r,r&)r0r%r$rrr1�s  z"start_unix_server.<locals>.factory)rr(Zcreate_unix_server)r0r2r$r%r+r1r)r0r%r$rr �s c@s[eZdZdZddd�Zdd�Zdd�Zd d �Zed d ��Z dS) �FlowControlMixina)Reusable flow control logic for StreamWriter.drain(). This implements the protocol methods pause_writing(), resume_reading() and connection_lost(). If the subclass overrides these it must call the super methods. StreamWriter.drain() must wait for _drain_helper() coroutine. NcCsF|dkrtj�|_n ||_d|_d|_d|_dS)NF)rr(�_loop�_paused� _drain_waiter�_connection_lost)rr$rrrr�s     zFlowControlMixin.__init__cCs?|j st�d|_|jj�r;tjd|�ndS)NTz%r pauses writing)r5�AssertionErrorr4� get_debugr�debug)rrrr� pause_writing�s zFlowControlMixin.pause_writingcCs{|jst�d|_|jj�r:tjd|�n|j}|dk rwd|_|j�sw|jd�qwndS)NFz%r resumes writing) r5r8r4r9rr:r6�done� set_result)r�waiterrrr�resume_writing�s     zFlowControlMixin.resume_writingcCsud|_|jsdS|j}|dkr/dSd|_|j�rHdS|dkrd|jd�n |j|�dS)NT)r7r5r6r<r=� set_exception)r�excr>rrr�connection_lost�s       z FlowControlMixin.connection_lostccsw|jrtd��n|js%dS|j}|dksL|j�sLt�tjd|j�}||_|DdHdS)NzConnection lostr$) r7�ConnectionResetErrorr5r6� cancelledr8r�Futurer4)rr>rrr� _drain_helper�s    zFlowControlMixin._drain_helper) rrr r!rr;r?rBrrFrrrrr3�s   r3csdeZdZdZdd�fdd�Zdd�Z�fdd�Zd d �Zd d �Z�S) ra=Helper class to adapt between Protocol and StreamReader. (This is a helper class instead of making StreamReader itself a Protocol subclass, because the StreamReader has other potential uses, and to prevent the user of the StreamReader to accidentally call inappropriate methods of the protocol.) Ncs2t�jd|�||_d|_||_dS)Nr$)rr�_stream_reader�_stream_writer�_client_connected_cb)rZ stream_readerr0r$)rrrr�s  zStreamReaderProtocol.__init__cCs~|jj|�|jdk rzt|||j|j�|_|j|j|j�}tj|�rz|jj|�qzndS)N) rG� set_transportrIrr4rHr Z iscoroutineZ create_task)rr-�resrrr�connection_made�s   z$StreamReaderProtocol.connection_madecs@|dkr|jj�n|jj|�t�j|�dS)N)rG�feed_eofr@rrB)rrA)rrrrB�s z$StreamReaderProtocol.connection_lostcCs|jj|�dS)N)rG� feed_data)r�datarrr� data_received�sz"StreamReaderProtocol.data_receivedcCs|jj�dS)NT)rGrM)rrrr� eof_received�s z!StreamReaderProtocol.eof_received) rrr r!rrLrBrPrQrr)rrr�s   c@s�eZdZdZdd�Zdd�Zedd��Zdd �Zd d �Z d d �Z dd�Z dd�Z ddd�Z edd��ZdS)ra'Wraps a Transport. This exposes write(), writelines(), [can_]write_eof(), get_extra_info() and close(). It adds drain() which returns an optional Future on which you can wait for flow control. It also adds a transport property which references the Transport directly. cCsI||_||_|dks3t|t�s3t�||_||_dS)N)� _transport� _protocol� isinstancerr8�_readerr4)rr-r&r,r$rrrr s   ! zStreamWriter.__init__cCsP|jjd|jg}|jdk r?|jd|j�nddj|�S)Nz transport=%rz reader=%rz<%s>� )rrrRrU�append�join)r�inforrr�__repr__szStreamWriter.__repr__cCs|jS)N)rR)rrrrr-szStreamWriter.transportcCs|jj|�dS)N)rR�write)rrOrrrr[szStreamWriter.writecCs|jj|�dS)N)rR� writelines)rrOrrrr\"szStreamWriter.writelinescCs |jj�S)N)rR� write_eof)rrrrr]%szStreamWriter.write_eofcCs |jj�S)N)rR� can_write_eof)rrrrr^(szStreamWriter.can_write_eofcCs |jj�S)N)rR�close)rrrrr_+szStreamWriter.closeNcCs|jj||�S)N)rR�get_extra_info)r�name�defaultrrrr`.szStreamWriter.get_extra_infoccsu|jdk r6|jj�}|dk r6|�q6n|jdk r_|jj�r_dVq_n|jj�DdHdS)z~Flush the write buffer. The intended use is to write w.write(data) yield from w.drain() N)rU� exceptionrRZ is_closingrSrF)rrArrr�drain1s    zStreamWriter.drain)rrr r!rrZ�propertyr-r[r\r]r^r_r`rrdrrrrrs        c@seZdZeddd�Zdd�Zdd�Zdd �Zd d �Zd d �Z dd�Z dd�Z dd�Z dd�Z edd��Zedd��Zeddd��Zed&dd��Zed d!��Zejred"d#��Zed$d%��ZndS)'rNcCs�|dkrtd��n||_|dkrBtj�|_n ||_t�|_d|_d|_d|_ d|_ d|_ dS)NrzLimit cannot be <= 0F) � ValueError�_limitrr(r4� bytearray�_buffer�_eof�_waiter� _exceptionrRr5)rr%r$rrrrLs         zStreamReader.__init__cCs�dg}|jr/|jdt|j��n|jrH|jd�n|jtkrn|jd|j�n|jr�|jd|j�n|jr�|jd|j�n|jr�|jd|j�n|j r�|jd�nd d j |�S) Nrz%d bytes�eofzl=%dzw=%rze=%rzt=%rZpausedz<%s>rV) rirWrrjrg�_DEFAULT_LIMITrkrlrRr5rX)rrYrrrrZ_s        zStreamReader.__repr__cCs|jS)N)rl)rrrrrcqszStreamReader.exceptioncCsJ||_|j}|dk rFd|_|j�sF|j|�qFndS)N)rlrkrDr@)rrAr>rrrr@ts      zStreamReader.set_exceptioncCsA|j}|dk r=d|_|j�s=|jd�q=ndS)z1Wakeup read*() functions waiting for data or EOF.N)rkrDr=)rr>rrr�_wakeup_waiter}s     zStreamReader._wakeup_waitercCs(|jdkstd��||_dS)NzTransport already set)rRr8)rr-rrrrJ�szStreamReader.set_transportcCs>|jr:t|j�|jkr:d|_|jj�ndS)NF)r5rrirgrR�resume_reading)rrrr�_maybe_resume_transport�s! z$StreamReader._maybe_resume_transportcCsd|_|j�dS)NT)rjro)rrrrrM�s zStreamReader.feed_eofcCs|jo|j S)z=Return True if the buffer is empty and 'feed_eof' was called.)rjri)rrrr�at_eof�szStreamReader.at_eofc Cs�|j std��|s dS|jj|�|j�|jdk r�|j r�t|j�d|jkr�y|jj �Wnt k r�d|_Yq�Xd|_ndS)Nzfeed_data after feed_eofrT) rjr8ri�extendrorRr5rrgZ pause_reading�NotImplementedError)rrOrrrrN�s   zStreamReader.feed_datac cs�|jdk r"td|��n|j s8td��|jrZd|_|jj�ntjd|j �|_z|jDdHWdd|_XdS)zpWait until feed_data() or feed_eof() is called. If stream was paused, automatically resume it. NzH%s() called while another coroutine is already waiting for incoming dataz_wait_for_data after EOFFr$) rk� RuntimeErrorrjr8r5rRrprrEr4)rZ func_namerrr�_wait_for_data�s    zStreamReader._wait_for_dataccs�d}t|�}y|j|�DdH}Wn�tk rX}z |jSWYdd}~Xn�tk r�}za|jj||j�r�|jd|j|�=n |jj�|j �t |j d��WYdd}~XnX|S)a�Read chunk of data from the stream until newline (b' ') is found. On success, return chunk that ends with newline. If only partial line can be read due to EOF, return incomplete line without terminating newline. When EOF was reached while no bytes read, empty bytes object is returned. If limit is reached, ValueError will be raised. In that case, if newline was found, complete line including newline will be removed from internal buffer. Else, internal buffer will be cleared. Limit is compared against part of the line without newline. If stream was paused, this function will automatically resume it if needed. s Nr) r� readuntilrrrri� startswithr#�clearrqrf�args)r�sep�seplen�line�errr�readline�s   &zStreamReader.readlines ccsnt|�}|dkr'td��n|jdk rB|j�nd}x�t|j�}|||kr�|jj||�}|dkr�Pn|d|}||jkr�td|��q�n|jr�t|j�}|jj �t |d��n|j d�DdHqKW||jkr/td|��n|jd||�}|jd||�=|j �t|�S) auRead chunk of data from the stream until `separator` is found. On success, chunk and its separator will be removed from internal buffer (i.e. consumed). Returned chunk will include separator at the end. Configured stream limit is used to check result. Limit means maximal length of chunk that can be returned, not counting the separator. If EOF occurs and complete separator still not found, IncompleteReadError(<partial data>, None) will be raised and internal buffer becomes empty. This partial data may contain a partial separator. If chunk cannot be read due to overlimit, LimitOverrunError will be raised and data will be left in internal buffer, so it can be read again, in some different way. If stream was paused, this function will automatically resume it if needed. rz,Separator should be at least one-byte stringNr z2Separator is not found, and chunk exceed the limitrwz2Separator is found, but chunk is longer than limit���) rrfrlri�findrgrrj�bytesryrrvrq)rZ separatorr|�offsetZbuflenZisep�chunkrrrrw�s4       zStreamReader.readuntilr ccs�|jdk r|j�n|dkr+dS|dkrg}x2|j|j�DdH}|saPn|j|�q@Wdj|�S|j r�|j r�|jd�DdHnt|jd|��}|jd|�=|j �|S)a�Read up to `n` bytes from the stream. If n is not provided, or set to -1, read until EOF and return all read bytes. If the EOF was received and the internal buffer is empty, return an empty bytes object. If n is zero, return empty bytes object immediatelly. If n is positive, this function try to read `n` bytes, and may return less or equal bytes than requested, but at least one byte. If EOF was received before any byte is read, this function returns empty byte object. Returned value is not limited with limit, configured at stream creation. If stream was paused, this function will automatically resume it if needed. Nr��read) rlr�rgrWrXrirjrvr�rq)r�n�blocks�blockrOrrrr�@s$     zStreamReader.readccs�|dkrtd��n|jdk r6|j�n|dkrFdSg}xr|dkr�|j|�DdH}|s�dj|�}t|t|�|��n|j|�|t|�8}qOW|dks�t�dj|�S)a�Read exactly `n` bytes. Raise an `IncompleteReadError` if EOF is reached before `n` bytes can be read. The `IncompleteReadError.partial` attribute of the exception will contain the partial read bytes. if n is zero, return empty bytes object. Returned value is not limited with limit, configured at stream creation. If stream was paused, this function will automatically resume it if needed. rz*readexactly size can not be less than zeroNr�)rfrlr�rXrrrWr8)rr�r�r�rrrr� readexactlyrs      zStreamReader.readexactlycCs|S)Nr)rrrr� __aiter__�szStreamReader.__aiter__ccs*|j�DdH}|dkr&t�n|S)Nr�)rZStopAsyncIteration)r�valrrr� __anext__�s  zStreamReader.__anext__r�)rrr rnrrZrcr@rorJrqrMrrrNrrvrrwr�r�r ZPY35r�r�rrrrrJs(          X1, i)r!�__all__Zsocket�hasattrrs�r r rrrr�logrrn�EOFErrorr� Exceptionrrrr r ZProtocolr3rrrrrrr�<module>sD      " B)G