
    vgL*                         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dS )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                   |    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dS )
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                            | ||           d S N    )ac_in_bufferac_out_bufferfifoproducer_fifoasyncore
dispatcher__init__)selfconnmaps      Z/var/www/pixelcanvas.ch/venv/lib/python3.11/site-packages/supervisor/medusa/asynchat_25.pyr   zasync_chat.__init__?   s?     !VV$$dD#66666r
   c                      t          d          Nzmust be implemented in subclassNotImplementedErrorr   datas     r   collect_incoming_dataz async_chat.collect_incoming_dataE       !"CDDDr
   c                      t          d          r   r   r   s    r   found_terminatorzasync_chat.found_terminatorH   r   r
   c                     || _         dS )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                  }n*# t          j        $ r |                                  Y d S w xY w| xj        |z  c_        | j        r	t          | j                  }|                                 }|s#|                     | j                   d| _        nt          |t                    st          |t                    r|}||k     r3|                     | j                   d| _        | xj        |z  c_        nM|                     | j        d |                    | j        |d          | _        d| _        |                                  nt          |          }| j                            |          }|dk    rT|dk    r"|                     | j        d |                    | j        ||z   d          | _        |                                  nxt          | j        |          }|r@||k    r8|                     | j        d |                     | j        | d          | _        d S |                     | j                   d| _        | j        d S d S )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55DD| 	 	 	FF	 	T!  1	0T&''B,,..J .0**D,=>>>$'!!J,, *0
:t0L0L *066..0ABBB(+D%OOr)OOO..0A"1"0EFFF(,(9!""(=D%&'DO))++++ "%Z)..z::B;;qyy22D4Efuf4MNNN(,(9%:N:O:O(PD%))++++ /0A:NNE 	0 B;; 668I'E6'8RSSS040A5&''0JD- 22D4EFFF,/)c  1	0 1	0 1	0 1	0 1	0s    #AAc                 .    |                                   d S r'   )initiate_sendr   s    r   handle_writezasync_chat.handle_write   s    r
   c                 .    |                                   d S r'   )closer   s    r   handle_closezasync_chat.handle_close   s    

r
   c                     t          |          }| j                            t          |                     |                                  d S r'   )r   r   pushsimple_producerr;   r   s     r   rA   zasync_chat.push   sD    ~~ 5 5666r
   c                 b    | j                             |           |                                  d S r'   )r   rA   r;   )r   producers     r   push_with_producerzasync_chat.push_with_producer   s1    ***r
   c                 <    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                 Z    | j         dk    o| j                                        o| 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                             d           dS )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                                        }|6| j        s-| j                                         |                                  d S t          |t                    r+| j                                         | xj        |z  c_        d S |                                }|r| j        |z   | _        d S | j                                         nd S r'   )	r0   r   firstr   popr>   r1   bytesmore)r   pr   s      r   refill_bufferzasync_chat.refill_buffer   s    	4%&& &,,.. 9- %*..000

F5)) &**,,,&&!+&&Fvvxx -)-);d)BD&F&**,,,,+	r
   c                 Z   | j         }t          | j                  |k     r|                                  | j        rn| j        ri	 |                     | j        d |                   }|r| j        |d          | _        d S d S # t          j        $ r |                                  Y d S w xY wd S d S 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#&>?? G)-);HII)FD&&&G G <   !!###		 		 		 		s   8A? ?#B&%B&c                 t    d| _         d| _        | j        r"| j                                         | j         d S d S r	   )r   r   r   rP   r   s    r   discard_bufferszasync_chat.discard_buffers   sS       	%""$$$   	% 	% 	% 	% 	%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   s-       F F
 #"7 7 7 7E E EE E E    ?0 ?0 ?0B      
  @ @ @  ' ' '  0  "% % % % %r
   r   c                       e Zd ZddZd ZdS )rB      c                 "    || _         || _        d S r'   )r   buffer_size)r   r   rd   s      r   r   zsimple_producer.__init__   s    	&r
   c                     t          | j                  | j        k    r/| j        d | j                 }| j        | j        d          | _        |S | j        }d| _        |S r	   )r0   r   rd   )r   results     r   rR   zsimple_producer.more   s\    	??T---Y0 001F	$"2"3"34DIMYFDIMr
   N)rb   )r\   r]   r^   r   rR   r`   r
   r   rB   rB      s7        ' ' ' '    r
   rB   c                   4    e Zd ZddZd Zd Zd Zd Zd ZdS )	r   Nc                 *    |s	g | _         d S || _         d S r'   list)r   rj   s     r   r   zfifo.__init__   s      	DIIIDIIIr
   c                 *    t          | j                  S r'   )r0   rj   r   s    r   __len__zfifo.__len__  s    49~~r
   c                     | j         g k    S r'   ri   r   s    r   rI   zfifo.is_empty  s    yBr
   c                     | j         d         S )Nr   ri   r   s    r   rO   z
fifo.first	  s    y|r
   c                 :    | j                             |           d S r'   )rj   appendr   s     r   rA   z	fifo.push  s    	r
   c                 L    | j         rd| j                             d          fS dS )N   r   )r   N)rj   rP   r   s    r   rP   zfifo.pop  s*    9 	dimmA&&&&7r
   r'   )	r\   r]   r^   r   rl   rI   rO   rA   rP   r`   r
   r   r   r      ss                       r
   r   c                     t          |          dz
  }|rA|                     |d |                   s$|dz  }|r|                     |d |                   $|S )Nrr   )r0   endswith)haystackneedlels      r   r4   r4   $  sk    FaA
 H%%fRaRj11 	Q  H%%fRaRj11 Hr
   )r_   r-   supervisor.medusar   r   supervisor.compatr   r   r   r   rB   r   r4   r`   r
   r   <module>rz      s   8 *  5 5 5 5 5 5 " " " " " " & & & & & &s% s% s% s% s%(% s% s% s%l               P    r
   