mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-02-17 06:00:42 +01:00
String: Use the 'codecs' module and make it py3k-friendly.
This commit is contained in:
parent
de5747aecb
commit
65eb79b8f2
@ -28,7 +28,9 @@
|
|||||||
# POSSIBILITY OF SUCH DAMAGE.
|
# POSSIBILITY OF SUCH DAMAGE.
|
||||||
###
|
###
|
||||||
|
|
||||||
|
import sys
|
||||||
import types
|
import types
|
||||||
|
import codecs
|
||||||
import binascii
|
import binascii
|
||||||
|
|
||||||
import supybot.utils as utils
|
import supybot.utils as utils
|
||||||
@ -72,10 +74,18 @@ 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>.
|
||||||
"""
|
"""
|
||||||
|
if encoding in 'base64 bz2 hex quopri uu zlib':
|
||||||
|
encoding += '_codec'
|
||||||
try:
|
try:
|
||||||
irc.reply(text.encode(encoding).rstrip('\n'))
|
encoder = codecs.getencoder(encoding)
|
||||||
except LookupError:
|
except LookupError:
|
||||||
irc.errorInvalid(_('encoding'), encoding)
|
irc.errorInvalid(_('encoding'), encoding)
|
||||||
|
text = encoder(text)[0]
|
||||||
|
if sys.version_info[0] < 3 and isinstance(text, unicode):
|
||||||
|
text = text.encode('utf-8')
|
||||||
|
elif sys.version_info[0] >= 3 and isinstance(text, bytes):
|
||||||
|
text = text.decode()
|
||||||
|
irc.reply(text.rstrip('\n'))
|
||||||
encode = wrap(encode, ['something', 'text'])
|
encode = wrap(encode, ['something', 'text'])
|
||||||
|
|
||||||
@internationalizeDocstring
|
@internationalizeDocstring
|
||||||
@ -86,19 +96,26 @@ 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>.
|
||||||
"""
|
"""
|
||||||
|
if encoding in 'base64 bz2 hex quopri uu zlib':
|
||||||
|
encoding += '_codec'
|
||||||
try:
|
try:
|
||||||
s = text.decode(encoding)
|
decoder = codecs.getdecoder(encoding)
|
||||||
# Not all encodings decode to a unicode object. Only encode those
|
|
||||||
# that do.
|
|
||||||
if isinstance(s, unicode):
|
|
||||||
s = s.encode('utf-8')
|
|
||||||
irc.reply(s)
|
|
||||||
except LookupError:
|
except LookupError:
|
||||||
irc.errorInvalid(_('encoding'), encoding)
|
irc.errorInvalid(_('encoding'), encoding)
|
||||||
|
if sys.version_info[0] >= 3:
|
||||||
|
text = text.encode()
|
||||||
|
try:
|
||||||
|
text = decoder(text)[0]
|
||||||
except binascii.Error:
|
except binascii.Error:
|
||||||
irc.errorInvalid(_('base64 string'),
|
irc.errorInvalid(_('base64 string'),
|
||||||
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
|
||||||
|
if sys.version_info[0] < 3 and isinstance(text, unicode):
|
||||||
|
text = text.encode('utf-8')
|
||||||
|
elif sys.version_info[0] >= 3 and isinstance(text, bytes):
|
||||||
|
text = text.decode()
|
||||||
|
irc.reply(text)
|
||||||
decode = wrap(decode, ['something', 'text'])
|
decode = wrap(decode, ['something', 'text'])
|
||||||
|
|
||||||
@internationalizeDocstring
|
@internationalizeDocstring
|
||||||
|
Loading…
x
Reference in New Issue
Block a user