From 31d065907b2ed1149964799c7c1aede6103bf9d8 Mon Sep 17 00:00:00 2001 From: Trevor Bergeron Date: Mon, 27 Nov 2023 15:27:37 -0500 Subject: [PATCH 1/2] plugin: add capability.__str__ --- sopel/plugin.py | 7 +++++++ sopel/plugins/capabilities.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/sopel/plugin.py b/sopel/plugin.py index 1b38149ca..de480cea9 100644 --- a/sopel/plugin.py +++ b/sopel/plugin.py @@ -273,6 +273,13 @@ def __init__( self._cap_req: tuple[str, ...] = tuple(sorted(cap_req)) self._handler: Optional[CapabilityHandler] = handler + def __str__(self) -> str: + caps = ", ".join(repr(cap) for cap in self._cap_req) + handler = "" + if self._handler and hasattr(self._handler, "__name__"): + handler = " ({}())".format(self._handler.__name__) + return "".format(caps, handler) + @property def cap_req(self) -> tuple[str, ...]: """Capability request as a sorted tuple. diff --git a/sopel/plugins/capabilities.py b/sopel/plugins/capabilities.py index 20ea69fe5..be93bd9d1 100644 --- a/sopel/plugins/capabilities.py +++ b/sopel/plugins/capabilities.py @@ -233,7 +233,7 @@ def register(self, plugin_name: str, request: capability) -> None: plugin_caps[plugin_name] = ( request, False, ) - LOGGER.debug('Capability Request registered: %s', str(request)) + LOGGER.debug('Capability Request registered: %s', request) def request_available( self, From 972b94e3271be2a43ec6726f6ed69b50f9416817 Mon Sep 17 00:00:00 2001 From: dgw Date: Wed, 13 Dec 2023 02:28:59 -0600 Subject: [PATCH 2/2] test: basic sanity checks of `plugin.capability.__str__()` method --- test/test_plugin.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/test_plugin.py b/test/test_plugin.py index 324a22389..c53f4ff9a 100644 --- a/test/test_plugin.py +++ b/test/test_plugin.py @@ -33,6 +33,19 @@ def test_capability(cap_ack_wrapped): assert result == (True, None) +def test_capability_as_string(): + handler = plugin.capability('batch') + assert str(handler).startswith('") + + def _batch_callback(cap_req, bot, acknowledged): + ... + + handler = plugin.capability('batch', handler=_batch_callback) + + assert '(_batch_callback())' in str(handler) + + def test_capability_handler_define_once(): @plugin.capability('away-notify') def handler(name, bot, acknowledged):