From 11a484c67bb36c894bd6a861c175e40557dd7076 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sat, 14 May 2011 13:22:42 -0400 Subject: [PATCH] improvements to .tasks listall and .tasks start; adding parse_kwargs() to Data --- irc/commands/tasks.py | 46 +++++++++++++++++++++++++++++++++++++++------- irc/data.py | 20 ++++++++++++++++++++ 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/irc/commands/tasks.py b/irc/commands/tasks.py index 5a191ad..2c87899 100644 --- a/irc/commands/tasks.py +++ b/irc/commands/tasks.py @@ -2,9 +2,10 @@ # Manage wiki tasks from IRC, and check on thread status. -import threading +import threading, re from irc.base_command import BaseCommand +from irc.data import * from wiki import task_manager from config.main import * from config.irc import * @@ -40,7 +41,7 @@ class Tasks(BaseCommand): elif data.args[0] == "start": self.do_start() - elif data.args[0] == "listall": + elif data.args[0] in ["listall", "all"]: self.do_listall() else: # they asked us to do something we don't know @@ -72,16 +73,47 @@ class Tasks(BaseCommand): def do_listall(self): tasks = task_manager.task_list.keys() - self.connection.reply(self.data, ', '.join(tasks)) + threads = map(lambda t: t.name, threading.enumerate()) + tasklist = [] + + tasks.sort() + + for task in tasks: + if task in threads: + tasklist.append("\x0302{}\x0301 (\x02active\x0F)".format(task)) + else: + tasklist.append("\x0302{}\x0301 (idle)".format(task)) + + tasklist = ", ".join(tasklist) + + msg = "{} tasks loaded: {}.".format(len(tasks), tasklist) + self.connection.reply(self.data, msg) def do_start(self): - kwargs = {} + data = self.data + try: - task_manager.start_task(self.data.args[1], **kwargs) + task_name = data.args[1] except IndexError: # no task name given - self.connection.reply(self.data, "what task do you want me to start?") + self.connection.reply(data, "what task do you want me to start?") + return + + try: + data.parse_kwargs() + except KwargParseException, arg: + self.connection.reply(data, "error parsing argument: \x0303{}\x0301.".format(arg)) + return + + if task_name not in task_manager.task_list.keys(): # this task does not exist or hasn't been loaded + self.connection.reply(data, "task could not be found; either wiki/tasks/{}.py doesn't exist, or it wasn't loaded correctly.".format(task_name)) + return + + if data.kwargs: + task_manager.start_task(task_name, **data.kwargs) + self.connection.reply(data, "task \x0302{}\x0301 started with arguments: {}.".format(task_name, data.kwargs)) else: - self.connection.reply(self.data, "task '{}' started.".format(self.data.args[1])) + task_manager.start_task(task_name) + self.connection.reply(data, "task \x0302{}\x0301 started.".format(task_name)) def get_main_thread_name(self): """Return the "proper" name of the MainThread; e.g. "irc-frontend" or "irc-watcher".""" diff --git a/irc/data.py b/irc/data.py index 60b68ef..781c2ed 100644 --- a/irc/data.py +++ b/irc/data.py @@ -2,6 +2,14 @@ # A class to store data from an individual line received on IRC. +import re + +class KwargParseException(Exception): + """Couldn't parse a certain keyword argument in self.args, probably because + it was given incorrectly: e.g., no value (abc), just a value (=xyz), just + an equal sign (=), instead of the correct (abc=xyz).""" + pass + class Data(object): def __init__(self): """store data from an individual line received on IRC""" @@ -33,3 +41,15 @@ class Data(object): self.command = self.command.lower() # lowercase command name except AttributeError: pass + + def parse_kwargs(self): + """parse command arguments from self.args, given as !command key1=value1 key2=value2..., into a dict self.kwargs: {'key1': 'value2', 'key2': 'value2'...}""" + self.kwargs = {} + for arg in self.args[2:]: + try: + key, value = re.findall("^(.*?)\=(.*?)$", arg)[0] + except IndexError: + raise KwargParseException(arg) + if not key or not value: + raise KwargParseException(arg) + self.kwargs[key] = value