core (xmpp): helper method to launch a plugin method:

the `p` client method can be used to launch a method for a plugin, with a default value if
the plugin is not available (or an Exception to raise).
This commit is contained in:
Goffi 2020-04-03 18:02:31 +02:00
commit 7201730938

View file

@ -60,6 +60,28 @@ NS_ROSTER_VER = "urn:xmpp:features:rosterver"
ROSTER_VER_KEY = "@version@"
class ClientPluginWrapper:
"""Use a plugin with default value if plugin is missing"""
def __init__(self, client, plugin_name, missing):
self.client = client
self.plugin = client.host_app.plugins.get(plugin_name)
if self.plugin is None:
self.plugin_name = plugin_name
self.missing = missing
def __getattr__(self, attr):
if self.plugin is None:
missing = self.missing
if isinstance(missing, type) and issubclass(missing, Exception):
raise missing(f"plugin {self.plugin_name!r} is not available")
elif isinstance(missing, Exception):
raise missing
else:
return lambda *args, **kwargs: missing
return partial(getattr(self.plugin, attr), self.client)
class SatXMPPEntity:
"""Common code for Client and Component"""
# profile is added there when startConnection begins and removed when it is finished
@ -736,6 +758,21 @@ class SatXMPPEntity:
log.warning(_("No message found"))
return data
## helper methods ##
def p(self, plugin_name, missing=exceptions.MissingModule):
"""Get a plugin if available
@param plugin_name(str): name of the plugin
@param missing(object): value to return if plugin is missing
if it is a subclass of Exception, it will be raised with a helping str as
argument.
@return (object): requested plugin wrapper, or default value
The plugin wrapper will return the method with client set as first
positional argument
"""
return ClientPluginWrapper(self, plugin_name, missing)
@implementer(iwokkel.IDisco)
class SatXMPPClient(SatXMPPEntity, wokkel_client.XMPPClient):