summaryrefslogtreecommitdiff
path: root/sleekxmpp/xmlstream/handler/base.py
blob: 01c1991a079ab8367592f60dd462dee9d5675cb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-
"""
    sleekxmpp.xmlstream.handler.base
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Part of SleekXMPP: The Sleek XMPP Library

    :copyright: (c) 2011 Nathanael C. Fritz
    :license: MIT, see LICENSE for more details
"""

import weakref


class BaseHandler(object):

    """
    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:`~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.
    """

    def __init__(self, name, matcher, stream=None):
        #: The name of the handler
        self.name = name

        #: The XML stream this handler is assigned to
        self.stream = None
        if stream is not None:
            self.stream = weakref.ref(stream)
            stream.register_handler(self)

        self._destroy = False
        self._payload = None
        self._matcher = matcher

    def match(self, xml):
        """Compare a stanza or XML object with the handler's matcher.

        :param xml: An XML or
            :class:`~sleekxmpp.xmlstream.stanzabase.ElementBase` object
        """
        return self._matcher.match(xml)

    def prerun(self, payload):
        """Prepare the handler for execution while the XML
        stream is being processed.

        :param payload: A :class:`~sleekxmpp.xmlstream.stanzabase.ElementBase`
                        object.
        """
        self._payload = payload

    def run(self, payload):
        """Execute the handler after XML stream processing and during the
        main event loop.

        :param payload: A :class:`~sleekxmpp.xmlstream.stanzabase.ElementBase`
                        object.
        """
        self._payload = payload

    def check_delete(self):
        """Check if the handler should be removed from the list
        of stream handlers.
        """
        return self._destroy


# To comply with PEP8, method names now use underscores.
# Deprecated method names are re-mapped for backwards compatibility.
BaseHandler.checkDelete = BaseHandler.check_delete