String: Apply/unapply base64 encoding when dealing with a binary encoding.

This commit is contained in:
Valentin Lorentz 2012-08-05 17:55:29 +02:00
parent 5ae3e3fdcf
commit a40ebae4da

View File

@ -31,6 +31,7 @@
import sys import sys
import types import types
import codecs import codecs
import base64
import binascii import binascii
import supybot.utils as utils import supybot.utils as utils
@ -74,17 +75,30 @@ class String(callbacks.Plugin):
available in the documentation of the Python codecs module: available in the documentation of the Python codecs module:
<http://docs.python.org/library/codecs.html#standard-encodings>. <http://docs.python.org/library/codecs.html#standard-encodings>.
""" """
# Binary codecs are prefixed with _codec in Python 3
if encoding in 'base64 bz2 hex quopri uu zlib': if encoding in 'base64 bz2 hex quopri uu zlib':
encoding += '_codec' encoding += '_codec'
if encoding.endswith('_codec'):
text = text.encode()
# Do the encoding
try: try:
encoder = codecs.getencoder(encoding) encoder = codecs.getencoder(encoding)
except LookupError: except LookupError:
irc.errorInvalid(_('encoding'), encoding) irc.errorInvalid(_('encoding'), encoding)
text = encoder(text)[0] text = encoder(text)[0]
# If this is a binary codec, re-encode it with base64
if encoding.endswith('_codec') and encoding != 'base64_codec':
text = codecs.getencoder('base64_codec')(text)[0].decode()
# Change result into a string
if sys.version_info[0] < 3 and isinstance(text, unicode): if sys.version_info[0] < 3 and isinstance(text, unicode):
text = text.encode('utf-8') text = text.encode('utf-8')
elif sys.version_info[0] >= 3 and isinstance(text, bytes): elif sys.version_info[0] >= 3 and isinstance(text, bytes):
text = text.decode() text = text.decode()
# Reply
irc.reply(text.rstrip('\n')) irc.reply(text.rstrip('\n'))
encode = wrap(encode, ['something', 'text']) encode = wrap(encode, ['something', 'text'])
@ -96,13 +110,20 @@ class String(callbacks.Plugin):
available in the documentation of the Python codecs module: available in the documentation of the Python codecs module:
<http://docs.python.org/library/codecs.html#standard-encodings>. <http://docs.python.org/library/codecs.html#standard-encodings>.
""" """
# Binary codecs are prefixed with _codec in Python 3
if encoding in 'base64 bz2 hex quopri uu zlib': if encoding in 'base64 bz2 hex quopri uu zlib':
encoding += '_codec' encoding += '_codec'
# If this is a binary codec, pre-decode it with base64
if encoding.endswith('_codec') and encoding != 'base64_codec':
text = codecs.getdecoder('base64_codec')(text.encode())[0]
# Do the decoding
try: try:
decoder = codecs.getdecoder(encoding) decoder = codecs.getdecoder(encoding)
except LookupError: except LookupError:
irc.errorInvalid(_('encoding'), encoding) irc.errorInvalid(_('encoding'), encoding)
if sys.version_info[0] >= 3: if sys.version_info[0] >= 3 and not isinstance(text, bytes):
text = text.encode() text = text.encode()
try: try:
text = decoder(text)[0] text = decoder(text)[0]
@ -111,10 +132,14 @@ class String(callbacks.Plugin):
s=_('Base64 strings must be a multiple of 4 in ' s=_('Base64 strings must be a multiple of 4 in '
'length, padded with \'=\' if necessary.')) 'length, padded with \'=\' if necessary.'))
return return
# Change result into a string
if sys.version_info[0] < 3 and isinstance(text, unicode): if sys.version_info[0] < 3 and isinstance(text, unicode):
text = text.encode('utf-8') text = text.encode('utf-8')
elif sys.version_info[0] >= 3 and isinstance(text, bytes): elif sys.version_info[0] >= 3 and isinstance(text, bytes):
text = text.decode() text = text.decode()
# Reply
irc.reply(text) irc.reply(text)
decode = wrap(decode, ['something', 'text']) decode = wrap(decode, ['something', 'text'])