a
    i3e'?                     @   s0  d Z g dZddlZeedr,eddg 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ZG dd deZe
d dedddZe
d!dedddZeedre
d"dedddZe
d#dedddZG dd de	jZG dd dee	jZG dd dZG dd dZdS )$zStream-related things.)StreamReaderStreamWriterStreamReaderProtocolopen_connectionstart_serverIncompleteReadError    NAF_UNIXopen_unix_connectionstart_unix_server   )
coroutines)events)futures)	protocols)	coroutine)loggeri   c                   @   s   e Zd ZdZdd ZdS )r   z
    Incomplete read error. Attributes:

    - partial: read bytes string before the end of stream was reached
    - expected: total number of expected bytes
    c                 C   s(   t | dt||f  || _|| _d S )Nz-%s bytes read on a total of %s expected bytes)EOFError__init__lenpartialexpected)selfr   r    r   B/home/pi/bot/my_env/lib/python3.9/site-packages/asyncio/streams.pyr      s
    
zIncompleteReadError.__init__N)__name__
__module____qualname____doc__r   r   r   r   r   r      s   r   )looplimitc          	      +   sf   |du rt  }t||d}t||d |j fdd| |fi |E dH \}}t| ||}||fS )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.)
    Nr   r   r   c                      s    S Nr   r   protocolr   r   <lambda>?       z!open_connection.<locals>.<lambda>)r   get_event_loopr   r   create_connectionr   )	hostportr   r   kwdsreader	transport_writerr   r#   r   r   &   s    r   c                +   s<   du rt   fdd}j|||fi |E dH S )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.
    Nc                     s   t d} t|  d}|S Nr    r!   r   r   r,   r$   client_connected_cbr   r   r   r   factory_   s
    zstart_server.<locals>.factory)r   r'   create_server)r4   r)   r*   r   r   r+   r5   r   r3   r   r   D   s    r   c                +   sd   |du rt  }t||d}t||d |j fdd| fi |E dH \}}t| ||}||fS )z@Similar to `open_connection` but works with UNIX Domain Sockets.Nr    r!   c                      s    S r"   r   r   r#   r   r   r%   t   r&   z&open_unix_connection.<locals>.<lambda>)r   r'   r   r   Zcreate_unix_connectionr   )pathr   r   r+   r,   r-   r.   r/   r   r#   r   r	   k   s    c                +   s:   du rt   fdd}j||fi |E dH S )z=Similar to `start_server` but works with UNIX Domain Sockets.Nc                     s   t d} t|  d}|S r0   r1   r2   r3   r   r   r5      s
    z"start_unix_server.<locals>.factory)r   r'   Zcreate_unix_server)r4   r7   r   r   r+   r5   r   r3   r   r
   y   s    c                   @   s>   e Zd ZdZdddZdd Zdd Zd	d
 Zedd Z	dS )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.
    Nc                 C   s0   |d u rt  | _n|| _d| _d | _d| _d S NF)r   r'   _loop_paused_drain_waiter_connection_lost)r   r   r   r   r   r      s    zFlowControlMixin.__init__c                 C   s*   | j r
J d| _ | j r&td|  d S )NTz%r pauses writing)r;   r:   	get_debugr   debugr   r   r   r   pause_writing   s    

zFlowControlMixin.pause_writingc                 C   sP   | j s
J d| _ | j r&td|  | j}|d urLd | _| sL|d  d S )NFz%r resumes writing)r;   r:   r>   r   r?   r<   done
set_resultr   waiterr   r   r   resume_writing   s    

zFlowControlMixin.resume_writingc                 C   sV   d| _ | jsd S | j}|d u r"d S d | _| r4d S |d u rH|d  n
|| d S NT)r=   r;   r<   rB   rC   set_exceptionr   excrE   r   r   r   connection_lost   s    z FlowControlMixin.connection_lostc                 c   sT   | j rtd| jsd S | j}|d u s2| s2J tj| jd}|| _|E d H  d S )NzConnection lostr!   )r=   ConnectionResetErrorr;   r<   	cancelledr   Futurer:   rD   r   r   r   _drain_helper   s    zFlowControlMixin._drain_helper)N)
r   r   r   r   r   rA   rF   rK   r   rO   r   r   r   r   r8      s   	
	r8   c                       sF   e Zd ZdZd fdd	Zdd Z fddZd	d
 Zdd Z  Z	S )r   a=  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.)
    Nc                    s$   t  j|d || _d | _|| _d S )Nr!   )superr   _stream_reader_stream_writer_client_connected_cb)r   Zstream_readerr4   r   	__class__r   r   r      s    zStreamReaderProtocol.__init__c                 C   sT   | j | | jd urPt|| | j | j| _| | j | j}t|rP| j| d S r"   )	rQ   set_transportrS   r   r:   rR   r   iscoroutineZcreate_task)r   r-   resr   r   r   connection_made   s    

z$StreamReaderProtocol.connection_madec                    s0   |d u r| j   n| j | t | d S r"   )rQ   feed_eofrH   rP   rK   r   rJ   rT   r   r   rK      s    z$StreamReaderProtocol.connection_lostc                 C   s   | j | d S r"   )rQ   	feed_datar   datar   r   r   data_received   s    z"StreamReaderProtocol.data_receivedc                 C   s   | j   d S r"   )rQ   rZ   r@   r   r   r   eof_received   s    z!StreamReaderProtocol.eof_received)NN)
r   r   r   r   r   rY   rK   r_   r`   __classcell__r   r   rT   r   r      s   r   c                   @   sj   e Zd ZdZdd Zdd Zedd Zdd	 Zd
d Z	dd Z
dd Zdd ZdddZedd ZdS )r   a'  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.
    c                 C   s2   || _ || _|d u s"t|ts"J || _|| _d S r"   )
_transport	_protocol
isinstancer   _readerr:   )r   r-   r$   r,   r   r   r   r   r      s
    zStreamWriter.__init__c                 C   s:   | j jd| j g}| jd ur,|d| j  dd| S )Nztransport=%rz	reader=%rz<%s> )rU   r   rb   re   appendjoin)r   infor   r   r   __repr__  s    
zStreamWriter.__repr__c                 C   s   | j S r"   rb   r@   r   r   r   r-     s    zStreamWriter.transportc                 C   s   | j | d S r"   )rb   writer]   r   r   r   rl     s    zStreamWriter.writec                 C   s   | j | d S r"   )rb   
writelinesr]   r   r   r   rm     s    zStreamWriter.writelinesc                 C   s
   | j  S r"   )rb   	write_eofr@   r   r   r   rn     s    zStreamWriter.write_eofc                 C   s
   | j  S r"   )rb   can_write_eofr@   r   r   r   ro     s    zStreamWriter.can_write_eofc                 C   s
   | j  S r"   )rb   closer@   r   r   r   rp     s    zStreamWriter.closeNc                 C   s   | j ||S r"   )rb   get_extra_info)r   namedefaultr   r   r   rq     s    zStreamWriter.get_extra_infoc                 c   s4   | j dur | j  }|dur || j E dH  dS )z~Flush the write buffer.

        The intended use is to write

          w.write(data)
          yield from w.drain()
        N)re   	exceptionrc   rO   r[   r   r   r   drain!  s
    	

zStreamWriter.drain)N)r   r   r   r   r   rj   propertyr-   rl   rm   rn   ro   rp   rq   r   ru   r   r   r   r   r      s   	

r   c                   @   s   e Zd ZedfddZdd Zdd Zd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dZedd ZdS )r   Nc                 C   sJ   || _ |d u rt | _n|| _t | _d| _d | _d | _d | _	d| _
d S r9   )_limitr   r'   r:   	bytearray_buffer_eof_waiter
_exceptionrb   r;   )r   r   r   r   r   r   r   3  s    zStreamReader.__init__c                 C   s   | j S r"   )r|   r@   r   r   r   rt   B  s    zStreamReader.exceptionc                 C   s0   || _ | j}|d ur,d | _| s,|| d S r"   )r|   r{   rM   rH   rI   r   r   r   rH   E  s    zStreamReader.set_exceptionc                 C   s*   | j }|dur&d| _ | s&|d dS )z=Wakeup read() or readline() function waiting for data or EOF.N)r{   rM   rC   rD   r   r   r   _wakeup_waiterN  s
    zStreamReader._wakeup_waiterc                 C   s   | j d u sJ d|| _ d S )NzTransport already setrk   )r   r-   r   r   r   rV   V  s    zStreamReader.set_transportc                 C   s*   | j r&t| j| jkr&d| _ | j  d S r9   )r;   r   ry   rw   rb   Zresume_readingr@   r   r   r   _maybe_resume_transportZ  s    z$StreamReader._maybe_resume_transportc                 C   s   d| _ |   d S rG   )rz   r}   r@   r   r   r   rZ   _  s    zStreamReader.feed_eofc                 C   s   | j o| j S )z=Return True if the buffer is empty and 'feed_eof' was called.)rz   ry   r@   r   r   r   at_eofc  s    zStreamReader.at_eofc                 C   s   | j rJ d|sd S | j| |   | jd ur|| js|t| jd| j kr|z| j  W n t	yt   d | _Y n0 d| _d S )Nzfeed_data after feed_eof   T)
rz   ry   extendr}   rb   r;   r   rw   Zpause_readingNotImplementedErrorr]   r   r   r   r\   g  s    
zStreamReader.feed_datac                 c   sJ   | j durtd| tj| jd| _ z| j E dH  W d| _ nd| _ 0 dS )z/Wait until feed_data() or feed_eof() is called.NzH%s() called while another coroutine is already waiting for incoming datar!   )r{   RuntimeErrorr   rN   r:   )r   	func_namer   r   r   _wait_for_data}  s    
zStreamReader._wait_for_datac                 c   s   | j d ur| j t }d}|r| jr|r| jd}|dk rT|| j | j  n,|d7 }|| jd |  | jd |= d}t|| jkr|   t	dq| j
rq|r| dE d H  q|   t|S )NT   
r   r   FzLine is too longreadline)r|   rx   ry   findr   clearr   rw   r~   
ValueErrorrz   r   bytes)r   lineZ
not_enoughZicharr   r   r   r     s.    


zStreamReader.readlinec                 c   s   | j d ur| j |sdS |dk rRg }| | jE d H }|s<qH|| q$d|S | jsn| jsn| dE d H  |dk st| j|krt	| j}| j
  nt	| jd | }| jd |= |   |S )Nr&   r   read)r|   r   rw   rg   rh   ry   rz   r   r   r   r   r~   )r   nblocksblockr^   r   r   r   r     s(    


zStreamReader.readc                 c   sn   | j d ur| j g }|dkrd| |E d H }|sLd|}t|t|| || |t|8 }qd|S )Nr   r&   )r|   r   rh   r   r   rg   )r   r   r   r   r   r   r   r   readexactly  s    
	

zStreamReader.readexactly)r   )r   r   r   _DEFAULT_LIMITr   rt   rH   r}   rV   r~   rZ   r   r\   r   r   r   r   r   r   r   r   r   r   1  s    	
 "r   )NN)NN)N)N)r   __all__sockethasattrr    r   r   r   r   r   logr   r   r   r   r   r   r	   r
   Protocolr8   r   r   r   r   r   r   r   <module>   sB   
#
B(>