From 800946dee24a4ef94d4f2f604bc4e9557561c58e Mon Sep 17 00:00:00 2001 From: darkbarker Date: Wed, 21 Oct 2020 22:33:44 +0500 Subject: MAGIC_EXTENSION support (file --extension) --- magic.py | 6 +++++- test/test.py | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) 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') -- cgit v1.2.1