1 # -*- coding: utf-8 -*-
3 sleekxmpp.xmlstream.handler.base
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 Part of SleekXMPP: The Sleek XMPP Library
8 :copyright: (c) 2011 Nathanael C. Fritz
9 :license: MIT, see LICENSE for more details
15 class BaseHandler(object):
18 Base class for stream handlers. Stream handlers are matched with
19 incoming stanzas so that the stanza may be processed in some way.
20 Stanzas may be matched with multiple handlers.
22 Handler execution may take place in two phases: during the incoming
23 stream processing, and in the main event loop. The :meth:`prerun()`
24 method is executed in the first case, and :meth:`run()` is called
27 :param string name: The name of the handler.
28 :param matcher: A :class:`~sleekxmpp.xmlstream.matcher.base.MatcherBase`
29 derived object that will be used to determine if a
30 stanza should be accepted by this handler.
31 :param stream: The :class:`~sleekxmpp.xmlstream.xmlstream.XMLStream`
32 instance that the handle will respond to.
35 def __init__(self
, name
, matcher
, stream
=None):
36 #: The name of the handler
39 #: The XML stream this handler is assigned to
41 if stream
is not None:
42 self
.stream
= weakref
.ref(stream
)
43 stream
.register_handler(self
)
47 self
._matcher
= matcher
50 """Compare a stanza or XML object with the handler's matcher.
53 :class:`~sleekxmpp.xmlstream.stanzabase.ElementBase` object
55 return self
._matcher
.match(xml
)
57 def prerun(self
, payload
):
58 """Prepare the handler for execution while the XML
59 stream is being processed.
61 :param payload: A :class:`~sleekxmpp.xmlstream.stanzabase.ElementBase`
64 self
._payload
= payload
66 def run(self
, payload
):
67 """Execute the handler after XML stream processing and during the
70 :param payload: A :class:`~sleekxmpp.xmlstream.stanzabase.ElementBase`
73 self
._payload
= payload
75 def check_delete(self
):
76 """Check if the handler should be removed from the list
82 # To comply with PEP8, method names now use underscores.
83 # Deprecated method names are re-mapped for backwards compatibility.
84 BaseHandler
.checkDelete
= BaseHandler
.check_delete