From b8205a9ae4376b89b6a2bca9cb3444ede35c5164 Mon Sep 17 00:00:00 2001 From: nicoco Date: Fri, 19 Jan 2024 21:26:08 +0100 Subject: [PATCH] Update plugin: XEP-0317 (hats) Merge changes from nicoco's MR that I missed, improving tests and interface. --- slixmpp/plugins/xep_0317/hats.py | 8 ++---- slixmpp/plugins/xep_0317/stanza.py | 18 ++++++++++-- slixmpp/pluginsdict.py | 2 ++ tests/test_stanza_xep_0317.py | 44 +++++++++++++++++++++++++++--- 4 files changed, 60 insertions(+), 12 deletions(-) diff --git a/slixmpp/plugins/xep_0317/hats.py b/slixmpp/plugins/xep_0317/hats.py index 79900557..7be99d83 100644 --- a/slixmpp/plugins/xep_0317/hats.py +++ b/slixmpp/plugins/xep_0317/hats.py @@ -1,6 +1,4 @@ from slixmpp.plugins import BasePlugin -from slixmpp import Presence -from slixmpp.xmlstream import register_stanza_plugin from . import stanza @@ -8,13 +6,11 @@ class XEP_0317(BasePlugin): """ XEP-0317: Hats """ - name = 'xep_0317' description = 'XEP-0317: Hats' - dependencies = {'xep_0030', 'xep_0050'} + dependencies = {'xep_0030', 'xep_0045', 'xep_0050'} stanza = stanza namespace = stanza.NS def plugin_init(self): - register_stanza_plugin(stanza.Hat, stanza.Hats) - register_stanza_plugin(stanza.Hats, Presence) + stanza.register_plugin() diff --git a/slixmpp/plugins/xep_0317/stanza.py b/slixmpp/plugins/xep_0317/stanza.py index f049f6df..f0734295 100644 --- a/slixmpp/plugins/xep_0317/stanza.py +++ b/slixmpp/plugins/xep_0317/stanza.py @@ -1,4 +1,5 @@ -from slixmpp.xmlstream import ElementBase +from slixmpp import Presence +from slixmpp.xmlstream import ElementBase, register_stanza_plugin NS = 'urn:xmpp:hats:0' @@ -25,6 +26,13 @@ class Hats(ElementBase): namespace = NS plugin_attrib = 'hats' + def add_hats(self, data: list[tuple[str, str]]) -> None: + for uri, title in data: + hat = Hat() + hat["uri"] = uri + hat["title"] = title + self.append(hat) + class Hat(ElementBase): """ @@ -38,6 +46,12 @@ class Hat(ElementBase): """ name = 'hat' - namespace = NS plugin_attrib = 'hat' + namespace = NS interfaces = {'title', 'uri'} + plugin_multi_attrib = "hats" + + +def register_plugin() -> None: + register_stanza_plugin(Hats, Hat, iterable=True) + register_stanza_plugin(Presence, Hats) diff --git a/slixmpp/pluginsdict.py b/slixmpp/pluginsdict.py index 58fd8123..4e5b7b9e 100644 --- a/slixmpp/pluginsdict.py +++ b/slixmpp/pluginsdict.py @@ -76,6 +76,7 @@ from slixmpp.plugins.xep_0297 import XEP_0297 from slixmpp.plugins.xep_0300 import XEP_0300 from slixmpp.plugins.xep_0308 import XEP_0308 from slixmpp.plugins.xep_0313 import XEP_0313 +from slixmpp.plugins.xep_0317 import XEP_0317 from slixmpp.plugins.xep_0319 import XEP_0319 from slixmpp.plugins.xep_0332 import XEP_0332 from slixmpp.plugins.xep_0333 import XEP_0333 @@ -171,6 +172,7 @@ class PluginsDict(TypedDict): xep_0300: XEP_0300 xep_0308: XEP_0308 xep_0313: XEP_0313 + xep_0317: XEP_0317 xep_0319: XEP_0319 xep_0332: XEP_0332 xep_0333: XEP_0333 diff --git a/tests/test_stanza_xep_0317.py b/tests/test_stanza_xep_0317.py index 36dd0993..821a5d37 100644 --- a/tests/test_stanza_xep_0317.py +++ b/tests/test_stanza_xep_0317.py @@ -2,16 +2,15 @@ import unittest from slixmpp import Presence from slixmpp.test import SlixTest import slixmpp.plugins.xep_0317 as xep_0317 -from slixmpp.xmlstream import register_stanza_plugin +from slixmpp.plugins.xep_0317 import stanza class TestStanzaHats(SlixTest): def setUp(self): - register_stanza_plugin(Presence, xep_0317.Hats) - register_stanza_plugin(xep_0317.Hats, xep_0317.Hats) + stanza.register_plugin() - def testCreateHats(self): + def test_create_hats(self): raw_xml = """ @@ -27,5 +26,42 @@ class TestStanzaHats(SlixTest): self.check(hats, raw_xml, use_values=False) + def test_set_single_hat(self): + presence = Presence() + presence["hats"]["hat"]["uri"] = "test-uri" + presence["hats"]["hat"]["title"] = "test-title" + self.check( + presence, # language=XML + """ + + + + + + """, + ) + + def test_set_multi_hat(self): + presence = Presence() + presence["hats"].add_hats([("uri1", "title1"), ("uri2", "title2")]) + self.check( + presence, # language=XML + """ + + + + + + + """, + ) + + def test_get_hats(self): + presence = Presence() + presence["hats"].add_hats([("uri1", "title1"), ("uri2", "title2")]) + for i, hat in enumerate(presence["hats"]["hats"], start=1): + self.assertEqual(hat["uri"], f"uri{i}") + self.assertEqual(hat["title"], f"title{i}") + suite = unittest.TestLoader().loadTestsFromTestCase(TestStanzaHats)