Merge branch 'stanzabase-make-get-plugin-public-api' into 'master'

stanzabase: make _get_plugin part of the public API

See merge request poezio/slixmpp!119
This commit is contained in:
Link Mauve 2021-02-04 19:28:29 +01:00
commit c82e1a4039
2 changed files with 23 additions and 15 deletions

View file

@ -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:

View file

@ -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 = 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):