
    kcL*                         d Z ddlZddlmZ ddlmZ ddlmZ  G d dej                        Z	 G d d	      Z
 G d
 d      Zd Zy)a  A class supporting chat-style (command/response) protocols.

This class adds support for 'chat' style protocols - where one side
sends a 'command', and the other sends a response (examples would be
the common internet protocols - smtp, nntp, ftp, etc..).

The handle_read() method looks at the input stream for the current
'terminator' (usually '\r\n' for single-line responses, '\r\n.\r\n'
for multi-line output), calling self.found_terminator() on its
receipt.

for example:
Say you build an async nntp client using this class.  At the start
of the connection, you'll have self.terminator set to '\r\n', in
order to process the single-line greeting.  Just before issuing a
'LIST' command you'll set it to '\r\n.\r\n'.  The output of the LIST
command will be accumulated (using your own 'collect_incoming_data'
method) up to the terminator, and then control will be returned to
you - by calling your self.found_terminator() method.
    N)asyncore_25)long)as_bytesc                   z    e Zd ZdZdZdZddZd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)
async_chatzThis is an abstract class.  You must derive from this class, and add
    the two methods collect_incoming_data() and found_terminator()i   Nc                     d| _         d| _        t               | _        t        j
                  j                  | ||       y N    )ac_in_bufferac_out_bufferfifoproducer_fifoasyncore
dispatcher__init__)selfconnmaps      ?/usr/lib/python3/dist-packages/supervisor/medusa/asynchat_25.pyr   zasync_chat.__init__?   s4     !V$$dD#6r
   c                     t        d      Nzmust be implemented in subclassNotImplementedErrorr   datas     r   collect_incoming_dataz async_chat.collect_incoming_dataE       !"CDDr
   c                     t        d      r   r   r   s    r   found_terminatorzasync_chat.found_terminatorH   r   r
   c                     || _         y)zRSet the input delimiter.  Can be a fixed string of any length, an integer, or NoneN
terminator)r   terms     r   set_terminatorzasync_chat.set_terminatorK   s	    r
   c                     | j                   S Nr"   r   s    r   get_terminatorzasync_chat.get_terminatorO   s    r
   c                    	 | j                  | j                        }| xj
                  |z  c_        | j
                  rt        | j
                        }| j                         }|s$| j                  | j
                         d| _        nt        |t              st        |t              r|}||k  r9| j                  | j
                         d| _        | xj                  |z  c_        n4| j                  | j
                  d |        | j
                  |d  | _        d| _        | j                          nt        |      }| j
                  j                  |      }|dk7  rK|dkD  r| j                  | j
                  d |        | j
                  ||z   d  | _        | j                          ntt        | j
                  |      }|r:||k7  r4| j                  | j
                  d |         | j
                  | d  | _        y | j                  | j
                         d| _        | j
                  ry y # t        j                  $ r | j	                          Y y w xY w)Nr
   r   )recvac_in_buffer_sizesocketerrorhandle_errorr   lenr(   r   
isinstanceintr   r#   r    findfind_prefix_at_end)r   r   lbr#   nterminator_lenindexs          r   handle_readzasync_chat.handle_readW   s   	99d445D
 	T! T&&'B,,.J**D,=,=>$'!J,
:t0L6..0A0AB(+D%OOr)O..0A0A"10EF(,(9(9!"(=D%&'DO))+ "%Z))..z:B;qy22D4E4Efu4MN(,(9(9%:N:O(PD%))+ /0A0A:NE B; 668I8I'E68RS040A0A5&'0JD- 22D4E4EF,/)c  || 		s   H. .#IIc                 $    | j                          y r'   )initiate_sendr   s    r   handle_writezasync_chat.handle_write   s    r
   c                 $    | j                          y r'   )closer   s    r   handle_closezasync_chat.handle_close   s    

r
   c                     t        |      }| j                  j                  t        |             | j	                          y r'   )r   r   pushsimple_producerr;   r   s     r   rA   zasync_chat.push   s0    ~ 56r
   c                 Z    | j                   j                  |       | j                          y r'   )r   rA   r;   )r   producers     r   push_with_producerzasync_chat.push_with_producer   s"    *r
   c                 F    t        | j                        | j                  k  S )z4predicate for inclusion in the readable for select())r0   r   r,   r   s    r   readablezasync_chat.readable   s    4$$%)?)???r
   c                 v    | j                   dk(  xr( | j                  j                         xr | j                   S )z4predicate for inclusion in the writable for select()r
   )r   r   is_empty	connectedr   s    r   writablezasync_chat.writable   s?    
 ##s* ""++- 	r
   c                 :    | j                   j                  d       y)zAautomatically close this channel once the outgoing queue is emptyN)r   rA   r   s    r   close_when_donezasync_chat.close_when_done   s    &r
   c                    	 t        | j                        r| j                  j                         }|7| j                  s*| j                  j	                          | j                          y t        |t              r0| j                  j	                          | xj                  |z  c_        y |j                         }|r| j                  |z   | _        y | j                  j	                          ny r'   )	r0   r   firstr   popr>   r1   bytesmore)r   pr   s      r   refill_bufferzasync_chat.refill_buffer   s    4%%&&&,,. 9--**..0

