Merge pull request #952 from GLolol/aka-2

Aka: add 'list' command & update listCommands override
This commit is contained in:
Valentin Lorentz 2014-12-26 22:37:24 +01:00
commit f4425de7c5
3 changed files with 39 additions and 15 deletions

View File

@ -32,7 +32,7 @@ msgstr "Tämä Aka on jo olemassa."
#: plugin.py:169 plugin.py:181 plugin.py:195 plugin.py:291 plugin.py:308 #: plugin.py:169 plugin.py:181 plugin.py:195 plugin.py:291 plugin.py:308
#: plugin.py:325 #: plugin.py:325
msgid "This Aka does not exist" msgid "This Aka does not exist."
msgstr "Tätä Akaa ei ole olemassa." msgstr "Tätä Akaa ei ole olemassa."
#: plugin.py:293 #: plugin.py:293

View File

@ -32,7 +32,6 @@ import re
import os import os
import sys import sys
import datetime import datetime
import operator
import supybot.conf as conf import supybot.conf as conf
import supybot.utils as utils import supybot.utils as utils
@ -166,7 +165,7 @@ if sqlite3:
SET locked=1, locked_at=?, locked_by=? WHERE name = ?""", SET locked=1, locked_at=?, locked_by=? WHERE name = ?""",
(datetime.datetime.now(), by, name)) (datetime.datetime.now(), by, name))
if cursor.rowcount == 0: if cursor.rowcount == 0:
raise AkaError(_('This Aka does not exist')) raise AkaError(_('This Aka does not exist.'))
db.commit() db.commit()
def unlock_aka(self, channel, name, by): def unlock_aka(self, channel, name, by):
@ -178,7 +177,7 @@ if sqlite3:
cursor.execute("""UPDATE aliases SET locked=0, locked_at=? cursor.execute("""UPDATE aliases SET locked=0, locked_at=?
WHERE name = ?""", (datetime.datetime.now(), name)) WHERE name = ?""", (datetime.datetime.now(), name))
if cursor.rowcount == 0: if cursor.rowcount == 0:
raise AkaError(_('This Aka does not exist')) raise AkaError(_('This Aka does not exist.'))
db.commit() db.commit()
def get_aka_lock(self, channel, name): def get_aka_lock(self, channel, name):
@ -192,7 +191,7 @@ if sqlite3:
if r: if r:
return (bool(r[0]), r[1], r[2]) return (bool(r[0]), r[1], r[2])
else: else:
raise AkaError(_('This Aka does not exist')) raise AkaError(_('This Aka does not exist.'))
available_db.update({'sqlite3': SQLiteAkaDB}) available_db.update({'sqlite3': SQLiteAkaDB})
elif sqlalchemy: elif sqlalchemy:
Base = sqlalchemy.ext.declarative.declarative_base() Base = sqlalchemy.ext.declarative.declarative_base()
@ -288,7 +287,7 @@ elif sqlalchemy:
aka = db.query(SQLAlchemyAlias) \ aka = db.query(SQLAlchemyAlias) \
.filter(SQLAlchemyAlias.name == name).one() .filter(SQLAlchemyAlias.name == name).one()
except sqlalchemy.orm.exc.NoResultFound: except sqlalchemy.orm.exc.NoResultFound:
raise AkaError(_('This Aka does not exist')) raise AkaError(_('This Aka does not exist.'))
if aka.locked: if aka.locked:
raise AkaError(_('This Aka is already locked.')) raise AkaError(_('This Aka is already locked.'))
aka.locked = True aka.locked = True
@ -305,7 +304,7 @@ elif sqlalchemy:
aka = db.query(SQLAlchemyAlias) \ aka = db.query(SQLAlchemyAlias) \
.filter(SQLAlchemyAlias.name == name).one() .filter(SQLAlchemyAlias.name == name).one()
except sqlalchemy.orm.exc.NoResultFound: except sqlalchemy.orm.exc.NoResultFound:
raise AkaError(_('This Aka does not exist')) raise AkaError(_('This Aka does not exist.'))
if not aka.locked: if not aka.locked:
raise AkaError(_('This Aka is already unlocked.')) raise AkaError(_('This Aka is already unlocked.'))
aka.locked = False aka.locked = False
@ -322,7 +321,7 @@ elif sqlalchemy:
.query(SQLAlchemyAlias.locked, SQLAlchemyAlias.locked_by, SQLAlchemyAlias.locked_at)\ .query(SQLAlchemyAlias.locked, SQLAlchemyAlias.locked_by, SQLAlchemyAlias.locked_at)\
.filter(SQLAlchemyAlias.name == name).one() .filter(SQLAlchemyAlias.name == name).one()
except sqlalchemy.orm.exc.NoResultFound: except sqlalchemy.orm.exc.NoResultFound:
raise AkaError(_('This Aka does not exist')) raise AkaError(_('This Aka does not exist.'))
available_db.update({'sqlalchemy': SqlAlchemyAkaDB}) available_db.update({'sqlalchemy': SqlAlchemyAkaDB})
@ -397,11 +396,9 @@ class Aka(callbacks.Plugin):
isCommand = isCommandMethod isCommand = isCommandMethod
def listCommands(self): def listCommands(self):
channel = dynamic.channel or 'global' commands = ['add', 'remove', 'lock', 'unlock', 'importaliasdatabase',
return list(set(list(map(callbacks.formatCommand, 'show', 'list', 'set']
self._db.get_aka_list(channel) + return commands
self._db.get_aka_list('global'))) +
['add', 'remove', 'lock', 'unlock', 'importaliasdatabase']))
def getCommand(self, args, check_other_plugins=True): def getCommand(self, args, check_other_plugins=True):
canonicalName = callbacks.canonicalName canonicalName = callbacks.canonicalName
@ -713,6 +710,28 @@ class Aka(callbacks.Plugin):
irc.replySuccess() irc.replySuccess()
importaliasdatabase = wrap(importaliasdatabase, ['owner']) importaliasdatabase = wrap(importaliasdatabase, ['owner'])
def list(self, irc, msg, args, optlist):
"""[--channel <#channel>]
Lists all Akas defined for <channel>. If <channel> is not specified,
lists all global Akas."""
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
aka_list = self._db.get_aka_list(channel)
if aka_list:
aka_values = [self._db.get_alias(channel, aka) for aka in aka_list]
s = ('{0}: "{1}"'.format(ircutils.bold(k), v) for (k, v) in
zip(aka_list, aka_values))
irc.replies(s)
else:
irc.error(_("No Akas found."))
list = wrap(list, [getopts({'channel': 'channel'})])
Class = Aka Class = Aka

