;
Éâ"Ic               @   s€   d  Z  d d l Z d d l Z d d l m Z d d d „ Z Gd „  d e j ƒ Z Gd „  d ƒ Z	 Gd	 „  d
 ƒ Z
 d „  Z d S(   u¥  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(   u   dequec             C   sR   t  |  ƒ | d  k o
 d } n | d  k o t |  ƒ } n |  | | … } | S(   Ni    (   u
   memoryviewu   Noneu   len(   u   obju   startu   stopu   x(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   buffer4   s    

c             B   sË   |  Ee  Z d  Z d 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 d S(   u‡   This is an abstract class.  You must derive from this class, and add
    the two methods collect_incoming_data() and found_terminator()i   i    u   latin1c             C   s8   d |  _  g  |  _ t ƒ  |  _ t j j |  | | ƒ d  S(   Ns    (   u   ac_in_bufferu   incomingu   dequeu   producer_fifou   asyncoreu
   dispatcheru   __init__(   u   selfu   socku   map(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   __init__P   s    		c             C   s   t  d ƒ ‚ d  S(   Nu   must be implemented in subclass(   u   NotImplementedError(   u   selfu   data(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   collect_incoming_dataa   s    c             C   s   |  j  j | ƒ d  S(   N(   u   incomingu   append(   u   selfu   data(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   _collect_incoming_datad   s    c             C   s&   d j  |  j ƒ } |  j d  d  … =| S(   Ns    (   u   joinu   incoming(   u   selfu   d(    (    u%   /mit/python/lib/python3.0/asynchat.pyu	   _get_datag   s    c             C   s   t  d ƒ ‚ d  S(   Nu   must be implemented in subclass(   u   NotImplementedError(   u   self(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   found_terminatorl   s    c             C   s=   t  | t ƒ o  |  j o t | |  j ƒ } n | |  _ d S(   uR   Set the input delimiter.  Can be a fixed string of any length, an integer, or NoneN(   u
   isinstanceu   stru   use_encodingu   bytesu   encodingu
   terminator(   u   selfu   term(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   set_terminatoro   s    c             C   s   |  j  S(   N(   u
   terminator(   u   self(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   get_terminatoru   s    c             C   su  y |  j  |  j ƒ } Wn8 t j k
 o) } z |  j ƒ  d  SWYd  d  } ~ Xn Xt | t ƒ o  |  j o t t |  j	 ƒ } n |  j
 | |  _
 xÝ|  j
 oÒt |  j
 ƒ } |  j ƒ  } | p |  j |  j
 ƒ d |  _
 q” t | t ƒ o‡ | } | | k  o- |  j |  j
 ƒ d |  _
 |  j | |  _ ql|  j |  j
 d  | … ƒ |  j
 | d  … |  _
 d |  _ |  j ƒ  q” t | ƒ } |  j
 j | ƒ } | d k oS | d k o |  j |  j
 d  | … ƒ n |  j
 | | d  … |  _
 |  j ƒ  q” t |  j
 | ƒ } | oH | | k o6 |  j |  j
 d  | … ƒ |  j
 | d  … |  _
 n Pq” |  j |  j
 ƒ d |  _
 q” Wd  S(   Ns    i    iÿÿÿÿ(   u   recvu   ac_in_buffer_sizeu   socketu   erroru   handle_erroru
   isinstanceu   stru   use_encodingu   bytesu   encodingu   ac_in_bufferu   lenu   get_terminatoru   collect_incoming_datau   intu
   terminatoru   found_terminatoru   findu   find_prefix_at_end(   u   selfu   datau   whyu   lbu
   terminatoru   nu   terminator_lenu   index(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   handle_read}   sP    
 
			c             C   s   |  j  ƒ  d  S(   N(   u   initiate_send(   u   self(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   handle_writeÁ   s    c             C   s   |  j  ƒ  d  S(   N(   u   close(   u   self(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   handle_closeÄ   s    c             C   s   |  j  } t | ƒ | k oE xR t d t | ƒ | ƒ D]$ } |  j j | | | | … ƒ q5 Wn |  j j | ƒ |  j ƒ  d  S(   Ni    (   u   ac_out_buffer_sizeu   lenu   rangeu   producer_fifou   appendu   initiate_send(   u   selfu   datau   sabsu   i(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   pushÇ   s    	 &c             C   s   |  j  j | ƒ |  j ƒ  d  S(   N(   u   producer_fifou   appendu   initiate_send(   u   selfu   producer(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   push_with_producerÐ   s    c             C   s   d S(   u4   predicate for inclusion in the readable for select()i   (    (   u   self(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   readableÔ   s    c             C   s   |  j  p |  j S(   u4   predicate for inclusion in the writable for select()(   u   producer_fifou	   connected(   u   self(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   writableÜ   s    c             C   s   |  j  j d ƒ d S(   uA   automatically close this channel once the outgoing queue is emptyN(   u   producer_fifou   appendu   None(   u   self(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   close_when_doneà   s    c             C   sŠ  xƒ|  j  ox|  j on|  j  d } | p* |  j  d =| d  k o |  j ƒ  d  Sn |  j } y t | d | ƒ } WnH t k
 o< | j ƒ  } | o |  j  j | ƒ n |  j  d =w Yn Xt	 | t
 ƒ o  |  j o t | |  j ƒ } n y |  j | ƒ } Wn% t j k
 o |  j ƒ  d  SYn X| oO | t | ƒ k  p | t | ƒ k  o | | d  … |  j  d <q€|  j  d =n d  SWd  S(   Ni    (   u   producer_fifou	   connectedu   Noneu   handle_closeu   ac_out_buffer_sizeu   bufferu	   TypeErroru   moreu
   appendleftu
   isinstanceu   stru   use_encodingu   bytesu   encodingu   sendu   socketu   erroru   handle_erroru   len(   u   selfu   firstu   obsu   datau   num_sent(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   initiate_sendä   s:     

		
	

&c             C   s*   d |  _  |  j d  d  … =|  j j ƒ  d  S(   Ns    (   u   ac_in_bufferu   incomingu   producer_fifou   clear(   u   self(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   discard_buffers  s    	N(   u   __name__u
   __module__u   __doc__u   ac_in_buffer_sizeu   ac_out_buffer_sizeu   use_encodingu   encodingu   Noneu   __init__u   collect_incoming_datau   _collect_incoming_datau	   _get_datau   found_terminatoru   set_terminatoru   get_terminatoru   handle_readu   handle_writeu   handle_closeu   pushu   push_with_produceru   readableu   writableu   close_when_doneu   initiate_sendu   discard_buffers(   u
   __locals__(    (    u%   /mit/python/lib/python3.0/asynchat.pyu
   async_chatA   s,   
							D									*u
   async_chatc             B   s#   |  Ee  Z d  d „ Z d „  Z d S(   i   c             C   s   | |  _  | |  _ d  S(   N(   u   datau   buffer_size(   u   selfu   datau   buffer_size(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   __init__  s    	c             C   sg   t  |  j ƒ |  j k o4 |  j d  |  j … } |  j |  j d  … |  _ | S|  j } d |  _ | Sd  S(   Ns    (   u   lenu   datau   buffer_size(   u   selfu   result(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   more  s    		N(   u   __name__u
   __module__u   __init__u   more(   u
   __locals__(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   simple_producer  s   
u   simple_producerc             B   sG   |  Ee  Z d d  „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   c             C   s*   | p t  ƒ  |  _ n t  | ƒ |  _ d  S(   N(   u   dequeu   list(   u   selfu   list(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   __init__%  s    c             C   s   t  |  j ƒ S(   N(   u   lenu   list(   u   self(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   __len__+  s    c             C   s   |  j  S(   N(   u   list(   u   self(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   is_empty.  s    c             C   s   |  j  d S(   Ni    (   u   list(   u   self(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   first1  s    c             C   s   |  j  j | ƒ d  S(   N(   u   listu   append(   u   selfu   data(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   push4  s    c             C   s&   |  j  o d |  j  j ƒ  f Sd Sd  S(   Ni   i    (   i    N(   u   listu   popleftu   None(   u   self(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   pop7  s    
N(	   u   __name__u
   __module__u   Noneu   __init__u   __len__u   is_emptyu   firstu   pushu   pop(   u
   __locals__(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   fifo$  s   
				u   fifoc             C   sH   t  | ƒ d } x1 | o) |  j | d  | … ƒ o | d 8} q W| S(   Ni   (   u   lenu   endswith(   u   haystacku   needleu   l(    (    u%   /mit/python/lib/python3.0/asynchat.pyu   find_prefix_at_endL  s
     "(   u   __doc__u   socketu   asyncoreu   collectionsu   dequeu   Noneu   bufferu
   dispatcheru
   async_chatu   simple_produceru   fifou   find_prefix_at_end(    (    (    u%   /mit/python/lib/python3.0/asynchat.pyu   <module>/   s   Ó(