Module: add timer support to modules
modules that want to create a timer should implement timer_interval() to return a non-zero value (the timer interval, in seconds) and timer_do() (the method that does stuff)
This commit is contained in:
parent
5885983afd
commit
0c5fe348dc
30
Module.py
30
Module.py
|
@ -22,6 +22,7 @@ import inspect
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
from threading import Timer
|
||||||
|
|
||||||
from extlib import irclib
|
from extlib import irclib
|
||||||
|
|
||||||
|
@ -31,6 +32,11 @@ class Module(object):
|
||||||
def priority(self):
|
def priority(self):
|
||||||
return 50
|
return 50
|
||||||
|
|
||||||
|
def timer_interval(self):
|
||||||
|
"""Run a timer-based event every N seconds (0 to disable)."""
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
def __init__(self, irc, config, server):
|
def __init__(self, irc, config, server):
|
||||||
"""Construct a feature module. Inheritors can do special things here, but
|
"""Construct a feature module. Inheritors can do special things here, but
|
||||||
should be sure to call Module.__init__.
|
should be sure to call Module.__init__.
|
||||||
|
@ -54,6 +60,10 @@ class Module(object):
|
||||||
# set up database for this module
|
# set up database for this module
|
||||||
self.db_init()
|
self.db_init()
|
||||||
|
|
||||||
|
# register the timer, if the implementing module has one
|
||||||
|
if self.timer_interval() > 0:
|
||||||
|
Timer(self.timer_interval(), self.timer_bootstrap, ()).start()
|
||||||
|
|
||||||
# print what was loaded, for debugging
|
# print what was loaded, for debugging
|
||||||
print("loaded " + self.__class__.__name__)
|
print("loaded " + self.__class__.__name__)
|
||||||
|
|
||||||
|
@ -216,6 +226,26 @@ class Module(object):
|
||||||
|
|
||||||
print("looks like someone forgot to implement do!")
|
print("looks like someone forgot to implement do!")
|
||||||
|
|
||||||
|
def timer_bootstrap(self):
|
||||||
|
"""Run a thread that does something periodically.
|
||||||
|
|
||||||
|
This only has real functionality if timer_interval() is
|
||||||
|
defined to return non-zero.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# don't actually do stuff if, since the last timer registration,
|
||||||
|
# the interval was set to 0
|
||||||
|
if self.timer_interval() > 0:
|
||||||
|
self.timer_do()
|
||||||
|
|
||||||
|
if self.timer_interval() > 0:
|
||||||
|
Timer(self.timer_interval(), self.timer_bootstrap, ()).start()
|
||||||
|
|
||||||
|
def timer_do(self):
|
||||||
|
"""If we're invoking the timer, do something (that you should implement)."""
|
||||||
|
|
||||||
|
print("looks like someone forgot to implement timer_do!")
|
||||||
|
|
||||||
def help_description(self):
|
def help_description(self):
|
||||||
"""Return a quick list of commands or other summary, should be
|
"""Return a quick list of commands or other summary, should be
|
||||||
less than two lines. If you want the module hidden from "!help",
|
less than two lines. If you want the module hidden from "!help",
|
||||||
|
|
Loading…
Reference in New Issue