From 04fed7e9a90cf59a940b4191a63c9526e32457f0 Mon Sep 17 00:00:00 2001 From: Bryce Lampe Date: Fri, 27 Jan 2017 07:55:29 -0800 Subject: [PATCH 1/3] Fix an issue where multiple handlers weren't registering correctly for one regex --- mattermost_bot/bot.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/mattermost_bot/bot.py b/mattermost_bot/bot.py index 26b7d69..d97669c 100644 --- a/mattermost_bot/bot.py +++ b/mattermost_bot/bot.py @@ -80,8 +80,8 @@ def _load_plugins(plugin): def get_plugins(self, category, text): has_matching_plugin = False - for matcher in self.commands[category]: - m = matcher.search(text) + for matcher in self.commands[category] + m = matcher.r.search(text) if m: has_matching_plugin = True yield self.commands[category][matcher], m.groups() @@ -90,9 +90,14 @@ def get_plugins(self, category, text): yield None, None +class Matcher(object): + """This allows us to map the same regex to multiple handlers.""" + def __init__(self, regex): + self.r = regex + def respond_to(regexp, flags=0): def wrapper(func): - PluginsManager.commands['respond_to'][re.compile(regexp, flags)] = func + PluginsManager.commands['respond_to'][Matcher(re.compile(regexp, flags))] = func logger.info( 'registered respond_to plugin "%s" to "%s"', func.__name__, regexp) return func @@ -102,7 +107,7 @@ def wrapper(func): def listen_to(regexp, flags=0): def wrapper(func): - PluginsManager.commands['listen_to'][re.compile(regexp, flags)] = func + PluginsManager.commands['listen_to'][Matcher(re.compile(regexp, flags))] = func logger.info( 'registered listen_to plugin "%s" to "%s"', func.__name__, regexp) return func From 6627ad588917992d45298cfeeefcdeca21fb93c1 Mon Sep 17 00:00:00 2001 From: Bryce Lampe Date: Wed, 1 Feb 2017 13:22:52 -0800 Subject: [PATCH 2/3] Fix typo --- mattermost_bot/bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mattermost_bot/bot.py b/mattermost_bot/bot.py index d97669c..5a1e829 100644 --- a/mattermost_bot/bot.py +++ b/mattermost_bot/bot.py @@ -80,7 +80,7 @@ def _load_plugins(plugin): def get_plugins(self, category, text): has_matching_plugin = False - for matcher in self.commands[category] + for matcher in self.commands[category]: m = matcher.r.search(text) if m: has_matching_plugin = True From 10ee27554df12712b6f70501db0d9c5afeb6022d Mon Sep 17 00:00:00 2001 From: Bryce Lampe Date: Thu, 2 Feb 2017 20:29:16 -0800 Subject: [PATCH 3/3] Simplify by using DEBUG to bypass the cache --- mattermost_bot/bot.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/mattermost_bot/bot.py b/mattermost_bot/bot.py index 5a1e829..d8ba7ce 100644 --- a/mattermost_bot/bot.py +++ b/mattermost_bot/bot.py @@ -81,7 +81,7 @@ def _load_plugins(plugin): def get_plugins(self, category, text): has_matching_plugin = False for matcher in self.commands[category]: - m = matcher.r.search(text) + m = matcher.search(text) if m: has_matching_plugin = True yield self.commands[category][matcher], m.groups() @@ -90,14 +90,9 @@ def get_plugins(self, category, text): yield None, None -class Matcher(object): - """This allows us to map the same regex to multiple handlers.""" - def __init__(self, regex): - self.r = regex - def respond_to(regexp, flags=0): def wrapper(func): - PluginsManager.commands['respond_to'][Matcher(re.compile(regexp, flags))] = func + PluginsManager.commands['respond_to'][re.compile(regexp, flags | re.DEBUG)] = func logger.info( 'registered respond_to plugin "%s" to "%s"', func.__name__, regexp) return func @@ -107,7 +102,7 @@ def wrapper(func): def listen_to(regexp, flags=0): def wrapper(func): - PluginsManager.commands['listen_to'][Matcher(re.compile(regexp, flags))] = func + PluginsManager.commands['listen_to'][re.compile(regexp, flags | re.DEBUG)] = func logger.info( 'registered listen_to plugin "%s" to "%s"', func.__name__, regexp) return func