comment nitpicking and restyling
This commit is contained in:
parent
48498898bf
commit
91e535978f
81
Module.py
81
Module.py
|
@ -17,19 +17,20 @@
|
||||||
from irclib import irclib
|
from irclib import irclib
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
# Base class used for creating classes that have real functionality.
|
||||||
|
|
||||||
class Module(object):
|
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):
|
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.config = config
|
||||||
self.modlist = modlist
|
self.modlist = modlist
|
||||||
self.register_handlers(server)
|
self.register_handlers(server)
|
||||||
|
@ -40,23 +41,23 @@ class Module(object):
|
||||||
self.botircname = config.get('IRC', 'name')
|
self.botircname = config.get('IRC', 'name')
|
||||||
self.rehash = rehash # Is there another way to call the rehash function in dr.botzo?
|
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
|
||||||
# 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,
|
||||||
# inheriting from Module should implement this and set up the appropriate handlers,
|
# e.g.:
|
||||||
# e.g.:
|
#
|
||||||
#
|
# server.add_global_handler('privmsg', self.on_privmsg)
|
||||||
# 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
|
||||||
# Module.on_pubmsg and Module.on_privmsg are defined so far, the rest, you're on your
|
# own.
|
||||||
# own.
|
|
||||||
|
|
||||||
|
def register_handlers(self, server):
|
||||||
print "looks like someone forgot to implement register_handlers!"
|
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,
|
||||||
# Does some variable setup and initial sanity checking before calling Module.do,
|
# which should be implemented by subclasses and what can be ultimately responsible
|
||||||
# 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.
|
||||||
# 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())
|
nick = irclib.nm_to_n(event.source())
|
||||||
userhost = irclib.nm_to_uh(event.source())
|
userhost = irclib.nm_to_uh(event.source())
|
||||||
replypath = event.target()
|
replypath = event.target()
|
||||||
|
@ -83,11 +84,11 @@ class Module(object):
|
||||||
|
|
||||||
self.do(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
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,
|
||||||
# Does some variable setup and initial sanity checking before calling Module.do,
|
# which should be implemented by subclasses and what can be ultimately responsible
|
||||||
# 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.
|
||||||
# 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())
|
nick = irclib.nm_to_n(event.source())
|
||||||
userhost = irclib.nm_to_uh(event.source())
|
userhost = irclib.nm_to_uh(event.source())
|
||||||
replypath = nick
|
replypath = nick
|
||||||
|
@ -105,17 +106,17 @@ class Module(object):
|
||||||
|
|
||||||
self.do(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
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
|
||||||
# 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
|
||||||
# that we should do what is basically a text replacement on. The intent is to
|
# allow things like the following:
|
||||||
# allow things like the following:
|
#
|
||||||
#
|
# command arg1 [anothercommand arg1 arg2]
|
||||||
# command arg1 [anothercommand arg1 arg2]
|
#
|
||||||
#
|
# where the output of anothercommand is command's arg2..n. It's mostly for
|
||||||
# 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
|
||||||
# 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.
|
||||||
# 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('[')
|
start_idx = what.find('[')
|
||||||
subcmd = what[start_idx+1:]
|
subcmd = what[start_idx+1:]
|
||||||
end_idx = subcmd.rfind(']')
|
end_idx = subcmd.rfind(']')
|
||||||
|
@ -148,10 +149,10 @@ class Module(object):
|
||||||
else:
|
else:
|
||||||
return attempt
|
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
|
||||||
# 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
|
||||||
# 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!"
|
print "looks like someone forgot to implement do!"
|
||||||
|
|
||||||
# vi:tabstop=4:expandtab:autoindent
|
# vi:tabstop=4:expandtab:autoindent
|
||||||
|
|
|
@ -49,6 +49,7 @@ class DrBotIRC(irclib.IRC):
|
||||||
# This finds all the currently loaded modules that start with "modules" (as all
|
# 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
|
# 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.
|
# reload() on them. This will only work if the folder name doesn't change.
|
||||||
|
|
||||||
def rehash():
|
def rehash():
|
||||||
myre = re.compile('modules')
|
myre = re.compile('modules')
|
||||||
for i in sys.modules:
|
for i in sys.modules:
|
||||||
|
@ -67,12 +68,15 @@ def rehash():
|
||||||
# Create the actual module objects, which will readd the handlers we removed
|
# 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
|
# earlier, and add them to the modObjs list, which we can use during the
|
||||||
# next rehash to remove the handlers.
|
# next rehash to remove the handlers.
|
||||||
|
|
||||||
def reload_modules(moduleList):
|
def reload_modules(moduleList):
|
||||||
for mod in moduleList:
|
for mod in moduleList:
|
||||||
cls = globals()[mod]
|
cls = globals()[mod]
|
||||||
|
|
||||||
# Importing the names imports a module since the file name and class
|
# Importing the names imports a module since the file name and class
|
||||||
# name are the same. Look for the class definition in each module
|
# name are the same. Look for the class definition in each module
|
||||||
# with the same name and create that object.
|
# with the same name and create that object.
|
||||||
|
|
||||||
if inspect.ismodule(cls):
|
if inspect.ismodule(cls):
|
||||||
for name, obj in inspect.getmembers(cls):
|
for name, obj in inspect.getmembers(cls):
|
||||||
if inspect.isclass(obj) and re.search(mod, obj.__name__):
|
if inspect.isclass(obj) and re.search(mod, obj.__name__):
|
||||||
|
|
|
@ -18,8 +18,9 @@ from Module import Module
|
||||||
from irclib import irclib
|
from irclib import irclib
|
||||||
from ConfigParser import NoOptionError
|
from ConfigParser import NoOptionError
|
||||||
|
|
||||||
|
# Class that adds a countdown item to the bot
|
||||||
|
|
||||||
class Countdown(Module):
|
class Countdown(Module):
|
||||||
# Class that adds a countdown item to the bot
|
|
||||||
|
|
||||||
def __init__(self, config, server, modlist, rehash):
|
def __init__(self, config, server, modlist, rehash):
|
||||||
super(Countdown, self).__init__(config, server, modlist, rehash)
|
super(Countdown, self).__init__(config, server, modlist, rehash)
|
||||||
|
|
|
@ -19,8 +19,9 @@ from irclib import irclib
|
||||||
import re
|
import re
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
# Rolls dice, for RPGs mostly
|
||||||
|
|
||||||
class Dice(Module):
|
class Dice(Module):
|
||||||
# Rolls dice, for RPGs mostly
|
|
||||||
|
|
||||||
def __init__(self, config, server, modlist, rehash):
|
def __init__(self, config, server, modlist, rehash):
|
||||||
super(Dice, self).__init__(config, server, modlist, rehash)
|
super(Dice, self).__init__(config, server, modlist, rehash)
|
||||||
|
|
|
@ -18,8 +18,9 @@ from Module import Module
|
||||||
from irclib import irclib
|
from irclib import irclib
|
||||||
from ConfigParser import NoOptionError
|
from ConfigParser import NoOptionError
|
||||||
|
|
||||||
|
# Returns a random fact/quote/whatever from one or more files
|
||||||
|
|
||||||
class FactFile(Module):
|
class FactFile(Module):
|
||||||
# Returns a random fact/quote/whatever from one or more files
|
|
||||||
|
|
||||||
def __init__(self, config, server, modlist, rehash):
|
def __init__(self, config, server, modlist, rehash):
|
||||||
super(FactFile, self).__init__(config, server, modlist, rehash)
|
super(FactFile, self).__init__(config, server, modlist, rehash)
|
||||||
|
@ -34,7 +35,7 @@ class FactFile(Module):
|
||||||
try:
|
try:
|
||||||
filename = self.config.get('fact', whats[0])
|
filename = self.config.get('fact', whats[0])
|
||||||
|
|
||||||
# open file
|
# open file
|
||||||
if os.path.isfile(filename):
|
if os.path.isfile(filename):
|
||||||
# http://www.regexprn.com/2008/11/read-random-line-in-large-file-in.html
|
# http://www.regexprn.com/2008/11/read-random-line-in-large-file-in.html
|
||||||
with open(filename, 'r') as file:
|
with open(filename, 'r') as file:
|
||||||
|
|
|
@ -19,10 +19,11 @@ from irclib import irclib
|
||||||
from urllib2 import urlopen
|
from urllib2 import urlopen
|
||||||
from urllib import urlencode
|
from urllib import urlencode
|
||||||
|
|
||||||
|
# Class that translates text via Google Translate.
|
||||||
|
#
|
||||||
|
# http://code.google.com/apis/ajaxlanguage/documentation/
|
||||||
|
|
||||||
class GoogleTranslate(Module):
|
class GoogleTranslate(Module):
|
||||||
# Class that translates text via Google Translate.
|
|
||||||
#
|
|
||||||
# http://code.google.com/apis/ajaxlanguage/documentation/
|
|
||||||
|
|
||||||
def __init__(self, config, server, modlist, rehash):
|
def __init__(self, config, server, modlist, rehash):
|
||||||
super(GoogleTranslate, self).__init__(config, server, modlist, rehash)
|
super(GoogleTranslate, self).__init__(config, server, modlist, rehash)
|
||||||
|
|
|
@ -19,8 +19,9 @@ from Module import Module
|
||||||
from irclib import irclib
|
from irclib import irclib
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
# All kinds of miscellaneous irc stuff
|
||||||
|
|
||||||
class IrcAdmin(Module):
|
class IrcAdmin(Module):
|
||||||
# All kinds of miscellaneous irc stuff
|
|
||||||
|
|
||||||
def __init__(self, config, server, modlist, rehash):
|
def __init__(self, config, server, modlist, rehash):
|
||||||
super(IrcAdmin, self).__init__(config, server, modlist, rehash)
|
super(IrcAdmin, self).__init__(config, server, modlist, rehash)
|
||||||
|
|
|
@ -20,9 +20,10 @@ from ConfigParser import NoOptionError
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from dateutil.tz import *
|
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):
|
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):
|
def __init__(self, config, server, modlist, rehash):
|
||||||
super(Seen, self).__init__(config, server, modlist, rehash)
|
super(Seen, self).__init__(config, server, modlist, rehash)
|
||||||
|
|
Loading…
Reference in New Issue