Filter: Use the 'codecs' module instead of .encode and .decode.

This commit is contained in:
Valentin Lorentz 2012-08-04 19:43:11 +02:00
parent 9dcfce01c7
commit a89ff32297
2 changed files with 13 additions and 7 deletions

View File

@ -29,6 +29,7 @@
### ###
import re import re
import codecs
import string import string
import random import random
from cStringIO import StringIO from cStringIO import StringIO
@ -102,6 +103,7 @@ class Filter(callbacks.Plugin):
[('checkChannelCapability', 'op'), [('checkChannelCapability', 'op'),
additional('commandName')]) additional('commandName')])
_hebrew_remover = utils.str.MultipleRemover('aeiou')
@internationalizeDocstring @internationalizeDocstring
def hebrew(self, irc, msg, args, text): def hebrew(self, irc, msg, args, text):
"""<text> """<text>
@ -110,8 +112,7 @@ class Filter(callbacks.Plugin):
named 'hebrew' it's because I (jemfinch) thought of it in Hebrew class, named 'hebrew' it's because I (jemfinch) thought of it in Hebrew class,
and printed Hebrew often elides the vowels.) and printed Hebrew often elides the vowels.)
""" """
text = filter(lambda c: c not in 'aeiou', text) irc.reply(self._hebrew_remover(text))
irc.reply(text)
hebrew = wrap(hebrew, ['text']) hebrew = wrap(hebrew, ['text'])
@internationalizeDocstring @internationalizeDocstring
@ -174,6 +175,7 @@ class Filter(callbacks.Plugin):
irc.reply(''.join(L)) irc.reply(''.join(L))
unbinary = wrap(unbinary, ['text']) unbinary = wrap(unbinary, ['text'])
_hex_encoder = staticmethod(codecs.getencoder('hex_codec'))
@internationalizeDocstring @internationalizeDocstring
def hexlify(self, irc, msg, args, text): def hexlify(self, irc, msg, args, text):
"""<text> """<text>
@ -181,9 +183,10 @@ class Filter(callbacks.Plugin):
Returns a hexstring from the given string; a hexstring is a string Returns a hexstring from the given string; a hexstring is a string
composed of the hexadecimal value of each character in the 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']) hexlify = wrap(hexlify, ['text'])
_hex_decoder = staticmethod(codecs.getdecoder('hex_codec'))
@internationalizeDocstring @internationalizeDocstring
def unhexlify(self, irc, msg, args, text): def unhexlify(self, irc, msg, args, text):
"""<hexstring> """<hexstring>
@ -192,11 +195,12 @@ class Filter(callbacks.Plugin):
<hexstring> must be a string of hexadecimal digits. <hexstring> must be a string of hexadecimal digits.
""" """
try: try:
irc.reply(text.decode('hex_codec')) irc.reply(self._hex_decoder(text.encode('utf8'))[0].decode('utf8'))
except TypeError: except TypeError:
irc.error(_('Invalid input.')) irc.error(_('Invalid input.'))
unhexlify = wrap(unhexlify, ['text']) unhexlify = wrap(unhexlify, ['text'])
_rot13_encoder = codecs.getencoder('rot-13')
@internationalizeDocstring @internationalizeDocstring
def rot13(self, irc, msg, args, text): def rot13(self, irc, msg, args, text):
"""<text> """<text>
@ -205,7 +209,7 @@ class Filter(callbacks.Plugin):
commonly used for text that simply needs to be hidden from inadvertent commonly used for text that simply needs to be hidden from inadvertent
reading by roaming eyes, since it's easily reversible. 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']) rot13 = wrap(rot13, ['text'])
@internationalizeDocstring @internationalizeDocstring

View File

@ -30,6 +30,7 @@
from supybot.test import * from supybot.test import *
import re import re
import codecs
import supybot.utils as utils import supybot.utils as utils
import supybot.callbacks as callbacks import supybot.callbacks as callbacks
@ -134,8 +135,9 @@ class FilterTest(ChannelPluginTestCase):
self.assertResponse('spellit asasdfasdf12345@#$!%^', self.assertResponse('spellit asasdfasdf12345@#$!%^',
'asasdfasdf12345@#$!%^') 'asasdfasdf12345@#$!%^')
_rot13_encoder = codecs.getencoder('rot-13')
def testOutfilter(self): def testOutfilter(self):
s = self.nick.encode('rot13') s = self._rot13_encoder(self.nick)[0]
self.assertNotError('outfilter rot13') self.assertNotError('outfilter rot13')
self.assertResponse('rot13 foobar', '%s: foobar' % s) self.assertResponse('rot13 foobar', '%s: foobar' % s)
self.assertNotError('outfilter rot13') self.assertNotError('outfilter rot13')
@ -148,7 +150,7 @@ class FilterTest(ChannelPluginTestCase):
self.assertResponse('rot13 foobar', 'sbbone') self.assertResponse('rot13 foobar', 'sbbone')
def testOutfilterAction(self): def testOutfilterAction(self):
s = self.nick.encode('rot13') s = self._rot13_encoder(self.nick)[0]
self.assertNotError('outfilter rot13') self.assertNotError('outfilter rot13')
self.assertResponse('rot13 foobar', '%s: foobar' % s) self.assertResponse('rot13 foobar', '%s: foobar' % s)
m = self.getMsg('action foobar') m = self.getMsg('action foobar')