From b75cb8bfe02404643b88634417825c6b5d4af137 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Thu, 14 Apr 2011 19:49:40 -0400 Subject: [PATCH] moving arg handling to data; adding a help command; cleaning git command a bit --- irc/commands/git.py | 52 +++++++++++++--------------------------------------- irc/commands/help.py | 33 +++++++++++++++++++++++++++++++++ irc/commands/test.py | 2 +- irc/data.py | 26 +++++++++++++++++++++----- irc/triggers.py | 12 +++++++++--- 5 files changed, 77 insertions(+), 48 deletions(-) create mode 100644 irc/commands/help.py diff --git a/irc/commands/git.py b/irc/commands/git.py index 6475328..2f0f83e 100644 --- a/irc/commands/git.py +++ b/irc/commands/git.py @@ -1,64 +1,41 @@ # -*- coding: utf-8 -*- -# Commands to interface with the bot's git repository. +"""Commands to interface with the bot's git repository; use '!git help' for sub-command list.""" import shlex, subprocess from config.irc_config import * actions, data = None, None -args = None def call(a, d): global actions, data actions, data = a, d - if not check_user_is_admin(): + if data.host not in ADMINS: + actions.say(data.chan, "\x02%s\x0F: you must be a bot admin to use this command." % data.nick) return - - get_args() - if not check_has_args(): + if not data.args: + actions.say(data.chan, "\x02%s\x0F: no arguments provided." % data.nick) return - if args[0] == "help": + if data.args[0] == "help": do_help() - elif args[0] == "branch": + elif data.args[0] == "branch": do_branch() - elif args[0] == "branches": + elif data.args[0] == "branches": do_branches() - elif args[0] == "checkout": + elif data.args[0] == "checkout": do_checkout() - elif args[0] == "pull": + elif data.args[0] == "pull": do_pull() - else: - unknown_arg() # they asked us to do something we don't know - -def get_args(): - """get command arguments""" - global args - args = data.msg.strip().split(' ') # strip out extra whitespace and split the message into a list - while '' in args: # remove any empty arguments - args.remove('') - args = args[1:] # remove the command itself - -def check_user_is_admin(): - """check if the user is a bot admin (and can use this command, as a result)""" - if data.host not in ADMINS: - actions.say(data.chan, "\x02%s\x0F: you must be a bot admin to use this command." % data.nick) - return False - return True - -def check_has_args(): - """check if they provided arguments along with the !git command""" - if not args: - actions.say(data.chan, "\x02%s\x0F: no arguments provided." % data.nick) - return False - return True + else: # they asked us to do something we don't know + actions.say(data.chan, "\x02%s\x0F: unknown argument: \x0303%s\x0301." % (data.nick, data.args[0])) def exec_shell(command): """execute a shell command and get the output""" @@ -96,7 +73,7 @@ def do_branches(): def do_checkout(): """switch branches""" try: - branch = args[1] + branch = data.args[1] except IndexError: # no branch name provided actions.say(data.chan, "\x02%s\x0F: switch to which branch?" % data.nick) return @@ -123,6 +100,3 @@ def do_pull(): actions.say(data.chan, "\x02%s\x0F: done; no new changes." % data.nick) else: actions.say(data.chan, "\x02%s\x0F: done; new changes merged." % data.nick) - -def unknown_arg(): - actions.say(data.chan, "\x02%s\x0F: unknown argument: \x0303%s\x0301." % (data.nick, args[0])) diff --git a/irc/commands/help.py b/irc/commands/help.py new file mode 100644 index 0000000..2adb7eb --- /dev/null +++ b/irc/commands/help.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- + +"""Generates help information.""" + +actions, data = None, None + +def call(a, d): + global actions, data + actions, data = a, d + + if not data.args: + do_general_help() + + else: + do_command_help() + +def do_general_help(): + actions.say(data.chan, "\x02%s\x0F: I am a bot! You can get help for any command by typing '!help '." % (data.nick)) + +def do_command_help(): + command = data.args[0] + + try: + exec "from irc.commands import %s as this_command" % command + except ImportError: + actions.say(data.chan, "\x02%s\x0F: command \x0303%s\x0301 not found!" % (data.nick, command)) + + info = this_command.__doc__ + + if info: + actions.say(data.chan, "\x02%s\x0F: Info for command \x0303%s\x0301: \x0302%s\x0301" % (data.nick, command, info)) + else: + actions.say(data.chan, "\x02%s\x0F: Sorry, no information for \x0303%s\x0301." % (data.nick, command)) diff --git a/irc/commands/test.py b/irc/commands/test.py index a937791..6f2680e 100644 --- a/irc/commands/test.py +++ b/irc/commands/test.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# A simple command to test the bot. +"""Test the bot!""" import random diff --git a/irc/data.py b/irc/data.py index ddf5059..a717d0e 100644 --- a/irc/data.py +++ b/irc/data.py @@ -5,8 +5,24 @@ class Data: def __init__(self): """store data from an individual line received on IRC""" - self.chan = None - self.nick = None - self.ident = None - self.host = None - self.msg = None + self.chan = str() + self.nick = str() + self.ident = str() + self.host = str() + self.msg = str() + + def parse_args(self): + """parse command arguments from self.msg into self.command and self.args""" + args = self.msg.strip().split(' ') # strip out extra whitespace and split the message into a list + while '' in args: # remove any empty arguments + args.remove('') + + try: + self.command = args[0] # the command itself + except IndexError: + self.command = None + + try: + self.args = args[1:] # the command arguments + except IndexError: + self.args = None diff --git a/irc/triggers.py b/irc/triggers.py index 28ec3d0..67786f6 100644 --- a/irc/triggers.py +++ b/irc/triggers.py @@ -2,9 +2,11 @@ # Check what events on IRC we can respond to. -from irc.commands import test, git +from irc.commands import test, help, git def check(actions, data, hook): + data.parse_args() # parse command arguments into data.command and data.args + if hook == "join": pass @@ -15,7 +17,11 @@ def check(actions, data, hook): pass if hook == "msg": - if data.msg == "!test": + if data.command == "!test": test.call(actions, data) - if data.msg.startswith("!git"): + + elif data.command == "!help": + help.call(actions, data) + + elif data.command == "!git": git.call(actions, data)