mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-08 19:22:33 +01:00
Update ircutils.standardSubstitute to use string.Template
In the process, deprecate utils.str.perlVariableSubstitute. Since string.Template doesn't support callable values though, we also sub-class IrcDict and override __getitem__ to call the value if it is callable. Signed-off-by: James Vega <jamessan@users.sourceforge.net>
This commit is contained in:
parent
c4de386d8f
commit
f7cedae9ad
7
RELNOTES
7
RELNOTES
@ -1,3 +1,10 @@
|
||||
Version 0.83.5
|
||||
|
||||
utils.str.perlVariableSubstitute is deprecated in favor of using Python's
|
||||
string.Template directly. perlVariableSubstitute will be removed in a future
|
||||
release.
|
||||
|
||||
|
||||
Version 0.83.4.1
|
||||
|
||||
Simple bug-fix release for a couple changes that were introduced last minute
|
||||
|
@ -1,5 +1,6 @@
|
||||
###
|
||||
# Copyright (c) 2002-2005, Jeremiah Fincher
|
||||
# Copyright (c) 2009, James Vega
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
@ -508,6 +509,12 @@ class IrcDict(utils.InsensitivePreservingDict):
|
||||
s = toLower(s)
|
||||
return s
|
||||
|
||||
class CallableValueIrcDict(IrcDict):
|
||||
def __getitem__(self, k):
|
||||
v = super(IrcDict, self).__getitem__(k)
|
||||
if callable(v):
|
||||
v = v()
|
||||
return v
|
||||
|
||||
class IrcSet(utils.NormalizingSet):
|
||||
"""A sets.Set using IrcStrings instead of regular strings."""
|
||||
@ -634,7 +641,7 @@ def standardSubstitute(irc, msg, text, env=None):
|
||||
return 'someone'
|
||||
ctime = time.ctime()
|
||||
localtime = time.localtime()
|
||||
vars = IrcDict({
|
||||
vars = CallableValueIrcDict({
|
||||
'who': msg.nick,
|
||||
'nick': msg.nick,
|
||||
'user': msg.user,
|
||||
@ -658,8 +665,9 @@ def standardSubstitute(irc, msg, text, env=None):
|
||||
})
|
||||
if env is not None:
|
||||
vars.update(env)
|
||||
return utils.str.perlVariableSubstitute(vars, text)
|
||||
|
||||
t = string.Template(text)
|
||||
t.idpattern = '[a-zA-Z][a-zA-Z0-9]*'
|
||||
return t.safe_substitute(vars)
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys, doctest
|
||||
|
@ -204,7 +204,26 @@ class FunctionsTestCase(SupyTestCase):
|
||||
finally:
|
||||
conf.supybot.protocols.irc.strictRfc.setValue(original)
|
||||
|
||||
def testStandardSubstitute(self):
|
||||
# Stub out random msg and irc objects that provide what
|
||||
# standardSubstitute wants
|
||||
msg = ircmsgs.IrcMsg(':%s PRIVMSG #channel :stuff' % self.hostmask)
|
||||
class Irc(object):
|
||||
nick = 'bob'
|
||||
irc = Irc()
|
||||
|
||||
f = ircutils.standardSubstitute
|
||||
vars = {'foo': 'bar', 'b': 'c', 'i': 100,
|
||||
'f': lambda: 'called'}
|
||||
self.assertEqual(f(irc, msg, '$foo', vars), 'bar')
|
||||
self.assertEqual(f(irc, msg, '${foo}', vars), 'bar')
|
||||
self.assertEqual(f(irc, msg, '$b', vars), 'c')
|
||||
self.assertEqual(f(irc, msg, '${b}', vars), 'c')
|
||||
self.assertEqual(f(irc, msg, '$i', vars), '100')
|
||||
self.assertEqual(f(irc, msg, '${i}', vars), '100')
|
||||
self.assertEqual(f(irc, msg, '$f', vars), 'called')
|
||||
self.assertEqual(f(irc, msg, '${f}', vars), 'called')
|
||||
self.assertEqual(f(irc, msg, '$b:$i', vars), 'c:100')
|
||||
|
||||
def testBanmask(self):
|
||||
for msg in msgs:
|
||||
|
@ -317,21 +317,6 @@ class StrTest(SupyTestCase):
|
||||
f = utils.str.perlReToReplacer('s/\b(\w+)\b/\1./g')
|
||||
self.assertEqual(f('foo bar baz'), 'foo. bar. baz.')
|
||||
|
||||
def testPerlVariableSubstitute(self):
|
||||
f = utils.str.perlVariableSubstitute
|
||||
vars = {'foo': 'bar', 'b a z': 'baz', 'b': 'c', 'i': 100,
|
||||
'f': lambda: 'called'}
|
||||
self.assertEqual(f(vars, '$foo'), 'bar')
|
||||
self.assertEqual(f(vars, '${foo}'), 'bar')
|
||||
self.assertEqual(f(vars, '$b'), 'c')
|
||||
self.assertEqual(f(vars, '${b}'), 'c')
|
||||
self.assertEqual(f(vars, '$i'), '100')
|
||||
self.assertEqual(f(vars, '${i}'), '100')
|
||||
self.assertEqual(f(vars, '$f'), 'called')
|
||||
self.assertEqual(f(vars, '${f}'), 'called')
|
||||
self.assertEqual(f(vars, '${b a z}'), 'baz')
|
||||
self.assertEqual(f(vars, '$b:$i'), 'c:100')
|
||||
|
||||
def testCommaAndify(self):
|
||||
f = utils.str.commaAndify
|
||||
L = ['foo']
|
||||
|
Loading…
Reference in New Issue
Block a user