o cl"@sdZddlmZddlZddlZddlmZddlmZGddde Z Gdd d e Z Gd d d e Z Gd d d e ZGddde ZGdddeZdS)zB Extend the regular Twisted reactor with event-handling features. )absolute_importN) deferToThread) format_objectc@eZdZdZdS) InvalidIDz=Raised when an invalid ID is used with reactor.cancel_call().N__name__ __module__ __qualname____doc__r r 7/usr/lib/python3/dist-packages/landscape/lib/reactor.pyrrc@r) CallHookErrorz-Raised when hooking on a reactor incorrectly.Nrr r r r rrrc@seZdZdZddZdS)EventIDzUnique 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. cCs||_||_dSN) _event_type_pair)self event_typepairr r r __init__s zEventID.__init__N)rr r r rr r r r rs rcs:eZdZdZfddZd ddZddZd d ZZS) EventHandlingReactorMixinaFire 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). cstt|i|_dSr)superrr_event_handlersr __class__r r r-s z"EventHandlingReactorMixin.__init__rcCs:||f}|j|g}|||jdddt||S)aRegister 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. cSs|dS)Nr )rr r r Bsz3EventHandlingReactorMixin.call_on..)key)r setdefaultappendsortr)rrhandlerpriorityrhandlersr r r call_on1s    z!EventHandlingReactorMixin.call_onc Ostd|g}t|j|d}|D]D\}}ztdt||||||i|WqtyDtdt||||| t yWtdt||||Yqwtd||S)acFire 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. zStarted firing %s.r z#Calling %s for %s with priority %d.zTKeyboard interrupt while running event handler %s for event type %r with args %r %r.zAError running event handler %s for event type %r with args %r %r.zFinished firing %s.) loggingdebuglistrgetrr"KeyboardInterrupt exceptionstop Exception)rrargskwargsresultsr&r$r%r r r fireFs0      zEventHandlingReactorMixin.firecCs0t|tur|j|j|jdStd|)zfUnregister an event handler. @param id: the L{EventID} of the handler to unregister. z&EventID instance expected, received %rN)typerrrremoverrridr r r cancel_calljs  z%EventHandlingReactorMixin.cancel_call)r) rr r r rr'r3r8 __classcell__r r rr r#s  $rc@seZdZddZdS) ReactorIDcCs ||_dSr)_timeout)rtimeoutr r r rws zReactorID.__init__N)rr r rr r r r r:us r:cseZdZdZfddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZZS)EventHandlingReactora Wrap and add functionalities to the Twisted reactor. This is essentially a facade around the twisted.internet.reactor and will delegate to it for mostly everything except event handling features which are implemented using EventHandlingReactorMixin. csFddlm}ddlm}||_||_||j|_tt | dS)Nr)reactor) LoopingCall) twisted.internetr>twisted.internet.taskr? _LoopingCall_reactor_cleanupcallFromThreadrr=r)rr>r?rr r rs  zEventHandlingReactor.__init__cCstS)z5Get current time. @see L{time.time} )timerr r r rFszEventHandlingReactor.timecOs|jj|i|S)zCall a function later. Simply call C{callLater(*args, **kwargs)} and return its result. @see: L{twisted.internet.interfaces.IReactorTime.callLater}. )rC callLater)rr0r1r r r call_laterszEventHandlingReactor.call_latercOs*|j|g|Ri|}|j|dd|S)zCall a function repeatedly. Create a new L{twisted.internet.task.LoopingCall} object and start it. @return: the created C{LoopingCall} object. F)now)rBstart)rsecondsfr0r1lcr r r call_everyszEventHandlingReactor.call_everycCsBt|tr t||St||jr|S|r|dSdS)aCancel 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) isinstancerrr8rBr.activecancelr6r r r r8s    z EventHandlingReactor.cancel_callcCs|j|dS)zASchedule a function to be called when the reactor starts running.N)rCcallWhenRunning)rrLr r r call_when_runningsz&EventHandlingReactor.call_when_runningcOs|jj|g|Ri|dS)a1Cause 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)rCrE)rrLr0r1r r r call_in_mains z!EventHandlingReactor.call_in_mainc sFfdd}fdd}t|g|Ri|}||||dS)a  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. csr|SdSrr )result)callbackr r on_successsz7EventHandlingReactor.call_in_thread..on_successcs6|j|j|jf}r|dStj|d|ddS)Nr)exc_info)r4valuetbr(error)failurerX)errbackr r on_failures z7EventHandlingReactor.call_in_thread..on_failureN)r addCallback addErrback) rrVr]rLr0r1rWr^deferredr )rVr]r call_in_threads  z#EventHandlingReactor.call_in_threadcCs|jj||ddS)z!Start listening on a Unix socket.T)wantPID)rC listenUNIXrsocketfactoryr r r listen_unixsz EventHandlingReactor.listen_unixcCs|j||S)zConnect to a Unix socket.)rC connectUNIXrer r r connect_unixsz!EventHandlingReactor.connect_unixcCs"|d|j|ddS)z2Start the reactor, a C{"run"} event will be fired.runr.N)r3rCrkrr r r rks  zEventHandlingReactor.runcCs|j|dS)z2Stop the reactor, a C{"stop"} event will be fired.N)rCr.rDrr r r r.s  zEventHandlingReactor.stopcCs$|jD] }|r|qdSr)rCgetDelayedCallsrPrQ)rcallr r r rDs zEventHandlingReactor._cleanup)rr r r rrFrHrNr8rSrTrbrhrjrkr.rDr9r r rr r={s     r=)r __future__rr(rFtwisted.internet.threadsrlandscape.lib.formatrr/rrobjectrrr:r=r r r r s    R