ó
1ËoQc           @   s{   d  Z  d Z d d l Z d d l Z d d l Z d d l Z e e d d  Z d d
 d     YZ d e	 f d	     YZ
 d S(   sA  Based on code from timeout_socket.py, with some tweaks for compatibility.
   These tweaks should really be rolled back into timeout_socket, but it's
   not totally clear who is maintaining it at this point. In the meantime,
   we'll use a different module name for our tweaked version to avoid any
   confusion.

   The original timeout_socket is by:

        Scott Cotton <scott@chronis.pobox.com>
        Lloyd Zusman <ljz@asfast.com>
        Phil Mayes <pmayes@olivebr.com>
        Piers Lauder <piers@cs.su.oz.au>
        Radovan Garabik <garabik@melkor.dnp.fmph.uniba.sk>
s   $Id$i˙˙˙˙Nt	   WSAEINVALi&'  t   TimeoutSocketc           B   sŞ   e  Z d  Z d d d  Z d   Z d   Z d d  Z d d  Z d Z	 d	 Z
 d
 d d  Z d   Z d d  Z d d  Z d d  Z d   Z d   Z d   Z RS(   s/   A socket imposter that supports timeout limits.i   c         C   sn   t  |  |  _ d |  _ | d  k r? t j t j t j  } n  | |  _ |  j j d  d |  _	 d |  _
 d  S(   Nt    i    (   t   floatt   timeoutt   inbuft   Nonet   sockett   AF_INETt   SOCK_STREAMt   sockt   setblockingt   _rbuft   _wbuf(   t   selfR   R
   (    (    s9   /usr/lib/python2.7/dist-packages/wstools/TimeoutSocket.pyt   __init__   s    			c         C   s   t  |  j |  S(   N(   t   getattrR
   (   R   t   name(    (    s9   /usr/lib/python2.7/dist-packages/wstools/TimeoutSocket.pyt   __getattr__'   s    c   	      G   s~  |  j  } |  j } y8 | j d  t | j |  | j | d k  d SWn!t j k
 rm} | sk   n  | j d  t | j  d k r d } n | \ } } | t	 j
 t	 j t	 j f k rĆ   n  t j g  | g g  |  \ } } } | rny t | j |  d SWqjt j k
 rf} t | j  d k r;d } n | \ } } | t	 j t f k r`d S  qjXqnn Xt d   d  S(   Ni    i   s   socket connect() timeout.(   R   R
   R   t   applyt   connectR   t   errort   lent   argst   errnot   EINPROGRESSt   EALREADYt   EWOULDBLOCKt   selectt   EISCONNR    t   TimeoutError(	   R   t   addrR   R
   t   whyt   codet   rt   wt   e(    (    s9   /usr/lib/python2.7/dist-packages/wstools/TimeoutSocket.pyR   +   s<    			$	i    c   
      C   s   t  |  } d } x t j g  |  j g g  |  j  \ } } } | r | | | d !} |  j j | |  }	 | |	 } | | k r | Sq n  t d   q d  S(   Ni    i    s   socket send() timeout.(   R   R   R
   R   t   sendR   (
   R   t   datat   flagst   totalt   nextR"   R#   R$   t   bufft   sent(    (    s9   /usr/lib/python2.7/dist-packages/wstools/TimeoutSocket.pyR%   O   s    *
c         C   sH   t  j  |  j g g  g  |  j  d r8 |  j j | |  St d   d  S(   Ni    s   socket recv() timeout.(   R   R
   R   t   recvR   (   R   t   amtR'   (    (    s9   /usr/lib/python2.7/dist-packages/wstools/TimeoutSocket.pyR,   ]   s    %i   i   R"   i˙˙˙˙c         C   s   |  j  d |  _  | |  _ |  S(   Ni   (   t   handlest   mode(   R   R/   t   buffsize(    (    s9   /usr/lib/python2.7/dist-packages/wstools/TimeoutSocket.pyt   makefilee   s    	c         C   sH   |  j  d |  _  |  j  d k rD |  j j   d k rD |  j j   n  d  S(   Ni   i    (   R.   R
   t   filenot   close(   R   (    (    s9   /usr/lib/python2.7/dist-packages/wstools/TimeoutSocket.pyR3   j   s    $c         C   s  t  | t d   s d } n  | d k rt |  j  } | | k rf |  j |  } |  j | |  _ | S| | } |  j g } d |  _ x | d k r|  j t | |  j   } | sš Pn  t |  } | | k ró | j | |   | | |  _ Pn  | j |  | | } q Wd j |  St d |  j  } |  j g } d |  _ x< |  j |  } | s^Pn  | j |  t	 | d d  } qEd j |  S(	   Ni   i˙˙˙˙i    R   i   i   i   i   (
   t
   isinstancet   typeR   R   R,   t   maxR0   t   appendt   joint   min(   R   t   nt   kR&   t   Lt   new(    (    s9   /usr/lib/python2.7/dist-packages/wstools/TimeoutSocket.pyt   reado   sB    	
		c         C   s&  d } |  j  j d  } x | d k  r­ d | k  oG t |  j   k n r­ |  j |  j  } | si Pn  | j d  } | d k r | t |  j   } n  |  j  | |  _  q W| d k  rĚ t |  j   } n
 | d } d | k oö t |  j   k  n r| } n  |  j  |  |  j  | } |  _  | S(   NR   s   
i    i   (   R   t   findR   R,   R0   (   R   t   limitR&   t   iR=   (    (    s9   /usr/lib/python2.7/dist-packages/wstools/TimeoutSocket.pyt   readline   s"    5
%	c         C   s_   d } g  } xL |  j    } | s% Pn  | j |  | t |  7} | r | | k r Pq q | S(   Ni    (   RB   R7   R   (   R   t   sizehintR(   t   listt   line(    (    s9   /usr/lib/python2.7/dist-packages/wstools/TimeoutSocket.pyt	   readlinesŚ   s    c         C   s   |  j  d j |   d  S(   NR   (   R%   R8   (   R   RD   (    (    s9   /usr/lib/python2.7/dist-packages/wstools/TimeoutSocket.pyt
   writelinesł   s    c         C   s   |  j  |  d  S(   N(   R%   (   R   R&   (    (    s9   /usr/lib/python2.7/dist-packages/wstools/TimeoutSocket.pyt   writeś   s    c         C   s   d  S(   N(    (   R   (    (    s9   /usr/lib/python2.7/dist-packages/wstools/TimeoutSocket.pyt   flushš   s    N(   t   __name__t
   __module__t   __doc__R   R   R   R   R%   R,   R0   R.   R1   R3   R>   RB   RF   RG   RH   RI   (    (    (    s9   /usr/lib/python2.7/dist-packages/wstools/TimeoutSocket.pyR      s    
		$	#		R   c           B   s   e  Z RS(    (   RJ   RK   (    (    (    s9   /usr/lib/python2.7/dist-packages/wstools/TimeoutSocket.pyR   ˝   s   (    (   RL   t   identt   stringR   R   R   R   R    R   t	   ExceptionR   (    (    (    s9   /usr/lib/python2.7/dist-packages/wstools/TimeoutSocket.pyt   <module>   s   Ł