Added docstrings.

This commit is contained in:
Jeremy Fincher 2003-10-04 12:17:31 +00:00
parent b9d265c102
commit 9053310ef1
1 changed files with 43 additions and 0 deletions

View File

@ -57,6 +57,7 @@ import ircutils
class IrcCommandDispatcher(object): class IrcCommandDispatcher(object):
"""Base class for classes that must dispatch on a command.""" """Base class for classes that must dispatch on a command."""
def dispatchCommand(self, command): def dispatchCommand(self, command):
"""Given a string 'command', dispatches to doCommand."""
return getattr(self, 'do' + command.capitalize(), None) return getattr(self, 'do' + command.capitalize(), None)
@ -67,17 +68,33 @@ class IrcCallback(IrcCommandDispatcher):
"doCommand" -- doPrivmsg, doNick, do433, etc. These will be called "doCommand" -- doPrivmsg, doNick, do433, etc. These will be called
on matching messages. on matching messages.
""" """
# priority determines the order in which callbacks are called. Lower
# numbers mean *higher* priority (like nice values in *nix). Higher
# priority means the callback is called *earlier* on the inFilter chain,
# *earlier* on the __call__ chain, and *later* on the outFilter chain.
priority = 99 priority = 99
def name(self): def name(self):
"""Returns the name of the callback."""
return self.__class__.__name__ return self.__class__.__name__
def inFilter(self, irc, msg): def inFilter(self, irc, msg):
"""Used for filtering/modifying messages as they're entering.
ircmsgs.IrcMsg objects are immutable, so this method is expected to
return another ircmsgs.IrcMsg object. Obviously the same IrcMsg
can be returned.
"""
return msg return msg
def outFilter(self, irc, msg): def outFilter(self, irc, msg):
"""Used for filtering/modifying messages as they're leaving.
As with inFilter, an IrcMsg is returned.
"""
return msg return msg
def __call__(self, irc, msg): def __call__(self, irc, msg):
"""Used for handling each message."""
method = self.dispatchCommand(msg.command) method = self.dispatchCommand(msg.command)
if method is not None: if method is not None:
try: try:
@ -89,9 +106,11 @@ class IrcCallback(IrcCommandDispatcher):
debug.msg(s) debug.msg(s)
def reset(self): def reset(self):
"""Resets the callback. Called when reconnected to the server."""
pass pass
def die(self): def die(self):
"""Makes the callback die. Called when the parent Irc object dies."""
pass pass
### ###
@ -107,18 +126,25 @@ class IrcMsgQueue(object):
than it originally was. A method to "score" methods, and a heapq to than it originally was. A method to "score" methods, and a heapq to
maintain a priority queue of the messages would be the ideal way to do maintain a priority queue of the messages would be the ideal way to do
intelligent queueing. intelligent queueing.
As it stands, however, we simple keep track of 'high priority' messages,
'low priority' messages, and normal messages, and just make sure to return
the 'high priority' ones before the normal ones before the 'low priority'
ones.
""" """
__slots__ = ('msgs', 'highpriority', 'normal', 'lowpriority') __slots__ = ('msgs', 'highpriority', 'normal', 'lowpriority')
def __init__(self): def __init__(self):
self.reset() self.reset()
def reset(self): def reset(self):
"""Clears the queue."""
self.highpriority = queue() self.highpriority = queue()
self.normal = queue() self.normal = queue()
self.lowpriority = queue() self.lowpriority = queue()
self.msgs = sets.Set() self.msgs = sets.Set()
def enqueue(self, msg): def enqueue(self, msg):
"""Enqueues a given message."""
if msg in self.msgs: if msg in self.msgs:
if not world.startup: if not world.startup:
debug.msg('Not adding msg %s to queue' % msg, 'normal') debug.msg('Not adding msg %s to queue' % msg, 'normal')
@ -132,6 +158,7 @@ class IrcMsgQueue(object):
self.normal.enqueue(msg) self.normal.enqueue(msg)
def dequeue(self): def dequeue(self):
"""Dequeues a given message."""
if self.highpriority: if self.highpriority:
msg = self.highpriority.dequeue() msg = self.highpriority.dequeue()
elif self.normal: elif self.normal:
@ -162,6 +189,7 @@ class ChannelState(object):
self.voices = ircutils.IrcSet() # sets.Set() self.voices = ircutils.IrcSet() # sets.Set()
def addUser(self, user): def addUser(self, user):
"Adds a given user to the ChannelState. Power prefixes are handled."
nick = user.lstrip('@%+') nick = user.lstrip('@%+')
while user and user[0] in '@%+': while user and user[0] in '@%+':
(marker, user) = (user[0], user[1:]) (marker, user) = (user[0], user[1:])
@ -174,6 +202,7 @@ class ChannelState(object):
self.users.add(nick) self.users.add(nick)
def replaceUser(self, oldNick, newNick): def replaceUser(self, oldNick, newNick):
"""Changes the user oldNick to newNick; used for NICK changes."""
# Note that this doesn't have to have the sigil (@%+) that users # Note that this doesn't have to have the sigil (@%+) that users
# have to have for addUser; it just changes the name of the user # have to have for addUser; it just changes the name of the user
# without changing any of his categories. # without changing any of his categories.
@ -183,6 +212,7 @@ class ChannelState(object):
s.add(newNick) s.add(newNick)
def removeUser(self, user): def removeUser(self, user):
"""Removes a given user from the channel."""
self.users.discard(user) self.users.discard(user)
self.ops.discard(user) self.ops.discard(user)
self.halfops.discard(user) self.halfops.discard(user)
@ -214,6 +244,7 @@ class IrcState(IrcCommandDispatcher):
self.reset() self.reset()
def reset(self): def reset(self):
"""Resets the state to normal, unconnected state."""
self.history.reset() self.history.reset()
self.channels = ircutils.IrcDict() self.channels = ircutils.IrcDict()
self.nicksToHostmasks = ircutils.IrcDict() self.nicksToHostmasks = ircutils.IrcDict()
@ -242,6 +273,7 @@ class IrcState(IrcCommandDispatcher):
return ret return ret
def addMsg(self, irc, msg): def addMsg(self, irc, msg):
"""Updates the state based on the irc object and the message."""
self.history.append(msg) self.history.append(msg)
if ircutils.isUserHostmask(msg.prefix) and not msg.command == 'NICK': if ircutils.isUserHostmask(msg.prefix) and not msg.command == 'NICK':
self.nicksToHostmasks[msg.nick] = msg.prefix self.nicksToHostmasks[msg.nick] = msg.prefix
@ -250,9 +282,11 @@ class IrcState(IrcCommandDispatcher):
method(irc, msg) method(irc, msg)
def getTopic(self, channel): def getTopic(self, channel):
"""Returns the topic for a given channel."""
return self.channels[channel].topic return self.channels[channel].topic
def nickToHostmask(self, nick): def nickToHostmask(self, nick):
"""Returns the hostmask for a given nick."""
return self.nicksToHostmasks[nick] return self.nicksToHostmasks[nick]
def do352(self, irc, msg): def do352(self, irc, msg):
@ -377,6 +411,7 @@ class Irc(object):
self.queue.enqueue(ircmsgs.user(self.ident, self.user)) self.queue.enqueue(ircmsgs.user(self.ident, self.user))
def reset(self): def reset(self):
"""Resets the Irc object. Useful for handling reconnects."""
self._nickmods = copy.copy(conf.nickmods) self._nickmods = copy.copy(conf.nickmods)
self.state.reset() self.state.reset()
self.queue.reset() self.queue.reset()
@ -391,10 +426,12 @@ class Irc(object):
callback.reset() callback.reset()
def addCallback(self, callback): def addCallback(self, callback):
"""Adds a callback to the callbacks list."""
self.callbacks.append(callback) self.callbacks.append(callback)
utils.sortBy(lambda cb: cb.priority, self.callbacks) utils.sortBy(lambda cb: cb.priority, self.callbacks)
def getCallback(self, name): def getCallback(self, name):
"""Gets a given callback by name."""
name = name.lower() name = name.lower()
for callback in self.callbacks: for callback in self.callbacks:
if callback.name().lower() == name: if callback.name().lower() == name:
@ -403,17 +440,21 @@ class Irc(object):
return None return None
def removeCallback(self, name): def removeCallback(self, name):
"""Removes a callback from the callback list."""
(bad, good) = partition(lambda cb: cb.name() == name, self.callbacks) (bad, good) = partition(lambda cb: cb.name() == name, self.callbacks)
self.callbacks[:] = good self.callbacks[:] = good
return bad return bad
def queueMsg(self, msg): def queueMsg(self, msg):
"""Queues a message to be sent to the server."""
self.queue.enqueue(msg) self.queue.enqueue(msg)
def sendMsg(self, msg): def sendMsg(self, msg):
"""Queues a message to be sent to the server *immediately*"""
self.fastqueue.enqueue(msg) self.fastqueue.enqueue(msg)
def takeMsg(self): def takeMsg(self):
"""Called by the IrcDriver; takes a message to be sent."""
now = time.time() now = time.time()
msg = None msg = None
if self.fastqueue: if self.fastqueue:
@ -467,6 +508,7 @@ class Irc(object):
return None return None
def feedMsg(self, msg): def feedMsg(self, msg):
"""Called by the IrcDriver; feeds a message received."""
debug.msg('%s %s'%(time.strftime(conf.logTimestampFormat), msg),'low') debug.msg('%s %s'%(time.strftime(conf.logTimestampFormat), msg),'low')
# Yeah, so this is odd. Some networks (oftc) seem to give us certain # Yeah, so this is odd. Some networks (oftc) seem to give us certain
# messages with our nick instead of our prefix. We'll fix that here. # messages with our nick instead of our prefix. We'll fix that here.
@ -536,6 +578,7 @@ class Irc(object):
debug.recoverableException() debug.recoverableException()
def die(self): def die(self):
"""Makes the Irc object die. Dead."""
for callback in self.callbacks: for callback in self.callbacks:
callback.die() callback.die()
if self.driver is not None: if self.driver is not None: