Skip to content

Commit fd279e0

Browse files
risicleahupp
authored andcommitted
Magic.__init__: add kwargs to enable/disable different types of magic detection
1 parent 64ed0bd commit fd279e0

2 files changed

Lines changed: 42 additions & 6 deletions

File tree

magic/__init__.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ class Magic:
4040

4141
def __init__(self, mime=False, magic_file=None, mime_encoding=False,
4242
keep_going=False, uncompress=False, raw=False, extension=False,
43-
follow_symlinks=False):
43+
follow_symlinks=False, check_tar=True, check_soft=True,
44+
check_apptype=True, check_elf=True, check_text=True,
45+
check_cdf=True, check_csv=True, check_encoding=True,
46+
check_json=True, check_simh=True):
4447
"""
4548
Create a new libmagic wrapper.
4649
@@ -69,6 +72,27 @@ def __init__(self, mime=False, magic_file=None, mime_encoding=False,
6972
if follow_symlinks:
7073
self.flags |= MAGIC_SYMLINK
7174

75+
if not check_tar:
76+
self.flags |= MAGIC_NO_CHECK_TAR
77+
if not check_soft:
78+
self.flags |= MAGIC_NO_CHECK_SOFT
79+
if not check_apptype:
80+
self.flags |= MAGIC_NO_CHECK_APPTYPE
81+
if not check_elf:
82+
self.flags |= MAGIC_NO_CHECK_ELF
83+
if not check_text:
84+
self.flags |= MAGIC_NO_CHECK_TEXT
85+
if not check_cdf:
86+
self.flags |= MAGIC_NO_CHECK_CDF
87+
if not check_csv:
88+
self.flags |= MAGIC_NO_CHECK_CSV
89+
if not check_encoding:
90+
self.flags |= MAGIC_NO_CHECK_ENCODING
91+
if not check_json:
92+
self.flags |= MAGIC_NO_CHECK_JSON
93+
if not check_simh:
94+
self.flags |= MAGIC_NO_CHECK_SIMH
95+
7296
self.cookie = magic_open(self.flags)
7397
self.lock = threading.Lock()
7498

@@ -411,10 +435,16 @@ def version():
411435
MAGIC_NO_CHECK_SOFT = 0x004000 # Don't check magic entries
412436
MAGIC_NO_CHECK_APPTYPE = 0x008000 # Don't check application type
413437
MAGIC_NO_CHECK_ELF = 0x010000 # Don't check for elf details
414-
MAGIC_NO_CHECK_ASCII = 0x020000 # Don't check for ascii files
415-
MAGIC_NO_CHECK_TROFF = 0x040000 # Don't check ascii/troff
416-
MAGIC_NO_CHECK_FORTRAN = 0x080000 # Don't check ascii/fortran
417-
MAGIC_NO_CHECK_TOKENS = 0x100000 # Don't check ascii/tokens
438+
MAGIC_NO_CHECK_TEXT = 0x020000 # Don't check for ascii files
439+
MAGIC_NO_CHECK_ASCII = 0x020000 # Deprecated alias for MAGIC_NO_CHECK_TEXT
440+
MAGIC_NO_CHECK_TROFF = 0x040000 # Don't check ascii/troff (deprecated)
441+
MAGIC_NO_CHECK_FORTRAN = 0x080000 # Don't check ascii/fortran (deprecated)
442+
MAGIC_NO_CHECK_TOKENS = 0x100000 # Don't check ascii/tokens (deprecated)
443+
MAGIC_NO_CHECK_CDF = 0x0040000 # Don't check for CDF files
444+
MAGIC_NO_CHECK_CSV = 0x0080000 # Don't check for CSV files
445+
MAGIC_NO_CHECK_ENCODING = 0x0200000 # Don't check text encodings
446+
MAGIC_NO_CHECK_JSON = 0x0400000 # Don't check for JSON files
447+
MAGIC_NO_CHECK_SIMH = 0x0800000 # Don't check for SIMH tape files
418448

419449
MAGIC_PARAM_INDIR_MAX = 0 # Recursion limit for indirect magic
420450
MAGIC_PARAM_NAME_MAX = 1 # Use count limit for name/use magic

magic/__init__.pyi

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Magic:
1111
flags: int = ...
1212
cookie: Any = ...
1313
lock: threading.Lock = ...
14-
def __init__(self, mime: bool = ..., magic_file: Optional[Any] = ..., mime_encoding: bool = ..., keep_going: bool = ..., uncompress: bool = ..., raw: bool = ..., extension: bool = ..., follow_symlinks: bool = ...) -> None: ...
14+
def __init__(self, mime: bool = ..., magic_file: Optional[Any] = ..., mime_encoding: bool = ..., keep_going: bool = ..., uncompress: bool = ..., raw: bool = ..., extension: bool = ..., follow_symlinks: bool = ..., check_tar: bool = ..., check_soft: bool = ..., check_apptype: bool = ..., check_elf: bool = ..., check_text: bool = ..., check_encoding: bool = ..., check_json: bool = ..., check_simh: bool = ...) -> None: ...
1515
def from_buffer(self, buf: Union[bytes, str]) -> Text: ...
1616
def from_file(self, filename: Union[bytes, str, PathLike]) -> Text: ...
1717
def from_descriptor(self, fd: int, mime: bool = ...) -> Text: ...
@@ -74,10 +74,16 @@ MAGIC_NO_CHECK_TAR: int
7474
MAGIC_NO_CHECK_SOFT: int
7575
MAGIC_NO_CHECK_APPTYPE: int
7676
MAGIC_NO_CHECK_ELF: int
77+
MAGIC_NO_CHECK_TEXT: int
7778
MAGIC_NO_CHECK_ASCII: int
7879
MAGIC_NO_CHECK_TROFF: int
7980
MAGIC_NO_CHECK_FORTRAN: int
81+
MAGIC_NO_CHECK_CDF: int
82+
MAGIC_NO_CHECK_CSV: int
8083
MAGIC_NO_CHECK_TOKENS: int
84+
MAGIC_NO_CHECK_ENCODING: int
85+
MAGIC_NO_CHECK_JSON: int
86+
MAGIC_NO_CHECK_SIMH: int
8187
MAGIC_PARAM_INDIR_MAX: int
8288
MAGIC_PARAM_NAME_MAX: int
8389
MAGIC_PARAM_ELF_PHNUM_MAX: int

0 commit comments

Comments
 (0)