Permalink
Browse files

Improved compatibility with old python-magic

Sadly there are two "magic" modules for python with
different APIs.  Improving compatibility wrapper to
better handle both.
  • Loading branch information...
1 parent f49ecb3 commit 6af49552113f62a55f513822006141565d3ea78a @mludvig mludvig committed Jan 9, 2012
Showing with 9 additions and 5 deletions.
  1. +9 −5 S3/S3.py
View
@@ -35,22 +35,26 @@
magic_ = magic.Magic(mime=True)
def mime_magic(file):
return magic_.from_file(file)
- except AttributeError:
+ except (TypeError, AttributeError):
## Older python-magic versions
- magic_ = magic.open(magic.MAGIC_MIME)
+ magic_ = magic.open(magic.MAGIC_MIME_TYPE)
magic_.load()
def mime_magic(file):
return magic_.file(file)
-except ImportError:
+except ImportError, e:
+ if e.message.find("magic") >= 0:
+ magic_message = "Module python-magic is not available."
+ else:
+ magic_message = "Module python-magic can't be used (%s)." % e.message
+ magic_message += " Guessing MIME types based on file extensions."
magic_warned = False
def mime_magic(file):
global magic_warned
if (not magic_warned):
- warning("python-magic is not available, guessing MIME types based on file extensions only")
+ warning(magic_message)
magic_warned = True
return mimetypes.guess_type(file)[0]
-
__all__ = []
class S3Request(object):
def __init__(self, s3, method_string, resource, headers, params = {}):

0 comments on commit 6af4955

Please sign in to comment.