diff options
author | Zach Seils <seils@cisco.com> | 2014-11-10 00:28:58 -0500 |
---|---|---|
committer | Zach Seils <seils@cisco.com> | 2014-11-10 00:28:58 -0500 |
commit | 6bc72cf260f1372f76d3e679287b7780ee4f1d3a (patch) | |
tree | f90db9c8c69520f16157568ac07f22fd8cced413 | |
parent | 012f8a90501d314a694c136b73179af12b2f41e8 (diff) | |
download | python-magic-6bc72cf260f1372f76d3e679287b7780ee4f1d3a.tar.gz |
Add support for MAGIC_COMPRESS through Magic constructor.
-rw-r--r-- | README.md | 19 | ||||
-rw-r--r-- | magic.py | 10 |
2 files changed, 22 insertions, 7 deletions
@@ -18,10 +18,21 @@ functionality is exposed to the command line by the Unix command There is also a `Magic` class that provides more direct control, including overriding the magic database file and turning on character -encoding dectection. This is not recommended for general use. In -particular, it its not safe for sharing across multiple threads and +encoding detection. This is not recommended for general use. In +particular, it's not safe for sharing across multiple threads and will fail throw if this is attempted. + >>> f = magic.Magic(uncompress=True) + >>> f.from_file('testdata/test.gz') + 'ASCII text (gzip compressed data, was "test", last modified: Sat Jun 28 + 21:32:52 2008, from Unix)' + +You can also combine the flag options: + + >>> f = magic.Magic(mime=True, uncompress=True) + >>> f.from_file('testdata/test.gz') + 'text/plain' + ## Installation The current stable version of python-magic is available on pypi and @@ -54,12 +65,12 @@ On OSX: Attempting to run the 32-bit libmagic DLL in a 64-bit build of python will fail with this error. I'm not aware of any publically available 64-bit builds of libmagic. You'll either need to build - them yourself (pleae share docs!), or switch to a 32-bit Python. + them yourself (please share docs!), or switch to a 32-bit Python. ## Author Written by Adam Hupp in 2001 for a project that never got off the -ground. It origionally used SWIG for the C library bindings, but +ground. It originally used SWIG for the C library bindings, but switched to ctypes once that was part of the python standard library. You can contact me via my [website](http://hupp.org/adam) or @@ -35,7 +35,7 @@ class Magic: """ def __init__(self, mime=False, magic_file=None, mime_encoding=False, - keep_going=False): + keep_going=False, uncompress=False): """ Create a new libmagic wrapper. @@ -43,6 +43,7 @@ class Magic: mime_encoding - if True, codec is returned magic_file - use a mime database other than the system default keep_going - don't stop at the first match, keep going + uncompress - Try to look inside compressed files. """ self.flags = MAGIC_NONE if mime: @@ -52,6 +53,9 @@ class Magic: if keep_going: self.flags |= MAGIC_CONTINUE + if uncompress: + self.flags |= MAGIC_COMPRESS + self.cookie = magic_open(self.flags) magic_load(self.cookie, magic_file) @@ -82,7 +86,7 @@ class Magic: return self._handle509Bug(e) def _handle509Bug(self, e): - # libmagic 5.09 has a bug where it might mail to identify the + # libmagic 5.09 has a bug where it might fail to identify the # mimetype of a file and returns null from magic_file (and # likely _buffer), but also does not return an error message. if e.message is None and (self.flags & MAGIC_MIME): @@ -102,7 +106,7 @@ class Magic: # during shutdown magic_close may have been cleared already so # make sure it exists before using it. - # the self.cookie check should be unnessary and was an + # the self.cookie check should be unnecessary and was an # incorrect fix for a threading problem, however I'm leaving # it in because it's harmless and I'm slightly afraid to # remove it. |