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:
parent
1a5996ac4b
commit
7201730938
1 changed files with 37 additions and 0 deletions
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Reference in a new issue