2011-12-05 00:26:14 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2010-03-26 21:32:16 +00:00
|
|
|
"""
|
2011-12-05 00:26:14 +00:00
|
|
|
sleekxmpp.xmlstream.handler.base
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2010-03-26 21:32:16 +00:00
|
|
|
|
2011-12-05 00:26:14 +00:00
|
|
|
Part of SleekXMPP: The Sleek XMPP Library
|
|
|
|
|
|
|
|
:copyright: (c) 2011 Nathanael C. Fritz
|
|
|
|
:license: MIT, see LICENSE for more details
|
2010-03-26 21:32:16 +00:00
|
|
|
"""
|
2009-06-03 22:56:51 +00:00
|
|
|
|
2011-10-08 21:31:30 +00:00
|
|
|
import weakref
|
|
|
|
|
2010-08-27 20:42:26 +00:00
|
|
|
|
2009-06-03 22:56:51 +00:00
|
|
|
class BaseHandler(object):
|
|
|
|
|
2010-08-27 20:42:26 +00:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
|
2011-12-05 00:26:14 +00:00
|
|
|
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:`~sleekxmpp.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:`~sleekxmpp.xmlstream.xmlstream.XMLStream`
|
|
|
|
instance that the handle will respond to.
|
2010-08-27 20:42:26 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name, matcher, stream=None):
|
2011-12-05 00:26:14 +00:00
|
|
|
#: The name of the handler
|
2010-08-27 20:42:26 +00:00
|
|
|
self.name = name
|
2011-12-05 00:26:14 +00:00
|
|
|
|
|
|
|
#: The XML stream this handler is assigned to
|
|
|
|
self.stream = None
|
2011-10-08 21:31:30 +00:00
|
|
|
if stream is not None:
|
|
|
|
self.stream = weakref.ref(stream)
|
2011-12-05 00:26:14 +00:00
|
|
|
stream.register_handler(self)
|
|
|
|
|
2010-08-27 20:42:26 +00:00
|
|
|
self._destroy = False
|
|
|
|
self._payload = None
|
|
|
|
self._matcher = matcher
|
|
|
|
|
|
|
|
def match(self, xml):
|
2011-12-05 00:26:14 +00:00
|
|
|
"""Compare a stanza or XML object with the handler's matcher.
|
2010-08-27 20:42:26 +00:00
|
|
|
|
2011-12-05 00:26:14 +00:00
|
|
|
:param xml: An XML or
|
|
|
|
:class:`~sleekxmpp.xmlstream.stanzabase.ElementBase` object
|
2010-08-27 20:42:26 +00:00
|
|
|
"""
|
|
|
|
return self._matcher.match(xml)
|
|
|
|
|
|
|
|
def prerun(self, payload):
|
2011-12-05 00:26:14 +00:00
|
|
|
"""Prepare the handler for execution while the XML
|
|
|
|
stream is being processed.
|
2010-08-27 20:42:26 +00:00
|
|
|
|
2011-12-05 00:26:14 +00:00
|
|
|
:param payload: A :class:`~sleekxmpp.xmlstream.stanzabase.ElementBase`
|
|
|
|
object.
|
2010-08-27 20:42:26 +00:00
|
|
|
"""
|
|
|
|
self._payload = payload
|
|
|
|
|
|
|
|
def run(self, payload):
|
2011-12-05 00:26:14 +00:00
|
|
|
"""Execute the handler after XML stream processing and during the
|
2010-08-27 20:42:26 +00:00
|
|
|
main event loop.
|
|
|
|
|
2011-12-05 00:26:14 +00:00
|
|
|
:param payload: A :class:`~sleekxmpp.xmlstream.stanzabase.ElementBase`
|
|
|
|
object.
|
2010-08-27 20:42:26 +00:00
|
|
|
"""
|
|
|
|
self._payload = payload
|
2009-06-03 22:56:51 +00:00
|
|
|
|
2010-10-18 13:06:54 +00:00
|
|
|
def check_delete(self):
|
2011-12-05 00:26:14 +00:00
|
|
|
"""Check if the handler should be removed from the list
|
|
|
|
of stream handlers.
|
2010-08-27 20:42:26 +00:00
|
|
|
"""
|
|
|
|
return self._destroy
|
2011-02-14 18:49:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
# To comply with PEP8, method names now use underscores.
|
|
|
|
# Deprecated method names are re-mapped for backwards compatibility.
|
|
|
|
BaseHandler.checkDelete = BaseHandler.check_delete
|