stanzapath.py 1.6 KB
Newer Older
1

2 3 4 5 6
# slixmpp.xmlstream.matcher.stanzapath
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Part of Slixmpp: The Slick XMPP Library
# :copyright: (c) 2011 Nathanael C. Fritz
# :license: MIT, see LICENSE for more details
mathieui's avatar
mathieui committed
7
from typing import cast, List
louiz’'s avatar
louiz’ committed
8
from slixmpp.xmlstream.matcher.base import MatcherBase
mathieui's avatar
mathieui committed
9
from slixmpp.xmlstream.stanzabase import fix_ns, StanzaBase
10

11 12 13 14 15 16 17

class StanzaPath(MatcherBase):

    """
    The StanzaPath matcher selects stanzas that match a given "stanza path",
    which is similar to a normal XPath except that it uses the interfaces and
    plugins of the stanza instead of the actual, underlying XML.
18 19

    :param criteria: Object to compare some aspect of a stanza against.
20
    """
mathieui's avatar
mathieui committed
21 22 23 24 25 26 27 28 29 30 31
    _criteria: List[str]
    _raw_criteria: str

    def __init__(self, criteria: str):
        self._criteria = cast(
            List[str],
            fix_ns(
                criteria, split=True, propagate_ns=False,
                default_ns='jabber:client'
            )
        )
32 33
        self._raw_criteria = criteria

mathieui's avatar
mathieui committed
34
    def match(self, stanza: StanzaBase) -> bool:
35 36 37
        """
        Compare a stanza against a "stanza path". A stanza path is similar to
        an XPath expression, but uses the stanza's interfaces and plugins
38
        instead of the underlying XML. See the documentation for the stanza
mathieui's avatar
mathieui committed
39
        :meth:`~slixmpp.xmlstream.stanzabase.StanzaBase.match()` method
40
        for more information.
41

mathieui's avatar
mathieui committed
42
        :param stanza: The :class:`~slixmpp.xmlstream.stanzabase.StanzaBase`
43
                       stanza to compare against.
44
        """
45
        return stanza.match(self._criteria) or stanza.match(self._raw_criteria)