Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/landscape/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/landscape/reactor.pyc


Tc@s*dZddlZddlZddlZddlZddlmZddlmZddl	m
Z
ddlmZde
fdYZd	e
fd
YZdefdYZd
efdYZdefdYZdefdYZdefdYZdefdYZdS(sB
Extend the regular Twisted reactor with event-handling features.
iN(tFailure(tConnectError(t
deferToThread(t
format_objectt	InvalidIDcBseZdZRS(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.pyRst
CallHookErrorcBseZdZRS(s-Raised when hooking on a reactor incorrectly.(RRR(((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyRstEventIDcBseZdZdZRS(sUnique 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||_||_dS(N(t_event_typet_pair(tselft
event_typetpair((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyt__init__ s	(RRRR(((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyR	stEventHandlingReactorMixincBs5eZdZdZddZdZdZRS(sFire 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).
    cCs tt|ji|_dS(N(tsuperRRt_event_handlers(R((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyR/sicCsN||f}|jj|g}|j||jddt||S(sRegister 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.
        tkeycSs|dS(Ni((R((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyt<lambda>Ds(Rt
setdefaulttappendtsortR	(RR
thandlertpriorityRthandlers((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pytcall_on3s


cOstjd|g}t|jj|d}x|D]\}}y6tjdt||||j|||Wq8tk
rtjdt|||||j	q8tjdt||||q8Xq8Wtjd||S(scFire 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.
        sStarted firing %s.s#Calling %s for %s with priority %d.sTKeyboard interrupt while running event handler %s for event type %r with args %r %r.sAError running event handler %s for event type %r with args %r %r.sFinished firing %s.((
tloggingtdebugtlistRtgetRRtKeyboardInterruptt	exceptiontstop(RR
targstkwargstresultsRRR((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pytfireHs(
	
		

	cCsCt|tkr/|j|jj|jntd|dS(sfUnregister an event handler.

        @param id: the L{EventID} of the handler to unregister.
        s&EventID instance expected, received %rN(ttypeR	RR
tremoveRR(Rtid((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pytcancel_callls(RRRRRR&R*(((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyR%s
		$t	ReactorIDcBseZdZRS(cCs
||_dS(N(t_timeout(Rttimeout((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyRys(RRR(((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyR+wstLandscapeReactorcBseZdZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZ
dZd
ZRS(sWrap 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}.
    cCs_ddlm}ddlm}||_||_|j|j|_tt	|j
dS(Ni(treactor(tLoopingCall(ttwisted.internetR/ttwisted.internet.taskR0t_LoopingCallt_reactort_cleanuptcallFromThreadRR.R(RR/R0((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyRs		
cCs
tjS(s5Get current time.

        @see L{time.time}
        (ttime(R((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyR7scOs|jj||S(sCall a function later.

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

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

        (R4t	callLater(RR#R$((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyt
call_laterscOs,|j|||}|j|dt|S(sCall a function repeatedly.

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

        @return: the created C{LoopingCall} object.
        tnow(R3tstarttFalse(RtsecondstfR#R$tlc((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyt
call_everyscCsXt|trtj||St||jr;|jS|jrT|jndS(sCancel 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	RR*R3R"tactivetcancel(RR)((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyR*s
cCs|jj|dS(sASchedule a function to be called when the reactor starts running.N(R4tcallWhenRunning(RR>((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pytcall_when_runningscOs|jj|||dS(s1Cause 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(R4R6(RR>R#R$((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pytcall_in_mains	c	sNfd}fd}t|||}|j||j|dS(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.
        csr|SdS(N((tresult(tcallback(s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyt
on_successscsF|j|j|jf}r+|ntj|dd|dS(Nitexc_info(R'tvaluettbRterror(tfailureRJ(terrback(s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyt
on_failures
N(RtaddCallbackt
addErrback(	RRHROR>R#R$RIRPtdeferred((RHROs5/usr/lib/python2.7/dist-packages/landscape/reactor.pytcall_in_threads


cCs|jj||dtS(s!Start listening on a Unix socket.twantPID(R4t
listenUNIXtTrue(Rtsockettfactory((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pytlisten_unixscCs|jj||S(sConnect to a Unix socket.(R4tconnectUNIX(RRXRY((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pytconnect_unixscCs+|jd|jj|jddS(s2Start the reactor, a C{"run"} event will be fired.trunR"N(R&R4R](R((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyR]s

cCs|jj|jdS(s2Stop the reactor, a C{"stop"} event will be fired.N(R4R"R5(R((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyR"s
cCs7x0|jjD]}|jr|jqqWdS(N(R4tgetDelayedCallsRBRC(Rtcall((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyR5s(RRRRR7R9R@R*RERFRTRZR\R]R"R5(((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyR.}s				
									t
FakeReactorIDcBseZdZRS(cCst|_||_dS(N(RWRBt_data(Rtdata((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyRs	(RRR(((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyR`stFakeReactorcBseZdZiZdZdZdZdZdZdZ	dZ
dZd	Zd
Z
dZdZd
ZdZdZdZdZRS(swA 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.
    cCsTtt|jd|_g|_i|_g|_ddlm}||_	dS(Nii(R/(
RRcRt
_current_timet_callsthostst_threaded_callbacksR1R/R4(RR/((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyRs				cCs
t|jS(N(tfloatRd(R((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyR7%scOs<|j|}||||f}tj|j|t|S(N(Rdtbisecttinsort_leftReR`(RR=R>R#R$tscheduled_timeR_((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyR9(s
cs7fdjS(NcsSjj_yWn#jrHjnnXdS(N(R9RaRBR*((R>R=R$RR#R_tfake(s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyRl0s	(R9(RR=R>R#R$((RlR>R=RR#R_R$s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyR@.s!cCs`t|tkrF|j|jkr:|jj|jnt|_ntt|j	|dS(N(
R'R`RaReR(R<RBRRcR*(RR)((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyR*>s
cCs|jd|dS(Ni(R9(RR>((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyREFscs#|jjfddS(s5Schedule a function for execution in the main thread.cs
S(N(((R$R#R>(s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyRLsN(RgR(RR>R#R$((R$R#R>s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyRFJscOs'|j||||||jdS(ssEmulate 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RHROR>R#R$((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyRTNscs3dtffdY}|j<|S(NtFakePortcseZfdZRS(csjjdS(N(t
_socket_pathstpop(toself(tsocket_pathR(s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyt
stopListeningas(RRRt((RsR(s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyRo_s(tobjectRp(RRsRYRo((RsRs5/usr/lib/python2.7/dist-packages/landscape/reactor.pyRZ]s
cCss|jj|}ddlm}|rD|||}|jn+t}ttd}|j|||S(Ni(t
FakeConnectorsNo such file or directory(	RpRtlandscape.lib.tests.test_ampRvtconnectRuRRtclientConnectionFailed(RtpathRYtserverRvt	connectorRN((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyR\gs
	cCsO|jdt|_x%|jr=|j|jddqW|jddS(sAContinuously advance this reactor until reactor.stop() is called.R]iR"N(R&RWt_runningtadvanceRe(R((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyR]ss

	cCs
t|_dS(N(R<R}(R((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyR"{scCsx|jr|jdd|j|kr|jjd}||d|j8}|d|_y|d|d|dWqtk
r}tj|qXqW|j|7_dS(sHAdvance 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}).
        iiiiN(ReRdRqt	ExceptionRR!(RR=R_te((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyR~~s
c	Csy|||}WnWtk
rl}tj}|dkrY|jtj|d|q|j||nX|r|j||ndS(NRJ(RtsysRJtNoneRFRRM(	RRHROR>R#R$RGRRJ((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyRmscCsNxG|jrIy|jjdWqtk
rE}tj|qXqWdS(Ni(RgRqRRR!(RR((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyRns
cCs"|jd|j}||_dS(Ng?(R@Rnt_run_threaded_callbacks_id(RR)((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyt_hook_threaded_callbacksscCs|j|jdS(N(R*R(R((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyt_unhook_threaded_callbackss(RRRRpRR7R9R@R*RERFRTRZR\R]R"R~RmRnRR(((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyRcs&									
					
		(RR7RRRittwisted.python.failureRttwisted.internet.errorRttwisted.internet.threadsRt
landscape.logRRRRRuR	RR+R.R`Rc(((s5/usr/lib/python2.7/dist-packages/landscape/reactor.pyt<module>s 
R