diff --git a/wiki/base_task.py b/wiki/base_task.py index 4d48070..e5abeba 100644 --- a/wiki/base_task.py +++ b/wiki/base_task.py @@ -1,12 +1,19 @@ # -*- coding: utf-8 -*- -# A base class for bot tasks that edit Wikipedia. - class BaseTask(object): + """A base class for bot tasks that edit Wikipedia.""" + task_name = None + def __init__(self): - """A base class for bot tasks that edit Wikipedia.""" - self.task_name = None + """This is called once immediately after the task class is loaded by + the task manager (in wiki.task_manager.load_class_from_file()).""" + pass def run(self, **kwargs): - """Run this task.""" + """This is called directly by task_manager.start_task() and is the main + way to make a task do stuff. kwargs will be any keyword arguments + passed to start_task(), which are (of course) optional. The same task + instance is preserved between runs, so you can theoretically store data + in self (e.g. start_task('mytask', action='store', data='foo')) and + then use it later (e.g. start_task('mytask', action='save')).""" pass diff --git a/wiki/task_manager.py b/wiki/task_manager.py index de40db1..a1d4cca 100644 --- a/wiki/task_manager.py +++ b/wiki/task_manager.py @@ -33,7 +33,7 @@ def load_tasks(): def load_class_from_file(f): """Look in a given file for the task class.""" global task_list - + module = f[:-3] # strip .py from end try: exec "from wiki.tasks import %s as m" % module @@ -42,13 +42,13 @@ def load_class_from_file(f): traceback.print_exc() return try: - task_class = m.Task() + task_class = m.Task except: print "Couldn't find or get task class in file %s:" % f traceback.print_exc() return task_name = task_class.task_name - task_list[task_name] = task_class + task_list[task_name] = task_class() print "Added task %s from wiki/tasks/%s..." % (task_name, f) def start_tasks(now=time.gmtime()): @@ -70,13 +70,11 @@ def start_task(task_name, **kwargs): try: task = task_list[task_name] except KeyError: - print ("Couldn't find task '{0}': wiki/tasks/{1}.py does not " + - "exist.").format(task_name, task_name) + print ("Couldn't find task '{0}': wiki/tasks/{0}.py does not exist.").format(task_name) return - task_thread = threading.Thread(target=lambda: task_wrapper(task, **kwargs)) - task_thread.name = "{0} ({1})".format(task_name, time.strftime( - "%b %d %H:%M:%S")) + task_thread = threading.Thread(target=lambda: task_wrapper(task, **kwargs)) + task_thread.name = "{0} ({1})".format(task_name, time.strftime("%b %d %H:%M:%S")) # stop bot task threads automagically if the main bot stops task_thread.daemon = True @@ -88,8 +86,7 @@ def task_wrapper(task, **kwargs): try: task.run(**kwargs) except: - print "Task '{0}' raised an exception and had to stop:".format( - task.task_name) + print "Task '{0}' raised an exception and had to stop:".format(task.task_name) traceback.print_exc() else: print "Task '{0}' finished without error.".format(task.task_name)