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

Regex group support #130

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ def giveme(message, something):
message.reply('Here is {}'.format(something))
```

You can use group name as kwargs:
```python
from slackbot.bot import respond_to

@respond_to('Hello (?P<name>.+)')
def say_hello(message, name):
message.reply('Hello! {}'.format(name)
```

If you would like to have a command like 'stats' and 'stats start_date end_date', you can create reg ex like so:

```python
Expand Down
4 changes: 2 additions & 2 deletions slackbot/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ def dispatch_msg(self, msg):

def _dispatch_msg_handler(self, category, msg):
responded = False
for func, args in self._plugins.get_plugins(category, msg.get('text', None)):
for func, args, kwargs in self._plugins.get_plugins(category, msg.get('text', None)):
if func:
responded = True
try:
func(Message(self._client, msg), *args)
func(Message(self._client, msg), *args, **kwargs)
except:
logger.exception(
'failed to handle message %s with plugin "%s"',
Expand Down
13 changes: 11 additions & 2 deletions slackbot/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,17 @@ def get_plugins(self, category, text):
for matcher in self.commands[category]:
m = matcher.search(text)
if m:
kwargs = m.groupdict()
args = tuple(
v for i, v in enumerate(m.groups())
if (i + 1) not in matcher.groupindex.values()
)
has_matching_plugin = True
yield self.commands[category][matcher], to_utf8(m.groups())
kwargs_utf8 = {
to_utf8(k): to_utf8(v)
for k, v in kwargs.items()
}
yield self.commands[category][matcher], to_utf8(args), kwargs_utf8

if not has_matching_plugin:
yield None, None
yield None, None, None
6 changes: 6 additions & 0 deletions tests/unit/kwarg_plugin_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from slackbot.bot import respond_to


@respond_to('Hello, (?P<name>.+)')
def say_hello(message, name):
message.reply('Hello! {}'.format(name))
6 changes: 3 additions & 3 deletions tests/unit/test_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ def default_okay(self, message):

def get_plugins(self, category, message):
if message == 'no_plugin_defined':
return [[None, None]]
return [[None, None, None]]
if category == 'default_reply':
return [[getattr(self, 'default_'+message), []]]
return [[getattr(self, 'default_'+message), [], {}]]
else:
return [[getattr(self, message), []]]
return [[getattr(self, message), [], {}]]


class FakeClient:
Expand Down
17 changes: 16 additions & 1 deletion tests/unit/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,25 @@ def test_import_plugin_single_module():
assert 'fake_plugin_module' in sys.modules


def test_kwargs():
pm = PluginsManager()
pm._load_plugins('kwarg_plugin_module')

msg = {
'category': 'respond_to',
'text': 'Hello, Jeong Arm',
}

func, args, kwargs = next(pm.get_plugins(msg['category'], msg['text']))
assert not args
assert kwargs == {'name': 'Jeong Arm'}


def test_get_plugins_none_text():
p = PluginsManager()
p.commands['respond_to'][re.compile(r'^dummy regexp$')] = lambda x: x
# Calling get_plugins() with `text == None`
for func, args in p.get_plugins('respond_to', None):
for func, args, kwargs in p.get_plugins('respond_to', None):
assert func is None
assert args is None
assert kwargs is None