comment nitpicking and restyling

This commit is contained in:
Brian S. Stephan 2010-07-29 00:04:01 -05:00
parent 48498898bf
commit 91e535978f
8 changed files with 61 additions and 50 deletions

View File

@ -17,19 +17,20 @@
from irclib import irclib
import re
# Base class used for creating classes that have real functionality.
class Module(object):
# Base class used for creating classes that have real functionality.
# Constructor for a feature module. Inheritors should not do anything special
# here, instead they should implement register_handlers and do, or else this will
# be a very uneventful affair.
#
# Classes that are interested in allowing an indirect call to their do routine
# should add themselves to modlist inside their __init__. This will allow other
# modules to call do and see if anything can handle text they may have seen (such
# as in recursive commands).
def __init__(self, config, server, modlist, rehash):
# Constructor for a feature module. Inheritors should not do anything special
# here, instead they should implement register_handlers and do, or else this will
# be a very uneventful affair.
#
# Classes that are interested in allowing an indirect call to their do routine
# should add themselves to modlist inside their __init__. This will allow other
# modules to call do and see if anything can handle text they may have seen (such
# as in recursive commands).
self.config = config
self.modlist = modlist
self.register_handlers(server)
@ -40,23 +41,23 @@ class Module(object):
self.botircname = config.get('IRC', 'name')
self.rehash = rehash # Is there another way to call the rehash function in dr.botzo?
def register_handlers(self, server):
# This is called by __init__ and sets up server.add_global_handlers. Classes
# inheriting from Module should implement this and set up the appropriate handlers,
# e.g.:
#
# server.add_global_handler('privmsg', self.on_privmsg)
#
# Module.on_pubmsg and Module.on_privmsg are defined so far, the rest, you're on your
# own.
# This is called by __init__ and sets up server.add_global_handlers. Classes
# inheriting from Module should implement this and set up the appropriate handlers,
# e.g.:
#
# server.add_global_handler('privmsg', self.on_privmsg)
#
# Module.on_pubmsg and Module.on_privmsg are defined so far, the rest, you're on your
# own.
def register_handlers(self, server):
print "looks like someone forgot to implement register_handlers!"
def on_pubmsg(self, connection, event):
# Does some variable setup and initial sanity checking before calling Module.do,
# which should be implemented by subclasses and what can be ultimately responsible
# for the work. Of course, you are free to reimplement on_pubmsg on your own too.
# Does some variable setup and initial sanity checking before calling Module.do,
# which should be implemented by subclasses and what can be ultimately responsible
# for the work. Of course, you are free to reimplement on_pubmsg on your own too.
def on_pubmsg(self, connection, event):
nick = irclib.nm_to_n(event.source())
userhost = irclib.nm_to_uh(event.source())
replypath = event.target()
@ -83,11 +84,11 @@ class Module(object):
self.do(connection, event, nick, userhost, replypath, what, admin_unlocked)
def on_privmsg(self, connection, event):
# Does some variable setup and initial sanity checking before calling Module.do,
# which should be implemented by subclasses and what can be ultimately responsible
# for the work. Of course, you are free to reimplement on_privmsg on your own too.
# Does some variable setup and initial sanity checking before calling Module.do,
# which should be implemented by subclasses and what can be ultimately responsible
# for the work. Of course, you are free to reimplement on_privmsg on your own too.
def on_privmsg(self, connection, event):
nick = irclib.nm_to_n(event.source())
userhost = irclib.nm_to_uh(event.source())
replypath = nick
@ -105,17 +106,17 @@ class Module(object):
self.do(connection, event, nick, userhost, replypath, what, admin_unlocked)
def try_recursion(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
# Upon seeing a line intended for this module, see if there are subcommands
# that we should do what is basically a text replacement on. The intent is to
# allow things like the following:
#
# command arg1 [anothercommand arg1 arg2]
#
# where the output of anothercommand is command's arg2..n. It's mostly for
# amusement purposes, but maybe there are legitimate uses. This is intended to
# be attempted after you've determined the line should be handled by your module.
# Upon seeing a line intended for this module, see if there are subcommands
# that we should do what is basically a text replacement on. The intent is to
# allow things like the following:
#
# command arg1 [anothercommand arg1 arg2]
#
# where the output of anothercommand is command's arg2..n. It's mostly for
# amusement purposes, but maybe there are legitimate uses. This is intended to
# be attempted after you've determined the line should be handled by your module.
def try_recursion(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
start_idx = what.find('[')
subcmd = what[start_idx+1:]
end_idx = subcmd.rfind(']')
@ -148,10 +149,10 @@ class Module(object):
else:
return attempt
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
# Implement this method in your subclass to have a fairly-automatic hook into
# IRC functionality. This is called by the default on_pubmsg and on_privmsg
# Implement this method in your subclass to have a fairly-automatic hook into
# IRC functionality. This is called by the default on_pubmsg and on_privmsg
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
print "looks like someone forgot to implement do!"
# vi:tabstop=4:expandtab:autoindent

View File

@ -49,6 +49,7 @@ class DrBotIRC(irclib.IRC):
# This finds all the currently loaded modules that start with "modules" (as all
# the bot modules are currently in a subfolder called modules) and calls
# reload() on them. This will only work if the folder name doesn't change.
def rehash():
myre = re.compile('modules')
for i in sys.modules:
@ -67,12 +68,15 @@ def rehash():
# Create the actual module objects, which will readd the handlers we removed
# earlier, and add them to the modObjs list, which we can use during the
# next rehash to remove the handlers.
def reload_modules(moduleList):
for mod in moduleList:
cls = globals()[mod]
# Importing the names imports a module since the file name and class
# name are the same. Look for the class definition in each module
# with the same name and create that object.
if inspect.ismodule(cls):
for name, obj in inspect.getmembers(cls):
if inspect.isclass(obj) and re.search(mod, obj.__name__):

View File

@ -18,8 +18,9 @@ from Module import Module
from irclib import irclib
from ConfigParser import NoOptionError
# Class that adds a countdown item to the bot
class Countdown(Module):
# Class that adds a countdown item to the bot
def __init__(self, config, server, modlist, rehash):
super(Countdown, self).__init__(config, server, modlist, rehash)

View File

@ -19,8 +19,9 @@ from irclib import irclib
import re
import random
# Rolls dice, for RPGs mostly
class Dice(Module):
# Rolls dice, for RPGs mostly
def __init__(self, config, server, modlist, rehash):
super(Dice, self).__init__(config, server, modlist, rehash)

View File

@ -18,8 +18,9 @@ from Module import Module
from irclib import irclib
from ConfigParser import NoOptionError
# Returns a random fact/quote/whatever from one or more files
class FactFile(Module):
# Returns a random fact/quote/whatever from one or more files
def __init__(self, config, server, modlist, rehash):
super(FactFile, self).__init__(config, server, modlist, rehash)
@ -34,7 +35,7 @@ class FactFile(Module):
try:
filename = self.config.get('fact', whats[0])
# open file
# open file
if os.path.isfile(filename):
# http://www.regexprn.com/2008/11/read-random-line-in-large-file-in.html
with open(filename, 'r') as file:

View File

@ -19,10 +19,11 @@ from irclib import irclib
from urllib2 import urlopen
from urllib import urlencode
# Class that translates text via Google Translate.
#
# http://code.google.com/apis/ajaxlanguage/documentation/
class GoogleTranslate(Module):
# Class that translates text via Google Translate.
#
# http://code.google.com/apis/ajaxlanguage/documentation/
def __init__(self, config, server, modlist, rehash):
super(GoogleTranslate, self).__init__(config, server, modlist, rehash)

View File

@ -19,8 +19,9 @@ from Module import Module
from irclib import irclib
import traceback
# All kinds of miscellaneous irc stuff
class IrcAdmin(Module):
# All kinds of miscellaneous irc stuff
def __init__(self, config, server, modlist, rehash):
super(IrcAdmin, self).__init__(config, server, modlist, rehash)

View File

@ -20,9 +20,10 @@ from ConfigParser import NoOptionError
from datetime import datetime
from dateutil.tz import *
# Keeps track of when people say things in public channels, and reports on when
# they last said something.
class Seen(Module):
# Keeps track of when people say things in public channels, and reports on when
# they last said something.
def __init__(self, config, server, modlist, rehash):
super(Seen, self).__init__(config, server, modlist, rehash)