collector.py 2.29 KB
Newer Older
1

2 3 4 5 6
# slixmpp.xmlstream.handler.collector
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Part of Slixmpp: The Slick XMPP Library
# :copyright: (c) 2012 Nathanael C. Fritz, Lance J.T. Stout
# :license: MIT, see LICENSE for more details
mathieui's avatar
mathieui committed
7 8
from __future__ import annotations

9
import logging
mathieui's avatar
mathieui committed
10
from typing import List, Optional, TYPE_CHECKING
11

mathieui's avatar
mathieui committed
12
from slixmpp.xmlstream.stanzabase import StanzaBase
louiz’'s avatar
louiz’ committed
13
from slixmpp.xmlstream.handler.base import BaseHandler
mathieui's avatar
mathieui committed
14
from slixmpp.xmlstream.matcher.base import MatcherBase
15

mathieui's avatar
mathieui committed
16 17
if TYPE_CHECKING:
    from slixmpp.xmlstream.xmlstream import XMLStream
18 19 20 21 22 23 24 25 26 27 28 29 30

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.
louiz’'s avatar
louiz’ committed
31
    :param matcher: A :class:`~slixmpp.xmlstream.matcher.base.MatcherBase`
32
                    derived object for matching stanza objects.
louiz’'s avatar
louiz’ committed
33
    :param stream: The :class:`~slixmpp.xmlstream.xmlstream.XMLStream`
34 35
                   instance this handler should monitor.
    """
mathieui's avatar
mathieui committed
36
    _stanzas: List[StanzaBase]
37

mathieui's avatar
mathieui committed
38
    def __init__(self, name: str, matcher: MatcherBase, stream: Optional[XMLStream] = None):
39
        BaseHandler.__init__(self, name, matcher, stream=stream)
mathieui's avatar
mathieui committed
40
        self._stanzas = []
41

mathieui's avatar
mathieui committed
42
    def prerun(self, payload: StanzaBase) -> None:
43 44 45
        """Store the matched stanza when received during processing.

        :param payload: The matched
mathieui's avatar
mathieui committed
46
            :class:`~slixmpp.xmlstream.stanzabase.StanzaBase` object.
47
        """
mathieui's avatar
mathieui committed
48
        self._stanzas.append(payload)
49

mathieui's avatar
mathieui committed
50
    def run(self, payload: StanzaBase) -> None:
51 52 53
        """Do not process this handler during the main event loop."""
        pass

mathieui's avatar
mathieui committed
54
    def stop(self) -> List[StanzaBase]:
55 56 57 58
        """
        Stop collection of matching stanzas, and return the ones that
        have been stored so far.
        """
mathieui's avatar
mathieui committed
59 60 61 62 63 64
        stream_ref = self.stream
        if stream_ref is None:
            raise ValueError('stop() called without a stream!')
        stream = stream_ref()
        if stream is None:
            raise ValueError('stop() called without a stream!')
65
        self._destroy = True
mathieui's avatar
mathieui committed
66 67
        stream.remove_handler(self.name)
        return self._stanzas