Skip to content

Commit 6842e01

Browse files
committed
Generalize priorities to all hook types
1 parent 01084b8 commit 6842e01

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

cloudbot/bot.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import re
66
import os
77
import gc
8+
from operator import attrgetter
89

910
from sqlalchemy import create_engine
1011

@@ -299,11 +300,8 @@ def add_hook(hook, _event, _run_before=False):
299300
formatting.get_text_list([command for command, plugin in potential_matches])))
300301

301302
# Regex hooks
302-
def regex_priority(t):
303-
return t[1].priority
304-
305303
regex_matched = False
306-
for regex, regex_hook in sorted(self.plugin_manager.regex_hooks, key=regex_priority, reverse=True):
304+
for regex, regex_hook in self.plugin_manager.regex_hooks:
307305
if not regex_hook.run_on_cmd and cmd_match:
308306
continue
309307

cloudbot/plugin.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,14 @@ def load_plugin(self, path):
228228
self.sieves.append(sieve_hook)
229229
self._log_hook(sieve_hook)
230230

231-
# sort sieve hooks by priority
232-
self.sieves.sort(key=lambda x: x.priority)
231+
# Sort hooks
232+
self.regex_hooks.sort(key=lambda x: x[1].priority)
233+
dicts_of_lists_of_hooks = (self.event_type_hooks, self.raw_triggers)
234+
lists_of_hooks = [self.catch_all_triggers, self.sieves]
235+
lists_of_hooks.extend(d.values() for d in dicts_of_lists_of_hooks)
236+
237+
for lst in lists_of_hooks:
238+
lst.sort(key=lambda x: x.priority)
233239

234240
# we don't need this anymore
235241
del plugin.run_on_start
@@ -596,6 +602,7 @@ def __init__(self, _type, plugin, func_hook):
596602
self.permissions = func_hook.kwargs.pop("permissions", [])
597603
self.single_thread = func_hook.kwargs.pop("singlethread", False)
598604
self.action = func_hook.kwargs.pop("action", Action.CONTINUE)
605+
self.priority = func_hook.kwargs.pop("priority", Priority.NORMAL)
599606

600607
if func_hook.kwargs:
601608
# we should have popped all the args, so warn if there are any left
@@ -652,7 +659,6 @@ def __init__(self, plugin, regex_hook):
652659
:type regex_hook: cloudbot.util.hook._RegexHook
653660
"""
654661
self.run_on_cmd = regex_hook.kwargs.pop("run_on_cmd", False)
655-
self.priority = regex_hook.kwargs.pop("priority", Priority.NORMAL)
656662
self.only_no_match = regex_hook.kwargs.pop("only_no_match", False)
657663

658664
self.regexes = regex_hook.regexes

0 commit comments

Comments
 (0)