Browse code

Improved compatibility with old python-magic

Sadly there are two "magic" modules for python with
different APIs. Improving compatibility wrapper to
better handle both.

Michal Ludvig authored on 2012/01/09 10:29:39
Showing 1 changed files
... ...
@@ -35,22 +35,26 @@ try:
35 35
         magic_ = magic.Magic(mime=True)
36 36
         def mime_magic(file):
37 37
             return magic_.from_file(file)
38
-    except AttributeError:
38
+    except (TypeError, AttributeError):
39 39
         ## Older python-magic versions
40
-        magic_ = magic.open(magic.MAGIC_MIME)
40
+        magic_ = magic.open(magic.MAGIC_MIME_TYPE)
41 41
         magic_.load()
42 42
         def mime_magic(file):
43 43
             return magic_.file(file)
44
-except ImportError:
44
+except ImportError, e:
45
+    if e.message.find("magic") >= 0:
46
+        magic_message = "Module python-magic is not available."
47
+    else:
48
+        magic_message = "Module python-magic can't be used (%s)." % e.message
49
+    magic_message += " Guessing MIME types based on file extensions."
45 50
     magic_warned = False
46 51
     def mime_magic(file):
47 52
         global magic_warned
48 53
         if (not magic_warned):
49
-            warning("python-magic is not available, guessing MIME types based on file extensions only")
54
+            warning(magic_message)
50 55
             magic_warned = True
51 56
         return mimetypes.guess_type(file)[0]
52 57
 
53
-
54 58
 __all__ = []
55 59
 class S3Request(object):
56 60
     def __init__(self, s3, method_string, resource, headers, params = {}):