ó
ÍÿŠTc           @   s*  d  Z  d d l 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
 d d l m Z 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 e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   sB   
Extend the regular Twisted reactor with event-handling features.
iÿÿÿÿN(   t   Failure(   t   ConnectError(   t   deferToThread(   t   format_objectt	   InvalidIDc           B   s   e  Z d  Z RS(   s=   Raised when an invalid ID is used with reactor.cancel_call().(   t   __name__t
   __module__t   __doc__(    (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR      s   t   CallHookErrorc           B   s   e  Z d  Z RS(   s-   Raised when hooking on a reactor incorrectly.(   R   R   R   (    (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR      s   t   EventIDc           B   s   e  Z d  Z d „  Z RS(   sß   Unique identifier for an event handler.

    @param event_type: Name of the event type handled by the handler.
    @param pair: Binary tuple C{(handler, priority)} holding the handler
        function and its priority.
    c         C   s   | |  _  | |  _ d  S(   N(   t   _event_typet   _pair(   t   selft
   event_typet   pair(    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyt   __init__    s    	(   R   R   R   R   (    (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR	      s   t   EventHandlingReactorMixinc           B   s5   e  Z d  Z d „  Z d d „ Z d „  Z d „  Z RS(   s„  Fire events identified by strings and register handlers for them.

    Note that event handlers are executed synchronously when the C{fire} method
    is called, so unit-tests can generally exercise events without needing to
    run the real Twisted reactor (except of course if the event handlers
    themselves contain asynchronous calls that need the Twisted reactor
    running).
    c         C   s    t  t |  ƒ j ƒ  i  |  _ d  S(   N(   t   superR   R   t   _event_handlers(   R   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR   /   s    i    c         C   sN   | | f } |  j  j | g  ƒ } | j | ƒ | j d d „  ƒ t | | ƒ S(   sÕ  Register an event handler.

        The handler will be invoked every time an event of the given type
        is fired (there's no need to re-register the handler after the
        event is fired).

        @param event_type: The name of the event type to handle.
        @param handler: The function handling the given event type.
        @param priority: The priority of the given handler function.

        @return: The L{EventID} of the registered handler.
        t   keyc         S   s   |  d S(   Ni   (    (   R   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyt   <lambda>D   s    (   R   t
   setdefaultt   appendt   sortR	   (   R   R   t   handlert   priorityR   t   handlers(    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyt   call_on3   s
    c         O   s÷   t  j d | ƒ g  } t |  j j | d ƒ ƒ } x¯ | D]§ \ } } y6 t  j d t | ƒ | | ƒ | j | | | Ž  ƒ Wq8 t k
 r¹ t  j d t | ƒ | | | ƒ |  j	 ƒ  ‚  q8 t  j d t | ƒ | | | ƒ q8 Xq8 Wt  j d | ƒ | S(   sc  Fire an event of a given type.

        Call all handlers registered for the given C{event_type}, in order
        of priority.

        @param event_type: The name of the event type to fire.
        @param args: Positional arguments to pass to the registered handlers.
        @param kwargs: Keyword arguments to pass to the registered handlers.
        s   Started firing %s.s#   Calling %s for %s with priority %d.sT   Keyboard interrupt while running event handler %s for event type %r with args %r %r.sA   Error running event handler %s for event type %r with args %r %r.s   Finished firing %s.(    (
   t   loggingt   debugt   listR   t   getR   R   t   KeyboardInterruptt	   exceptiont   stop(   R   R   t   argst   kwargst   resultsR   R   R   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyt   fireH   s(    
			
	c         C   sC   t  | ƒ t k r/ |  j | j j | j ƒ n t d | ƒ ‚ d S(   sf   Unregister an event handler.

        @param id: the L{EventID} of the handler to unregister.
        s&   EventID instance expected, received %rN(   t   typeR	   R   R
   t   removeR   R   (   R   t   id(    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyt   cancel_calll   s    (   R   R   R   R   R   R&   R*   (    (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR   %   s
   		$t	   ReactorIDc           B   s   e  Z d  „  Z RS(   c         C   s   | |  _  d  S(   N(   t   _timeout(   R   t   timeout(    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR   y   s    (   R   R   R   (    (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR+   w   s   t   LandscapeReactorc           B   sƒ   e  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  Wrap and add functionalities to the Twisted reactor.

    This is essentially a facade around the L{twisted.internet.reactor} and
    will delegate to it for mostly everything except event handling features
    which are implemented using L{EventHandlingReactorMixin}.
    c         C   s_   d d l  m } d d l m } | |  _ | |  _ |  j ƒ  | j |  _ t t	 |  ƒ j
 ƒ  d  S(   Niÿÿÿÿ(   t   reactor(   t   LoopingCall(   t   twisted.internetR/   t   twisted.internet.taskR0   t   _LoopingCallt   _reactort   _cleanupt   callFromThreadR   R.   R   (   R   R/   R0   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR   …   s    		
c         C   s
   t  j  ƒ  S(   s5   Get current time.

        @see L{time.time}
        (   t   time(   R   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR7   Ž   s    c         O   s   |  j  j | | Ž  S(   s°   Call a function later.

        Simply call C{callLater(*args, **kwargs)} and return its result.

        @see: L{twisted.internet.interfaces.IReactorTime.callLater}.

        (   R4   t	   callLater(   R   R#   R$   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyt
   call_later•   s    c         O   s,   |  j  | | | Ž } | j | d t ƒ| S(   s±   Call a function repeatedly.

        Create a new L{twisted.internet.task.LoopingCall} object and
        start it.

        @return: the created C{LoopingCall} object.
        t   now(   R3   t   startt   False(   R   t   secondst   fR#   R$   t   lc(    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyt
   call_everyŸ   s    c         C   sX   t  | t ƒ r t j |  | ƒ St  | |  j ƒ r; | j ƒ  S| j ƒ  rT | j ƒ  n  d S(   s  Cancel a scheduled function or event handler.

        @param id: The function call or handler to remove. It can be an
            L{EventID}, a L{LoopingCall} or a C{IDelayedCall}, as returned
            by L{call_on}, L{call_every} and L{call_later} respectively.
        N(   t
   isinstanceR	   R   R*   R3   R"   t   activet   cancel(   R   R)   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR*   «   s    
c         C   s   |  j  j | ƒ d S(   sA   Schedule a function to be called when the reactor starts running.N(   R4   t   callWhenRunning(   R   R>   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyt   call_when_running¹   s    c         O   s   |  j  j | | | Ž d S(   s1  Cause a function to be executed by the reactor thread.

        @param f: The callable object to execute.
        @param args: The arguments to call it with.
        @param kwargs: The keyword arguments to call it with.

        @see: L{twisted.internet.interfaces.IReactorThreads.callFromThread}
        N(   R4   R6   (   R   R>   R#   R$   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyt   call_in_main½   s    	c   	         sN   ‡  f d †  } ‡ f d †  } t  | | | Ž } | j | ƒ | j | ƒ d S(   s  
        Execute a callable object in a new separate thread.

        @param callback: A function to call in case C{f} was successful, it
            will be passed the return value of C{f}.
        @param errback: A function to call in case C{f} raised an exception,
            it will be pass a C{(type, value, traceback)} tuple giving
            information about the raised exception (see L{sys.exc_info}).

        @note: Both C{callback} and C{errback} will be executed in the
            the parent thread.
        c            s   ˆ  r ˆ  |  ƒ Sd  S(   N(    (   t   result(   t   callback(    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyt
   on_successÕ   s    c            sF   |  j  |  j |  j f } ˆ  r+ ˆ  | Œ  n t j | d d | ƒd  S(   Ni   t   exc_info(   R'   t   valuet   tbR   t   error(   t   failureRJ   (   t   errback(    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyt
   on_failureÙ   s    N(   R   t   addCallbackt
   addErrback(	   R   RH   RO   R>   R#   R$   RI   RP   t   deferred(    (   RH   RO   s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyt   call_in_threadÈ   s
    c         C   s   |  j  j | | d t ƒS(   s!   Start listening on a Unix socket.t   wantPID(   R4   t
   listenUNIXt   True(   R   t   sockett   factory(    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyt   listen_unixä   s    c         C   s   |  j  j | | ƒ S(   s   Connect to a Unix socket.(   R4   t   connectUNIX(   R   RX   RY   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyt   connect_unixè   s    c         C   s+   |  j  d ƒ |  j j ƒ  |  j  d ƒ d S(   s2   Start the reactor, a C{"run"} event will be fired.t   runR"   N(   R&   R4   R]   (   R   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR]   ì   s    c         C   s   |  j  j ƒ  |  j ƒ  d S(   s2   Stop the reactor, a C{"stop"} event will be fired.N(   R4   R"   R5   (   R   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR"   ó   s    c         C   s7   x0 |  j  j ƒ  D] } | j ƒ  r | j ƒ  q q Wd  S(   N(   R4   t   getDelayedCallsRB   RC   (   R   t   call(    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR5   ø   s    (   R   R   R   R   R7   R9   R@   R*   RE   RF   RT   RZ   R\   R]   R"   R5   (    (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR.   }   s   				
									t   FakeReactorIDc           B   s   e  Z d  „  Z RS(   c         C   s   t  |  _ | |  _ d  S(   N(   RW   RB   t   _data(   R   t   data(    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR     s    	(   R   R   R   (    (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR`      s   t   FakeReactorc           B   s­   e  Z d  Z i  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 „  Z RS(   sw  A fake reactor with the same API of L{LandscapeReactor}.

    This reactor emulates the asychronous interface of L{LandscapeReactor}, but
    implementing it in a synchronous way, for easier unit-testing.

    Note that the C{listen_unix} method is *not* emulated, but rather inherited
    blindly from L{UnixReactorMixin}, this means that there's no way to control
    it in a synchronous way (see the docstring of the mixin). A better approach
    would be to fake the AMP transport (i.e. fake the twisted abstractions
    around Unix sockets), and implement a fake version C{listen_unix}, but this
    hasn't been done yet.
    c         C   sT   t  t |  ƒ j ƒ  d |  _ g  |  _ i  |  _ g  |  _ d d l m } | |  _	 d  S(   Ni    iÿÿÿÿ(   R/   (
   R   Rc   R   t   _current_timet   _callst   hostst   _threaded_callbacksR1   R/   R4   (   R   R/   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR     s    				c         C   s   t  |  j ƒ S(   N(   t   floatRd   (   R   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR7   %  s    c         O   s<   |  j  | } | | | | f } t j |  j | ƒ t | ƒ S(   N(   Rd   t   bisectt   insort_leftRe   R`   (   R   R=   R>   R#   R$   t   scheduled_timeR_   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR9   (  s    c            s7   ‡ ‡ ‡ ‡ ‡ ‡ ‡  f d †  ‰  ˆ j  ˆ ˆ  ƒ ‰ ˆ S(   Nc              sS   ˆ j  ˆ ˆ ƒ j ˆ _ y ˆ  ˆ ˆ Ž  Wn# ˆ j rH ˆ j ˆ ƒ n  ‚  n Xd  S(   N(   R9   Ra   RB   R*   (    (   R>   R=   R$   R   R#   R_   t   fake(    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyRl   0  s    	(   R9   (   R   R=   R>   R#   R$   (    (   Rl   R>   R=   R   R#   R_   R$   s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR@   .  s    !c         C   s`   t  | ƒ t k rF | j |  j k r: |  j j | j ƒ n  t | _ n t t |  ƒ j	 | ƒ d  S(   N(
   R'   R`   Ra   Re   R(   R<   RB   R   Rc   R*   (   R   R)   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR*   >  s
    c         C   s   |  j  d | ƒ d  S(   Ni    (   R9   (   R   R>   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyRE   F  s    c            s#   |  j  j ‡  ‡ ‡ f d †  ƒ d S(   s5   Schedule a function for execution in the main thread.c              s   ˆ ˆ ˆ  Ž  S(   N(    (    (   R$   R#   R>   (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR   L  s    N(   Rg   R   (   R   R>   R#   R$   (    (   R$   R#   R>   s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyRF   J  s    c         O   s'   |  j  | | | | | ƒ |  j ƒ  d S(   ss  Emulate L{LandscapeReactor.call_in_thread} without spawning threads.

        Note that running threaded callbacks here doesn't reflect reality,
        since they're usually run while the main reactor loop is active. At
        the same time, this is convenient as it means we don't need to run
        the the real Twisted reactor with to test actions performed on
        completion of specific events (e.g. L{MessageExchange.exchange} uses
        call_in_thread to run the HTTP request in a separate thread, because
        we use libcurl which is blocking). IOW, it's easier to test things
        synchronously.
        N(   t
   _in_threadt   _run_threaded_callbacks(   R   RH   RO   R>   R#   R$   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyRT   N  s    c            s3   d t  f ‡  ‡ f d †  ƒ  Y} | ˆ j ˆ  <| ƒ  S(   Nt   FakePortc              s   e  Z ‡  ‡ f d  †  Z RS(   c            s   ˆ j  j ˆ  ƒ d  S(   N(   t   _socket_pathst   pop(   t   oself(   t   socket_pathR   (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyt   stopListeninga  s    (   R   R   Rt   (    (   Rs   R   (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyRo   _  s   (   t   objectRp   (   R   Rs   RY   Ro   (    (   Rs   R   s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyRZ   ]  s    c         C   ss   |  j  j | ƒ } d d l m } | rD | | | ƒ } | j ƒ  n+ t ƒ  } t t d ƒ ƒ } | j | | ƒ | S(   Niÿÿÿÿ(   t   FakeConnectors   No such file or directory(	   Rp   R   t   landscape.lib.tests.test_ampRv   t   connectRu   R    R   t   clientConnectionFailed(   R   t   pathRY   t   serverRv   t	   connectorRN   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR\   g  s    	c         C   sO   |  j  d ƒ t |  _ x% |  j r= |  j |  j d d ƒ q W|  j  d ƒ d S(   sA   Continuously advance this reactor until reactor.stop() is called.R]   i    R"   N(   R&   RW   t   _runningt   advanceRe   (   R   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR]   s  s
    	c         C   s   t  |  _ d  S(   N(   R<   R}   (   R   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR"   {  s    c         C   sµ   xŸ |  j  r¡ |  j  d d |  j | k r¡ |  j  j d ƒ } | | d |  j 8} | d |  _ y | d | d | d Ž  Wq t k
 r } t j | ƒ q Xq W|  j | 7_ d S(   sH  Advance this reactor C{seconds} into the future.

        This method is not part of the L{LandscapeReactor} API and is specific
        to L{FakeReactor}. It's meant to be used only in unit tests for
        advancing time and triggering the relevant scheduled calls (see
        also C{call_later} and C{call_every}).
        i    i   i   i   N(   Re   Rd   Rq   t	   ExceptionR   R!   (   R   R=   R_   t   e(    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyR~   ~  s    c   	      C   sŠ   y | | | Ž  } WnW t  k
 rl } t j ƒ  } | d  k rY |  j t j | d | ƒq† |  j | | Œ n X| r† |  j | | ƒ n  d  S(   NRJ   (   R   t   sysRJ   t   NoneRF   R   RM   (	   R   RH   RO   R>   R#   R$   RG   R€   RJ   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyRm   –  s    c         C   sN   xG |  j  rI y |  j  j d ƒ ƒ  Wq t k
 rE } t j | ƒ q Xq Wd  S(   Ni    (   Rg   Rq   R   R   R!   (   R   R€   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyRn   £  s
    c         C   s"   |  j  d |  j ƒ } | |  _ d  S(   Ng      à?(   R@   Rn   t   _run_threaded_callbacks_id(   R   R)   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyt   _hook_threaded_callbacksª  s    c         C   s   |  j  |  j ƒ d  S(   N(   R*   Rƒ   (   R   (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyt   _unhook_threaded_callbacks®  s    (   R   R   R   Rp   R   R7   R9   R@   R*   RE   RF   RT   RZ   R\   R]   R"   R~   Rm   Rn   R„   R…   (    (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyRc     s&   									
							(   R   R7   R   R   Ri   t   twisted.python.failureR    t   twisted.internet.errorR   t   twisted.internet.threadsR   t   landscape.logR   R   R   R   Ru   R	   R   R+   R.   R`   Rc   (    (    (    s5   /usr/lib/python2.7/dist-packages/landscape/reactor.pyt   <module>   s    Rƒ