mò
­fIc           @   sq   d  Z  d k Z d k Z d k l Z d e i f d „  ƒ  YZ d f  d „  ƒ  YZ d f  d „  ƒ  YZ d	 „  Z	 d S(
   s¥  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(   s   dequet
   async_chatc           B   s­   t  Z d  Z d Z d Z e 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 RS(   s‡   This is an abstract class.  You must derive from this class, and add
    the two methods collect_incoming_data() and found_terminator()i   c         C   s5   d |  _ d |  _ t ƒ  |  _ t i i |  | ƒ d  S(   Nt    (	   t   selft   ac_in_buffert   ac_out_buffert   fifot   producer_fifot   asyncoret
   dispatchert   __init__t   conn(   R   R
   (    (    t%   /mit/python/lib/python2.4/asynchat.pyR	   >   s    		c         C   s   t  d ‚ d  S(   Ns   must be implemented in subclass(   t   NotImplementedError(   R   t   data(    (    R   t   collect_incoming_dataD   s    c         C   s   t  d ‚ d  S(   Ns   must be implemented in subclass(   R   (   R   (    (    R   t   found_terminatorG   s    c         C   s   | |  _ d S(   sR   Set the input delimiter.  Can be a fixed string of any length, an integer, or NoneN(   t   termR   t
   terminator(   R   R   (    (    R   t   set_terminatorJ   s     c         C   s   |  i S(   N(   R   R   (   R   (    (    R   t   get_terminatorN   s    c         C   s"  y |  i |  i ƒ } Wn& t i j
 o } |  i ƒ  d  Sn X|  i | |  _ xÌ|  i oÁt	 |  i ƒ } |  i ƒ  } | d  j p | d j o |  i |  i ƒ d |  _ qR t | t ƒ o{ | } | | j  o- |  i |  i ƒ d |  _ |  i | |  _ q|  i |  i |  ƒ |  i | |  _ d |  _ |  i ƒ  qR t	 | ƒ } |  i i | ƒ } | d j oG | d j o |  i |  i |  ƒ n |  i | | |  _ |  i ƒ  qR t |  i | ƒ } | o< | | j o* |  i |  i |  ƒ |  i | |  _ n PqR |  i |  i ƒ d |  _ qR Wd  S(   NR   i    iÿÿÿÿ(   R   t   recvt   ac_in_buffer_sizeR   t   sockett   errort   whyt   handle_errorR   t   lent   lbR   R   t   NoneR   t
   isinstancet   intt   nR   t   terminator_lent   findt   indext   find_prefix_at_end(   R   R"   R   R   R    R   R   R   (    (    R   t   handle_readV   sL    
	 
			c         C   s   |  i ƒ  d  S(   N(   R   t   initiate_send(   R   (    (    R   t   handle_write˜   s    c         C   s   |  i ƒ  d  S(   N(   R   t   close(   R   (    (    R   t   handle_close›   s    c         C   s$   |  i i t | ƒ ƒ |  i ƒ  d  S(   N(   R   R   t   pusht   simple_producerR   R%   (   R   R   (    (    R   R)   ž   s    c         C   s   |  i i | ƒ |  i ƒ  d  S(   N(   R   R   R)   t   producerR%   (   R   R+   (    (    R   t   push_with_producer¢   s    c         C   s   t  |  i ƒ |  i j S(   s4   predicate for inclusion in the readable for select()N(   R   R   R   R   (   R   (    (    R   t   readable¦   s     c         C   s(   |  i d j o |  i i ƒ  o |  i S(   s4   predicate for inclusion in the writable for select()R   N(   R   R   R   t   is_emptyt	   connected(   R   (    (    R   t   writableª   s     c         C   s   |  i i d ƒ d S(   sA   automatically close this channel once the outgoing queue is emptyN(   R   R   R)   R   (   R   (    (    R   t   close_when_done´   s     c         C   sÚ   xÓ t  |  i ƒ oº |  i i ƒ  } | d  j o- |  i p |  i i ƒ  |  i ƒ  n d  Sn6 t	 | t
 ƒ o% |  i i ƒ  |  i | |  _ d  Sn | i ƒ  } | o |  i | |  _ d  SqÑ |  i i ƒ  q d  Sq Wd  S(   Ni   (   R   R   R   t   firstt   pR   R   t   popR'   R   t   strt   moreR   (   R   R3   R   (    (    R   t   refill_bufferº   s&      
c         C   s§   |  i } t |  i ƒ | j  o |  i ƒ  n |  i ol |  i ob y5 |  i |  i |  ƒ } | o |  i | |  _ n Wq£ t	 i
 j
 o } |  i ƒ  d  Sq£ Xn d  S(   N(   R   t   ac_out_buffer_sizet   obsR   R   R7   R/   t   sendt   num_sentR   R   R   R   (   R   R9   R   R;   (    (    R   R%   Ò   s    	
c         C   s5   d |  _ d |  _ x |  i o |  i i ƒ  q Wd  S(   NR   (   R   R   R   R   R4   (   R   (    (    R   t   discard_buffersã   s
    		 
(   t   __name__t
   __module__t   __doc__R   R8   R   R	   R   R   R   R   R$   R&   R(   R)   R,   R-   R0   R1   R7   R%   R<   (    (    (    R   R    5   s&    					B						
			R*   c           B   s   t  Z d d „ Z d „  Z RS(   Ni   c         C   s   | |  _  | |  _ d  S(   N(   R   R   t   buffer_size(   R   R   R@   (    (    R   R	   í   s    	c         C   s^   t  |  i ƒ |  i j o+ |  i |  i  } |  i |  i |  _ | Sn |  i } d |  _ | Sd  S(   NR   (   R   R   R   R@   t   result(   R   RA   (    (    R   R6   ñ   s    		(   R=   R>   R	   R6   (    (    (    R   R*   ë   s   R   c           B   sA   t  Z d  d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   Nc         C   s*   | p t ƒ  |  _  n t | ƒ |  _  d  S(   N(   t   listt   dequeR   (   R   RB   (    (    R   R	   ü   s    c         C   s   t  |  i ƒ S(   N(   R   R   RB   (   R   (    (    R   t   __len__  s    c         C   s   |  i S(   N(   R   RB   (   R   (    (    R   R.     s    c         C   s   |  i d S(   Ni    (   R   RB   (   R   (    (    R   R2     s    c         C   s   |  i i | ƒ d  S(   N(   R   RB   t   appendR   (   R   R   (    (    R   R)     s    c         C   s)   |  i o d |  i i ƒ  f Sn d Sd  S(   Ni   i    (   i    N(   R   RB   t   popleftR   (   R   (    (    R   R4     s    
(	   R=   R>   R   R	   RD   R.   R2   R)   R4   (    (    (    R   R   û   s   				c         C   sB   t  | ƒ d } x+ | o# |  i | |  ƒ o | d 8} q W| S(   Ni   (   R   t   needlet   lt   haystackt   endswith(   RI   RG   RH   (    (    R   R#   #  s
     (
   R?   R   R   t   collectionsRC   R   R    R*   R   R#   (   RC   R   R   R#   R*   R   R    (    (    R   t   ?/   s   		¶(