
[XMc           @   s  d  Z  d d l m Z d d l m Z d d l m Z m Z m Z m Z d d l m	 Z	 m
 Z
 m Z d d l m Z d d l m Z d d l m Z m Z m Z d d	 l m Z m Z d
 e j f d     YZ d e j f d     YZ d d d     YZ d d  d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ  d e f d     YZ! d e f d     YZ" d S(!   sJ   
Assorted functionality which is commonly useful when writing unit tests.
i(   t   StringIO(   t
   implements(   t
   ITransportt	   IConsumert   IPushProducert
   IConnector(   t   IReactorTCPt   IReactorSSLt   IReactorUNIX(   t   IListeningPort(   t   basic(   t   protocolt   errort   address(   t   IPv4Addresst   UNIXAddresst   AccumulatingProtocolc           B   sK   e  Z d  Z d Z Z d Z d Z d Z d Z	 d   Z
 d   Z d   Z RS(   sg  
    L{AccumulatingProtocol} is an L{IProtocol} implementation which collects
    the data delivered to it and can fire a Deferred when it is connected or
    disconnected.

    @ivar made: A flag indicating whether C{connectionMade} has been called.
    @ivar data: A string giving all the data passed to C{dataReceived}.
    @ivar closed: A flag indicated whether C{connectionLost} has been called.
    @ivar closedReason: The value of the I{reason} parameter passed to
        C{connectionLost}.
    @ivar closedDeferred: If set to a L{Deferred}, this will be fired when
        C{connectionLost} is called.
    i    t    c         C   sV   d |  _  |  j d  k	 rR |  j j d  k	 rR |  j j } d  |  j _ | j |   n  d  S(   Ni   (   t   madet   factoryt   Nonet   protocolConnectionMadet   callback(   t   selft   d(    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   connectionMade.   s    	c         C   s   |  j  | 7_  d  S(   N(   t   data(   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   dataReceived6   s    c         C   sH   d |  _  | |  _ |  j d  k	 rD |  j d  } |  _ | j d   n  d  S(   Ni   (   t   closedt   closedReasont   closedDeferredR   R   (   R   t   reasonR   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   connectionLost9   s
    		N(   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   R   R   R   R    (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR      s   
		t   LineSendingProtocolc           B   s5   e  Z e Z e d   Z d   Z d   Z d   Z RS(   c         C   s    | |  _  g  |  _ | |  _ d  S(   N(   t   linest   responset   start(   R   R%   R'   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   __init__D   s    
	c         C   s#   |  j  r t |  j |  j  n  d  S(   N(   R'   t   mapt   sendLineR%   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR   I   s    	c         C   s<   |  j  s( t |  j |  j  g  |  _ n  |  j j |  d  S(   N(   R'   R)   R*   R%   R&   t   append(   R   t   line(    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   lineReceivedM   s    	c         C   s   t  |  _ d  S(   N(   t   Truet   lostConn(   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR    S   s    (	   R!   R"   t   FalseR/   R.   R(   R   R-   R    (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR$   A   s
   		t   FakeDatagramTransportc           B   s&   e  Z e   Z d    Z e d  Z RS(   c         C   s   g  |  _  d  S(   N(   t   written(   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR(   Z   s    c         C   s   |  j  j | | f  d  S(   N(   R2   R+   (   R   t   packett   addr(    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   write]   s    (   R!   R"   t   objectt   noAddrR(   R5   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR1   W   s   		t   StringTransportc           B   s   e  Z d  Z e e e e  e 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 RS(   s  
    A transport implementation which buffers data in memory and keeps track of
    its other state without providing any behavior.

    L{StringTransport} has a number of attributes which are not part of any of
    the interfaces it claims to implement.  These attributes are provided for
    testing purposes.  Implementation code should not use any of these
    attributes; they are not provided by other transports.

    @ivar disconnecting: A C{bool} which is C{False} until L{loseConnection} is
        called, then C{True}.

    @ivar producer: If a producer is currently registered, C{producer} is a
        reference to it.  Otherwise, C{None}.

    @ivar streaming: If a producer is currently registered, C{streaming} refers
        to the value of the second parameter passed to C{registerProducer}.

    @ivar hostAddr: C{None} or an object which will be returned as the host
        address of this transport.  If C{None}, a nasty tuple will be returned
        instead.

    @ivar peerAddr: C{None} or an object which will be returned as the peer
        address of this transport.  If C{None}, a nasty tuple will be returned
        instead.

    @ivar producerState: The state of this L{StringTransport} in its capacity
        as an L{IPushProducer}.  One of C{'producing'}, C{'paused'}, or
        C{'stopped'}.

    @ivar io: A L{StringIO} which holds the data which has been written to this
        transport since the last call to L{clear}.  Use L{value} instead of
        accessing this directly.
    t	   producingc         C   sG   |  j    | d  k	 r" | |  _ n  | d  k	 r: | |  _ n  t |  _ d  S(   N(   t   clearR   t   hostAddrt   peerAddrR.   t	   connected(   R   t   hostAddresst   peerAddress(    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR(      s    
c         C   s   t    |  _ d S(   s   
        Discard all data written to this transport so far.

        This is not a transport method.  It is intended for tests.  Do not use
        it in implementation code.
        N(   R    t   io(   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR:      s    c         C   s   |  j  j   S(   sJ  
        Retrieve all data which has been buffered by this transport.

        This is not a transport method.  It is intended for tests.  Do not use
        it in implementation code.

        @return: A C{str} giving all data written to this transport since the
            last call to L{clear}.
        @rtype: C{str}
        (   R@   t   getvalue(   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   value   s    c         C   s2   t  | t  r t d   n  |  j j |  d  S(   Ns   Data must not be unicode(   t
   isinstancet   unicodet	   TypeErrorR@   R5   (   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR5      s    c         C   s   |  j  j d j |   d  S(   NR   (   R@   R5   t   join(   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   writeSequence   s    c         C   s   t  |  _ d S(   s~   
        Close the connection. Does nothing besides toggle the C{disconnecting}
        instance variable to C{True}.
        N(   R.   t   disconnecting(   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   loseConnection   s    c         C   s)   |  j  d  k r" t j d d d  S|  j  S(   Nt   TCPs   192.168.1.1i1  (   R<   R   R   R   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   getPeer   s    c         C   s)   |  j  d  k r" t j d d d  S|  j  S(   NRJ   s   10.0.0.1i90  (   R;   R   R   R   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   getHost   s    c         C   s4   |  j  d  k	 r t d   n  | |  _  | |  _ d  S(   Ns   Cannot register two producers(   t   producerR   t   RuntimeErrort	   streaming(   R   RM   RO   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   registerProducer   s    	c         C   s4   |  j  d  k r t d   n  d  |  _  d  |  _ d  S(   Ns5   Cannot unregister a producer unless one is registered(   RM   R   RN   RO   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   unregisterProducer   s
    	c         C   s:   |  j  r t d   n  |  j d k r6 t d   n  d  S(   Ns,   Cannot resume producing after loseConnectiont   stoppeds    Cannot resume a stopped producer(   RH   RN   t   producerState(   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   _checkState   s
    	c         C   s   |  j    d |  _ d  S(   Nt   paused(   RT   RS   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   pauseProducing   s    
c         C   s   d |  _  d  S(   NRR   (   RS   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   stopProducing   s    c         C   s   |  j    d |  _ d  S(   NR9   (   RT   RS   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   resumeProducing   s    
N(   R!   R"   R#   R   R   R   R   R0   RH   R   RM   RO   R;   R<   RS   R(   R:   RB   R5   RG   RI   RK   RL   RP   RQ   RT   RV   RW   RX   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR8   a   s,   "	
												t    StringTransportWithDisconnectionc           B   s   e  Z d    Z RS(   c         C   s2   |  j  r. t |  _  |  j j t j d   n  d  S(   Ns   Bye.(   R=   R0   R   R    R   t   ConnectionDone(   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRI      s    		(   R!   R"   RI   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRY      s   t   StringIOWithoutClosingc           B   s   e  Z d  Z d   Z RS(   s*   
    A StringIO that can't be closed.
    c         C   s   d S(   s   
        Do nothing.
        N(    (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   close  s    (   R!   R"   R#   R\   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR[      s   t	   _FakePortc           B   s<   e  Z d  Z e e  d   Z d   Z d   Z d   Z RS(   s   
    A fake L{IListeningPort} to be used in tests.

    @ivar _hostAddress: The L{IAddress} this L{IListeningPort} is pretending
        to be listening on.
    c         C   s   | |  _  d S(   sz   
        @param hostAddress: An L{IAddress} this L{IListeningPort} should
            pretend to be listening on.
        N(   t   _hostAddress(   R   R>   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR(     s    c         C   s   d S(   sQ   
        Fake L{IListeningPort.startListening} that doesn't do anything.
        N(    (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   startListening  s    c         C   s   d S(   sP   
        Fake L{IListeningPort.stopListening} that doesn't do anything.
        N(    (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   stopListening!  s    c         C   s   |  j  S(   sN   
        Fake L{IListeningPort.getHost} that returns our L{IAddress}.
        (   R^   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRL   '  s    (	   R!   R"   R#   R   R	   R(   R_   R`   RL   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR]   
  s   
			t   _FakeConnectorc           B   sK   e  Z d  Z e e  e Z d   Z d   Z d   Z	 d   Z
 d   Z RS(   s*  
    A fake L{IConnector} that allows us to inspect if it has been told to stop
    connecting.
    
    @ivar stoppedConnecting: has this connector's
        L{FakeConnector.stopConnecting} method been invoked yet?

    @ivar _address: An L{IAddress} provider that represents our destination.
    c         C   s   | |  _  d S(   ss   
        @param address: An L{IAddress} provider that represents this
            connector's destination.
        N(   t   _address(   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR(   =  s    c         C   s   t  |  _ d S(   sv   
        Implement L{IConnector.stopConnecting} and set
        L{FakeConnector.stoppedConnecting} to C{True}
        N(   R.   t   stoppedConnecting(   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   stopConnectingE  s    c         C   s   d S(   s@   
        Implement L{IConnector.disconnect} as a no-op.
        N(    (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt
   disconnectM  s    c         C   s   d S(   s=   
        Implement L{IConnector.connect} as a no-op.
        N(    (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   connectS  s    c         C   s   |  j  S(   sp   
        Implement L{IConnector.getDestination} to return the C{address} passed
        to C{__init__}.
        (   Rb   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   getDestinationY  s    (   R!   R"   R#   R   R   R0   Rc   R(   Rd   Re   Rf   Rg   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRa   /  s   	
				t   MemoryReactorc           B   s   e  Z d  Z e e e e  d   Z d d d  Z d d d  Z
 d d d  Z d d d  Z d d	 d
 d  Z d d
 d  Z RS(   s  
    A fake reactor to be used in tests.  This reactor doesn't actually do
    much that's useful yet.  It accepts TCP connection setup attempts, but
    they will never succeed.

    @ivar tcpClients: a list that keeps track of connection attempts (ie, calls
        to C{connectTCP}).
    @type tcpClients: C{list}

    @ivar tcpServers: a list that keeps track of server listen attempts (ie, calls
        to C{listenTCP}).
    @type tcpServers: C{list}

    @ivar sslClients: a list that keeps track of connection attempts (ie,
        calls to C{connectSSL}).
    @type sslClients: C{list}

    @ivar sslServers: a list that keeps track of server listen attempts (ie,
        calls to C{listenSSL}).
    @type sslServers: C{list}

    @ivar unixClients: a list that keeps track of connection attempts (ie,
        calls to C{connectUNIX}).
    @type unixClients: C{list}

    @ivar unixServers: a list that keeps track of server listen attempts (ie,
        calls to C{listenUNIX}).
    @type unixServers: C{list}
    c         C   s:   g  |  _  g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ d S(   s0   
        Initialize the tracking lists.
        N(   t
   tcpClientst
   tcpServerst
   sslClientst
   sslServerst   unixClientst   unixServers(   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR(     s    					i2   R   c         C   s2   |  j  j | | | | f  t t d d |   S(   si   
        Fake L{reactor.listenTCP}, that logs the call and returns an
        L{IListeningPort}.
        RJ   s   0.0.0.0(   Rj   R+   R]   R   (   R   t   portR   t   backlogt	   interface(    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt	   listenTCP  s    i   c         C   s5   |  j  j | | | | | f  t t d | |   S(   sf   
        Fake L{reactor.connectTCP}, that logs the call and returns an
        L{IConnector}.
        RJ   (   Ri   R+   Ra   R   (   R   t   hostRo   R   t   timeoutt   bindAddress(    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt
   connectTCP  s    c         C   s5   |  j  j | | | | | f  t t d d |   S(   si   
        Fake L{reactor.listenSSL}, that logs the call and returns an
        L{IListeningPort}.
        RJ   s   0.0.0.0(   Rl   R+   R]   R   (   R   Ro   R   t   contextFactoryRp   Rq   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt	   listenSSL  s    c         C   s8   |  j  j | | | | | | f  t t d | |   S(   sf   
        Fake L{reactor.connectSSL}, that logs the call and returns an
        L{IConnector}.
        RJ   (   Rk   R+   Ra   R   (   R   Rs   Ro   R   Rw   Rt   Ru   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt
   connectSSL  s    i  i    c         C   s/   |  j  j | | | | | f  t t |   S(   sj   
        Fake L{reactor.listenUNIX}, that logs the call and returns an
        L{IListeningPort}.
        (   Rn   R+   R]   R   (   R   R   R   Rp   t   modet   wantPID(    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt
   listenUNIX  s    c         C   s,   |  j  j | | | | f  t t |   S(   sg   
        Fake L{reactor.connectUNIX}, that logs the call and returns an
        L{IConnector}.
        (   Rm   R+   Ra   R   (   R   R   R   Rt   t   checkPID(    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   connectUNIX  s    N(   R!   R"   R#   R   R   R   R   R(   Rr   R   Rv   Rx   Ry   R|   R~   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRh   b  s   		
	t   RaisingMemoryReactorc           B   s   e  Z d  Z e e e e  d d d  Z d d d  Z	 d d d  Z
 d d d  Z d d d  Z d d	 d
 d  Z d d
 d  Z RS(   s   
    A fake reactor to be used in tests.  It accepts TCP connection setup
    attempts, but they will fail.

    @ivar _listenException: An instance of an L{Exception}
    @ivar _connectException: An instance of an L{Exception}
    c         C   s   | |  _  | |  _ d S(   s   
        @param listenException: An instance of an L{Exception} to raise when any
            C{listen} method is called.

        @param connectException: An instance of an L{Exception} to raise when
            any C{connect} method is called.
        N(   t   _listenExceptiont   _connectException(   R   t   listenExceptiont   connectException(    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR(     s    	i2   R   c         C   s   |  j   d S(   sR   
        Fake L{reactor.listenTCP}, that raises L{self._listenException}.
        N(   R   (   R   Ro   R   Rp   Rq   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRr     s    i   c         C   s   |  j   d S(   sT   
        Fake L{reactor.connectTCP}, that raises L{self._connectException}.
        N(   R   (   R   Rs   Ro   R   Rt   Ru   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRv     s    c         C   s   |  j   d S(   sR   
        Fake L{reactor.listenSSL}, that raises L{self._listenException}.
        N(   R   (   R   Ro   R   Rw   Rp   Rq   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRx     s    c         C   s   |  j   d S(   sT   
        Fake L{reactor.connectSSL}, that raises L{self._connectException}.
        N(   R   (   R   Rs   Ro   R   Rw   Rt   Ru   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRy     s    i  i    c         C   s   |  j   d S(   sS   
        Fake L{reactor.listenUNIX}, that raises L{self._listenException}.
        N(   R   (   R   R   R   Rp   Rz   R{   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR|     s    c         C   s   |  j   d S(   sU   
        Fake L{reactor.connectUNIX}, that raises L{self._connectException}.
        N(   R   (   R   R   R   Rt   R}   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR~     s    N(   R!   R"   R#   R   R   R   R   R   R(   Rr   Rv   Rx   Ry   R|   R~   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR     s   N(    (    (#   R#   R    t   zope.interfaceR   t   twisted.internet.interfacesR   R   R   R   R   R   R   R	   t   twisted.protocolsR
   t   twisted.internetR   R   R   t   twisted.internet.addressR   R   t   ProtocolR   t   LineReceiverR$   R1   R8   RY   R[   R6   R]   Ra   Rh   R   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt   <module>   s$   "*
%3h