# HG changeset patch # User Goffi # Date 1585929751 -7200 # Node ID f16c96c7a91a208dfdf697891d41fe5d8612da19 # Parent 5d67502bdc8cfd6dd2f9613bca64027d821bea78 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). diff -r 5d67502bdc8c -r f16c96c7a91a sat/core/xmpp.py --- a/sat/core/xmpp.py Fri Apr 03 18:02:27 2020 +0200 +++ b/sat/core/xmpp.py Fri Apr 03 18:02:31 2020 +0200 @@ -60,6 +60,28 @@ 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 @@ 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):