From f7cedae9ad8e4a0a3f986be493cc901d2de096c0 Mon Sep 17 00:00:00 2001 From: James Vega Date: Thu, 15 Oct 2009 21:21:08 -0400 Subject: [PATCH] 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 --- RELNOTES | 7 +++++++ src/ircutils.py | 14 +++++++++++--- test/test_ircutils.py | 19 +++++++++++++++++++ test/test_utils.py | 15 --------------- 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/RELNOTES b/RELNOTES index b7dbce7cf..bf32ebeb5 100644 --- a/RELNOTES +++ b/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 diff --git a/src/ircutils.py b/src/ircutils.py index 44b06817f..24731be64 100644 --- a/src/ircutils.py +++ b/src/ircutils.py @@ -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 diff --git a/test/test_ircutils.py b/test/test_ircutils.py index a6b73b154..6dc11e21d 100644 --- a/test/test_ircutils.py +++ b/test/test_ircutils.py @@ -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: diff --git a/test/test_utils.py b/test/test_utils.py index c71703924..c4efb4979 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -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']