summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2021-02-04 18:56:18 +0100
committermathieui <mathieui@mathieui.net>2021-02-04 19:25:55 +0100
commit69b265b975fd9b1111d00e1086921061319bd26b (patch)
tree31902f6ca9b816ce82b70dd09e05d0ef5f384bc3
parent917cb555d504bf9bb217870582fdac3f48365985 (diff)
downloadslixmpp-69b265b975fd9b1111d00e1086921061319bd26b.tar.gz
slixmpp-69b265b975fd9b1111d00e1086921061319bd26b.tar.bz2
slixmpp-69b265b975fd9b1111d00e1086921061319bd26b.tar.xz
slixmpp-69b265b975fd9b1111d00e1086921061319bd26b.zip
stanzabase: make _get_plugin part of the public API
it is the only way I know of checking if an element is present in a stanza without creating it or checking the XML manually.
-rw-r--r--slixmpp/plugins/xep_0405/mix_pam.py2
-rw-r--r--slixmpp/xmlstream/stanzabase.py36
2 files changed, 23 insertions, 15 deletions
diff --git a/slixmpp/plugins/xep_0405/mix_pam.py b/slixmpp/plugins/xep_0405/mix_pam.py
index f8e5a9d9..437ca792 100644
--- a/slixmpp/plugins/xep_0405/mix_pam.py
+++ b/slixmpp/plugins/xep_0405/mix_pam.py
@@ -106,7 +106,7 @@ class XEP_0405(BasePlugin):
contacts = []
mix = []
for item in result['roster']:
- channel = item._get_plugin('channel', check=True)
+ channel = item.get_plugin('channel', check=True)
if channel:
mix.append(item)
else:
diff --git a/slixmpp/xmlstream/stanzabase.py b/slixmpp/xmlstream/stanzabase.py
index 141197ba..da9c7e06 100644
--- a/slixmpp/xmlstream/stanzabase.py
+++ b/slixmpp/xmlstream/stanzabase.py
@@ -12,11 +12,12 @@
:license: MIT, see LICENSE for more details
"""
-from __future__ import with_statement, unicode_literals
+from __future__ import annotations
import copy
import logging
import weakref
+from typing import Optional
from xml.etree import ElementTree as ET
from slixmpp.xmlstream import JID
@@ -466,7 +467,13 @@ class ElementBase(object):
"""
return self.init_plugin(attrib, lang)
- def _get_plugin(self, name, lang=None, check=False):
+ def get_plugin(self, name: str, lang: Optional[str] = None, check: bool = False) -> Optional[ElementBase]:
+ """Retrieve a stanza plugin.
+
+ :param check: Return None instead of creating the object if True.
+ :param name: Stanza plugin attribute name.
+ :param lang: xml:lang of the element to retrieve.
+ """
if lang is None:
lang = self.get_lang()
@@ -614,7 +621,7 @@ class ElementBase(object):
self[full_interface] = value
elif interface in self.plugin_attrib_map:
if interface not in iterable_interfaces:
- plugin = self._get_plugin(interface, lang)
+ plugin = self.get_plugin(interface, lang)
if plugin:
plugin.values = value
return self
@@ -660,7 +667,7 @@ class ElementBase(object):
if self.plugin_overrides:
name = self.plugin_overrides.get(get_method, None)
if name:
- plugin = self._get_plugin(name, lang)
+ plugin = self.get_plugin(name, lang)
if plugin:
handler = getattr(plugin, get_method, None)
if handler:
@@ -677,7 +684,7 @@ class ElementBase(object):
else:
return self._get_attr(attrib)
elif attrib in self.plugin_attrib_map:
- plugin = self._get_plugin(attrib, lang)
+ plugin = self.get_plugin(attrib, lang)
if plugin and plugin.is_extension:
return plugin[full_attrib]
return plugin
@@ -732,7 +739,7 @@ class ElementBase(object):
if self.plugin_overrides:
name = self.plugin_overrides.get(set_method, None)
if name:
- plugin = self._get_plugin(name, lang)
+ plugin = self.get_plugin(name, lang)
if plugin:
handler = getattr(plugin, set_method, None)
if handler:
@@ -764,7 +771,7 @@ class ElementBase(object):
else:
self.__delitem__(attrib)
elif attrib in self.plugin_attrib_map:
- plugin = self._get_plugin(attrib, lang)
+ plugin = self.get_plugin(attrib, lang)
if plugin:
plugin[full_attrib] = value
return self
@@ -816,7 +823,7 @@ class ElementBase(object):
if self.plugin_overrides:
name = self.plugin_overrides.get(del_method, None)
if name:
- plugin = self._get_plugin(attrib, lang)
+ plugin = self.get_plugin(attrib, lang)
if plugin:
handler = getattr(plugin, del_method, None)
if handler:
@@ -832,7 +839,7 @@ class ElementBase(object):
else:
self._del_attr(attrib)
elif attrib in self.plugin_attrib_map:
- plugin = self._get_plugin(attrib, lang, check=True)
+ plugin = self.get_plugin(attrib, lang, check=True)
if not plugin:
return self
if plugin.is_extension:
@@ -1037,12 +1044,10 @@ class ElementBase(object):
parent_path = "/".join(path[:len(path) - level - 1])
elements = self.xml.findall(element_path)
-
if parent_path == '':
- parent_path = None
- if parent_path is not None:
+ parent_path = None
+ if parent_path is not None:
parent = self.xml.find(parent_path)
-
if elements:
if parent is None:
parent = self.xml
@@ -1117,7 +1122,7 @@ class ElementBase(object):
next_tag = xpath[1].split('@')[0].split('}')[-1]
langs = [name[1] for name in self.plugins if name[0] == next_tag]
for lang in langs:
- plugin = self._get_plugin(next_tag, lang)
+ plugin = self.get_plugin(next_tag, lang)
if plugin and plugin.match(xpath[1:]):
return True
return False
@@ -1341,6 +1346,9 @@ class ElementBase(object):
"""Use the stanza's serialized XML as its representation."""
return self.__str__()
+ # Compatibility.
+ _get_plugin = get_plugin
+
class StanzaBase(ElementBase):