From 3d8037ffa64e103930715c8789d23dd6bcd4afcb Mon Sep 17 00:00:00 2001 From: Red_M Date: Sat, 25 Nov 2017 13:24:49 +1000 Subject: [PATCH 1/3] Add timer hooks. --- core/main.py | 10 ++++++++++ plugins/util/hook.py | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/core/main.py b/core/main.py index 2946e375..a0bdfbba 100644 --- a/core/main.py +++ b/core/main.py @@ -227,3 +227,13 @@ def main(conn, out): input.inp = m dispatch(input, "regex", func, args) + + # TIMERS + for func, args in bot.plugs['timer']: + def timed_wrapper(func,timer_cycle,args): + while True: + print(args) + func(args) + time.sleep(timer_cycle) + func = timed_wrapper(func,args['timer'],inp) + dispatch(input, "timer", func, args) diff --git a/plugins/util/hook.py b/plugins/util/hook.py index 349eac69..2072da51 100644 --- a/plugins/util/hook.py +++ b/plugins/util/hook.py @@ -91,6 +91,20 @@ def annotate(func): return func return annotate +def timer(timer, **kwargs): + args = kwargs + + def timer_wrapper(func): + args['name'] = func.func_name + args['timer'] = timer + singlethread(func) + _hook_add(func, ['timer', (func, args)], 'timer') + return func + + if isinstance(timer,type(0)): + return timer_wrapper + else: + raise ValueError("timer decorators require a time to cyclically run.") def regex(regex, flags=0, **kwargs): args = kwargs From 596bc07fd73383333a0909812dbbfd3f32dda2ee Mon Sep 17 00:00:00 2001 From: Red_M Date: Sat, 25 Nov 2017 13:27:50 +1000 Subject: [PATCH 2/3] Missed a piece of debug. --- core/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/core/main.py b/core/main.py index a0bdfbba..6d53267b 100644 --- a/core/main.py +++ b/core/main.py @@ -232,7 +232,6 @@ def main(conn, out): for func, args in bot.plugs['timer']: def timed_wrapper(func,timer_cycle,args): while True: - print(args) func(args) time.sleep(timer_cycle) func = timed_wrapper(func,args['timer'],inp) From f051014fca683b3f309ef9655fd513f22c9fef35 Mon Sep 17 00:00:00 2001 From: Red_M Date: Sat, 25 Nov 2017 13:58:00 +1000 Subject: [PATCH 3/3] Fix timers, so it doesn't block the entire bot. Timers are called when someone speaks anywhere for the first time. Use @hook.event('004') to start timers on bot connect. --- core/main.py | 5 ----- plugins/util/hook.py | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/core/main.py b/core/main.py index 6d53267b..b48cc965 100644 --- a/core/main.py +++ b/core/main.py @@ -230,9 +230,4 @@ def main(conn, out): # TIMERS for func, args in bot.plugs['timer']: - def timed_wrapper(func,timer_cycle,args): - while True: - func(args) - time.sleep(timer_cycle) - func = timed_wrapper(func,args['timer'],inp) dispatch(input, "timer", func, args) diff --git a/plugins/util/hook.py b/plugins/util/hook.py index 2072da51..9316163a 100644 --- a/plugins/util/hook.py +++ b/plugins/util/hook.py @@ -1,5 +1,6 @@ import inspect import re +import time def _hook_add(func, add, name=''): @@ -91,10 +92,25 @@ def annotate(func): return func return annotate +class Timer(): + def __init__(self,func,cycle): + self.func = func + self.func_name = func.func_name + self.__name__ = func.__name__ + self.func_code = func.func_code + self._args = ['bot'] + self.cycle = cycle + + def __call__(self,inp,**kwargs): + while True: + self.func(inp,**kwargs) + time.sleep(self.cycle) + def timer(timer, **kwargs): args = kwargs def timer_wrapper(func): + func = Timer(func,timer) args['name'] = func.func_name args['timer'] = timer singlethread(func)