summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Hupp <adam@hupp.org>2020-10-23 11:53:44 -0700
committerGitHub <noreply@github.com>2020-10-23 11:53:44 -0700
commit2a755bed47e654d353f002eb2ee19820dc21d53a (patch)
tree7876b1e26cccbcb85141be29e735c716f2c4c625
parent57b18fc1153bd98950820364938b2dd8be3784ec (diff)
parent800946dee24a4ef94d4f2f604bc4e9557561c58e (diff)
downloadpython-magic-2a755bed47e654d353f002eb2ee19820dc21d53a.tar.gz
Merge pull request #224 from darkbarker/extension-support
MAGIC_EXTENSION support (file --extension)
-rw-r--r--magic.py6
-rwxr-xr-xtest/test.py7
2 files changed, 12 insertions, 1 deletions
diff --git a/magic.py b/magic.py
index cdf40c5..773e4ae 100644
--- a/magic.py
+++ b/magic.py
@@ -37,7 +37,7 @@ class Magic:
"""
def __init__(self, mime=False, magic_file=None, mime_encoding=False,
- keep_going=False, uncompress=False, raw=False):
+ keep_going=False, uncompress=False, raw=False, extension=False):
"""
Create a new libmagic wrapper.
@@ -47,6 +47,7 @@ class Magic:
keep_going - don't stop at the first match, keep going
uncompress - Try to look inside compressed files.
raw - Do not try to decode "non-printable" chars.
+ extension - Print a slash-separated list of valid extensions for the file type found.
"""
self.cookie = None
@@ -61,6 +62,8 @@ class Magic:
self.flags |= MAGIC_COMPRESS
if raw:
self.flags |= MAGIC_RAW
+ if extension:
+ self.flags |= MAGIC_EXTENSION
self.cookie = magic_open(self.flags)
self.lock = threading.Lock()
@@ -357,6 +360,7 @@ MAGIC_MIME_ENCODING = 0x000400 # Return the MIME encoding
# TODO: should be
# MAGIC_MIME = MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING
MAGIC_MIME = 0x000010 # Return a mime string
+MAGIC_EXTENSION = 0x1000000 # Return a /-separated list of extensions
MAGIC_CONTINUE = 0x000020 # Return all matches
MAGIC_CHECK = 0x000040 # Print warnings to stderr
diff --git a/test/test.py b/test/test.py
index 6f0f496..15a4115 100755
--- a/test/test.py
+++ b/test/test.py
@@ -111,6 +111,13 @@ class MagicTest(unittest.TestCase):
finally:
del os.environ['TZ']
+ def test_extension(self):
+ m = magic.Magic(extension=True)
+ self.assert_values(m, {
+ 'test.gz': 'gz/tgz/tpz/zabw/svgz',
+ 'name_use.jpg': 'jpeg/jpg/jpe/jfif',
+ })
+
def test_unicode_result_nonraw(self):
m = magic.Magic(raw=False)
src = os.path.join(MagicTest.TESTDATA_DIR, 'pgpunicode')