From e9cbb1f60bb4f8a35572cb94f20b19df77c04211 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Tue, 23 Jul 2013 21:10:26 +0200 Subject: [PATCH] Add support for adding global or 'other-channel'-specific Akas. --- plugins/Aka/plugin.py | 33 +++++++++++++++++++++++---------- plugins/Aka/test.py | 8 +++++--- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/plugins/Aka/plugin.py b/plugins/Aka/plugin.py index b84694ff8..4b9bfa317 100644 --- a/plugins/Aka/plugin.py +++ b/plugins/Aka/plugin.py @@ -101,10 +101,15 @@ if sqlalchemy: return list_ def get_alias(self, channel, name): - return self.get_db(channel).query(Alias.alias) \ - .filter(Alias.name == name).one()[0] + try: + return self.get_db(channel).query(Alias.alias) \ + .filter(Alias.name == name).one()[0] + except sqlalchemy.orm.exc.NoResultFound: + return None def add_aka(self, channel, name, alias): + if self.has_aka(channel, name): + raise AliasError(_('This Aka already exists.')) if sys.version_info[0] < 3: if isinstance(name, str): name = name.decode('utf8') @@ -178,7 +183,7 @@ class Aka(callbacks.Plugin): def listCommands(self): channel = dynamic.channel return set(self._db.get_aka_list(channel) + - self._db.get_aka_list() + + self._db.get_aka_list('global') + self.__parent.listCommands()) def getCommandMethod(self, command=None, name=None): @@ -246,23 +251,23 @@ class Aka(callbacks.Plugin): return f def _add_aka(self, channel, name, alias): - if self.isCommandMethod(name): + if self.__parent.isCommandMethod(name): raise AliasError(_('You can\'t overwrite commands in ' 'this plugin.')) biggestDollar = findBiggestDollar(alias) biggestAt = findBiggestAt(alias) wildcard = '$*' in alias if biggestAt and wildcard: - raise AliasError('Can\'t mix $* and optional args (@1, etc.)') + raise AliasError(_('Can\'t mix $* and optional args (@1, etc.)')) if alias.count('$*') > 1: - raise AliasError('There can be only one $* in an alias.') + raise AliasError(_('There can be only one $* in an alias.')) self._db.add_aka(channel, name, alias) def _remove_aka(self, channel, name): self._db.remove_aka(channel, name) - def add(self, irc, msg, args, channel, name, alias): - """[<#channel|global>] + def add(self, irc, msg, args, optlist, name, alias): + """[--channel <#channel>] Defines an alias that executes . The should be in the standard "command argument [nestedcommand argument]" @@ -271,6 +276,13 @@ class Aka(callbacks.Plugin): etc. can be used for optional arguments. $* simply means "all remaining arguments," and cannot be combined with optional arguments. """ + channel = 'global' + for (option, arg) in optlist: + if option == 'channel': + if not ircutils.isChannel(arg): + irc.error(_('%r is not a valid channel.') % arg, + Raise=True) + channel = arg if ' ' not in alias: # If it's a single word, they probably want $*. alias += ' $*' @@ -281,8 +293,9 @@ class Aka(callbacks.Plugin): irc.replySuccess() except AliasError as e: irc.error(str(e)) - add = wrap(add, [first(('literal', 'global'), 'channel'), - 'commandName', 'text']) + add = wrap(add, [getopts({ + 'channel': 'somethingWithoutSpaces', + }),'commandName', 'text']) def remove(self, irc, msg, args, channel, name): """[<#channel|global>] diff --git a/plugins/Aka/test.py b/plugins/Aka/test.py index 77caa8a26..6c7c6fa97 100644 --- a/plugins/Aka/test.py +++ b/plugins/Aka/test.py @@ -129,12 +129,14 @@ class AkaTestCase(ChannelPluginTestCase): def testChannelPriority(self): self.assertNotError('aka add spam "echo foo"') - self.assertNotError('aka add --channel #channel spam "echo bar"') + self.assertNotError('aka add --channel %s spam "echo bar"' % + self.channel) self.assertResponse('spam', 'bar') - self.assertNotError('aka add --channel #channel egg "echo baz"') + self.assertNotError('aka add --channel %s egg "echo baz"' % + self.channel) self.assertNotError('aka add egg "echo qux"') - self.assertResponse('spam', 'baz') + self.assertResponse('egg', 'baz') # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: