from ctypes import *
from ctypes.util import find_library
+
def _init():
"""
Loads the shared library through ctypes and returns a library
- L{ctypes.CDLL} instance
+ L{ctypes.CDLL} instance
"""
return ctypes.cdll.LoadLibrary(find_library('magic'))
MAGIC_NO_CHECK_BUILTIN = NO_CHECK_BUILTIN = 4173824
+
class magic_set(Structure):
pass
magic_set._fields_ = []
_errno.restype = c_int
_errno.argtypes = [magic_t]
+
class Magic(object):
def __init__(self, ms):
self._magic_t = ms
as a filename or None if an error occurred and the MAGIC_ERROR flag
is set. A call to errno() will return the numeric error code.
"""
- try: # attempt python3 approach first
+ try: # attempt python3 approach first
if isinstance(filename, bytes):
bi = filename
else:
as a buffer or None if an error occurred and the MAGIC_ERROR flag
is set. A call to errno() will return the numeric error code.
"""
- try: # attempt python3 approach first
+ try: # attempt python3 approach firstg
return str(_buffer(self._magic_t, buf, len(buf)), 'utf-8')
except:
return _buffer(self._magic_t, buf, len(buf))
Returns a textual explanation of the last error or None
if there was no error.
"""
- try: # attempt python3 approach first
+ try: # attempt python3 approach first
return str(_error(self._magic_t), 'utf-8')
except:
return _error(self._magic_t)
-
+
def setflags(self, flags):
"""
- Set flags on the magic object which determine how magic checking behaves;
- a bitwise OR of the flags described in libmagic(3), but without the MAGIC_
- prefix.
+ Set flags on the magic object which determine how magic checking
+ behaves; a bitwise OR of the flags described in libmagic(3), but
+ without the MAGIC_ prefix.
Returns -1 on systems that don't support utime(2) or utimes(2)
when PRESERVE_ATIME is set.
def load(self, filename=None):
"""
- Must be called to load entries in the colon separated list of database files
- passed as argument or the default database file if no argument before
- any magic queries can be performed.
-
+ Must be called to load entries in the colon separated list of database
+ files passed as argument or the default database file if no argument
+ before any magic queries can be performed.
+
Returns 0 on success and -1 on failure.
"""
return _load(self._magic_t, filename)
Returns 0 on success and -1 on failure.
"""
return _list(self._magic_t, dbs)
-
+
def errno(self):
"""
Returns a numeric error code. If return value is 0, an internal
"""
return _errno(self._magic_t)
+
def open(flags):
"""
Returns a magic object on success and None on failure.