Pro.magic
— magic library API¶
Usage Example¶
The following code example demonstrates how to use the magic library API:
from Pro.magic import *
def magicTest():
m = magic_open(MAGIC_NONE)
if not m or magic_load(m, magic_builtin_db_name()) != 0:
return
buf = bytes([
0x4D, 0x5A, 0x90, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00,
0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0x00, 0x00,
0x0E, 0x1F, 0xBA, 0x0E, 0x00, 0xB4, 0x09, 0xCD, 0x21, 0xB8, 0x01, 0x4C, 0xCD, 0x21, 0x54, 0x68,
0x69, 0x73, 0x20, 0x70, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x20, 0x63, 0x61, 0x6E, 0x6E, 0x6F,
0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6E, 0x20, 0x69, 0x6E, 0x20, 0x44, 0x4F, 0x53, 0x20,
0x6D, 0x6F, 0x64, 0x65, 0x2E, 0x0D, 0x0D, 0x0A, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x3C, 0x56, 0xBC, 0xD9, 0x78, 0x37, 0xD2, 0x8A, 0x78, 0x37, 0xD2, 0x8A, 0x78, 0x37, 0xD2, 0x8A,
0x5F, 0xF1, 0xBF, 0x8A, 0x73, 0x37, 0xD2, 0x8A, 0x5F, 0xF1, 0x52, 0x8A, 0x79, 0x37, 0xD2, 0x8A,
0x5F, 0xF1, 0xA9, 0x8A, 0x65, 0x37, 0xD2, 0x8A, 0x78, 0x37, 0xD3, 0x8A, 0xB4, 0x35, 0xD2, 0x8A,
0x71, 0x4F, 0x51, 0x8A, 0x96, 0x37, 0xD2, 0x8A, 0x71, 0x4F, 0x47, 0x8A, 0x6A, 0x37, 0xD2, 0x8A,
0x66, 0x65, 0x56, 0x8A, 0x7B, 0x37, 0xD2, 0x8A, 0x5F, 0xF1, 0xBC, 0x8A, 0x71, 0x37, 0xD2, 0x8A,
0x71, 0x4F, 0x56, 0x8A, 0x2D, 0x36, 0xD2, 0x8A, 0x66, 0x65, 0x46, 0x8A, 0x79, 0x37, 0xD2, 0x8A,
0x71, 0x4F, 0x43, 0x8A, 0x79, 0x37, 0xD2, 0x8A, 0x52, 0x69, 0x63, 0x68, 0x78, 0x37, 0xD2, 0x8A,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45
])
info = magic_buffer(m, buf)
print(info)
magic_close(m)
Module API¶
Pro.magic module API.
Attributes:
Return the Apple creator and type.
Print warnings to standard error output.
Check inside compressed files.
Perform transparent decompression.
Return all matches instead of the first.
Enable debugging.
Look inside device files.
Treat file not found errors as real errors.
Return a list of valid extensions for the file type.
Return the MIME type and encoding.
Return the MIME encoding of the file.
Return the MIME type.
Suppress the description field in the output.
No special handling.
Don’t check for application type.
Don’t perform any built-in tests; only use the magic file.
Don’t check for Composite Document Files (CDF).
Don’t check for compressed files.
Don’t check for ELF files.
Don’t check for text encodings.
Don’t check soft magic entries.
Don’t check for tar files.
Don’t check for text files.
Don’t check for tokens.
Preserve access time on exit.
Don’t translate unprintable characters.
Follow symbolic links.
Functions:
magic_buffer
(m, buf)Retrieves a textual description of the contents of the provided buffer.
Returns the pathname of the default magic database.
magic_check
(m, fname)Checks the validity of entries in the magic database file.
magic_close
(m)Closes the magic database and deallocates resources.
magic_compile
(m, fname)Compiles the specified magic database file.
magic_descriptor
(m, fd)Retrieves a textual description of the contents of the file associated with the given file descriptor.
magic_errno
(m)Retrieves the last operating system error number encountered.
magic_error
(m)Retrieves a textual description of the last error encountered.
magic_file
(m, fname)Retrieves a textual description of the contents of the specified file.
magic_getpath
(fname, action)Retrieves the path to the magic database file based on the given action.
magic_list
(m, fname)Lists magic entries in the specified magic database file.
magic_load
(m, fname)Loads the magic database file specified.
magic_open
(flags)Allocates a magic cookie and returns a magic database handle.
magic_setflags
(m, flags)Sets flags on the magic cookie to control magic behavior.
- MAGIC_APPLE: Final[int]¶
Return the Apple creator and type.
See also
magic_file()
andmagic_buffer()
.
- MAGIC_CHECK: Final[int]¶
Print warnings to standard error output.
See also
magic_check()
andmagic_compile()
.
- MAGIC_COMPRESS: Final[int]¶
Check inside compressed files.
See also
magic_file()
andmagic_buffer()
.
- MAGIC_COMPRESS_TRANSP: Final[int]¶
Perform transparent decompression.
See also
magic_file()
andmagic_buffer()
.
- MAGIC_CONTINUE: Final[int]¶
Return all matches instead of the first.
See also
magic_file()
andmagic_buffer()
.
- MAGIC_DEBUG: Final[int]¶
Enable debugging.
See also
magic_open()
andmagic_setflags()
.
- MAGIC_DEVICES: Final[int]¶
Look inside device files.
See also
magic_file()
.
- MAGIC_ERROR: Final[int]¶
Treat file not found errors as real errors.
See also
magic_file()
andmagic_buffer()
.
- MAGIC_EXTENSION: Final[int]¶
Return a list of valid extensions for the file type.
See also
magic_file()
andmagic_buffer()
.
- MAGIC_MIME: Final[int]¶
Return the MIME type and encoding.
See also
magic_file()
andmagic_buffer()
.
- MAGIC_MIME_ENCODING: Final[int]¶
Return the MIME encoding of the file.
See also
magic_file()
andmagic_buffer()
.
- MAGIC_MIME_TYPE: Final[int]¶
Return the MIME type.
See also
magic_file()
andmagic_buffer()
.
- MAGIC_NODESC: Final[int]¶
Suppress the description field in the output.
See also
magic_file()
andmagic_buffer()
.
- MAGIC_NONE: Final[int]¶
No special handling.
See also
magic_open()
.
- MAGIC_NO_CHECK_APPTYPE: Final[int]¶
Don’t check for application type.
See also
magic_setflags()
.
- MAGIC_NO_CHECK_BUILTIN: Final[int]¶
Don’t perform any built-in tests; only use the magic file.
See also
magic_setflags()
.
- MAGIC_NO_CHECK_CDF: Final[int]¶
Don’t check for Composite Document Files (CDF).
See also
magic_setflags()
.
- MAGIC_NO_CHECK_COMPRESS: Final[int]¶
Don’t check for compressed files.
See also
magic_setflags()
.
- MAGIC_NO_CHECK_ELF: Final[int]¶
Don’t check for ELF files.
See also
magic_setflags()
.
- MAGIC_NO_CHECK_ENCODING: Final[int]¶
Don’t check for text encodings.
See also
magic_setflags()
.
- MAGIC_NO_CHECK_SOFT: Final[int]¶
Don’t check soft magic entries.
See also
magic_setflags()
.
- MAGIC_NO_CHECK_TAR: Final[int]¶
Don’t check for tar files.
See also
magic_setflags()
.
- MAGIC_NO_CHECK_TEXT: Final[int]¶
Don’t check for text files.
See also
magic_setflags()
.
- MAGIC_NO_CHECK_TOKENS: Final[int]¶
Don’t check for tokens.
See also
magic_setflags()
.
- MAGIC_PRESERVE_ATIME: Final[int]¶
Preserve access time on exit.
See also
magic_open()
.
- MAGIC_RAW: Final[int]¶
Don’t translate unprintable characters.
See also
magic_file()
andmagic_buffer()
.
- MAGIC_SYMLINK: Final[int]¶
Follow symbolic links.
See also
magic_file()
.
- magic_buffer(m: magic_t, buf: bytes) → str¶
Retrieves a textual description of the contents of the provided buffer.
- Parameters
m (magic_t) – The magic database handle.
buf (bytes) – The buffer to examine.
- Returns
Returns a textual description of the buffer’s contents.
- Return type
str
See also
magic_file()
andmagic_descriptor()
.
- magic_builtin_db_name() → str¶
- Returns
Returns the pathname of the default magic database.
- Return type
str
See also
magic_load()
.
- magic_check(m: magic_t, fname: str) → int¶
Checks the validity of entries in the magic database file.
- Parameters
m (magic_t) – The magic database handle.
fname (str) – The pathname of the magic database file to check.
- Returns
Returns
0
if successful; otherwise returns-1
.- Return type
int
See also
magic_compile()
andmagic_load()
.
- magic_close(m: magic_t) → None¶
Closes the magic database and deallocates resources.
- Parameters
m (magic_t) – The magic database handle to close.
See also
magic_open()
.
- magic_compile(m: magic_t, fname: str) → int¶
Compiles the specified magic database file.
- Parameters
m (magic_t) – The magic database handle.
fname (str) – The pathname of the magic database file to compile.
- Returns
Returns
0
if successful; otherwise returns-1
.- Return type
int
See also
magic_check()
andmagic_load()
.
- magic_descriptor(m: magic_t, fd: int) → str¶
Retrieves a textual description of the contents of the file associated with the given file descriptor.
- Parameters
m (magic_t) – The magic database handle.
fd (int) – The file descriptor of the file to examine.
- Returns
Returns a textual description of the file’s contents.
- Return type
str
See also
magic_file()
andmagic_buffer()
.
- magic_errno(m: magic_t) → int¶
Retrieves the last operating system error number encountered.
- Parameters
m (magic_t) – The magic database handle.
- Returns
Returns the last error number (errno).
- Return type
int
See also
magic_error()
.
- magic_error(m: magic_t) → str¶
Retrieves a textual description of the last error encountered.
- Parameters
m (magic_t) – The magic database handle.
- Returns
Returns a textual description of the last error.
- Return type
str
See also
magic_errno()
.
- magic_file(m: magic_t, fname: str) → str¶
Retrieves a textual description of the contents of the specified file.
- Parameters
m (magic_t) – The magic database handle.
fname (str) – The pathname of the file to examine.
- Returns
Returns a textual description of the file’s contents.
- Return type
str
See also
magic_buffer()
andmagic_descriptor()
.
- magic_getpath(fname: str, action: int) → str¶
Retrieves the path to the magic database file based on the given action.
- Parameters
fname (str) – The filename of the magic database.
action (int) – The action to perform.
- Returns
Returns the path to the magic database file.
- Return type
str
See also
magic_load()
andmagic_compile()
.
- magic_list(m: magic_t, fname: str) → int¶
Lists magic entries in the specified magic database file.
- Parameters
m (magic_t) – The magic database handle.
fname (str) – The pathname of the magic database file to list.
- Returns
Returns
0
if successful; otherwise returns-1
.- Return type
int
See also
magic_load()
.
- magic_load(m: magic_t, fname: str) → int¶
Loads the magic database file specified.
- Parameters
m (magic_t) – The magic database handle.
fname (str) – The pathname of the magic database file to load. If
None
, the default database is used.- Returns
Returns
0
if successful; otherwise returns-1
.- Return type
int
See also
magic_open()
.
- magic_open(flags: int) → magic_t¶
Allocates a magic cookie and returns a magic database handle.
- Parameters
flags (int) – Flags specifying magic behavior.
- Returns
Returns a magic database handle if successful; otherwise returns
None
.- Return type
magic_t
See also
magic_close()
andmagic_setflags()
.
- magic_setflags(m: magic_t, flags: int) → int¶
Sets flags on the magic cookie to control magic behavior.
- Parameters
m (magic_t) – The magic database handle.
flags (int) – Flags to set.
- Returns
Returns
0
if successful; otherwise returns-1
.- Return type
int
See also
magic_open()
andmagic_load()
.