summaryrefslogtreecommitdiff
path: root/alot/helper.py
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2018-07-17 10:10:10 -0700
committerGitHub <noreply@github.com>2018-07-17 10:10:10 -0700
commit920043b3f7dc039c9e75aa62134cc662dfa94fda (patch)
tree0e81ae50580b86436f7f42c7f9b45d513679f295 /alot/helper.py
parente4e82d3b81e50fc69fd4a929ff162f335b644ba0 (diff)
parentc51ad1c31b9ea0c9dd0b155b2fa1235eab8ddaa5 (diff)
Merge pull request #1266 from mjg/message-from-bytes
Message from bytes
Diffstat (limited to 'alot/helper.py')
-rw-r--r--alot/helper.py32
1 files changed, 24 insertions, 8 deletions
diff --git a/alot/helper.py b/alot/helper.py
index 70d375c8..d0c21a49 100644
--- a/alot/helper.py
+++ b/alot/helper.py
@@ -22,7 +22,6 @@ 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
@@ -384,17 +383,34 @@ def guess_mimetype(blob):
def guess_encoding(blob):
- """Use chardet to guess the encoding of a given data blob
+ """
+ uses file magic to determine the encoding of the given data blob.
- :param blob: A blob of bytes
- :type blob: bytes
+ :param blob: file content as read by file.read()
+ :type blob: data
:returns: encoding
:rtype: str
"""
- info = chardet.detect(blob)
- logging.debug('Encoding %s with confidence %f',
- info['encoding'], info['confidence'])
- return info['encoding']
+ # 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')
def try_decode(blob):