Bläddra i källkod

moving arg handling to data; adding a help command; cleaning git command a bit

tags/v0.1
Ben Kurtovic 13 år sedan
förälder
incheckning
b75cb8bfe0
5 ändrade filer med 77 tillägg och 48 borttagningar
  1. +13
    -39
      irc/commands/git.py
  2. +33
    -0
      irc/commands/help.py
  3. +1
    -1
      irc/commands/test.py
  4. +21
    -5
      irc/data.py
  5. +9
    -3
      irc/triggers.py

+ 13
- 39
irc/commands/git.py Visa fil

@@ -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]))

+ 33
- 0
irc/commands/help.py Visa fil

@@ -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 <command>'." % (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))

+ 1
- 1
irc/commands/test.py Visa fil

@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

# A simple command to test the bot.
"""Test the bot!"""

import random



+ 21
- 5
irc/data.py Visa fil

@@ -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

+ 9
- 3
irc/triggers.py Visa fil

@@ -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)

Laddar…
Avbryt
Spara