From dcd07a2ec4653f2d16597e5c4f12504bc62aa6e3 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sat, 4 Aug 2012 19:13:35 +0200 Subject: [PATCH] ChannelStats & Filter & Math & Unit: use utils instead of str.translate. --- plugins/ChannelStats/plugin.py | 3 ++- plugins/Filter/plugin.py | 2 +- plugins/Math/plugin.py | 10 ++++++---- plugins/Unix/plugin.py | 3 ++- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/plugins/ChannelStats/plugin.py b/plugins/ChannelStats/plugin.py index 0f0e3482b..028e05335 100644 --- a/plugins/ChannelStats/plugin.py +++ b/plugins/ChannelStats/plugin.py @@ -297,6 +297,7 @@ class ChannelStats(callbacks.Plugin): name, channel)) stats = wrap(stats, ['channeldb', additional('something')]) + _calc_match_forbidden_chars = re.compile('[_[\]]') _env = {'__builtins__': types.ModuleType('__builtins__')} _env.update(math.__dict__) @internationalizeDocstring @@ -311,7 +312,7 @@ class ChannelStats(callbacks.Plugin): """ # XXX I could do this the right way, and abstract out a safe eval, # or I could just copy/paste from the Math plugin. - if expr != expr.translate(utils.str.chars, '_[]'): + if self._calc_match_forbidden_chars.match(expr): irc.error(_('There\'s really no reason why you should have ' 'underscores or brackets in your mathematical ' 'expression. Please remove them.'), Raise=True) diff --git a/plugins/Filter/plugin.py b/plugins/Filter/plugin.py index 3ed19c3ce..345553ba9 100644 --- a/plugins/Filter/plugin.py +++ b/plugins/Filter/plugin.py @@ -656,7 +656,7 @@ class Filter(callbacks.Plugin): Returns with the l's made into r's and r's made into l's. """ - text = text.translate(self._azn_trans) + text = self._azn_trans(text) irc.reply(text) azn = wrap(azn, ['text']) diff --git a/plugins/Math/plugin.py b/plugins/Math/plugin.py index 00a78cf67..bf87eef02 100644 --- a/plugins/Math/plugin.py +++ b/plugins/Math/plugin.py @@ -148,6 +148,8 @@ class Math(callbacks.Plugin): else: return '%s%s' % (realS, imagS) + _calc_match_forbidden_chars = re.compile('[_[\]]') + _calc_remover = utils.str.MultipleRemover('_[] \t') ### # So this is how the 'calc' command works: # First, we make a nice little safe environment for evaluation; basically, @@ -167,12 +169,12 @@ class Math(callbacks.Plugin): crash to the bot with something like '10**10**10**10'. One consequence is that large values such as '10**24' might not be exact. """ - if text != text.translate(utils.str.chars, '_[]'): + if self._calc_match_forbidden_chars.match(text): irc.error(_('There\'s really no reason why you should have ' 'underscores or brackets in your mathematical ' 'expression. Please remove them.')) return - #text = text.translate(utils.str.chars, '_[] \t') + text = self._calc_remover(text) if 'lambda' in text: irc.error(_('You can\'t use lambda in this command.')) return @@ -221,14 +223,14 @@ class Math(callbacks.Plugin): math, and can thus cause the bot to suck up CPU. Hence it requires the 'trusted' capability to use. """ - if text != text.translate(utils.str.chars, '_[]'): + if self._calc_match_forbidden_chars.match(text): irc.error(_('There\'s really no reason why you should have ' 'underscores or brackets in your mathematical ' 'expression. Please remove them.')) return # This removes spaces, too, but we'll leave the removal of _[] for # safety's sake. - text = text.translate(utils.str.chars, '_[] \t') + text = self._calc_remover(text) if 'lambda' in text: irc.error(_('You can\'t use lambda in this command.')) return diff --git a/plugins/Unix/plugin.py b/plugins/Unix/plugin.py index e8ccdf635..839826c84 100644 --- a/plugins/Unix/plugin.py +++ b/plugins/Unix/plugin.py @@ -48,6 +48,7 @@ import supybot.callbacks as callbacks from supybot.i18n import PluginInternationalization, internationalizeDocstring _ = PluginInternationalization('Unix') +_progstats_endline_remover = utils.str.MultipleRemover('\r\n') def progstats(): pw = pwd.getpwuid(os.getuid()) response = format('Process ID %i running as user %q and as group %q ' @@ -55,7 +56,7 @@ def progstats(): 'Running on Python %s.', os.getpid(), pw[0], pw[3], os.getcwd(), ' '.join(sys.argv), - sys.version.translate(utils.str.chars, '\r\n')) + _progstats_endline_remover(sys.version)) return response class TimeoutError(IOError):