5)&&**,&&!+&vvx)-););d)BD&&&**,+ r
   c                 ^   | j                   }t        | j                        |k  r| j                          | j                  rD| j                  r7	 | j                  | j                  d |       }|r| j                  |d  | _        y y y y # t        j                  $ r | j                          Y y w xY wr'   )	ac_out_buffer_sizer0   r   rT   rJ   sendr-   r.   r/   )r   obsnum_sents      r   r;   zasync_chat.initiate_send   s    %%""#c) $..99d&8&8#&>?)-););HI)FD& 	 #1 << !!#s   4B #B,+B,c                     d| _         d| _        | j                  r(| j                  j                          | j                  r'y y r	   )r   r   r   rP   r   s    r   discard_bufferszasync_chat.discard_buffers   s8       ""$   r
   )NN)__name__
__module____qualname____doc__r,   rV   r   r   r    r%   r(   r9   r<   r?   rA   rE   rG   rK   rM   rT   r;   r[    r
   r   r   r   6   sj    F
 #"7EE?0B
@'0"%r
   r   c                       e Zd ZddZd Zy)rB   c                      || _         || _        y r'   )r   buffer_size)r   r   rc   s      r   r   zsimple_producer.__init__   s    	&r
   c                     t        | j                        | j                  kD  r9| j                  d | j                   }| j                  | j                  d  | _        |S | j                  }d| _        |S r	   )r0   r   rc   )r   results     r   rR   zsimple_producer.more   sb    		?T---YY0 0 01F		$"2"2"34DIMYYFDIMr
   N)i   )r\   r]   r^   r   rR   r`   r
   r   rB   rB      s    'r
   rB   c                   2    e Zd ZddZd Zd Zd Zd Zd Zy)	r   Nc                 &    |sg | _         y || _         y r'   list)r   ri   s     r   r   zfifo.__init__   s    DIDIr
   c                 ,    t        | j                        S r'   )r0   ri   r   s    r   __len__zfifo.__len__  s    499~r
   c                      | j                   g k(  S r'   rh   r   s    r   rI   zfifo.is_empty  s    yyBr
   c                      | j                   d   S )Nr   rh   r   s    r   rO   z
fifo.first	  s    yy|r
   c                 :    | j                   j                  |       y r'   )ri   appendr   s     r   rA   z	fifo.push  s    		r
   c                 V    | j                   rd| j                   j                  d      fS y)N   r   )r   N)ri   rP   r   s    r   rP   zfifo.pop  s$    99diimmA&&&r
   r'   )	r\   r]   r^   r   rk   rI   rO   rA   rP   r`   r
   r   r   r      s     r
   r   c                     t        |      dz
  }|r0| j                  |d |       s|dz  }|r| j                  |d |       s|S )Nrq   )r0   endswith)haystackneedlels      r   r4   r4   $  sK    FaA
H%%fRaj1	Q H%%fRaj1Hr
   )r_   r-   supervisor.medusar   r   supervisor.compatr   r   r   r   rB   r   r4   r`   r
   r   <module>ry      sG   8*  5 " &s%(%% s%l   Pr
   