From a89ff322970606fc67c7e0bc8829ed90cc13ff77 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sat, 4 Aug 2012 19:43:11 +0200 Subject: [PATCH] Filter: Use the 'codecs' module instead of .encode and .decode. --- plugins/Filter/plugin.py | 14 +++++++++----- plugins/Filter/test.py | 6 ++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/plugins/Filter/plugin.py b/plugins/Filter/plugin.py index 345553ba9..680eef212 100644 --- a/plugins/Filter/plugin.py +++ b/plugins/Filter/plugin.py @@ -29,6 +29,7 @@ ### import re +import codecs import string import random from cStringIO import StringIO @@ -102,6 +103,7 @@ class Filter(callbacks.Plugin): [('checkChannelCapability', 'op'), additional('commandName')]) + _hebrew_remover = utils.str.MultipleRemover('aeiou') @internationalizeDocstring def hebrew(self, irc, msg, args, text): """ @@ -110,8 +112,7 @@ class Filter(callbacks.Plugin): named 'hebrew' it's because I (jemfinch) thought of it in Hebrew class, and printed Hebrew often elides the vowels.) """ - text = filter(lambda c: c not in 'aeiou', text) - irc.reply(text) + irc.reply(self._hebrew_remover(text)) hebrew = wrap(hebrew, ['text']) @internationalizeDocstring @@ -174,6 +175,7 @@ class Filter(callbacks.Plugin): irc.reply(''.join(L)) unbinary = wrap(unbinary, ['text']) + _hex_encoder = staticmethod(codecs.getencoder('hex_codec')) @internationalizeDocstring def hexlify(self, irc, msg, args, text): """ @@ -181,9 +183,10 @@ class Filter(callbacks.Plugin): Returns a hexstring from the given string; a hexstring is a string composed of the hexadecimal value of each character in the string """ - irc.reply(text.encode('hex_codec')) + irc.reply(self._hex_encoder(text.encode('utf8'))[0].decode('utf8')) hexlify = wrap(hexlify, ['text']) + _hex_decoder = staticmethod(codecs.getdecoder('hex_codec')) @internationalizeDocstring def unhexlify(self, irc, msg, args, text): """ @@ -192,11 +195,12 @@ class Filter(callbacks.Plugin): must be a string of hexadecimal digits. """ try: - irc.reply(text.decode('hex_codec')) + irc.reply(self._hex_decoder(text.encode('utf8'))[0].decode('utf8')) except TypeError: irc.error(_('Invalid input.')) unhexlify = wrap(unhexlify, ['text']) + _rot13_encoder = codecs.getencoder('rot-13') @internationalizeDocstring def rot13(self, irc, msg, args, text): """ @@ -205,7 +209,7 @@ class Filter(callbacks.Plugin): commonly used for text that simply needs to be hidden from inadvertent reading by roaming eyes, since it's easily reversible. """ - irc.reply(text.encode('rot13')) + irc.reply(self._rot13_encoder(text)[0]) rot13 = wrap(rot13, ['text']) @internationalizeDocstring diff --git a/plugins/Filter/test.py b/plugins/Filter/test.py index 9c76e4331..a22d1745f 100644 --- a/plugins/Filter/test.py +++ b/plugins/Filter/test.py @@ -30,6 +30,7 @@ from supybot.test import * import re +import codecs import supybot.utils as utils import supybot.callbacks as callbacks @@ -134,8 +135,9 @@ class FilterTest(ChannelPluginTestCase): self.assertResponse('spellit asasdfasdf12345@#$!%^', 'asasdfasdf12345@#$!%^') + _rot13_encoder = codecs.getencoder('rot-13') def testOutfilter(self): - s = self.nick.encode('rot13') + s = self._rot13_encoder(self.nick)[0] self.assertNotError('outfilter rot13') self.assertResponse('rot13 foobar', '%s: foobar' % s) self.assertNotError('outfilter rot13') @@ -148,7 +150,7 @@ class FilterTest(ChannelPluginTestCase): self.assertResponse('rot13 foobar', 'sbbone') def testOutfilterAction(self): - s = self.nick.encode('rot13') + s = self._rot13_encoder(self.nick)[0] self.assertNotError('outfilter rot13') self.assertResponse('rot13 foobar', '%s: foobar' % s) m = self.getMsg('action foobar')