summaryrefslogtreecommitdiff
path: root/alot/helper.py
diff options
context:
space:
mode:
authorHamish Downer <dmishd@gmail.com>2013-04-24 23:52:59 +0100
committerHamish Downer <dmishd@gmail.com>2013-04-24 23:52:59 +0100
commit4ba81e628181899e68a50094b88ede97b24493f0 (patch)
tree0935c2e9a1ff9412c0a9138b723cbaad76258dfb /alot/helper.py
parent7642647db734ef5ef36905bfb532816b3c22e7c2 (diff)
Fix guess_encoding()
Turns out commit 7a9da3b0f obliterated a difference that mattered - the different argument to magic.open() in guess_mimetype() and guess_encoding() This commit reinstates that difference, and has a correct use of the pip python-magic library to get the encoding.
Diffstat (limited to 'alot/helper.py')
-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')