mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-02-18 14:40:51 +01:00
Updated to use commands.
This commit is contained in:
parent
e720f46a5a
commit
b6a92fc2b6
167
src/Config.py
167
src/Config.py
@ -47,7 +47,6 @@ import supybot.world as world
|
|||||||
import supybot.ircdb as ircdb
|
import supybot.ircdb as ircdb
|
||||||
from supybot.commands import *
|
from supybot.commands import *
|
||||||
import supybot.ircutils as ircutils
|
import supybot.ircutils as ircutils
|
||||||
import supybot.privmsgs as privmsgs
|
|
||||||
import supybot.registry as registry
|
import supybot.registry as registry
|
||||||
import supybot.callbacks as callbacks
|
import supybot.callbacks as callbacks
|
||||||
|
|
||||||
@ -55,9 +54,6 @@ import supybot.callbacks as callbacks
|
|||||||
# Now, to setup the registry.
|
# Now, to setup the registry.
|
||||||
###
|
###
|
||||||
|
|
||||||
#class InvalidRegistryName(callbacks.Error):
|
|
||||||
# pass
|
|
||||||
|
|
||||||
def getWrapper(name):
|
def getWrapper(name):
|
||||||
parts = registry.split(name)
|
parts = registry.split(name)
|
||||||
if not parts or parts[0] not in ('supybot', 'users'):
|
if not parts or parts[0] not in ('supybot', 'users'):
|
||||||
@ -98,29 +94,28 @@ if os.name == 'posix':
|
|||||||
signal.signal(signal.SIGHUP, _hupHandler)
|
signal.signal(signal.SIGHUP, _hupHandler)
|
||||||
|
|
||||||
|
|
||||||
class Config(callbacks.Privmsg):
|
def getConfigVar(irc, msg, args, state):
|
||||||
def callCommand(self, name, irc, msg, *L, **kwargs):
|
name = args[0]
|
||||||
#XXX For some reason, that confuses jamessan, InvalidRegistryName
|
|
||||||
# is not really being caught here, but registry.InvalidRegistryName
|
|
||||||
# is. So, we'll just re-raise registry.InvalidRegsitryName instead
|
|
||||||
# of using Config.InvalidRegistryName
|
|
||||||
try:
|
|
||||||
super(Config, self).callCommand(name, irc, msg, *L, **kwargs)
|
|
||||||
except registry.InvalidRegistryName, e:
|
|
||||||
irc.errorInvalid('configuration variable', str(e))
|
|
||||||
except registry.InvalidRegistryValue, e:
|
|
||||||
irc.error(str(e))
|
|
||||||
|
|
||||||
def _canonicalizeName(self, name):
|
|
||||||
if name.startswith('conf.'):
|
if name.startswith('conf.'):
|
||||||
name = name[5:]
|
name = name[5:]
|
||||||
if not name.startswith('supybot') and not name.startswith('users'):
|
if not name.startswith('supybot') and not name.startswith('users'):
|
||||||
name = 'supybot.' + name
|
name = 'supybot.' + name
|
||||||
return name
|
try:
|
||||||
|
|
||||||
def _list(self, name):
|
|
||||||
name = self._canonicalizeName(name)
|
|
||||||
group = getWrapper(name)
|
group = getWrapper(name)
|
||||||
|
state.args.append(group)
|
||||||
|
del args[0]
|
||||||
|
except registry.InvalidRegistryName, e:
|
||||||
|
irc.errorInvalid('configuration variable', str(e))
|
||||||
|
addConverter('configVar', getConfigVar)
|
||||||
|
|
||||||
|
class Config(callbacks.Privmsg):
|
||||||
|
def callCommand(self, name, irc, msg, *L, **kwargs):
|
||||||
|
try:
|
||||||
|
super(Config, self).callCommand(name, irc, msg, *L, **kwargs)
|
||||||
|
except registry.InvalidRegistryValue, e:
|
||||||
|
irc.error(str(e))
|
||||||
|
|
||||||
|
def _list(self, group):
|
||||||
L = []
|
L = []
|
||||||
for (vname, v) in group._children.iteritems():
|
for (vname, v) in group._children.iteritems():
|
||||||
if hasattr(v, 'channelValue') and v.channelValue:
|
if hasattr(v, 'channelValue') and v.channelValue:
|
||||||
@ -131,7 +126,7 @@ class Config(callbacks.Privmsg):
|
|||||||
utils.sortBy(str.lower, L)
|
utils.sortBy(str.lower, L)
|
||||||
return L
|
return L
|
||||||
|
|
||||||
def list(self, irc, msg, args):
|
def list(self, irc, msg, args, group):
|
||||||
"""<group>
|
"""<group>
|
||||||
|
|
||||||
Returns the configuration variables available under the given
|
Returns the configuration variables available under the given
|
||||||
@ -140,20 +135,18 @@ class Config(callbacks.Privmsg):
|
|||||||
it can be separately configured for each channel using the 'channel'
|
it can be separately configured for each channel using the 'channel'
|
||||||
command in this plugin, it is preceded by an '#' sign.
|
command in this plugin, it is preceded by an '#' sign.
|
||||||
"""
|
"""
|
||||||
name = privmsgs.getArgs(args)
|
L = self._list(group)
|
||||||
L = self._list(name)
|
|
||||||
if L:
|
if L:
|
||||||
irc.reply(utils.commaAndify(L))
|
irc.reply(utils.commaAndify(L))
|
||||||
else:
|
else:
|
||||||
irc.error('There don\'t seem to be any values in %s.' % name)
|
irc.error('There don\'t seem to be any values in %s.' % name)
|
||||||
|
list = wrap(list, ['configVar'])
|
||||||
|
|
||||||
def search(self, irc, msg, args):
|
def search(self, irc, msg, args, word):
|
||||||
"""<word>
|
"""<word>
|
||||||
|
|
||||||
Searches for <word> in the current configuration variables.
|
Searches for <word> in the current configuration variables.
|
||||||
"""
|
"""
|
||||||
word = privmsgs.getArgs(args)
|
|
||||||
word = word.lower()
|
|
||||||
L = []
|
L = []
|
||||||
for (name, _) in conf.supybot.getValues(getChildren=True):
|
for (name, _) in conf.supybot.getValues(getChildren=True):
|
||||||
if word in name.lower():
|
if word in name.lower():
|
||||||
@ -164,90 +157,73 @@ class Config(callbacks.Privmsg):
|
|||||||
irc.reply(utils.commaAndify(L))
|
irc.reply(utils.commaAndify(L))
|
||||||
else:
|
else:
|
||||||
irc.reply('There were no matching configuration variables.')
|
irc.reply('There were no matching configuration variables.')
|
||||||
|
search = wrap(search, ['lowered']) # XXX compose with withoutSpaces?
|
||||||
|
|
||||||
def config(self, irc, msg, args):
|
def _getValue(self, irc, msg, group):
|
||||||
"""<name> [<value>]
|
if hasattr(group, 'value'):
|
||||||
|
if not group._private:
|
||||||
If <value> is given, sets the value of <name> to <value>. Otherwise,
|
irc.reply(str(group) or ' ')
|
||||||
returns the current value of <name>. You may omit the leading
|
|
||||||
"supybot." in the name if you so choose.
|
|
||||||
"""
|
|
||||||
if len(args) >= 2:
|
|
||||||
self._set(irc, msg, args)
|
|
||||||
else:
|
else:
|
||||||
self._get(irc, msg, args)
|
capability = getCapability(group._name)
|
||||||
|
if ircdb.checkCapability(msg.prefix, capability):
|
||||||
|
irc.reply(str(group), private=True)
|
||||||
|
else:
|
||||||
|
irc.errorNoCapability(capability)
|
||||||
|
else:
|
||||||
|
irc.error('That registry variable has no value. Use the list '
|
||||||
|
'command in this plugin to see what variables are '
|
||||||
|
'available in this group.')
|
||||||
|
|
||||||
def channel(self, irc, msg, args):
|
def _setValue(self, irc, msg, group, value):
|
||||||
|
capability = getCapability(group._name)
|
||||||
|
if ircdb.checkCapability(msg.prefix, capability):
|
||||||
|
# I think callCommand catches exceptions here. Should it?
|
||||||
|
group.set(value)
|
||||||
|
irc.replySuccess()
|
||||||
|
else:
|
||||||
|
irc.errorNoCapability(capability)
|
||||||
|
|
||||||
|
def channel(self, irc, msg, args, channel, group, value):
|
||||||
"""[<channel>] <name> [<value>]
|
"""[<channel>] <name> [<value>]
|
||||||
|
|
||||||
If <value> is given, sets the channel configuration variable for <name>
|
If <value> is given, sets the channel configuration variable for <name>
|
||||||
to <value> for <channel>. Otherwise, returns the current channel
|
to <value> for <channel>. Otherwise, returns the current channel
|
||||||
configuration value of <name>. <channel> is only necessary if the
|
configuration value of <name>. <channel> is only necessary if the
|
||||||
message isn't sent in the channel itself."""
|
message isn't sent in the channel itself."""
|
||||||
channel = privmsgs.getChannel(msg, args)
|
if not group.channelValue:
|
||||||
if not args:
|
|
||||||
raise callbacks.ArgumentError
|
|
||||||
args[0] = self._canonicalizeName(args[0])
|
|
||||||
wrapper = getWrapper(args[0])
|
|
||||||
if not wrapper.channelValue:
|
|
||||||
irc.error('That configuration variable is not a channel-specific '
|
irc.error('That configuration variable is not a channel-specific '
|
||||||
'configuration variable.')
|
'configuration variable.')
|
||||||
return
|
return
|
||||||
components = registry.split(args[0])
|
group = group.get(channel)
|
||||||
components.append(channel)
|
if value is not None:
|
||||||
args[0] = registry.join(components)
|
self._setValue(irc, msg, group, value)
|
||||||
self.config(irc, msg, args)
|
else:
|
||||||
|
self._getValue(irc, msg, group)
|
||||||
|
channel = wrap(channel, ['channel', 'configVar', additional('text')])
|
||||||
|
|
||||||
def _get(self, irc, msg, args):
|
def config(self, irc, msg, args, group, value):
|
||||||
"""<name>
|
"""<name> [<value>]
|
||||||
|
|
||||||
Shows the current value of the configuration variable <name>.
|
If <value> is given, sets the value of <name> to <value>. Otherwise,
|
||||||
|
returns the current value of <name>. You may omit the leading
|
||||||
|
"supybot." in the name if you so choose.
|
||||||
"""
|
"""
|
||||||
name = privmsgs.getArgs(args)
|
if value is not None:
|
||||||
name = self._canonicalizeName(name)
|
self._setValue(irc, msg, group, value)
|
||||||
wrapper = getWrapper(name)
|
|
||||||
if hasattr(wrapper, 'value'):
|
|
||||||
if not wrapper._private:
|
|
||||||
irc.reply(str(wrapper) or ' ')
|
|
||||||
else:
|
else:
|
||||||
capability = getCapability(name)
|
self._getValue(irc, msg, group)
|
||||||
if ircdb.checkCapability(msg.prefix, capability):
|
config = wrap(config, ['configVar', additional('text')])
|
||||||
irc.reply(str(wrapper), private=True)
|
|
||||||
else:
|
|
||||||
irc.errorNoCapability(capability)
|
|
||||||
else:
|
|
||||||
irc.error('That registry variable has no value. Use the list '
|
|
||||||
'command in this plugin to see what values are '
|
|
||||||
'available in this group.')
|
|
||||||
|
|
||||||
def _set(self, irc, msg, args):
|
def help(self, irc, msg, args, group):
|
||||||
"""<name> <value>
|
|
||||||
|
|
||||||
Sets the current value of the configuration variable <name> to <value>.
|
|
||||||
"""
|
|
||||||
(name, value) = privmsgs.getArgs(args, required=2)
|
|
||||||
name = self._canonicalizeName(name)
|
|
||||||
capability = getCapability(name)
|
|
||||||
if ircdb.checkCapability(msg.prefix, capability):
|
|
||||||
wrapper = getWrapper(name)
|
|
||||||
wrapper.set(value)
|
|
||||||
irc.replySuccess()
|
|
||||||
else:
|
|
||||||
irc.errorNoCapability(capability)
|
|
||||||
|
|
||||||
def help(self, irc, msg, args):
|
|
||||||
"""<name>
|
"""<name>
|
||||||
|
|
||||||
Returns the description of the configuration variable <name>.
|
Returns the description of the configuration variable <name>.
|
||||||
"""
|
"""
|
||||||
name = privmsgs.getArgs(args)
|
if hasattr(group, '_help'):
|
||||||
name = self._canonicalizeName(name)
|
s = group.help()
|
||||||
wrapper = getWrapper(name)
|
|
||||||
if hasattr(wrapper, 'help'):
|
|
||||||
s = wrapper.help()
|
|
||||||
if s:
|
if s:
|
||||||
if hasattr(wrapper, 'value') and not wrapper._private:
|
if hasattr(group, 'value') and not group._private:
|
||||||
s += ' (Current value: %s)' % wrapper
|
s += ' (Current value: %s)' % group
|
||||||
irc.reply(s)
|
irc.reply(s)
|
||||||
else:
|
else:
|
||||||
irc.reply('That configuration group exists, but seems to have '
|
irc.reply('That configuration group exists, but seems to have '
|
||||||
@ -255,17 +231,16 @@ class Config(callbacks.Privmsg):
|
|||||||
'any children values.')
|
'any children values.')
|
||||||
else:
|
else:
|
||||||
irc.error('%s has no help.' % name)
|
irc.error('%s has no help.' % name)
|
||||||
|
help = wrap(help, ['configVar'])
|
||||||
|
|
||||||
def default(self, irc, msg, args):
|
def default(self, irc, msg, args, group):
|
||||||
"""<name>
|
"""<name>
|
||||||
|
|
||||||
Returns the default value of the configuration variable <name>.
|
Returns the default value of the configuration variable <name>.
|
||||||
"""
|
"""
|
||||||
name = privmsgs.getArgs(args)
|
v = group.__class__(group._default, '')
|
||||||
name = self._canonicalizeName(name)
|
|
||||||
wrapper = getWrapper(name)
|
|
||||||
v = wrapper.__class__(wrapper._default, '')
|
|
||||||
irc.reply(str(v))
|
irc.reply(str(v))
|
||||||
|
default = wrap(default, ['configVar'])
|
||||||
|
|
||||||
def reload(self, irc, msg, args):
|
def reload(self, irc, msg, args):
|
||||||
"""takes no arguments
|
"""takes no arguments
|
||||||
@ -275,7 +250,7 @@ class Config(callbacks.Privmsg):
|
|||||||
"""
|
"""
|
||||||
_reload() # This was factored out for SIGHUP handling.
|
_reload() # This was factored out for SIGHUP handling.
|
||||||
irc.replySuccess()
|
irc.replySuccess()
|
||||||
reload = privmsgs.checkCapability(reload, 'owner')
|
reload = wrap(reload, [('checkCapability', 'owner')])
|
||||||
|
|
||||||
def export(self, irc, msg, args, filename):
|
def export(self, irc, msg, args, filename):
|
||||||
"""<filename>
|
"""<filename>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user