Ϫf?4pdZddlmZddlmZGddZGddZGdd ZGd d Zy ) z+ Event Dispatching and Callback utilities. )log)xpathceZdZdZdZdZy)_MethodWrapperz3 Internal class for tracking method calls. c.||_||_||_yN)methodargskwargs)selfr r r s >"a''rN) rrrrrr#r%r-r0rrr rr s!*X$'(4(rrcBeZdZdZd dZdZd dZd dZdZdZ d d Z y) EventDispatcherav Event dispatching service. The C{EventDispatcher} allows observers to be registered for certain events that are dispatched. There are two types of events: XPath events and Named events. Every dispatch is triggered by calling L{dispatch} with a data object and, for named events, the name of the event. When an XPath type event is dispatched, the associated object is assumed to be an L{Element} instance, which is matched against all registered XPath queries. For every match, the respective observer will be called with the data object. A named event will simply call each registered observer for that particular event name, with the data object. Unlike XPath type events, the data object is not restricted to L{Element}, but can be anything. When registering observers, the event that is to be observed is specified using an L{xpath.XPathQuery} instance or a string. In the latter case, the string can also contain the string representation of an XPath expression. To distinguish these from named events, each named event should start with a special prefix that is stored in C{self.prefix}. It defaults to C{//event/}. Observers registered using L{addObserver} are persistent: after the observer has been triggered by a dispatch, it remains registered for a possible next dispatch. If instead L{addOnetimeObserver} was used to observe an event, the observer is removed from the list of observers after the first observed event. Observers can also be prioritized, by providing an optional C{priority} parameter to the L{addObserver} and L{addOnetimeObserver} methods. Higher priority observers are then called before lower priority observers. Finally, observers can be unregistered by using L{removeObserver}. cJ||_i|_i|_d|_g|_y)Nr)prefix_eventObservers_xpathObservers_dispatchDepth _updateQueue)r eventprefixs r rzEventDispatcher.__init__s)! !!rc t|tjr|j}||fS|j|dt |jk(r|j }||fStj|}|j}||fSr) isinstancer XPathQueryr6r4r/r5 internQuery)r event observerss r _getEventAndObserversz%EventDispatcher._getEventAndObserverss eU-- .,,Ii{{e$6c$++&677 00 i))%0 00 irc6|jd|||g|i|y)z Register a one-time observer for an event. Like L{addObserver}, but is only triggered at most once. See there for a description of the parameters. TN _addObserverr r> observerfnpriorityr r s r addOnetimeObserverz"EventDispatcher.addOnetimeObservers$ $z8MdMfMrc6|jd|||g|i|y)a Register an observer for an event. Each observer will be registered with a certain priority. Higher priority observers get called before lower priority observers. @param event: Name or XPath query for the event to be monitored. @type event: C{str} or L{xpath.XPathQuery}. @param observerfn: Function to be called when the specified event has been triggered. This callable takes one parameter: the data object that triggered the event. When specified, the C{*args} and C{**kwargs} parameters to addObserver are being used as additional parameters to the registered observer callable. @param priority: (Optional) priority of this observer in relation to other observer that match the same event. Defaults to C{0}. @type priority: C{int} FNrBrDs r addObserverzEventDispatcher.addObservers$* % HNtNvNrc >jdkDr%jjfdyj\}|vrt }|i|<n!|} | vrt }||<n| }|j giy)Nrc6jgiSrrB)r r>r rEr"rFr sr z.EventDispatcher._addObserver..s.)))UJ;?CIr)r7r8appendr@rr#) r r"r>rErFr r r?cblpriorityObserverss ``````` r rCzEventDispatcher._addObservers    "    $ $  55e<y 9 $.C#(#,Ih  )( 3 --"n-0 (#E*'.=d=f=rcjdkDr!jjfdyj\}g}|j D]X\}}|j D]@\}}|k(s |j |j s.|j||fBZ|D] \}}|||= y)a_ Remove callable as observer for an event. The observer callable is removed for all priority levels for the specified event. @param event: Event for which the observer callable was registered. @type event: C{str} or L{xpath.XPathQuery} @param observerfn: Observer callable to be unregistered. rc(jSr)removeObserver)r>rEr sr rLz0EventDispatcher.removeObserver..sT-@-@ -SrN)r7r8rMr@r(r%r0) r r>rEr? emptyListsrFrOquery callbacklists ``` r rRzEventDispatcher.removeObservers    "    $ $%S T 55e<y +4??+< = 'H''8'>'>'@ =#|E> // ;#++-"))8U*;<  = = * +OHe(#E* +rNcdd}|xjdz c_dk7r|j}fd}n|j}d}t|j }|j |j g}|D]^}||jD]F\} } || |s| j|d}| js4|j|| fH`|D] \}} ||| = |xjdzc_|jdk(r|jD] } |  g|_ |S)a Dispatch an event. When C{event} is L{None}, an XPath type event is triggered, and C{obj} is assumed to be an instance of L{Element}. Otherwise, C{event} holds the name of the named event being triggered. In the latter case, C{obj} can be anything. @param obj: The object to be dispatched. @param event: Optional event name. @type event: C{str} FNc|k(Srr)rTobjr>s r rLz*EventDispatcher.dispatch..(s u~rc$|j|Sr)matches)rTrYs r rLz*EventDispatcher.dispatch..,su}}S'9rTr) r7r5r6r'keyssortreverser(r-r0rMr8) r rYr> foundTargetr?match prioritiesrSrFrTrUfs ` r dispatchzEventDispatcher.dispatchsJ  q  D=,,I5E,,I9E)..*+  " =H'0':'@'@'B =#|$ ))#."&K#++-"))8U*;<  = = * +OHe(#E* + q    ! #&&   "D r)z//event/)rr) rrrrrr@rGrIrCrRrcrrr r2r2ys-&P  NO.>4+>7rr2ceZdZdZdZy)XmlPipeaw XML stream pipe. Connects two objects that communicate stanzas through an XML stream like interface. Each of the ends of the pipe (sink and source) can be used to send XML stanzas to the other side, or add observers to process XML stanzas that were sent from the other side. XML pipes are usually used in place of regular XML streams that are transported over TCP. This is the reason for the use of the names source and sink for both ends of the pipe. The source side corresponds with the entity that initiated the TCP connection, whereas the sink corresponds with the entity that accepts that connection. In this object, though, the source and sink are treated equally. Unlike Jabber L{XmlStream}s, the sink and source objects are assumed to represent an eternal connected and initialized XML stream. As such, events corresponding to connection, disconnection, initialization and stream errors are not dispatched or processed. @since: 8.2 @ivar source: Source XML stream. @ivar sink: Sink XML stream. ct_t_fdj_fdj_y)Nc:jj|Sr)sinkrcrYr s r rLz"XmlPipe.__init__..kstyy'9'9#'>rc:jj|Sr)sourcercris r rLz"XmlPipe.__init__..lsT[[%9%9#%>r)r2rkrhsendr s`r rzXmlPipe.__init__hs.%' #% > > rN)rrrrrrrr rereLs 6?rreN) rtwisted.pythonrtwisted.words.xishrrrr2rerrr ros@  $''"V(V(rPPf ? ?r