
[XMc           @   s   d  Z  d d l m Z d d l m Z m Z d Z d Z d d d     YZ d d d	     YZ	 d d
 l
 Z
 d d
 l Z d d
 l Z d d
 l Z d e f d     YZ d Z d e f d     YZ d
 S(   s   
Implements a simple polling interface for file descriptors that don't work with
select() - this is pretty much only useful on Windows.
i(   t
   implements(   t	   IConsumert   IPushProducerg&.>g?t   _PollableResourcec           B   s    e  Z e Z d    Z d   Z RS(   c         C   s   t  |  _ d  S(   N(   t   Truet   active(   t   self(    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyt   activate   s    c         C   s   t  |  _ d  S(   N(   t   FalseR   (   R   (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyt
   deactivate   s    (   t   __name__t
   __module__R   R   R   R	   (    (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyR      s   	t   _PollingTimerc           B   sY   e  Z d    Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 RS(	   c         C   s1   | |  _  g  |  _ d  |  _ t |  _ t |  _ d  S(   N(   t   reactort
   _resourcest   Nonet
   _pollTimert   MAX_TIMEOUTt   _currentTimeoutR   t   _paused(   R   R   (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyt   __init__#   s
    				c         C   s   |  j  j |  |  j   d  S(   N(   R   t   appendt   _checkPollingState(   R   t   res(    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyt   _addPollableResource*   s    c         C   s9   x2 |  j  D] } | j r
 |  j   Pq
 q
 W|  j   d  S(   N(   R   R   t   _startPollingt   _stopPolling(   R   t   resource(    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyR   .   s
    	
c         C   s%   |  j  d  k r! |  j   |  _  n  d  S(   N(   R   R   t   _reschedule(   R   (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyR   6   s    c         C   s,   |  j  d  k	 r( |  j  j   d  |  _  n  d  S(   N(   R   R   t   cancel(   R   (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyR   :   s    c         C   s   t  |  _ d  S(   N(   R   R   (   R   (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyt   _pause?   s    c         C   s   t  |  _ |  j   d  S(   N(   R   R   R   (   R   (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyt   _unpauseB   s    	c         C   s&   |  j  s" |  j j |  j |  j  Sd  S(   N(   R   R   t	   callLaterR   t
   _pollEvent(   R   (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyR   F   s    	c         C   s   d } g  } xF |  j  D]; } | j r | | j   7} | j rQ | j |  qQ q q W|  j } | r |  j | d } | t k  r t } q n" |  j d } | t k r t } n  | |  _ | r |  j   |  _ n  d  S(   Ng        g      ?g       @(	   R   R   t	   checkWorkR   R   t   MIN_TIMEOUTR   R   R   (   R   t	   workUnitst	   anyActiveR   t
   newTimeout(    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyR!   J   s$    					(   R
   R   R   R   R   R   R   R   R   R   R!   (    (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyR       s   								Nt   _PollableReadPipec           B   sQ   e  Z e e  d    Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 RS(   c         C   s   | |  _  | |  _ | |  _ d  S(   N(   t   pipet   receivedCallbackt   lostCallback(   R   R(   R)   R*   (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyR   n   s    		c   	      C   s   d } g  } xx yW t  j |  j d  \ } } } | s: Pn  t j |  j | d   \ } } | j |  Wq t j k
 r d } Pq Xq d j	 |  } | r |  j
 |  n  | r |  j   n  t |  S(   Ni    i   t    (   t	   win32pipet   PeekNamedPipeR(   t	   win32filet   ReadFileR   R   t   win32apit   errort   joinR)   t   cleanupt   len(	   R   t   finishedt   fullDataReadt   buffert   bytesToReadt   resultt   hrt   datat   dataBuf(    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyR"   t   s$    c         C   s   |  j    |  j   d  S(   N(   R	   R*   (   R   (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyR3      s    
c         C   s/   y t  j |  j  Wn t j k
 r* n Xd  S(   N(   R0   t   CloseHandleR(   t
   pywintypesR1   (   R   (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyt   close   s    c         C   s   |  j    d  S(   N(   R?   (   R   (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyt   stopProducing   s    c         C   s   |  j    d  S(   N(   R	   (   R   (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyt   pauseProducing   s    c         C   s   |  j    d  S(   N(   R   (   R   (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyt   resumeProducing   s    (   R
   R   R    R   R   R"   R3   R?   R@   RA   RB   (    (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyR'   j   s   
						i@   i   t   _PollableWritePipec           B   sl   e  Z e e  d    Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d	   Z RS(
   c         C   sw   t  |  _ d  |  _ d |  _ d |  _ g  |  _ | |  _ | |  _ y t	 j
 | t	 j d  d   Wn t j k
 rr n Xd  S(   Ni    (   R   t   disconnectingR   t   producert   producerPausedt   streamingProducert   outQueuet	   writePipeR*   R,   t   SetNamedPipeHandleStatet   PIPE_NOWAITR>   R1   (   R   RI   R*   (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyR      s    								c         C   s   t  |  _ d  S(   N(   R   RD   (   R   (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyR?      s    c         C   s,   |  j  d  k	 r( d |  _ |  j  j   n  d  S(   Ni   (   RE   R   RF   RA   (   R   (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyt
   bufferFull   s    	c         C   sC   |  j  d  k	 r? |  j s" |  j r? d |  j  _ |  j  j   t St S(   Ni    (   RE   R   RG   RF   RB   R   R   (   R   (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyt   bufferEmpty   s    	c         C   sj   |  j  d k	 r+ t d | |  j  f   n  |  j sA | j   n% | |  _  | |  _ | sf | j   n  d S(   sw  Register to receive data from a producer.

        This sets this selectable to be a consumer for a producer.  When this
        selectable runs out of data on a write() call, it will ask the producer
        to resumeProducing(). A producer should implement the IProducer
        interface.

        FileDescriptor provides some infrastructure for producer methods.
        sH   Cannot register producer %s, because producer %s was never unregistered.N(   RE   R   t   RuntimeErrorR   R@   RG   RB   (   R   RE   t	   streaming(    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyt   registerProducer   s    
			c         C   s   d |  _ d S(   sD   Stop consuming data from a producer, without disconnecting.
        N(   R   RE   (   R   (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyt   unregisterProducer   s    c         C   sC   |  j    y t j |  j  Wn t j k
 r4 n X|  j   d  S(   N(   R	   R0   R=   RI   R>   R1   R*   (   R   (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyt   writeConnectionLost   s    
c         C   s   |  j  j |  d  S(   N(   RH   t   extend(   R   t   seq(    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyt   writeSequence   s    c         C   sL   |  j  r d  S|  j j |  t t t |  j   t k rH |  j   n  d  S(   N(   RD   RH   R   t   sumt   mapR4   t   FULL_BUFFER_SIZERL   (   R   R;   (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyt   write   s
    	c         C   sS  d } |  j  se |  j r& |  j   d Sy t j |  j d d   Wqe t j k
 ra |  j   | SXn  x |  j  r%|  j  j	 d  } d } t
 | t  r t d   n  y" t j |  j | d   \ } } Wn t j k
 r |  j   Pqh X| | 7} t |  | k rh |  j  j d | |  Pqh qh W|  j   } | rO|  j rO|  j   n  | S(   Ni    R+   s   unicode not allowed(   RH   RD   RR   R.   t	   WriteFileRI   R   R>   R1   t   popt
   isinstancet   unicodet	   TypeErrorR0   R4   t   insertRM   (   R   t   numBytesWrittenR;   t   errCodet   nBytesWrittent   resumed(    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyR"      s:    		



(   R
   R   R    R   R   R?   RL   RM   RP   RQ   RR   RU   RY   R"   (    (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyRC      s   
				
						(    (    i   (   t   __doc__t   zope.interfaceR    t   twisted.internet.interfacesR   R   R#   R   R   R   R,   R.   R0   R>   R'   RX   RC   (    (    (    sA   /usr/lib/python2.7/dist-packages/twisted/internet/_pollingfile.pyt   <module>   s   E6