Skip to content

Commit

Permalink
Merge pull request #2569 from half-duplex/cap-req-reg-log
Browse files Browse the repository at this point in the history
plugin: add capability.__str__ for better logs
  • Loading branch information
dgw committed Dec 14, 2023
2 parents 6a25d17 + 972b94e commit f3c88e6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
7 changes: 7 additions & 0 deletions sopel/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<capability {}{}>".format(caps, handler)

@property
def cap_req(self) -> tuple[str, ...]:
"""Capability request as a sorted tuple.
Expand Down
2 changes: 1 addition & 1 deletion sopel/plugins/capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions test/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('<capability')
assert str(handler).endswith("'batch'>")

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):
Expand Down

0 comments on commit f3c88e6

Please sign in to comment.