Ϫf#dZddlmZddlmZddlmZddlmZddl m Z m Z edZ edZ ed Zed ZGd d ej e j"ZGd dZGddeZGddeej*Zy)a XML Stream processing. An XML Stream is defined as a connection over which two XML documents are exchanged during the lifetime of the connection, one for each direction. The unit of interaction is a direct child element of the root element (stanza). The most prominent use of XML Streams is Jabber, but this module is generically usable. See Twisted Words for Jabber specific protocol support. Maintainer: Ralph Meijer @var STREAM_CONNECTED_EVENT: This event signals that the connection has been established. @type STREAM_CONNECTED_EVENT: L{str}. @var STREAM_END_EVENT: This event signals that the connection has been closed. @type STREAM_END_EVENT: L{str}. @var STREAM_ERROR_EVENT: This event signals that a parse error occurred. @type STREAM_ERROR_EVENT: L{str}. @var STREAM_START_EVENT: This event signals that the root element of the XML Stream has been received. For XMPP, this would be the C{} opening tag. @type STREAM_START_EVENT: L{str}. )intern)Type)protocol)failure)domishutilityz//event/stream/connectedz//event/stream/startz//event/stream/endz//event/stream/errorcReZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zy ) XmlStreama<Generic Streaming XML protocol handler. This protocol handler will parse incoming data as XML and dispatch events accordingly. Incoming stanzas can be handled by registering observers using XPath-like expressions that are matched against each stanza. See L{utility.EventDispatcher} for details. cltjj|d|_d|_d|_yN)rEventDispatcher__init__stream rawDataOutFn rawDataInFnselfs >/usr/lib/python3/dist-packages/twisted/words/xish/xmlstream.pyrzXmlStream.__init__9s-((.  ctj|_|j|j_|j |j_|j|j_y)zSets up XML Parser.N) r elementStreamronDocumentStartDocumentStartEvent onElement ElementEvent onDocumentEndDocumentEndEventrs r_initializeStreamzXmlStream._initializeStream?sF**, )-)=)= &#'>>  '+'9'9 $rcP|j|j|ty)zCalled when a connection is made. Sets up the XML parser and dispatches the L{STREAM_CONNECTED_EVENT} event indicating the connection has been established. N)rdispatchSTREAM_CONNECTED_EVENTrs rconnectionMadezXmlStream.connectionMadeLs   d23rc, |jr|j||jj|y#tj$rE|j t jt|jjYywxYw)a"Called whenever data is received. Passes the data to the XML parser. This can result in calls to the DOM handlers. If a parse error occurs, the L{STREAM_ERROR_EVENT} event is called to allow for cleanup actions, followed by dropping the connection. N) rrparser ParserErrorr rFailureSTREAM_ERROR_EVENT transportloseConnection)rdatas r dataReceivedzXmlStream.dataReceivedUsk ,  & KK  d #!! , MM'//+-? @ NN ) ) + ,s8;ABBc>|j|td|_y)z^Called when the connection is shut down. Dispatches the L{STREAM_END_EVENT}. N)r STREAM_END_EVENTr)rreasons rconnectionLostzXmlStream.connectionLostes f./ rc0|j|ty)zzCalled whenever the start tag of a root element has been received. Dispatches the L{STREAM_START_EVENT}. N)r STREAM_START_EVENT)r rootElements rrzXmlStream.onDocumentStartss d./rc&|j|y)zCalled whenever a direct child element of the root element has been received. Dispatches the received element. N)r )relements rrzXmlStream.onElementzs grc8|jjy)zCalled whenever the end tag of the root element has been received. Closes the connection. This causes C{connectionLost} being called. N)r(r)rs rrzXmlStream.onDocumentEnds %%'rc&||j_y)z(Set another function to handle elements.N)rr)rfns r setDispatchFnzXmlStream.setDispatchFns#%  rc:|j|j_y)z;Set the default function (C{onElement}) to handle elements.N)rrrrs rresetDispatchFnzXmlStream.resetDispatchFns#'>>  rctjj|r|j}t |t r|j d}|jr|j||jj|y)aRSend data over the stream. Sends the given C{obj} over the connection. C{obj} may be instances of L{domish.Element}, C{unicode} and C{str}. The first two will be properly serialized and/or encoded. C{str} objects must be in UTF-8 encoding. Note: because it is easy to make mistakes in maintaining a properly encoded C{str} object, it is advised to use C{unicode} objects everywhere when dealing with XML Streams. @param obj: Object to be sent over the stream. @type obj: L{domish.Element}, L{domish} or C{str} zutf-8N) rIElement providedBytoXml isinstancestrencoderr(write)robjs rsendzXmlStream.sendsb ?? % %c *))+C c3 **W%C      c " S!rN)__name__ __module__ __qualname____doc__rrr"r+r/rrrr8r:rDrrr r 0s> :4, 0(&2"rr c(eZdZdZdZdZdZdZy)BootstrapMixina XmlStream factory mixin to install bootstrap event observers. This mixin is for factories providing L{IProtocolFactory} to make sure bootstrap event observers are set up on protocols, before incoming data is processed. Such protocols typically derive from L{utility.EventDispatcher}, like L{XmlStream}. You can set up bootstrap event observers using C{addBootstrap}. The C{event} and C{fn} parameters correspond with the C{event} and C{observerfn} arguments to L{utility.EventDispatcher.addObserver}. @since: 8.2. @ivar bootstraps: The list of registered bootstrap event observers. @type bootstrap: C{list} cg|_yr ) bootstrapsrs rrzBootstrapMixin.__init__s rcP|jD]\}}|j||y)z Install registered bootstrap observers. @param dispatcher: Event dispatcher to add the observers to. @type dispatcher: L{utility.EventDispatcher} N)rM addObserver)r dispatchereventr7s rinstallBootstrapsz BootstrapMixin.installBootstrapss+ .IE2  " "5" - .rc>|jj||fy)z Add a bootstrap event handler. @param event: The event to register an observer for. @type event: C{str} or L{xpath.XPathQuery} @param fn: The observer callable to be registered. N)rMappendrrQr7s r addBootstrapzBootstrapMixin.addBootstrap r{+rc>|jj||fy)z Remove a bootstrap event handler. @param event: The event the observer is registered for. @type event: C{str} or L{xpath.XPathQuery} @param fn: The registered observer callable. N)rMremoverUs rremoveBootstrapzBootstrapMixin.removeBootstraprWrN)rErFrGrHrrRrVrZrIrrrKrKs$.,,rrKceZdZdZdZdZy)XmlStreamFactoryMixina XmlStream factory mixin that takes care of event handlers. All positional and keyword arguments passed to create this factory are passed on as-is to the protocol. @ivar args: Positional arguments passed to the protocol upon instantiation. @type args: C{tuple}. @ivar kwargs: Keyword arguments passed to the protocol upon instantiation. @type kwargs: C{dict}. cJtj|||_||_yr )rKrargskwargs)rr^r_s rrzXmlStreamFactoryMixin.__init__s%  rc|j|ji|j}||_|j ||S)z Create an instance of XmlStream. The returned instance will have bootstrap event observers registered and will proceed to handle input on an incoming connection. )rr^r_factoryrR)raddrxss r buildProtocolz#XmlStreamFactoryMixin.buildProtocols;T]]DII 5 5  r" rN)rErFrGrHrrdrIrrr\r\s  rr\c&eZdZUdZeZded<dZy)XmlStreamFactoryzJ Factory for XmlStream protocol objects as a reconnection client. zType[protocol.Protocol]rcN|jtj||S)a= Create a protocol instance. Overrides L{XmlStreamFactoryMixin.buildProtocol} to work with a L{ReconnectingClientFactory}. As this is called upon having an connection established, we are resetting the delay for reconnection attempts when the connection is lost again. ) resetDelayr\rd)rrbs rrdzXmlStreamFactory.buildProtocols! $224>>rN)rErFrGrHr r__annotations__rdrIrrrfrfs+4H'3 ?rrfN)rHsysrtypingrtwisted.internetrtwisted.pythonrtwisted.words.xishrrr!r1r-r'Protocolr r rKr\ReconnectingClientFactoryrfrIrrrqs :%". :;23./23z"!!7#:#:z"z2,2,jN>?,h.P.P?r