diff options
author | Lance Stout <lancestout@gmail.com> | 2012-09-25 20:18:30 -0700 |
---|---|---|
committer | Lance Stout <lancestout@gmail.com> | 2012-09-25 20:20:22 -0700 |
commit | f6e1fecdf8e173e9eade7ade1e6b397cf4b8635d (patch) | |
tree | b0497ccfc82781366cf9e17844be515dd661444d /sleekxmpp | |
parent | 94e8b2becf0cb7b81afbf384fb9d0ec61b8abf7d (diff) | |
download | slixmpp-f6e1fecdf8e173e9eade7ade1e6b397cf4b8635d.tar.gz slixmpp-f6e1fecdf8e173e9eade7ade1e6b397cf4b8635d.tar.bz2 slixmpp-f6e1fecdf8e173e9eade7ade1e6b397cf4b8635d.tar.xz slixmpp-f6e1fecdf8e173e9eade7ade1e6b397cf4b8635d.zip |
Add Collector stanza handler class.
This style of handler is necessary for capturing result sets from
queries that use multiple messages to send the results instead of
in a single result stanza. Notably, XEP-0313 (MAM).
Diffstat (limited to 'sleekxmpp')
-rw-r--r-- | sleekxmpp/xmlstream/handler/__init__.py | 1 | ||||
-rw-r--r-- | sleekxmpp/xmlstream/handler/collector.py | 66 |
2 files changed, 67 insertions, 0 deletions
diff --git a/sleekxmpp/xmlstream/handler/__init__.py b/sleekxmpp/xmlstream/handler/__init__.py index 7bcf0b71..83c87f01 100644 --- a/sleekxmpp/xmlstream/handler/__init__.py +++ b/sleekxmpp/xmlstream/handler/__init__.py @@ -7,6 +7,7 @@ """ from sleekxmpp.xmlstream.handler.callback import Callback +from sleekxmpp.xmlstream.handler.collector import Collector from sleekxmpp.xmlstream.handler.waiter import Waiter from sleekxmpp.xmlstream.handler.xmlcallback import XMLCallback from sleekxmpp.xmlstream.handler.xmlwaiter import XMLWaiter diff --git a/sleekxmpp/xmlstream/handler/collector.py b/sleekxmpp/xmlstream/handler/collector.py new file mode 100644 index 00000000..8f02f8c3 --- /dev/null +++ b/sleekxmpp/xmlstream/handler/collector.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- +""" + sleekxmpp.xmlstream.handler.collector + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + Part of SleekXMPP: The Sleek XMPP Library + + :copyright: (c) 2012 Nathanael C. Fritz, Lance J.T. Stout + :license: MIT, see LICENSE for more details +""" + +import logging + +from sleekxmpp.util import Queue, QueueEmpty +from sleekxmpp.xmlstream.handler.base import BaseHandler + + +log = logging.getLogger(__name__) + + +class Collector(BaseHandler): + + """ + The Collector handler allows for collecting a set of stanzas + that match a given pattern. Unlike the Waiter handler, a + Collector does not block execution, and will continue to + accumulate matching stanzas until told to stop. + + :param string name: The name of the handler. + :param matcher: A :class:`~sleekxmpp.xmlstream.matcher.base.MatcherBase` + derived object for matching stanza objects. + :param stream: The :class:`~sleekxmpp.xmlstream.xmlstream.XMLStream` + instance this handler should monitor. + """ + + def __init__(self, name, matcher, stream=None): + BaseHandler.__init__(self, name, matcher, stream=stream) + self._payload = Queue() + + def prerun(self, payload): + """Store the matched stanza when received during processing. + + :param payload: The matched + :class:`~sleekxmpp.xmlstream.stanzabase.ElementBase` object. + """ + self._payload.put(payload) + + def run(self, payload): + """Do not process this handler during the main event loop.""" + pass + + def stop(self): + """ + Stop collection of matching stanzas, and return the ones that + have been stored so far. + """ + self._destroy = True + results = [] + try: + while True: + results.append(self._payload.get(False)) + except QueueEmpty: + pass + + self.stream().remove_handler(self.name) + return results |