diff options
-rw-r--r-- | alot/helper.py | 30 | ||||
-rwxr-xr-x | setup.py | 3 |
2 files changed, 8 insertions, 25 deletions
diff --git a/alot/helper.py b/alot/helper.py index ba41986e..612e5dba 100644 --- a/alot/helper.py +++ b/alot/helper.py @@ -25,6 +25,7 @@ from email.mime.image import MIMEImage from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart +import chardet import urwid import magic from twisted.internet import reactor @@ -386,34 +387,15 @@ def guess_mimetype(blob): def guess_encoding(blob): - """ - uses file magic to determine the encoding of the given data blob. + """Use chardet to guess the encoding of a given data blob - :param blob: file content as read by file.read() - :type blob: data + :param blob: A blob of bytes + :type blob: bytes :returns: encoding :rtype: str """ - # this is a bit of a hack to support different versions of python magic. - # Hopefully at some point this will no longer be necessary - # - # the version with open() is the bindings shipped with the file source from - # http://darwinsys.com/file/ - this is what is used by the python-magic - # package on Debian/Ubuntu. However it is not available on pypi/via pip. - # - # the version with from_buffer() is available at - # https://github.com/ahupp/python-magic and directly installable via pip. - # - # for more detail see https://github.com/pazz/alot/pull/588 - if hasattr(magic, 'open'): - m = magic.open(magic.MAGIC_MIME_ENCODING) - m.load() - return m.buffer(blob) - elif hasattr(magic, 'from_buffer'): - m = magic.Magic(mime_encoding=True) - return m.from_buffer(blob) - else: - raise Exception('Unknown magic API') + info = chardet.detect(blob) + return info['encoding'] def try_decode(blob): @@ -48,7 +48,8 @@ setup( 'twisted>=10.2.0', 'python-magic', 'configobj>=4.7.0', - 'gpg' + 'gpg', + 'chardet', ], tests_require=[ 'mock', |