Made defaultplugin use the registry.

This commit is contained in:
Jeremy Fincher 2004-01-25 08:22:50 +00:00
parent 977d7a2279
commit b94a7a3b4e
6 changed files with 51 additions and 71 deletions

View File

@ -223,16 +223,25 @@ if __name__ == '__main__':
registry.open(registryFilename)
except registry.InvalidRegistryFile, e:
sys.stderr.write(str(e))
sys.stderr.write('\n')
sys.stderr.write(os.linesep)
sys.exit(-1)
except EnvironmentError, e:
sys.stderr.write(str(e))
sys.stderr.write(os.linesep)
sys.exit(-1)
import log
import conf
import world
def closeRegistry():
log.info('Writing registry file to %s', registryFilename)
# We only print if world.dying so we don't see these messages during
# upkeep.
if world.dying:
log.info('Writing registry file to %s', registryFilename)
registry.close(conf.supybot, registryFilename, annotated=True)
log.info('Finished writing registry file.')
if world.dying:
log.info('Finished writing registry file.')
world.flushers.append(closeRegistry)
nick = options.nick or conf.supybot.nick()

View File

@ -653,7 +653,7 @@ def main():
# Done!
output("""All done! Your new bot configuration is %s. If you're running
a *nix based OS, you can probably start your bot with the command line
"supybot ./%s". If you're not running a *nix or similar machine, you'll
"supybot %s". If you're not running a *nix or similar machine, you'll
just have to start it like you start all your other Python scripts.""" % \
(filename, filename))

View File

@ -59,7 +59,7 @@ def getWrapper(name):
parts.pop(0)
while parts:
try:
group = getattr(group, parts.pop(0))
group = group.get(parts.pop(0))
except registry.NonExistentRegistryEntry:
raise InvalidRegistryName, name
return group
@ -144,15 +144,18 @@ class Config(callbacks.Privmsg):
wrapper = getWrapper(name)
irc.reply(wrapper.default)
def flush(self, irc, msg, args):
"""<filename>
def reload(self, irc, msg, args):
"""takes no arguments
Flushes the current registry to the file given.
Reloads the various configuration files (user database, channel
database, registry, etc.).
"""
filename = privmsgs.getArgs(args)
registry.close(conf.supybot, filename)
# TODO: Reload registry.
ircdb.users.reload()
ircdb.channels.reload()
irc.replySuccess()
flush = privmsgs.checkCapability(flush, 'owner')
reload = privmsgs.checkCapability(reload, 'owner')
Class = Config

View File

@ -94,6 +94,24 @@ def loadPluginClass(irc, module):
assert not irc.getCallback(callback.name())
irc.addCallback(callback)
conf.registerGroup(conf.supybot, 'commands')
conf.registerGroup(conf.supybot.commands, 'defaultPlugins',
registry.GroupWithDefault(registry.String('', """
Determines what commands have default plugins set, and which
plugins are set to be the default for each of those
commands.""")))
conf.registerGlobalValue(conf.supybot.commands.defaultPlugins,
'list', registry.String('Misc', ''))
conf.registerGlobalValue(conf.supybot.commands.defaultPlugins,
'help', registry.String('Misc', ''))
conf.registerGlobalValue(conf.supybot.commands.defaultPlugins,
'capabilities', registry.String('User', ''))
conf.registerGlobalValue(conf.supybot.commands.defaultPlugins,
'addcapability', registry.String('Admin', ''))
conf.registerGlobalValue(conf.supybot.commands.defaultPlugins,
'removecapability', registry.String('Admin', ''))
conf.registerGlobalValue(conf.supybot.commands.defaultPlugins,
'reload', registry.String('Misc', ''))
class Owner(privmsgs.CapabilityCheckingPrivmsg):
# This plugin must be first; its priority must be lowest; otherwise odd
@ -104,11 +122,6 @@ class Owner(privmsgs.CapabilityCheckingPrivmsg):
def __init__(self):
callbacks.Privmsg.__init__(self)
setattr(self.__class__, 'exec', self.__class__._exec)
self.defaultPlugins = {'list': 'Misc',
'help': 'Misc',
'capabilities': 'User',
'addcapability': 'Admin',
'removecapability': 'Admin',}
for (name, s) in registry._cache.iteritems():
if name.startswith('supybot.plugins'):
try:
@ -142,9 +155,13 @@ class Owner(privmsgs.CapabilityCheckingPrivmsg):
ambiguousCommands = {}
if tokens:
command = callbacks.canonicalName(tokens[0])
if command in self.defaultPlugins:
tokens.insert(0, self.defaultPlugins[command])
else:
try:
plugin = conf.supybot.commands.defaultPlugins.get(command)()
if plugin:
tokens.insert(0, plugin)
else:
raise registry.NonExistentRegistryEntry
except registry.NonExistentRegistryEntry:
cbs = callbacks.findCallbackForCommand(irc, command)
if len(cbs) > 1:
names = [cb.name() for cb in cbs]
@ -200,44 +217,6 @@ class Owner(privmsgs.CapabilityCheckingPrivmsg):
else:
callbacks.IrcObjectProxy(irc, msg, tokens)
def defaultplugin(self, irc, msg, args):
"""<command> [<plugin>] [--remove]
Calls <command> from <plugin> by default, rather than complaining about
multiple plugins providing it. If <plugin> is not provided, shows the
current default plugin for <command>. If --remove is given, removes
the current default plugin for <command>.
"""
if '--remove' in args:
while '--remove' in args:
args.remove('--remove')
remove = True
else:
remove = False
(command, plugin) = privmsgs.getArgs(args, optional=1)
command = callbacks.canonicalName(command)
cbs = callbacks.findCallbackForCommand(irc, command)
if not cbs:
irc.error('That\'t not a valid command.')
return
if plugin:
self.defaultPlugins[command] = plugin
else:
try:
if remove:
del self.defaultPlugins[command]
else:
L = [command]
d = self.disambiguate(irc, L)
if d:
raise KeyError
assert len(L) == 2, 'Not disambiguated!'
irc.reply(L[0])
except KeyError:
irc.error('I have no default plugin for that command.')
return
irc.replySuccess()
if conf.allowEval:
def eval(self, irc, msg, args):
"""<expression>
@ -402,16 +381,6 @@ class Owner(privmsgs.CapabilityCheckingPrivmsg):
else:
irc.error('There was no callback %s' % name)
def reconf(self, irc, msg, args):
"""takes no arguments
Reloads the configuration files for the user and channel databases:
conf/users.conf and conf/channels.conf, by default.
"""
ircdb.users.reload()
ircdb.channels.reload()
irc.replySuccess()
Class = Owner

View File

@ -71,8 +71,8 @@ def registerChannelValue(group, name, value):
def registerGlobalValue(group, name, value):
group.register(name, value)
def registerGroup(group, name):
group.registerGroup(name)
def registerGroup(group, name, Group=None):
group.registerGroup(name, Group)
class ValidNick(registry.String):
def setValue(self, v):

View File

@ -123,7 +123,6 @@ class Value(object):
# the _cache.
return self.value
class Boolean(Value):
def set(self, s):
s = s.lower()