diff options
author | Adam Hupp <adam@hupp.org> | 2020-12-11 11:57:07 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-11 11:57:07 -0800 |
commit | a96081edc65ddcf20599b3fa1ef72eb2c55f1055 (patch) | |
tree | 05c80abbc80da3c96184999a33524fc9b069cabe | |
parent | 35026a19d6ff2b1934999ec6fb9c1812e4d4b07e (diff) | |
parent | ca14bfba7d1eeea543c9e00ea33d1487a49e68e0 (diff) | |
download | python-magic-a96081edc65ddcf20599b3fa1ef72eb2c55f1055.tar.gz |
Merge pull request #227 from psrok1/magic-descriptor
Added support for magic_descriptor routine
-rw-r--r-- | magic.py | 33 | ||||
-rw-r--r-- | magic.pyi | 3 | ||||
-rwxr-xr-x | test/test.py | 7 |
3 files changed, 43 insertions, 0 deletions
@@ -114,6 +114,13 @@ class Magic: except MagicException as e: return self._handle509Bug(e) + def from_descriptor(self, fd): + with self.lock: + try: + return maybe_decode(magic_descriptor(self.cookie, fd)) + except MagicException as e: + return self._handle509Bug(e) + def _handle509Bug(self, e): # libmagic 5.09 has a bug where it might fail to identify the # mimetype of a file and returns null from magic_file (and @@ -180,6 +187,20 @@ def from_buffer(buffer, mime=False): return m.from_buffer(buffer) +def from_descriptor(fd, mime=False): + """ + Accepts a file descriptor and returns the detected filetype. Return + value is the mimetype if mime=True, otherwise a human readable + name. + + >>> f = open("testdata/test.pdf") + >>> magic.from_descriptor(f.fileno()) + 'PDF document, version 1.2' + """ + m = _get_magic_type(mime) + return m.from_descriptor(fd) + + libmagic = None # Let's try to find magic or magic1 dll = ctypes.util.find_library('magic') \ @@ -287,6 +308,7 @@ _magic_file.errcheck = errorcheck_null def magic_file(cookie, filename): return _magic_file(cookie, coerce_filename(filename)) + _magic_buffer = libmagic.magic_buffer _magic_buffer.restype = c_char_p _magic_buffer.argtypes = [magic_t, c_void_p, c_size_t] @@ -297,6 +319,16 @@ def magic_buffer(cookie, buf): return _magic_buffer(cookie, buf, len(buf)) +_magic_descriptor = libmagic.magic_descriptor +_magic_descriptor.restype = c_char_p +_magic_descriptor.argtypes = [magic_t, c_int] +_magic_descriptor.errcheck = errorcheck_null + + +def magic_descriptor(cookie, fd): + return _magic_descriptor(cookie, fd) + + _magic_load = libmagic.magic_load _magic_load.restype = c_int _magic_load.argtypes = [magic_t, c_char_p] @@ -306,6 +338,7 @@ _magic_load.errcheck = errorcheck_negative_one def magic_load(cookie, filename): return _magic_load(cookie, coerce_filename(filename)) + magic_setflags = libmagic.magic_setflags magic_setflags.restype = c_int magic_setflags.argtypes = [magic_t, c_int] @@ -13,12 +13,14 @@ class Magic: def __init__(self, mime: bool = ..., magic_file: Optional[Any] = ..., mime_encoding: bool = ..., keep_going: bool = ..., uncompress: bool = ..., raw: bool = ...) -> None: ... def from_buffer(self, buf: Union[bytes, str]) -> Text: ... def from_file(self, filename: Union[bytes, str]) -> Text: ... + def from_descriptor(self, fd: int, mime: bool = ...) -> Text: ... def setparam(self, param: Any, val: Any): ... def getparam(self, param: Any): ... def __del__(self) -> None: ... def from_file(filename: Union[bytes, str], mime: bool = ...) -> Text: ... def from_buffer(buffer: Union[bytes, str], mime: bool = ...) -> Text: ... +def from_descriptor(fd: int, mime: bool = ...) -> Text: ... libmagic: Any dll: Any @@ -39,6 +41,7 @@ magic_errno: Any def magic_file(cookie: Any, filename: Any): ... def magic_buffer(cookie: Any, buf: Any): ... +def magic_descriptor(cookie: Any, fd: int): ... def magic_load(cookie: Any, filename: Any): ... magic_setflags: Any diff --git a/test/test.py b/test/test.py index eb5af89..3097e6e 100755 --- a/test/test.py +++ b/test/test.py @@ -56,6 +56,13 @@ class MagicTest(unittest.TestCase): self.assertEqual('application/pdf', magic.from_file(filename.encode('utf-8'), mime=True)) + def test_from_descriptor_str_and_bytes(self): + with open(os.path.join(self.TESTDATA_DIR, "test.pdf")) as f: + self.assertEqual('application/pdf', + magic.from_descriptor(f.fileno(), mime=True)) + self.assertEqual('application/pdf', + magic.from_descriptor(f.fileno(), mime=True)) + def test_from_buffer_str_and_bytes(self): m = magic.Magic(mime=True) |