diff options
author | Yann Rouillard <yann@pleiades.fr.eu.org> | 2013-07-01 23:35:10 +0200 |
---|---|---|
committer | Patrick Totzke <patricktotzke@gmail.com> | 2013-07-18 09:38:28 +0100 |
commit | 00ae89517d0b0d03fb7a48a36b498f82d87e1922 (patch) | |
tree | fbf54adf5423a85b6971de95b7e005eea4d2c5d3 /alot/helper.py | |
parent | 045ccff05175de053b469870fe76ee5a60fdf65b (diff) |
add a generic function to test libmagic version
Diffstat (limited to 'alot/helper.py')
-rw-r--r-- | alot/helper.py | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/alot/helper.py b/alot/helper.py index 662cb3b9..c97f187c 100644 --- a/alot/helper.py +++ b/alot/helper.py @@ -437,6 +437,29 @@ def guess_encoding(blob): raise Exception('Unknown magic API') +def libmagic_version_at_least(version): + """ + checks if the libmagic library installed is more recent than a given + version. + + :param version: minimum version expected in the form XYY (i.e. 5.14 -> 514) + with XYY >= 513 + """ + if hasattr(magic, 'open'): + magic_wrapper = magic._libraries['magic'] + elif hasattr(magic, 'from_buffer'): + magic_wrapper = magic.libmagic + else: + raise Exception('Unknown magic API') + + if not hasattr(magic_wrapper, 'magic_version'): + # The magic_version function has been introduced in libmagic 5.13, + # if it's not present, we can't guess right, so let's assume False + return False + + return (magic_wrapper.magic_version >= version) + + # TODO: make this work on blobs, not paths def mimewrap(path, filename=None, ctype=None): content = open(path, 'rb').read() @@ -446,11 +469,8 @@ def mimewrap(path, filename=None, ctype=None): # 'application/msword' (see #179 and #186 in libmagic bugtracker) # This is a workaround, based on file extension, useful as long # as distributions still ship libmagic 5.11. - # To see if we must apply the workaround, we just check the - # availability of the magic_version function which has been - # introduced in libmagic 5.13 - if (not hasattr(magic._libraries['magic'], 'magic_version') and - ctype == 'application/msword'): + if (ctype == 'application/msword' and + not libmagic_version_at_least(513)): mimetype, encoding = mimetypes.guess_type(path) if mimetype: ctype = mimetype |