From 39dad55b6f00e370fb8fad49fb7c2e58325eb8d5 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Fri, 18 Jul 2014 20:43:24 +0200 Subject: [PATCH] Make renaming of commands persistant. Closes GH-789. --- plugins/Owner/plugin.py | 36 +++++++----------------------------- src/plugin.py | 25 ++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/plugins/Owner/plugin.py b/plugins/Owner/plugin.py index 8bdf4f13e..6c0580d11 100644 --- a/plugins/Owner/plugin.py +++ b/plugins/Owner/plugin.py @@ -66,28 +66,6 @@ def registerDefaultPlugin(command, plugin): # This must be set, or the quotes won't be removed. conf.supybot.commands.defaultPlugins.get(command).set(plugin) -def registerRename(plugin, command=None, newName=None): - g = conf.registerGlobalValue(conf.supybot.commands.renames, plugin, - registry.SpaceSeparatedSetOfStrings([], """Determines what commands - in this plugin are to be renamed.""")) - if command is not None: - g().add(command) - v = conf.registerGlobalValue(g, command, registry.String('', '')) - if newName is not None: - v.setValue(newName) # In case it was already registered. - return v - else: - return g - -def renameCommand(cb, name, newName): - assert not hasattr(cb, newName), 'Cannot rename over existing attributes.' - assert newName == callbacks.canonicalName(newName), \ - 'newName must already be normalized.' - if name != newName: - method = getattr(cb.__class__, name) - setattr(cb.__class__, newName, method) - delattr(cb.__class__, name) - registerDefaultPlugin('list', 'Misc') registerDefaultPlugin('help', 'Misc') @@ -574,20 +552,20 @@ class Owner(callbacks.Plugin): irc.error('That command wasn\'t disabled.') enable = wrap(enable, [optional('plugin'), 'commandName']) - def rename(self, irc, msg, args, plugin, command, newName): + def rename(self, irc, msg, args, command_plugin, command, newName): """ Renames in to the . """ - if not plugin.isCommand(command): - what = 'command in the %s plugin' % plugin.name() + if not command_plugin.isCommand(command): + what = 'command in the %s plugin' % command_plugin.name() irc.errorInvalid(what, command) - if hasattr(plugin, newName): + if hasattr(command_plugin, newName): irc.error('The %s plugin already has an attribute named %s.' % - (plugin, newName)) + (command_plugin, newName)) return - registerRename(plugin.name(), command, newName) - renameCommand(plugin, command, newName) + plugin.registerRename(command_plugin.name(), command, newName) + plugin.renameCommand(command_plugin, command, newName) irc.replySuccess() rename = wrap(rename, ['plugin', 'commandName', 'commandName']) diff --git a/src/plugin.py b/src/plugin.py index 448a017e6..ebe7e970b 100644 --- a/src/plugin.py +++ b/src/plugin.py @@ -81,6 +81,28 @@ def loadPluginModule(name, ignoreDeprecation=False): linecache.checkcache() return module +def renameCommand(cb, name, newName): + assert not hasattr(cb, newName), 'Cannot rename over existing attributes.' + assert newName == callbacks.canonicalName(newName), \ + 'newName must already be normalized.' + if name != newName: + method = getattr(cb.__class__, name) + setattr(cb.__class__, newName, method) + delattr(cb.__class__, name) + +def registerRename(plugin, command=None, newName=None): + g = conf.registerGlobalValue(conf.supybot.commands.renames, plugin, + registry.SpaceSeparatedSetOfStrings([], """Determines what commands + in this plugin are to be renamed.""")) + if command is not None: + g().add(command) + v = conf.registerGlobalValue(g, command, registry.String('', '')) + if newName is not None: + v.setValue(newName) # In case it was already registered. + return v + else: + return g + def loadPluginClass(irc, module, register=None): """Loads the plugin Class from the given module into the given Irc.""" try: @@ -118,7 +140,8 @@ def loadPluginClass(irc, module, register=None): assert not irc.getCallback(plugin), \ 'There is already a %r plugin registered.' % plugin try: - renames = []#XXX registerRename(plugin)() + v = registerRename(plugin) + renames = conf.supybot.commands.renames.get(plugin)() if renames: for command in renames: v = registerRename(plugin, command)