1 # -*- coding: utf-8 -*-
3 sleekxmpp.xmlstream.handler.callback
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 Part of SleekXMPP: The Sleek XMPP Library
8 :copyright: (c) 2011 Nathanael C. Fritz
9 :license: MIT, see LICENSE for more details
12 from sleekxmpp
.xmlstream
.handler
.base
import BaseHandler
15 class Callback(BaseHandler
):
18 The Callback handler will execute a callback function with
21 The handler may execute the callback either during stream
22 processing or during the main event loop.
24 Callback functions are all executed in the same thread, so be aware if
25 you are executing functions that will block for extended periods of
26 time. Typically, you should signal your own events using the SleekXMPP
27 object's :meth:`~sleekxmpp.xmlstream.xmlstream.XMLStream.event()`
28 method to pass the stanza off to a threaded event handler for further
32 :param string name: The name of the handler.
33 :param matcher: A :class:`~sleekxmpp.xmlstream.matcher.base.MatcherBase`
34 derived object for matching stanza objects.
35 :param pointer: The function to execute during callback.
36 :param bool thread: **DEPRECATED.** Remains only for
37 backwards compatibility.
38 :param bool once: Indicates if the handler should be used only
39 once. Defaults to False.
40 :param bool instream: Indicates if the callback should be executed
41 during stream processing instead of in the
43 :param stream: The :class:`~sleekxmpp.xmlstream.xmlstream.XMLStream`
44 instance this handler should monitor.
47 def __init__(self
, name
, matcher
, pointer
, thread
=False,
48 once
=False, instream
=False, stream
=None):
49 BaseHandler
.__init
__(self
, name
, matcher
, stream
)
50 self
._pointer
= pointer
52 self
._instream
= instream
54 def prerun(self
, payload
):
55 """Execute the callback during stream processing, if
56 the callback was created with ``instream=True``.
58 :param payload: The matched
59 :class:`~sleekxmpp.xmlstream.stanzabase.ElementBase` object.
64 self
.run(payload
, True)
66 def run(self
, payload
, instream
=False):
67 """Execute the callback function with the matched stanza payload.
69 :param payload: The matched
70 :class:`~sleekxmpp.xmlstream.stanzabase.ElementBase` object.
71 :param bool instream: Force the handler to execute during stream
72 processing. This should only be used by
73 :meth:`prerun()`. Defaults to ``False``.
75 if not self
._instream
or instream
:
76 self
._pointer
(payload
)