Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin: add capability.__str__ for better logs #2569

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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