Added lots of docstrings.

This commit is contained in:
Jeremy Fincher 2003-10-04 11:34:44 +00:00
parent bc195a6b1e
commit 8d620207ea

View File

@ -41,7 +41,6 @@ how to use them.
from fix import * from fix import *
import re import re
import new
import sets import sets
import time import time
import shlex import shlex
@ -127,19 +126,22 @@ class RateLimiter:
self.unlimited = [] self.unlimited = []
def get(self): def get(self):
"""Returns the next un-ratelimited message, or the next rate-limited
message whose time has come up."""
if self.unlimited: if self.unlimited:
return self.unlimited.pop(0) return self.unlimited.pop(0)
elif self.limited: elif self.limited:
for i in range(len(self.limited)): for i in range(len(self.limited)):
msg = self.limited[i] msg = self.limited[i]
if not self.limit(msg, penalize=False): if not self._limit(msg, penalize=False):
return self.limited.pop(i) return self.limited.pop(i)
return None return None
else: else:
return None return None
def put(self, msg): def put(self, msg):
t = self.limit(msg) """Puts a message in for possible ratelimiting."""
t = self._limit(msg)
if t and not world.testing: if t and not world.testing:
s = 'Limiting message from %s for %s seconds' % (msg.prefix, t) s = 'Limiting message from %s for %s seconds' % (msg.prefix, t)
debug.msg(s, 'normal') debug.msg(s, 'normal')
@ -147,7 +149,7 @@ class RateLimiter:
else: else:
self.unlimited.append(msg) self.unlimited.append(msg)
def limit(self, msg, penalize=True): def _limit(self, msg, penalize=True):
if msg.prefix and ircutils.isUserHostmask(msg.prefix): if msg.prefix and ircutils.isUserHostmask(msg.prefix):
(nick, user, host) = ircutils.splitHostmask(msg.prefix) (nick, user, host) = ircutils.splitHostmask(msg.prefix)
key = '@'.join((user, host)) key = '@'.join((user, host))
@ -205,7 +207,7 @@ class Tokenizer:
# Add a '|' to tokens to have the pipe syntax. # Add a '|' to tokens to have the pipe syntax.
self.validChars = self.validChars.translate(string.ascii, tokens) self.validChars = self.validChars.translate(string.ascii, tokens)
def handleToken(self, token): def _handleToken(self, token):
while token and token[0] == '"' and token[-1] == token[0]: while token and token[0] == '"' and token[-1] == token[0]:
if len(token) > 1: if len(token) > 1:
token = token[1:-1].decode('string_escape') # 2.3+ token = token[1:-1].decode('string_escape') # 2.3+
@ -213,7 +215,7 @@ class Tokenizer:
break break
return token return token
def insideBrackets(self, lexer): def _insideBrackets(self, lexer):
ret = [] ret = []
while True: while True:
token = lexer.get_token() token = lexer.get_token()
@ -222,9 +224,9 @@ class Tokenizer:
elif token == ']': elif token == ']':
return ret return ret
elif token == '[': elif token == '[':
ret.append(self.insideBrackets(lexer)) ret.append(self._insideBrackets(lexer))
else: else:
ret.append(self.handleToken(token)) ret.append(self._handleToken(token))
return ret return ret
def tokenize(self, s): def tokenize(self, s):
@ -245,11 +247,11 @@ class Tokenizer:
ends.append(args) ends.append(args)
args = [] args = []
elif token == '[': elif token == '[':
args.append(self.insideBrackets(lexer)) args.append(self._insideBrackets(lexer))
elif token == ']': elif token == ']':
raise SyntaxError, 'Spurious "["' raise SyntaxError, 'Spurious "["'
else: else:
args.append(self.handleToken(token)) args.append(self._handleToken(token))
if ends: if ends:
if not args: if not args:
raise SyntaxError, '"|" with nothing following' raise SyntaxError, '"|" with nothing following'
@ -273,6 +275,7 @@ def tokenize(s):
return args return args
def getCommands(tokens): def getCommands(tokens):
"""Given tokens as output by tokenize, returns the command names."""
L = [] L = []
if tokens and isinstance(tokens, list): if tokens and isinstance(tokens, list):
L.append(tokens[0]) L.append(tokens[0])
@ -309,10 +312,6 @@ class IrcObjectProxy:
world.commandsProcessed += 1 world.commandsProcessed += 1
self.evalArgs() self.evalArgs()
def findCallback(self, commandName):
# Mostly for backwards compatibility now.
return findCallbackForCommand(self.irc, commandName)
def evalArgs(self): def evalArgs(self):
while self.counter < len(self.args): while self.counter < len(self.args):
if type(self.args[self.counter]) == str: if type(self.args[self.counter]) == str:
@ -328,7 +327,7 @@ class IrcObjectProxy:
self.finalEvaled = True self.finalEvaled = True
originalName = self.args.pop(0) originalName = self.args.pop(0)
name = canonicalName(originalName) name = canonicalName(originalName)
cb = self.findCallback(name) cb = findCallbackForCommand(self, name)
try: try:
if cb is not None: if cb is not None:
anticap = ircdb.makeAntiCapability(name) anticap = ircdb.makeAntiCapability(name)
@ -381,6 +380,16 @@ class IrcObjectProxy:
def reply(self, msg, s, noLengthCheck=False, prefixName=True, def reply(self, msg, s, noLengthCheck=False, prefixName=True,
action=False, private=False): action=False, private=False):
"""reply(msg, text) -> replies to msg with text
Keyword arguments:
noLengthCheck=False: True if the length shouldn't be checked
(used for 'more' handling)
prefixName=True: False if the nick shouldn't be prefixed to the
reply.
action=False: True if the reply should be an action.
private=False: True if the reply should be in private.
"""
# These use |= or &= based on whether or not they default to True or # These use |= or &= based on whether or not they default to True or
# False. Those that default to True use &=; those that default to # False. Those that default to True use &=; those that default to
# False use |=. # False use |=.
@ -435,6 +444,11 @@ class IrcObjectProxy:
self.evalArgs() self.evalArgs()
def error(self, msg, s, private=False): def error(self, msg, s, private=False):
"""error(msg, text) -> replies to msg with an error message of text.
Keyword arguments:
private=False: True if the error should be given in private.
"""
if isinstance(self.irc, self.__class__): if isinstance(self.irc, self.__class__):
self.irc.error(msg, s, private) self.irc.error(msg, s, private)
else: else:
@ -445,18 +459,19 @@ class IrcObjectProxy:
self.irc.queueMsg(reply(msg, s)) self.irc.queueMsg(reply(msg, s))
def killProxy(self): def killProxy(self):
"""Kills this proxy object and all its parents."""
if not isinstance(self.irc, irclib.Irc): if not isinstance(self.irc, irclib.Irc):
self.irc.killProxy() self.irc.killProxy()
self.__dict__ = {} self.__dict__ = {}
def getRealIrc(self): def getRealIrc(self):
"""Returns the real irclib.Irc object underlying this proxy chain."""
if isinstance(self.irc, irclib.Irc): if isinstance(self.irc, irclib.Irc):
return self.irc return self.irc
else: else:
return self.irc.getRealIrc() return self.irc.getRealIrc()
def __getattr__(self, attr): def __getattr__(self, attr):
#return getattr(self.getRealIrc(), attr)
return getattr(self.irc, attr) return getattr(self.irc, attr)
@ -515,8 +530,6 @@ class ConfigIrcProxy(object):
def error(self, msg, s, *args): def error(self, msg, s, *args):
debug.msg('ConfigIrcProxy saw an error: %s' % s, 'normal') debug.msg('ConfigIrcProxy saw an error: %s' % s, 'normal')
findCallback = IrcObjectProxy.findCallback.im_func
def getRealIrc(self): def getRealIrc(self):
irc = self.__dict__['irc'] irc = self.__dict__['irc']
while(hasattr(irc, 'getRealIrc')): while(hasattr(irc, 'getRealIrc')):