+e] @sdZdddddddgZdd lZeed rRejd d gnd dlmZd dlmZd dlmZd dlm Z d dlm Z d dlm Z d dl m Z d$ZGdddeZGdddeZe d d dd deddZe d d dd deddZeed re d dd dedd Ze d dd dedd ZnGdd d e jZGd!ddee jZGd"ddZGd#ddZd S)%zStream-related things. StreamReader StreamWriterStreamReaderProtocolopen_connection start_serverIncompleteReadErrorLimitOverrunErrorNZAF_UNIXopen_unix_connectionstart_unix_server) coroutines)compat)events)futures) protocols) coroutine)loggercs(eZdZdZfddZS)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) cs6tjdt||f||_||_dS)Nz-%d bytes read on a total of %r expected bytes)super__init__lenpartialexpected)selfrr) __class__4/opt/alt/python34/lib64/python3.4/asyncio/streams.pyr!s  zIncompleteReadError.__init__)__name__ __module__ __qualname____doc__rrr)rrrs cs(eZdZdZfddZS)rzReached buffer limit while looking for the separator. Attributes: - message: error message - consumed: total number of bytes that should be consumed cs&tj|||_||_dS)N)rrmessageconsumed)rr"r#)rrrr/s zLimitOverrunError.__init__)rrr r!rrr)rrr(s looplimitc +s|dkrtj}ntd|d|}t|d||jfdd|||DdH\}}t|||}||fS)aA 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$csS)Nrr)protocolrrNsz!open_connection..)rget_event_looprrZcreate_connectionr) hostportr$r%kwdsreader transport_writerr)r&rr5s )c+sNdkrtjnfdd}j||||DdHS)aStart 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.tdd}t|d}|S)Nr%r$)rr)r,r&)client_connected_cbr%r$rrfactoryns  zstart_server..factory)rr(Z create_server)r0r)r*r$r%r+r1r)r0r%r$rrSs c+s|dkrtj}ntd|d|}t|d||jfdd||DdH\}}t|||}||fS)z@Similar to `open_connection` but works with UNIX Domain Sockets.Nr%r$csS)Nrr)r&rrr'sz&open_unix_connection..)rr(rrZcreate_unix_connectionr)pathr$r%r+r,r-r.r/r)r&rr zs &c+sKdkrtjnfdd}j|||DdHS)z=Similar to `start_server` but works with UNIX Domain Sockets.Ncs.tdd}t|d}|S)Nr%r$)rr)r,r&)r0r%r$rrr1s  z"start_unix_server..factory)rr(Zcreate_unix_server)r0r2r$r%r+r1r)r0r%r$rr s c@s[eZdZdZdddZddZddZd 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$rrrrs     zFlowControlMixin.__init__cCs/d|_|jjr+tjd|ndS)NTz%r pauses writing)r5r4 get_debugrdebug)rrrr pause_writings zFlowControlMixin.pause_writingcCsld|_|jjr+tjd|n|j}|dk rhd|_|jsh|jdqhndS)NFz%r resumes writing)r5r4r8rr9r6done set_result)rwaiterrrrresume_writings     zFlowControlMixin.resume_writingcCsud|_|jsdS|j}|dkr/dSd|_|jrHdS|dkrd|jdn |j|dS)NT)r7r5r6r;r< set_exception)rexcr=rrrconnection_losts       z FlowControlMixin.connection_lostccsY|jrtdn|js%dS|j}tjd|j}||_|DdHdS)NzConnection lostr$)r7ConnectionResetErrorr5r6rFuturer4)rr=rrr _drain_helpers    zFlowControlMixin._drain_helper) rrr r!rr:r>rArrDrrrrr3s   r3csdeZdZdZddfddZddZfddZd d Zd d ZS) 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.) Ncs2tjd|||_d|_||_dS)Nr$)rr_stream_reader_stream_writer_client_connected_cb)rZ stream_readerr0r$)rrrrs  zStreamReaderProtocol.__init__cCs~|jj||jdk rzt|||j|j|_|j|j|j}tj|rz|jj|qzndS)N) rE set_transportrGrr4rFr Z iscoroutineZ create_task)rr-resrrrconnection_mades   z$StreamReaderProtocol.connection_madecs@|dkr|jjn|jj|tj|dS)N)rEfeed_eofr?rrA)rr@)rrrrAs z$StreamReaderProtocol.connection_lostcCs|jj|dS)N)rE feed_data)rdatarrr data_receivedsz"StreamReaderProtocol.data_receivedcCs|jjdS)NT)rErK)rrrr eof_receiveds z!StreamReaderProtocol.eof_received) rrr r!rrJrArNrOrr)rrrs   c@seZdZdZddZddZeddZdd Zd d Z d d Z ddZ ddZ dddZ eddZdS)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. cCs(||_||_||_||_dS)N) _transport _protocol_readerr4)rr-r&r,r$rrrr s   zStreamWriter.__init__cCsP|jjd|jg}|jdk r?|jd|jnddj|S)Nz transport=%rz reader=%rz<%s> )rrrPrRappendjoin)rinforrr__repr__szStreamWriter.__repr__cCs|jS)N)rP)rrrrr-szStreamWriter.transportcCs|jj|dS)N)rPwrite)rrMrrrrXszStreamWriter.writecCs|jj|dS)N)rP writelines)rrMrrrrY"szStreamWriter.writelinescCs |jjS)N)rP write_eof)rrrrrZ%szStreamWriter.write_eofcCs |jjS)N)rP can_write_eof)rrrrr[(szStreamWriter.can_write_eofcCs |jjS)N)rPclose)rrrrr\+szStreamWriter.closeNcCs|jj||S)N)rPget_extra_info)rnamedefaultrrrr].szStreamWriter.get_extra_infoccsu|jdk r6|jj}|dk r6|q6n|jdk r_|jjr_dVq_n|jjDdHdS)z~Flush the write buffer. The intended use is to write w.write(data) yield from w.drain() N)rR exceptionrPZ is_closingrQrD)rr@rrrdrain1s    zStreamWriter.drain)rrr r!rrWpropertyr-rXrYrZr[r\r]rrarrrrrs        c@seZdZedddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ eddZeddZedddZed&ddZed d!Zejred"d#Zed$d%ZndS)'rNcCs|dkrtdn||_|dkrBtj|_n ||_t|_d|_d|_d|_ d|_ d|_ dS)NrzLimit cannot be <= 0F) ValueError_limitrr(r4 bytearray_buffer_eof_waiter _exceptionrPr5)rr%r$rrrrLs         zStreamReader.__init__cCsdg}|jr/|jdt|jn|jrH|jdn|jtkrn|jd|jn|jr|jd|jn|jr|jd|jn|jr|jd|jn|j r|jdnd d j |S) Nrz%d byteseofzl=%dzw=%rze=%rzt=%rZpausedz<%s>rS) rfrTrrgrd_DEFAULT_LIMITrhrirPr5rU)rrVrrrrW_s        zStreamReader.__repr__cCs|jS)N)ri)rrrrr`qszStreamReader.exceptioncCsJ||_|j}|dk rFd|_|jsF|j|qFndS)N)rirh cancelledr?)rr@r=rrrr?ts      zStreamReader.set_exceptioncCsA|j}|dk r=d|_|js=|jdq=ndS)z1Wakeup read*() functions waiting for data or EOF.N)rhrlr<)rr=rrr_wakeup_waiter}s     zStreamReader._wakeup_waitercCs ||_dS)N)rP)rr-rrrrHszStreamReader.set_transportcCs>|jr:t|j|jkr:d|_|jjndS)NF)r5rrfrdrPresume_reading)rrrr_maybe_resume_transports! z$StreamReader._maybe_resume_transportcCsd|_|jdS)NT)rgrm)rrrrrKs zStreamReader.feed_eofcCs|jo|j S)z=Return True if the buffer is empty and 'feed_eof' was called.)rgrf)rrrrat_eofszStreamReader.at_eofc Cs|s dS|jj||j|jdk r|j rt|jd|jkry|jjWntk rd|_YqXd|_ndS)NrT) rfextendrmrPr5rrdZ pause_readingNotImplementedError)rrMrrrrLs   zStreamReader.feed_datac cs}|jdk r"td|n|jrDd|_|jjntjd|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 dataFr$)rh RuntimeErrorr5rPrnrrCr4)rZ func_namerrr_wait_for_datas    zStreamReader._wait_for_dataccsd}t|}y|j|DdH}Wntk rX}z |jSWYdd}~Xntk r}za|jj||jr|jd|j|=n |jj|j t |j dWYdd}~XnX|S)aRead 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 readuntilrrrrf startswithr#clearrorcargs)rsepseplenlineerrrreadlines   &zStreamReader.readlines ccsnt|}|dkr'tdn|jdk rB|jnd}xt|j}|||kr|jj||}|dkrPn|d|}||jkrtd|qn|jrt|j}|jj t |dn|j dDdHqKW||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(, 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 limitruz2Separator is found, but chunk is longer than limit) rrcrirffindrdrrgbytesrwrrtro)rZ separatorrzoffsetZbuflenZisepchunkrrrrus4       zStreamReader.readuntilr ccs|jdk r|jn|dkr+dS|dkrg}x2|j|jDdH}|saPn|j|q@Wdj|S|j r|j r|jdDdHnt|jd|}|jd|=|j |S)aRead 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. Nrread) rirrdrTrUrfrgrtrro)rnblocksblockrMrrrr@s$     zStreamReader.readccs|dkrtdn|jdk r6|jn|dkrFdSg}xr|dkr|j|DdH}|sdj|}t|t||n|j||t|8}qOWdj|S)aRead 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)rcrirrUrrrT)rrrrrrrr readexactlyrs     zStreamReader.readexactlycCs|S)Nr)rrrr __aiter__szStreamReader.__aiter__ccs*|jDdH}|dkr&tn|S)Nr)r}ZStopAsyncIteration)rvalrrr __anext__s  zStreamReader.__anext__r~)rrr rkrrWr`r?rmrHrorKrprLrrtr}rurrr ZPY35rrrrrrrJs(          X1, i)r!__all__Zsockethasattrrqr r rrrrlogrrkEOFErrorr Exceptionrrrr r ZProtocolr3rrrrrrrsD      " B)G