making comments more standard

This commit is contained in:
Brian S. Stephan 2010-07-26 21:59:11 -05:00
parent 2f52eb0d1a
commit 35b6545425
1 changed files with 44 additions and 61 deletions

View File

@ -29,42 +29,38 @@ from dateutil.tz import *
import irclib
class Module(object):
"""Base class used for creating classes that have real functionality.
"""
# Base class used for creating classes that have real functionality.
def __init__(self, config, server, modlist):
"""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).
"""
# 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)
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.
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.
nick = irclib.nm_to_n(event.source())
userhost = irclib.nm_to_uh(event.source())
@ -90,10 +86,9 @@ 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.
nick = irclib.nm_to_n(event.source())
userhost = irclib.nm_to_uh(event.source())
@ -110,16 +105,15 @@ 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.
start_idx = what.find('[')
subcmd = what[start_idx+1:]
@ -154,17 +148,15 @@ class Module(object):
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
print "looks like someone forgot to implement do!"
class GoogleTranslate(Module):
"""Class that translates text via Google Translate.
http://code.google.com/apis/ajaxlanguage/documentation/
"""
# Class that translates text via Google Translate.
#
# http://code.google.com/apis/ajaxlanguage/documentation/
def __init__(self, config, server, modlist):
super(GoogleTranslate, self).__init__(config, server, modlist)
@ -210,8 +202,7 @@ class GoogleTranslate(Module):
connection.privmsg(replypath, translation)
class Countdown(Module):
"""Class that adds a countdown item to the bot
"""
# Class that adds a countdown item to the bot
def __init__(self, config, server, modlist):
super(Countdown, self).__init__(config, server, modlist)
@ -286,8 +277,7 @@ class Countdown(Module):
except NoOptionError: pass
class Dice(Module):
"""Rolls dice, for RPGs mostly
"""
# Rolls dice, for RPGs mostly
def __init__(self, config, server, modlist):
super(Dice, self).__init__(config, server, modlist)
@ -367,9 +357,8 @@ class Dice(Module):
connection.privmsg(replypath, result)
class Seen(Module):
"""Keeps track of when people say things in public channels, and reports on when
they last said something.
"""
# Keeps track of when people say things in public channels, and reports on when
# they last said something.
def __init__(self, config, server, modlist):
super(Seen, self).__init__(config, server, modlist)
@ -406,8 +395,7 @@ class Seen(Module):
except NoOptionError: pass
class IrcAdmin(Module):
"""all kinds of miscellaneous irc stuff
"""
# All kinds of miscellaneous irc stuff
def __init__(self, config, server, modlist):
super(IrcAdmin, self).__init__(config, server, modlist)
@ -536,8 +524,7 @@ class IrcAdmin(Module):
connection.privmsg(replypath, replystr)
class FactFile(Module):
"""Returns a random fact/quote/whatever from one or more files
"""
# Returns a random fact/quote/whatever from one or more files
def __init__(self, config, server, modlist):
super(FactFile, self).__init__(config, server, modlist)
@ -574,7 +561,7 @@ class FactFile(Module):
class DrBotServerConnection(irclib.ServerConnection):
def privmsg(self, target, text):
"""Send a PRIVMSG command."""
# Send a PRIVMSG command.
# TODO: length limiting or splitting
self.send_raw("PRIVMSG %s :%s" % (target, text))
@ -584,10 +571,6 @@ class DrBotIRC(irclib.IRC):
self.connections.append(c)
return c
#####
# init
#####
# read config file
config = ConfigParser({'debug': 'false'})