# slixmpp.xmlstream.handler.base# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~# Part of Slixmpp: The Slick XMPP Library# :copyright: (c) 2011 Nathanael C. Fritz# :license: MIT, see LICENSE for more detailsfrom__future__importannotationsimportweakreffromweakrefimportReferenceTypefromtypingimportOptional,TYPE_CHECKING,Unionfromslixmpp.xmlstream.matcher.baseimportMatcherBasefromxml.etree.ElementTreeimportElementifTYPE_CHECKING:fromslixmpp.xmlstreamimportXMLStream,StanzaBase
[docs]classBaseHandler:""" Base class for stream handlers. Stream handlers are matched with incoming stanzas so that the stanza may be processed in some way. Stanzas may be matched with multiple handlers. Handler execution may take place in two phases: during the incoming stream processing, and in the main event loop. The :meth:`prerun()` method is executed in the first case, and :meth:`run()` is called during the second. :param string name: The name of the handler. :param matcher: A :class:`~slixmpp.xmlstream.matcher.base.MatcherBase` derived object that will be used to determine if a stanza should be accepted by this handler. :param stream: The :class:`~slixmpp.xmlstream.xmlstream.XMLStream` instance that the handle will respond to. """name:strstream:Optional[ReferenceType[XMLStream]]_destroy:bool_matcher:MatcherBase_payload:Optional[StanzaBase]def__init__(self,name:str,matcher:MatcherBase,stream:Optional[XMLStream]=None):#: The name of the handlerself.name=name#: The XML stream this handler is assigned toself.stream=NoneifstreamisnotNone:self.stream=weakref.ref(stream)stream.register_handler(self)self._destroy=Falseself._payload=Noneself._matcher=matcher
[docs]defmatch(self,xml:StanzaBase)->bool:"""Compare a stanza or XML object with the handler's matcher. :param xml: An XML or :class:`~slixmpp.xmlstream.stanzabase.StanzaBase` object """returnself._matcher.match(xml)
[docs]defprerun(self,payload:StanzaBase)->None:"""Prepare the handler for execution while the XML stream is being processed. :param payload: A :class:`~slixmpp.xmlstream.stanzabase.StanzaBase` object. """self._payload=payload
[docs]defrun(self,payload:StanzaBase)->None:"""Execute the handler after XML stream processing and during the main event loop. :param payload: A :class:`~slixmpp.xmlstream.stanzabase.StanzaBase` object. """self._payload=payload
[docs]defcheck_delete(self)->bool:"""Check if the handler should be removed from the list of stream handlers. """returnself._destroy