Super, and subclass SupyThread.

This commit is contained in:
Jeremy Fincher 2004-09-30 04:14:44 +00:00
parent ec9258a371
commit 75ef036569

View File

@ -61,7 +61,7 @@ import supybot.structures as structures
def thread(f): def thread(f):
"""Makes sure a command spawns a thread when called.""" """Makes sure a command spawns a thread when called."""
def newf(self, irc, msg, args, *L, **kwargs): def newf(self, irc, msg, args, *L, **kwargs):
if threading.currentThread() is world.mainThread: if world.isMainThread():
t = callbacks.CommandThread(target=irc._callCommand, t = callbacks.CommandThread(target=irc._callCommand,
args=(f.func_name, self), args=(f.func_name, self),
kwargs=kwargs) kwargs=kwargs)
@ -70,13 +70,12 @@ def thread(f):
f(self, irc, msg, args, *L, **kwargs) f(self, irc, msg, args, *L, **kwargs)
return utils.changeFunctionName(newf, f.func_name, f.__doc__) return utils.changeFunctionName(newf, f.func_name, f.__doc__)
class UrlSnarfThread(threading.Thread): class UrlSnarfThread(world.SupyThread):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
assert 'url' in kwargs assert 'url' in kwargs
kwargs['name'] = 'Thread #%s (for snarfing %s)' % \ kwargs['name'] = 'Thread #%s (for snarfing %s)' % \
(world.threadsSpawned, kwargs.pop('url')) (world.threadsSpawned, kwargs.pop('url'))
world.threadsSpawned += 1 super(UrlSnarfThread, self).__init__(*args, **kwargs)
threading.Thread.__init__(self, *args, **kwargs)
self.setDaemon(True) self.setDaemon(True)
class SnarfQueue(ircutils.FloodQueue): class SnarfQueue(ircutils.FloodQueue):
@ -152,7 +151,7 @@ decorators = ircutils.IrcDict({
def _int(s): def _int(s):
return int(float(s)) return int(float(s))
def getInt(irc, msg, args, state, default=None, type='integer', p=None): def getInt(irc, msg, args, state, type='integer', p=None):
try: try:
i = _int(args[0]) i = _int(args[0])
if p is not None: if p is not None:
@ -161,9 +160,6 @@ def getInt(irc, msg, args, state, default=None, type='integer', p=None):
state.args.append(_int(args[0])) state.args.append(_int(args[0]))
del args[0] del args[0]
except ValueError: except ValueError:
if default is not None:
state.args.append(default)
else:
irc.errorInvalid(type, args[0]) irc.errorInvalid(type, args[0])
def getPositiveInt(irc, msg, args, state, *L): def getPositiveInt(irc, msg, args, state, *L):
@ -177,7 +173,7 @@ def getNonNegativeInt(irc, msg, args, state, *L):
def getId(irc, msg, args, state): def getId(irc, msg, args, state):
getInt(irc, msg, args, state, type='id') getInt(irc, msg, args, state, type='id')
def getExpiry(irc, msg, args, state, default=None): def getExpiry(irc, msg, args, state):
now = int(time.time()) now = int(time.time())
try: try:
expires = _int(args[0]) expires = _int(args[0])
@ -186,30 +182,13 @@ def getExpiry(irc, msg, args, state, default=None):
state.args.append(expires) state.args.append(expires)
del args[0] del args[0]
except ValueError: except ValueError:
if default is not None:
if default:
default += now
state.args.append(default)
else:
irc.errorInvalid('number of seconds', args[0]) irc.errorInvalid('number of seconds', args[0])
# XXX This should be handled elsewhere; perhaps all optional args should
# consider their first extra arg to be a default.
except IndexError:
if default is not None:
if default:
default += now
state.args.append(default)
else:
raise
def getBoolean(irc, msg, args, state, default=None): def getBoolean(irc, msg, args, state):
try: try:
state.args.append(utils.toBool(args[0])) state.args.append(utils.toBool(args[0]))
del args[0] del args[0]
except ValueError: except ValueError:
if default is not None:
state.args.append(default)
else:
irc.errorInvalid('boolean', args[0]) irc.errorInvalid('boolean', args[0])
def getChannelDb(irc, msg, args, state, **kwargs): def getChannelDb(irc, msg, args, state, **kwargs):
@ -430,6 +409,12 @@ def args(irc,msg,args, types=[], state=None,
enforce = False enforce = False
name = name[:-1] name = name[:-1]
wrapper = wrappers[name] wrapper = wrappers[name]
if optional and specArgs:
# First arg is default.
default = specArgs[0]
specArgs = specArgs[1:]
if callable(default):
default = default()
try: try:
wrapper(irc, msg, args, state, *specArgs) wrapper(irc, msg, args, state, *specArgs)
except (callbacks.Error, ValueError, callbacks.ArgumentError), e: except (callbacks.Error, ValueError, callbacks.ArgumentError), e:
@ -498,12 +483,13 @@ def wrap(f, *argsArgs, **argsKwargs):
else: else:
f(self, irc, msg, args, *state.args, **state.kwargs) f(self, irc, msg, args, *state.args, **state.kwargs)
newf = utils.changeFunctionName(newf, f.func_name, f.__doc__)
decorators = argsKwargs.pop('decorators', None) decorators = argsKwargs.pop('decorators', None)
if decorators is not None: if decorators is not None:
decorators = map(_decorators.__getitem__, decorators) decorators = map(_decorators.__getitem__, decorators)
for decorator in decorators: for decorator in decorators:
newf = decorator(newf) newf = decorator(newf)
return utils.changeFunctionName(newf, f.func_name, f.__doc__) return newf
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: