-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"""This is a wrapper around pymagic. | ||
It aims to provide the same API but with the ability to load multiple magic | ||
files in the default api. | ||
""" | ||
import magic as pymagic | ||
from ctypes import c_int, c_char_p | ||
|
||
from helperFunctions.fileSystem import get_src_dir | ||
|
||
_magic_getpath = pymagic.libmagic.magic_getpath | ||
_magic_getpath.restype = c_char_p | ||
_magic_getpath.argtypes = [c_char_p, c_int] | ||
|
||
_sys_magic = _magic_getpath(None, 1) | ||
_fact_magic = f'{get_src_dir()}/bin/fact.mgc' | ||
_internal_symlink_magic = f'{get_src_dir()}/bin/internal_symlink.mgc' | ||
|
||
_magic_by_mime = {} | ||
|
||
|
||
def _get_magic(mime: bool) -> pymagic.Magic: | ||
if mime not in _magic_by_mime: | ||
_magic_by_mime[mime] = pymagic.Magic( | ||
mime=mime, | ||
magic_file=f'{_internal_symlink_magic}:{_fact_magic}:{_sys_magic}', | ||
) | ||
|
||
return _magic_by_mime[mime] | ||
|
||
|
||
def from_file(filename, mime=False): | ||
"""A wrapper for pymagic's ``magic.Magic.from_file``""" | ||
return _get_magic(mime).from_file(filename) | ||
|
||
|
||
def from_buffer(filename, mime=False): | ||
"""A wrapper for pymagic's ``magic.Magic.from_buffer``""" | ||
return _get_magic(mime).from_buffer(filename) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# ====================== faf internal ====================== | ||
|
||
# ---- faf internal link representation ---- | ||
0 string symbolic\ link\ -> symbolic link | ||
>17 string x to '%s' | ||
!:mime inode/symlink |