mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-02-18 14:40:51 +01:00
Added ability to specify optional arguments in aliases with @\d+.
This commit is contained in:
parent
5a15783e7a
commit
fb60da149b
@ -108,31 +108,41 @@ def findBiggestDollar(alias):
|
|||||||
if dollars:
|
if dollars:
|
||||||
return dollars[-1]
|
return dollars[-1]
|
||||||
else:
|
else:
|
||||||
return None
|
return 0
|
||||||
|
|
||||||
|
atRe = re.compile(r'@(\d+)')
|
||||||
|
def findBiggestAt(alias):
|
||||||
|
ats = atRe.findall(alias)
|
||||||
|
ats = map(int, ats)
|
||||||
|
ats.sort()
|
||||||
|
if ats:
|
||||||
|
return ats[-1]
|
||||||
|
else:
|
||||||
|
return 0
|
||||||
|
|
||||||
def makeNewAlias(name, alias):
|
def makeNewAlias(name, alias):
|
||||||
if findAliasCommand(name, alias):
|
if findAliasCommand(name, alias):
|
||||||
raise RecursiveAlias
|
raise RecursiveAlias
|
||||||
doChannel = '$channel' in alias
|
doChannel = '$channel' in alias
|
||||||
biggestDollar = findBiggestDollar(alias)
|
biggestDollar = findBiggestDollar(alias)
|
||||||
doDollars = bool(biggestDollar)
|
biggestAt = findBiggestAt(alias)
|
||||||
if biggestDollar is not None:
|
|
||||||
biggestDollar = int(biggestDollar)
|
|
||||||
else:
|
|
||||||
biggestDollar = 0
|
|
||||||
def f(self, irc, msg, args):
|
def f(self, irc, msg, args):
|
||||||
alias_ = alias
|
alias_ = alias
|
||||||
if doChannel:
|
if doChannel:
|
||||||
channel = privmsgs.getChannel(msg, args)
|
channel = privmsgs.getChannel(msg, args)
|
||||||
alias_ = alias.replace('$channel', channel)
|
alias_ = alias.replace('$channel', channel)
|
||||||
if doDollars:
|
if biggestDollar or biggestAt:
|
||||||
args = privmsgs.getArgs(args, needed=biggestDollar)
|
args = privmsgs.getArgs(args, needed=biggestDollar,
|
||||||
if biggestDollar == 1:
|
optional=biggestAt)
|
||||||
|
# Gotta have a tuple.
|
||||||
|
if biggestDollar + biggestAt == 1:
|
||||||
args = (args,)
|
args = (args,)
|
||||||
def replace(m):
|
def replace(m):
|
||||||
idx = int(m.group(1))
|
idx = int(m.group(1))
|
||||||
return args[idx-1]
|
return utils.dqrepr(args[idx-1])
|
||||||
alias_ = dollarRe.sub(replace, alias_)
|
alias_ = dollarRe.sub(replace, alias_)
|
||||||
|
args = args[biggestDollar:]
|
||||||
|
alias_ = atRe.sub(replace, alias_)
|
||||||
self.Proxy(irc.irc, msg, callbacks.tokenize(alias_))
|
self.Proxy(irc.irc, msg, callbacks.tokenize(alias_))
|
||||||
f.__doc__ ='<an alias, %s>\n\nAlias for %r' % \
|
f.__doc__ ='<an alias, %s>\n\nAlias for %r' % \
|
||||||
(utils.nItems(biggestDollar, 'argument'), alias)
|
(utils.nItems(biggestDollar, 'argument'), alias)
|
||||||
|
@ -51,8 +51,8 @@ class FunctionsTest(unittest.TestCase):
|
|||||||
self.failUnless(Alias.findAliasCommand(s, 'foo |%s' % s))
|
self.failUnless(Alias.findAliasCommand(s, 'foo |%s' % s))
|
||||||
|
|
||||||
def testFindBiggestDollar(self):
|
def testFindBiggestDollar(self):
|
||||||
self.assertEqual(Alias.findBiggestDollar(''), None)
|
self.assertEqual(Alias.findBiggestDollar(''), 0)
|
||||||
self.assertEqual(Alias.findBiggestDollar('foo'), None)
|
self.assertEqual(Alias.findBiggestDollar('foo'), 0)
|
||||||
self.assertEqual(Alias.findBiggestDollar('$0'), 0)
|
self.assertEqual(Alias.findBiggestDollar('$0'), 0)
|
||||||
self.assertEqual(Alias.findBiggestDollar('$1'), 1)
|
self.assertEqual(Alias.findBiggestDollar('$1'), 1)
|
||||||
self.assertEqual(Alias.findBiggestDollar('$2'), 2)
|
self.assertEqual(Alias.findBiggestDollar('$2'), 2)
|
||||||
@ -109,6 +109,11 @@ class AliasTestCase(PluginTestCase, PluginDocumentation):
|
|||||||
self.assertRaises(Alias.AliasError, cb.removeAlias, 'foobar')
|
self.assertRaises(Alias.AliasError, cb.removeAlias, 'foobar')
|
||||||
cb.removeAlias('foobar', evenIfFrozen=True)
|
cb.removeAlias('foobar', evenIfFrozen=True)
|
||||||
self.assertNoResponse('foobar', 2)
|
self.assertNoResponse('foobar', 2)
|
||||||
|
|
||||||
|
def testOptionalArgs(self):
|
||||||
|
self.assertNotError('alias myrepr "repr @1"')
|
||||||
|
self.assertResponse('myrepr foo', '"foo"')
|
||||||
|
self.assertResponse('myrepr ""', '""')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user