View File

@ -112,10 +112,10 @@ class AkaChannelTestCase(ChannelPluginTestCase):
cb._add_aka('global', 'foobar', 'echo sbbone') cb._add_aka('global', 'foobar', 'echo sbbone')
cb._db.lock_aka('global', 'foobar', 'evil_admin') cb._db.lock_aka('global', 'foobar', 'evil_admin')
self.assertResponse('foobar', 'sbbone') self.assertResponse('foobar', 'sbbone')
self.assertRegexp('list Aka', 'foobar') self.assertRegexp('aka list', 'foobar')
self.assertRaises(Aka.AkaError, cb._remove_aka, 'global', 'foobar') self.assertRaises(Aka.AkaError, cb._remove_aka, 'global', 'foobar')
cb._remove_aka('global', 'foobar', evenIfLocked=True) cb._remove_aka('global', 'foobar', evenIfLocked=True)
self.assertNotRegexp('list Aka', 'foobar') self.assertNotRegexp('aka list', 'foobar')
self.assertError('foobar') self.assertError('foobar')
def testOptionalArgs(self): def testOptionalArgs(self):
@ -226,5 +226,10 @@ class AkaTestCase(PluginTestCase):
self.assertRegexp('alias spam', 'there is no command named') self.assertRegexp('alias spam', 'there is no command named')
self.assertResponse('aka spam', 'egg') self.assertResponse('aka spam', 'egg')
def testList(self):
self.assertNotError('aka add foo bar')
self.assertRegexp('aka list', 'foo.*?bar \$\*')
self.assertNotError('aka add "foo bar" baz')
self.assertRegexp('aka list', 'foo.*?bar \$\*.*?foo bar.*?baz \$\*')
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: