summaryrefslogtreecommitdiff
path: root/alot
diff options
context:
space:
mode:
Diffstat (limited to 'alot')
-rw-r--r--alot/helper.py26
1 files changed, 23 insertions, 3 deletions
diff --git a/alot/helper.py b/alot/helper.py
index 20068ca7..e1fbdabe 100644
--- a/alot/helper.py
+++ b/alot/helper.py
@@ -375,7 +375,26 @@ def guess_mimetype(blob):
:rtype: str
"""
mimetype = 'application/octet-stream'
- magictype = guess_encoding(blob)
+ # 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 from https://github.com/ahupp/python-magic
+ # which is installable via pip.
+ #
+ # for more detail see https://github.com/pazz/alot/pull/588
+ if hasattr(magic, 'open'):
+ m = magic.open(magic.MAGIC_MIME_TYPE)
+ m.load()
+ magictype = m.buffer(blob)
+ elif hasattr(magic, 'from_buffer'):
+ magictype = magic.from_buffer(blob, mime=True)
+ else:
+ raise Exception('Unknown magic API')
+
# libmagic does not always return proper mimetype strings, cf. issue #459
if re.match(r'\w+\/\w+', magictype):
mimetype = magictype
@@ -403,11 +422,12 @@ def guess_encoding(blob):
#
# for more detail see https://github.com/pazz/alot/pull/588
if hasattr(magic, 'open'):
- m = magic.open(magic.MAGIC_MIME_TYPE)
+ m = magic.open(magic.MAGIC_MIME_ENCODING)
m.load()
return m.buffer(blob)
elif hasattr(magic, 'from_buffer'):
- return magic.from_buffer(blob, mime=True)
+ m = magic.Magic(mime_encoding=True)
+ return m.from_buffer(blob)
else:
raise Exception('Unknown magic API')