~e"dZddlZddlZddlmZddlmZGddeZGddeZ Gd d Z Gd d Z Gd dZ Gdde Z y)zB Extend the regular Twisted reactor with event-handling features. N) deferToThread) format_objectceZdZdZy) InvalidIDz=Raised when an invalid ID is used with reactor.cancel_call().N__name__ __module__ __qualname____doc__7/usr/lib/python3/dist-packages/landscape/lib/reactor.pyrr sGr rceZdZdZy) CallHookErrorz-Raised when hooking on a reactor incorrectly.Nrr r rrrs7r rceZdZdZdZy)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. c ||_||_yN) _event_type_pair)self event_typepairs r__init__zEventID.__init__s% r N)rr r r rr r rrrs r rc6eZdZdZfdZddZdZdZxZS)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). c0t|i|_yr)superr_event_handlers)r __class__s rrz"EventHandlingReactorMixin.__init__+s !r c||f}|jj|g}|j||jdt ||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. c |dS)Nr )rs rz3EventHandlingReactorMixin.call_on..@s tAwr )key)r setdefaultappendsortr)rrhandlerpriorityrhandlerss rcall_onz!EventHandlingReactorMixin.call_on/sN"''22:rB . /z4((r c  tjd|g}t|jj |d}|D]>\}} tjdt ||||j ||i|@tjd||S#t$r4tjdt |||||jt$r%tjdt ||||YwxYw)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*s rfirezEventHandlingReactorMixin.fireDs   *J7,,00R@A!)  GX  9!'*  w778 <  +Z8-% !!""'*  !!5!'*  s8BA&DDct|tur3|j|jj |j yt d|)zfUnregister an event handler. @param id: the L{EventID} of the handler to unregister. z$EventID instance expected, received N)typerrrremoverrrids r cancel_callz%EventHandlingReactorMixin.cancel_callusD 8w    0 7 7 AB2&IJ Jr )r) rr r r rr,r9r? __classcell__r s@rrr!s")*/bKr rceZdZdZy) ReactorIDc||_yr)_timeout)rtimeouts rrzReactorID.__init__s  r N)rr r rr r rrCrCs r rCcjeZdZdZfdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZxZS)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. cddlm}ddlm}||_||_|j |j|_t|%y)Nr)reactor) LoopingCall) twisted.internetrJtwisted.internet.taskrK _LoopingCall_reactor_cleanupcallFromThreadrr)rrJrKr s rrzEventHandlingReactor.__init__s:,5'  %44 r c*tjS)z5Get current time. @see L{time.time} )timers rrSzEventHandlingReactor.times yy{r c:|jj|i|S)zCall a function later. Simply call C{callLater(*args, **kwargs)} and return its result. @see: L{twisted.internet.interfaces.IReactorTime.callLater}. )rO callLater)rr6r7s r call_laterzEventHandlingReactor.call_laters 't}}&&777r cX|j|g|i|}|j|d|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)rNstart)rsecondsfr6r7lcs r call_everyzEventHandlingReactor.call_everys5T  q 24 26 2 e$ r ct|trtj||St||jr|j S|j r|jyy)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) isinstancerrr?rNr4activecancelr=s rr?z EventHandlingReactor.cancel_callsT b' ",88rB B b$++ ,779  99; IIK r c:|jj|y)zASchedule a function to be called when the reactor starts running.N)rOcallWhenRunning)rr\s rcall_when_runningz&EventHandlingReactor.call_when_runnings %%a(r cD|jj|g|i|y)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)rOrQ)rr\r6r7s r call_in_mainz!EventHandlingReactor.call_in_mains" % $$Q888r c~fd}fd}t|g|i|}|j||j|y)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. cr|Syrr )resultcallbacks r on_successz7EventHandlingReactor.call_in_thread..on_successs''r c|j|j|jf}r|ytj|d|y)Nr#)exc_info)r;valuetbr.error)failurernerrbacks r on_failurez7EventHandlingReactor.call_in_thread..on_failures8 gmmWZZ@H" hqkH=r N)r addCallback addErrback) rrkrsr\r6r7rlrtdeferreds `` rcall_in_threadz#EventHandlingReactor.call_in_threads? ( >!4T4V4Z(J'r c>|jj||dS)z!Start listening on a Unix socket.T)wantPID)rO listenUNIXrsocketfactorys r listen_unixz EventHandlingReactor.listen_unixs}}'''FFr c:|jj||S)zConnect to a Unix socket.)rO connectUNIXr|s r connect_unixz!EventHandlingReactor.connect_unixs}}((99r c||jd|jj|jdy)z2Start the reactor, a C{"run"} event will be fired.runr4N)r9rOrrTs rrzEventHandlingReactor.runs, %  &r cX|jj|jy)z2Stop the reactor, a C{"stop"} event will be fired.N)rOr4rPrTs rr4zEventHandlingReactor.stops  r c|jjD]#}|js|j%yr)rOgetDelayedCallsrarb)rcalls rrPzEventHandlingReactor._cleanups2MM113 D{{}  r )rr r r rrSrWr^r?rergrxrrrr4rPr@rAs@rrHrHsJ8  ) 9(:G: r rH)r r.rStwisted.internet.threadsrlandscape.lib.formatrr5rrrrrCrHr r rrsd 2.H H8I8  \K\K~  B4Br