diff options
author | Christos Zoulas <christos@zoulas.com> | 2022-07-24 23:55:19 +0000 |
---|---|---|
committer | Christos Zoulas <christos@zoulas.com> | 2022-07-24 23:55:19 +0000 |
commit | 835999a08198df69634c5ab5834b3e2201b406e6 (patch) | |
tree | 21f80d5631c5a1fa180bcd29d42690de375be8b2 /python | |
parent | 29c2c4ebb28497b6962e92d49a5583ea3b6dc9fa (diff) | |
download | file-git-835999a08198df69634c5ab5834b3e2201b406e6.tar.gz |
Better error checking and pyi file addition from (Hoel Bezier)
Diffstat (limited to 'python')
-rw-r--r-- | python/magic.py | 22 | ||||
-rw-r--r-- | python/magic.pyi | 93 |
2 files changed, 110 insertions, 5 deletions
diff --git a/python/magic.py b/python/magic.py index 4b074f31..8b48f757 100644 --- a/python/magic.py +++ b/python/magic.py @@ -272,16 +272,28 @@ def open(flags): Returns a magic object on success and None on failure. Flags argument as for setflags. """ - return Magic(_open(flags)) + magic_t = _open(flags) + if magic_t is None: + return None + return Magic(magic_t) # Objects used by `detect_from_` functions +class error(Exception): + pass + class MagicDetect(object): def __init__(self): - self.mime_magic = Magic(_open(MAGIC_MIME)) - self.mime_magic.load() - self.none_magic = Magic(_open(MAGIC_NONE)) - self.none_magic.load() + self.mime_magic = open(MAGIC_MIME) + if self.mime_magic is None: + raise error + if self.mime_magic.load() == -1: + raise error + self.none_magic = open(MAGIC_NONE) + if self.none_magic is None: + raise error + if self.none_magic.load() == -1: + raise error def __del__(self): self.mime_magic.close() diff --git a/python/magic.pyi b/python/magic.pyi new file mode 100644 index 00000000..0872de99 --- /dev/null +++ b/python/magic.pyi @@ -0,0 +1,93 @@ +from typing import NamedTuple +from io import IOBase + +MAGIC_NONE: int +NONE: int +MAGIC_DEBUG: int +DEBUG: int +MAGIC_SYMLINK: int +SYMLINK: int +MAGIC_COMPRESS: int +COMPRESS: int +MAGIC_DEVICES: int +DEVICES: int +MAGIC_MIME_TYPE: int +MIME_TYPE: int +MAGIC_CONTINUE: int +CONTINUE: int +MAGIC_CHECK: int +CHECK: int +MAGIC_PRESERVE_ATIME: int +PRESERVE_ATIME: int +MAGIC_RAW: int +RAW: int +MAGIC_ERROR: int +ERROR: int +MAGIC_MIME_ENCODING: int +MIME_ENCODING: int +MAGIC_MIME: int +MIME: int +MAGIC_APPLE: int +APPLE: int +MAGIC_NO_CHECK_COMPRESS: int +NO_CHECK_COMPRESS: int +MAGIC_NO_CHECK_TAR: int +NO_CHECK_TAR: int +MAGIC_NO_CHECK_SOFT: int +NO_CHECK_SOFT: int +MAGIC_NO_CHECK_APPTYPE: int +NO_CHECK_APPTYPE: int +MAGIC_NO_CHECK_ELF: int +NO_CHECK_ELF: int +MAGIC_NO_CHECK_TEXT: int +NO_CHECK_TEXT: int +MAGIC_NO_CHECK_CDF: int +NO_CHECK_CDF: int +MAGIC_NO_CHECK_TOKENS: int +NO_CHECK_TOKENS: int +MAGIC_NO_CHECK_ENCODING: int +NO_CHECK_ENCODING: int +MAGIC_NO_CHECK_BUILTIN: int +NO_CHECK_BUILTIN: int +MAGIC_PARAM_INDIR_MAX: int +PARAM_INDIR_MAX: int +MAGIC_PARAM_NAME_MAX: int +PARAM_NAME_MAX: int +MAGIC_PARAM_ELF_PHNUM_MAX: int +PARAM_ELF_PHNUM_MAX: int +MAGIC_PARAM_ELF_SHNUM_MAX: int +PARAM_ELF_SHNUM_MAX: int +MAGIC_PARAM_ELF_NOTES_MAX: int +PARAM_ELF_NOTES_MAX: int +MAGIC_PARAM_REGEX_MAX: int +PARAM_REGEX_MAX: int +MAGIC_PARAM_BYTES_MAX: int +PARAM_BYTES_MAX: int + +class FileMagic(NamedTuple): + mime_type: str + encoding: str + name: str + +class Magic: + def close(self) -> None: ... + def file(self, filename: str | bytes) -> str | None: ... + def descriptor(self, fd: int) -> str | None: ... + def buffer(self, buf: str | bytes) -> str | None: ... + def error(self) -> str | None: ... + def setflags(self, flags: int) -> int: ... + def load(self, filename: str | bytes | None = ...) -> int: ... + def compile(self, dbs: str | bytes) -> int: ... + def check(self, dbs: str | bytes) -> int: ... + def list(self, dbs: str | bytes) -> int: ... + def errno(self) -> int: ... + def getparam(self, param: int) -> int: ... + def setparam(self, param: int, value: int) -> int: ... + +def open(flags: int) -> Magic | None: ... + +class error(Exception): ... + +def detect_from_filename(filename: str | bytes) -> FileMagic: ... +def detect_from_fobj(fobj: IOBase) -> FileMagic: ... +def detect_from_content(byte_content: str | bytes) -> FileMagic: ... |