Added tons of new Types and globalConfigurables.

This commit is contained in:
Jeremy Fincher 2004-01-01 20:13:22 +00:00
parent 45af6513d8
commit 34d0cdc79b

View File

@ -62,6 +62,10 @@ class Dictionary(object):
self.types[name] = type self.types[name] = type
self.defaults[name] = default self.defaults[name] = default
def __contains__(self, name):
name = callbacks.canonicalName(name)
return name in self.defaults
def get(self, name, channel=None): def get(self, name, channel=None):
name = callbacks.canonicalName(name) name = callbacks.canonicalName(name)
if channel is not None: if channel is not None:
@ -117,12 +121,50 @@ def StrType(s):
raise Error, 'Value must be a string.' raise Error, 'Value must be a string.'
return v return v
def NoSpacesStrType(s):
try:
s = StrType(s)
if len(s.split(), 1) > 1:
raise Error
return s
except Error:
raise Error, 'Value must be a string with no space characters.'
def RegexpStrType(s):
try:
s = StrType(s)
r = utils.perlReToPythonRe(s)
return r
except ValueError, e:
raise Error, 'Value must be a valid regular expression: %s' % e
except Error:
raise Error, 'Value must be a valid regular expression.'
def RegexpOrNoneStrType(s):
try:
if not s:
return None
else:
return RegexpStrType(s)
except Error:
raise Error, 'Value must be a valid regular expression or the ' \
'empty string, representing nothing.'
def IntType(s): def IntType(s):
try: try:
return int(s) return int(s)
except ValueError: except ValueError:
raise Error, 'Value must be an int.' raise Error, 'Value must be an int.'
def PositiveIntType(s):
try:
i = IntType(s)
if i <= 0:
raise Error
return i
except Error:
raise Error, 'Value must be a positive, non-zero integer.'
class Mixin(object): class Mixin(object):
"""A mixin class to provide a "config" command that can be consistent """A mixin class to provide a "config" command that can be consistent
across all plugins, in order to unify the configuration for each plugin. across all plugins, in order to unify the configuration for each plugin.
@ -143,27 +185,46 @@ class Mixin(object):
for line in fd: for line in fd:
line = line.rstrip() line = line.rstrip()
(channel, name, value) = line.split(',', 2) (channel, name, value) = line.split(',', 2)
if channel == 'default':
channel = None
try: try:
# The eval here is to turn from "'foo'" to 'foo'. # The eval here is to turn from "'foo'" to 'foo'.
self.configurables.set(name, eval(value), channel) value = eval(value)
except Error, e: except Exception, e:
s = 'Couldn\'t read configurable from file: %s' self.log.error('Invalid configurable line: %r', line)
self.log.warning(s, e) continue
try:
if channel == 'global':
try:
self.globalConfigurables.set(name, value)
except AttributeError:
s = 'Attempt to set non-existent global ' \
'configurable %s' % value
self.log.warning(s, name)
else:
if channel == 'default':
channel = None
else:
assert ircutils.isChannel(channel)
self.configurables.set(name, value, channel)
except Error, e: # Type error conversion.
s = 'Couldn\'t type-convert configurable %s: %s'
self.log.warning(s, name, e)
except KeyError, e: except KeyError, e:
s = 'Configurable variable %s doesn\'t exist anymore.' s = 'Configurable variable %s doesn\'t exist anymore.'
self.log.warning(s, name) self.log.warning(s, name)
def die(self): def die(self):
fd = file(self.filename, 'w') fd = file(self.filename, 'w')
L = self.configurables.unparsedValues.items() def flushDictionary(d):
L.sort() L = d.unparsedValues.items()
for ((channel, name), value) in L: L.sort()
if channel is None: for ((channel, name), value) in L:
channel = 'default' if channel is None:
name = self.configurables.originalNames[name] channel = 'default'
fd.write('%s,%s,%r\n' % (channel, name, value)) name = d.originalNames[name]
fd.write('%s,%s,%r\n' % (channel, name, value))
if hasattr(self, 'globalConfigurables'):
flushDictionary(self.globalConfigurables)
flushDictionary(self.configurables)
fd.close() fd.close()
def config(self, irc, msg, args): def config(self, irc, msg, args):
@ -175,37 +236,49 @@ class Mixin(object):
config variables for this plugin. <channel> is only necessary if the config variables for this plugin. <channel> is only necessary if the
message isn't sent in the channel itself. message isn't sent in the channel itself.
""" """
try: if not args: # They want a list of the configurables.
channel = privmsgs.getChannel(msg, args) names = self.configurables.names()
capability = ircdb.makeChannelCapability(channel, 'op') if hasattr(self, 'globalConfigurables'):
except callbacks.ArgumentError: names.extend(self.globalConfigurables.names())
raise names.sort()
except callbacks.Error: irc.reply(msg, utils.commaAndify(names))
channel = None else:
capability = 'admin'
if not ircdb.checkCapability(msg.prefix, capability):
irc.error(msg, conf.replyNoCapability % capability)
return
(name, value) = privmsgs.getArgs(args, required=0, optional=2)
if not name:
irc.reply(msg, utils.commaAndify(self.configurables.names()))
return
try:
if not value:
help = self.configurables.help(name)
value = self.configurables.get(name, channel=channel)
s = '%s: %s (Current value: %r)' % (name, help, value)
irc.reply(msg, s)
return
try: try:
self.configurables.set(name, value, channel) channel = privmsgs.getChannel(msg, args)
irc.reply(msg, conf.replySuccess) capability = ircdb.makeChannelCapability(channel, 'op')
except Error, e: except callbacks.Error:
irc.error(msg, str(e)) channel = None
except KeyError: capability = 'admin'
irc.error(msg, 'There is no config variable %r' % name) (name, value) = privmsgs.getArgs(args, required=1, optional=1)
if name in self.configurables:
if value:
if ircdb.checkCapability(msg.prefix, capability):
try:
self.configurables.set(name, value, channel)
irc.reply(msg, conf.replySuccess)
except Error, e:
irc.error(msg, str(e))
else:
irc.error(msg, conf.replyNoCapability % capability)
else:
irc.reply(msg, self.configurables.help(name))
elif hasattr(self, 'globalConfigurables') and \
name in self.globalConfigurables:
if value:
if ircdb.checkCapability(msg.prefix, 'admin'):
try:
self.globalConfigurables.set(name, value, channel)
irc.reply(msg, conf.replySuccess)
except Error, e:
irc.error(msg, str(e))
else:
s = '%s is a global capability, and requires ' \
'the "admin" capability.'
irc.error(msg, s)
else:
irc.reply(msg, self.globalConfigurables.help(name))
else:
irc.error(msg, 'There is no config variable %r' % name)
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: