Changed to use the registry.

This commit is contained in:
Jeremy Fincher 2004-02-04 05:56:04 +00:00
parent 96568b3d64
commit 0d6136610e

View File

@ -30,7 +30,7 @@
### ###
""" """
Allows 'aliases' for other commands. Allows aliases for other commands.
""" """
__revision__ = "$Id$" __revision__ = "$Id$"
@ -44,6 +44,7 @@ import sets
import conf import conf
import utils import utils
import privmsgs import privmsgs
import registry
import callbacks import callbacks
import structures import structures
import unpreserve import unpreserve
@ -145,60 +146,29 @@ def makeNewAlias(name, alias):
f = utils.changeFunctionName(f, name, doc) f = utils.changeFunctionName(f, name, doc)
return f return f
def preserveAlias(fd, alias, command, locked): conf.registerPlugin('Alias')
def write(s): conf.registerGroup(conf.supybot.plugins.Alias, 'aliases')
fd.write(s)
fd.write(os.linesep)
write('alias %s' % alias)
write(' command %s' % command)
write(' locked %s' % locked)
write('')
filename = os.path.join(conf.supybot.directories.conf(), 'aliases.conf') filename = os.path.join(conf.supybot.directories.conf(), 'aliases.conf')
class Alias(callbacks.Privmsg): class Alias(callbacks.Privmsg):
def __init__(self): def __init__(self):
callbacks.Privmsg.__init__(self) callbacks.Privmsg.__init__(self)
# Schema: {alias: [command, locked]} # Schema: {alias: [command, locked]}
aliases = {} self.aliases = {}
class AliasCreator(object): group = conf.supybot.plugins.Alias.aliases
aliasName = None for (name, alias) in registry._cache.iteritems():
def __init__(self): name = name.lower()
self.aliasCommand = None if name.startswith('supybot.plugins.alias.aliases.'):
self.aliasLocked = None name = name[len('supybot.plugins.alias.aliases.'):]
self.setAliasName = False if '.' in name:
continue
def badCommand(self, command, rest, lineno): conf.registerGlobalValue(group, name, registry.String('', ''))
raise ValueError, \ conf.registerGlobalValue(group.get(name), 'locked',
'Invalid command on line %s: %s' % (lineno, command) registry.Boolean(False, ''))
for (name, value) in group.getValues(fullNames=False):
def alias(self, rest, lineno): name = name.lower() # Just in case.
if self.aliasName is not None: command = value()
raise ValueError, \ locked = value.locked()
'Unexpected alias command at line %s' % lineno self.aliases[name] = [command, locked]
self.setAliasName = True
AliasCreator.aliasName = rest
def command(self, rest, lineno):
if self.aliasName is None:
raise ValueError, \
'Unexpected alias configuration at line %s' % lineno
self.aliasCommand = rest
def locked(self, rest, lineno):
if self.aliasName is None:
raise ValueError, \
'Unexpected alias configuration at line %s' % lineno
self.aliasLocked = bool(eval(rest))
def finish(self):
if self.setAliasName:
return
aliases[self.aliasName] = (self.aliasCommand, self.aliasLocked)
AliasCreator.aliasName = None
reader = unpreserve.Reader(AliasCreator)
reader.readFile(filename)
self.aliases = aliases
def __call__(self, irc, msg): def __call__(self, irc, msg):
# Adding the aliases requires an Irc. So the first time we get called # Adding the aliases requires an Irc. So the first time we get called
@ -213,12 +183,6 @@ class Alias(callbacks.Privmsg):
del self.__class__.__call__ del self.__class__.__call__
callbacks.Privmsg.__call__(self, irc, msg) callbacks.Privmsg.__call__(self, irc, msg)
def die(self):
fd = file(filename, 'w')
for (alias, (command, locked)) in self.aliases.iteritems():
preserveAlias(fd, alias, command, locked)
fd.close()
def lock(self, irc, msg, args): def lock(self, irc, msg, args):
"""<alias> """<alias>
@ -228,6 +192,7 @@ class Alias(callbacks.Privmsg):
name = callbacks.canonicalName(name) name = callbacks.canonicalName(name)
if hasattr(self, name) and self.isCommand(name): if hasattr(self, name) and self.isCommand(name):
self.aliases[name][1] = True self.aliases[name][1] = True
conf.supybot.plugins.Alias.aliases.get(name).locked.setValue(True)
irc.replySuccess() irc.replySuccess()
else: else:
irc.error('There is no such alias.') irc.error('There is no such alias.')
@ -242,6 +207,7 @@ class Alias(callbacks.Privmsg):
name = callbacks.canonicalName(name) name = callbacks.canonicalName(name)
if hasattr(self, name) and self.isCommand(name): if hasattr(self, name) and self.isCommand(name):
self.aliases[name][1] = False self.aliases[name][1] = False
conf.supybot.plugins.Alias.aliases.get(name).locked.setValue(False)
irc.replySuccess() irc.replySuccess()
else: else:
irc.error('There is no such alias.') irc.error('There is no such alias.')
@ -273,6 +239,10 @@ class Alias(callbacks.Privmsg):
f = makeNewAlias(name, alias) f = makeNewAlias(name, alias)
except RecursiveAlias: except RecursiveAlias:
raise AliasError, 'You can\'t define a recursive alias.' raise AliasError, 'You can\'t define a recursive alias.'
conf.supybot.plugins.Alias.aliases.register(name,
registry.String(alias, ''))
conf.supybot.plugins.Alias.aliases.get(name).register('locked',
registry.Boolean(lock, ''))
setattr(self.__class__, name, f) setattr(self.__class__, name, f)
self.aliases[name] = [alias, lock] self.aliases[name] = [alias, lock]
@ -282,6 +252,7 @@ class Alias(callbacks.Privmsg):
if evenIfLocked or not self.aliases[name][1]: if evenIfLocked or not self.aliases[name][1]:
delattr(self.__class__, name) delattr(self.__class__, name)
del self.aliases[name] del self.aliases[name]
conf.supybot.plugins.Alias.aliases.unregister(name)
else: else:
raise AliasError, 'That alias is locked.' raise AliasError, 'That alias is locked.'
else: else: