Skip to content
This repository has been archived by the owner on Nov 4, 2018. It is now read-only.

Commit

Permalink
Improved compatibility with old python-magic
Browse files Browse the repository at this point in the history
Sadly there are two "magic" modules for python with
different APIs.  Improving compatibility wrapper to
better handle both.
  • Loading branch information
mludvig committed Jan 9, 2012
1 parent f49ecb3 commit 6af4955
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions S3/S3.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,22 +35,26 @@
magic_ = magic.Magic(mime=True) magic_ = magic.Magic(mime=True)
def mime_magic(file): def mime_magic(file):
return magic_.from_file(file) return magic_.from_file(file)
except AttributeError: except (TypeError, AttributeError):
## Older python-magic versions ## Older python-magic versions
magic_ = magic.open(magic.MAGIC_MIME) magic_ = magic.open(magic.MAGIC_MIME_TYPE)
magic_.load() magic_.load()
def mime_magic(file): def mime_magic(file):
return magic_.file(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 magic_warned = False
def mime_magic(file): def mime_magic(file):
global magic_warned global magic_warned
if (not 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 magic_warned = True
return mimetypes.guess_type(file)[0] return mimetypes.guess_type(file)[0]



__all__ = [] __all__ = []
class S3Request(object): class S3Request(object):
def __init__(self, s3, method_string, resource, headers, params = {}): def __init__(self, s3, method_string, resource, headers, params = {}):
Expand Down

0 comments on commit 6af4955

Please sign in to comment.