³ņ
\Kc           @   sz   d  Z  d d k Z d d k Z d d k l Z d e i f d     YZ d d d     YZ d d 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.
i’’’’N(   t   dequet
   async_chatc           B   s­   e  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 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   ac_in_buffert   ac_out_buffert   fifot   producer_fifot   asyncoret
   dispatchert   __init__(   t   selft   conn(    (    s   /usr/lib/python2.5/asynchat.pyR	   >   s    		c         C   s   t  d  d  S(   Ns   must be implemented in subclass(   t   NotImplementedError(   R
   t   data(    (    s   /usr/lib/python2.5/asynchat.pyt   collect_incoming_dataD   s    c         C   s   t  d  d  S(   Ns   must be implemented in subclass(   R   (   R
   (    (    s   /usr/lib/python2.5/asynchat.pyt   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
   terminator(   R
   t   term(    (    s   /usr/lib/python2.5/asynchat.pyt   set_terminatorJ   s    c         C   s   |  i  S(   N(   R   (   R
   (    (    s   /usr/lib/python2.5/asynchat.pyt   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   } | p |  i |  i  d |  _ qR t	 | t
  p 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’’’’(   t   recvt   ac_in_buffer_sizet   sockett   errort   handle_errorR   t   lenR   R   t
   isinstancet   intt   longR   R   t   findt   find_prefix_at_end(   R
   R   t   whyt   lbR   t   nt   terminator_lent   index(    (    s   /usr/lib/python2.5/asynchat.pyt   handle_readV   sL    
	 
 			c         C   s   |  i    d  S(   N(   t   initiate_send(   R
   (    (    s   /usr/lib/python2.5/asynchat.pyt   handle_write   s    c         C   s   |  i    d  S(   N(   t   close(   R
   (    (    s   /usr/lib/python2.5/asynchat.pyt   handle_close   s    c         C   s$   |  i  i t |   |  i   d  S(   N(   R   t   pusht   simple_producerR%   (   R
   R   (    (    s   /usr/lib/python2.5/asynchat.pyR)      s    c         C   s   |  i  i |  |  i   d  S(   N(   R   R)   R%   (   R
   t   producer(    (    s   /usr/lib/python2.5/asynchat.pyt   push_with_producer¢   s    c         C   s   t  |  i  |  i j S(   s4   predicate for inclusion in the readable for select()(   R   R   R   (   R
   (    (    s   /usr/lib/python2.5/asynchat.pyt   readable¦   s    c         C   s(   |  i  d j o |  i i   o |  i S(   s4   predicate for inclusion in the writable for select()R   (   R   R   t   is_emptyt	   connected(   R
   (    (    s   /usr/lib/python2.5/asynchat.pyt   writableŖ   s    c         C   s   |  i  i d  d S(   sA   automatically close this channel once the outgoing queue is emptyN(   R   R)   t   None(   R
   (    (    s   /usr/lib/python2.5/asynchat.pyt   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 d  S(   N(
   R   R   t   firstR1   R   t   popR'   R   t   strt   more(   R
   t   pR   (    (    s   /usr/lib/python2.5/asynchat.pyt   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(	   t   ac_out_buffer_sizeR   R   R8   R/   t   sendR   R   R   (   R
   t   obst   num_sentR   (    (    s   /usr/lib/python2.5/asynchat.pyR%   Ņ   s    	
c         C   s5   d |  _  d |  _ x |  i o |  i i   q Wd  S(   NR   (   R   R   R   R4   (   R
   (    (    s   /usr/lib/python2.5/asynchat.pyt   discard_buffersć   s
    		 
N(   t   __name__t
   __module__t   __doc__R   R9   R1   R	   R   R   R   R   R$   R&   R(   R)   R,   R-   R0   R2   R8   R%   R=   (    (    (    s   /usr/lib/python2.5/asynchat.pyR   5   s&   					B						
			R*   c           B   s   e  Z d  d  Z d   Z RS(   i   c         C   s   | |  _  | |  _ d  S(   N(   R   t   buffer_size(   R
   R   RA   (    (    s   /usr/lib/python2.5/asynchat.pyR	   ķ   s    	c         C   s^   t  |  i  |  i j o+ |  i |  i  } |  i |  i |  _ | Sn |  i } d |  _ | Sd  S(   NR   (   R   R   RA   (   R
   t   result(    (    s   /usr/lib/python2.5/asynchat.pyR6   ń   s    		(   R>   R?   R	   R6   (    (    (    s   /usr/lib/python2.5/asynchat.pyR*   ė   s   R   c           B   sA   e  Z d d   Z d   Z d   Z d   Z d   Z d   Z RS(   c         C   s*   | p t    |  _ n t  |  |  _ d  S(   N(   R    t   list(   R
   RC   (    (    s   /usr/lib/python2.5/asynchat.pyR	   ü   s    c         C   s   t  |  i  S(   N(   R   RC   (   R
   (    (    s   /usr/lib/python2.5/asynchat.pyt   __len__  s    c         C   s   |  i  S(   N(   RC   (   R
   (    (    s   /usr/lib/python2.5/asynchat.pyR.     s    c         C   s   |  i  d S(   Ni    (   RC   (   R
   (    (    s   /usr/lib/python2.5/asynchat.pyR3     s    c         C   s   |  i  i |  d  S(   N(   RC   t   append(   R
   R   (    (    s   /usr/lib/python2.5/asynchat.pyR)     s    c         C   s)   |  i  o d |  i  i   f Sn d Sd  S(   Ni   i    (   i    N(   RC   t   popleftR1   (   R
   (    (    s   /usr/lib/python2.5/asynchat.pyR4     s    
N(	   R>   R?   R1   R	   RD   R.   R3   R)   R4   (    (    (    s   /usr/lib/python2.5/asynchat.pyR   ū   s   				c         C   sB   t  |  d } x+ | o# |  i | |   o | d 8} q W| S(   Ni   (   R   t   endswith(   t   haystackt   needlet   l(    (    s   /usr/lib/python2.5/asynchat.pyR   #  s
     (    (    (
   R@   R   R   t   collectionsR    R   R   R*   R   R   (    (    (    s   /usr/lib/python2.5/asynchat.pys   <module>/   s   ¶(