ó
čžbc           @   sĘ   d  Z  d d l Z d d l Z d d l Z d d l m Z d d l m Z d d l m	 Z	 m
 Z
 e j e j e j e j f Z d e j 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   deque(   t   py3kwarning(   t   filterwarningst   catch_warningst
   async_chatc           B   sš   e  Z d  Z d Z d Z d 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   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   s8   d |  _  g  |  _ t   |  _ t j j |  | |  d  S(   Nt    (   t   ac_in_buffert   incomingR    t   producer_fifot   asyncoret
   dispatchert   __init__(   t   selft   sockt   map(    (    s   /usr/lib/python2.7/asynchat.pyR   E   s    		c         C   s   t  d   d  S(   Ns   must be implemented in subclass(   t   NotImplementedError(   R   t   data(    (    s   /usr/lib/python2.7/asynchat.pyt   collect_incoming_dataV   s    c         C   s   |  j  j |  d  S(   N(   R   t   append(   R   R   (    (    s   /usr/lib/python2.7/asynchat.pyt   _collect_incoming_dataY   s    c         C   s   d j  |  j  } |  j 2| S(   NR   (   t   joinR   (   R   t   d(    (    s   /usr/lib/python2.7/asynchat.pyt	   _get_data\   s    c         C   s   t  d   d  S(   Ns   must be implemented in subclass(   R   (   R   (    (    s   /usr/lib/python2.7/asynchat.pyt   found_terminatora   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.7/asynchat.pyt   set_terminatord   s    c         C   s   |  j  S(   N(   R   (   R   (    (    s   /usr/lib/python2.7/asynchat.pyt   get_terminatorh   s    c         C   s  y |  j  |  j  } Wn8 t j k
 rP } | j d t k rB d  S|  j   d  SX|  j | |  _ xŻ|  j rt |  j  } |  j	   } | sŞ |  j
 |  j  d |  _ qd t | t t f  r7| } | | k  rý |  j
 |  j  d |  _ |  j | |  _ q|  j
 |  j |   |  j | |  _ d |  _ |  j   qd t |  } |  j j |  } | d k rĽ| d k r|  j
 |  j |   n  |  j | | |  _ |  j   qd t |  j |  } | rö| | k rň|  j
 |  j |   |  j | |  _ n  Pqd |  j
 |  j  d |  _ qd Wd  S(   Ni    R   i˙˙˙˙(   t   recvt   ac_in_buffer_sizet   sockett   errort   argst   _BLOCKING_IO_ERRORSt   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.7/asynchat.pyt   handle_readp   sN    
			c         C   s   |  j    d  S(   N(   t   initiate_send(   R   (    (    s   /usr/lib/python2.7/asynchat.pyt   handle_write´   s    c         C   s   |  j    d  S(   N(   t   close(   R   (    (    s   /usr/lib/python2.7/asynchat.pyt   handle_closeˇ   s    c         C   sz   |  j  } t |  | k r\ xN t d t |  |  D]! } |  j j | | | | ! q4 Wn |  j j |  |  j   d  S(   Ni    (   t   ac_out_buffer_sizeR#   t   xrangeR   R   R/   (   R   R   t   sabst   i(    (    s   /usr/lib/python2.7/asynchat.pyt   pushş   s    	"c         C   s   |  j  j |  |  j   d  S(   N(   R   R   R/   (   R   t   producer(    (    s   /usr/lib/python2.7/asynchat.pyt   push_with_producerĂ   s    c         C   s   d S(   s4   predicate for inclusion in the readable for select()i   (    (   R   (    (    s   /usr/lib/python2.7/asynchat.pyt   readableÇ   s    c         C   s   |  j  p |  j S(   s4   predicate for inclusion in the writable for select()(   R   t	   connected(   R   (    (    s   /usr/lib/python2.7/asynchat.pyt   writableĎ   s    c         C   s   |  j  j d  d S(   sA   automatically close this channel once the outgoing queue is emptyN(   R   R   t   None(   R   (    (    s   /usr/lib/python2.7/asynchat.pyt   close_when_doneÓ   s    c         C   sf  x_|  j  ra|  j ra|  j  d } | sO |  j  d =| d  k rO |  j   d  Sn  |  j } y? t   0 t r~ t d d t  n  t	 | d |  } Wd  QXWnC t
 k
 rÜ | j   } | rĚ |  j  j |  q |  j  d =q n Xy |  j |  } Wn t j k
 r|  j   d  SX| r]| t |  k  s<| t |  k  rP| | |  j  d <q]|  j  d =n  d  SWd  S(   Ni    t   ignores   .*buffer(   R   R;   R=   R2   R3   R   R   R   t   DeprecationWarningt   buffert	   TypeErrort   moret
   appendleftt   sendR   R   R"   R#   (   R   t   firstt   obsR   t   num_sent(    (    s   /usr/lib/python2.7/asynchat.pyR/   ×   s:    

	


$c         C   s!   d |  _  |  j 2|  j j   d  S(   NR   (   R   R   R   t   clear(   R   (    (    s   /usr/lib/python2.7/asynchat.pyt   discard_buffers˙   s    	N(   t   __name__t
   __module__t   __doc__R   R3   R=   R   R   R   R   R   R   R   R.   R0   R2   R7   R9   R:   R<   R>   R/   RJ   (    (    (    s   /usr/lib/python2.7/asynchat.pyR   <   s(   							D									(t   simple_producerc           B   s   e  Z d  d  Z d   Z RS(   i   c         C   s   | |  _  | |  _ d  S(   N(   R   t   buffer_size(   R   R   RO   (    (    s   /usr/lib/python2.7/asynchat.pyR     s    	c         C   sY   t  |  j  |  j k r? |  j |  j  } |  j |  j |  _ | S|  j } d |  _ | Sd  S(   NR   (   R#   R   RO   (   R   t   result(    (    s   /usr/lib/python2.7/asynchat.pyRC     s    		(   RK   RL   R   RC   (    (    (    s   /usr/lib/python2.7/asynchat.pyRN     s   t   fifoc           B   sA   e  Z d d   Z d   Z d   Z d   Z d   Z d   Z RS(   c         C   s(   | s t    |  _ n t  |  |  _ d  S(   N(   R    t   list(   R   RR   (    (    s   /usr/lib/python2.7/asynchat.pyR     s    c         C   s   t  |  j  S(   N(   R#   RR   (   R   (    (    s   /usr/lib/python2.7/asynchat.pyt   __len__  s    c         C   s   |  j  S(   N(   RR   (   R   (    (    s   /usr/lib/python2.7/asynchat.pyt   is_empty  s    c         C   s   |  j  d S(   Ni    (   RR   (   R   (    (    s   /usr/lib/python2.7/asynchat.pyRF   "  s    c         C   s   |  j  j |  d  S(   N(   RR   R   (   R   R   (    (    s   /usr/lib/python2.7/asynchat.pyR7   %  s    c         C   s$   |  j  r d |  j  j   f Sd Sd  S(   Ni   i    (   i    N(   RR   t   popleftR=   (   R   (    (    s   /usr/lib/python2.7/asynchat.pyt   pop(  s    	N(	   RK   RL   R=   R   RS   RT   RF   R7   RV   (    (    (    s   /usr/lib/python2.7/asynchat.pyRQ     s   				c         C   s?   t  |  d } x( | r: |  j | |   r: | d 8} q W| S(   Ni   (   R#   t   endswith(   t   haystackt   needlet   l(    (    s   /usr/lib/python2.7/asynchat.pyR(   =  s    (    (    (   RM   R	   t   errnoR   t   collectionsR    t   sysR   t   warningsR   R   t   EAGAINt   EALREADYt   EINPROGRESSt   EWOULDBLOCKR!   R
   R   RN   RQ   R(   (    (    (    s   /usr/lib/python2.7/asynchat.pyt   <module>/   s   É(