3 Python bindings for libmagic
9 from ctypes
.util
import find_library
14 Loads the shared library through ctypes and returns a library
15 L{ctypes.CDLL} instance
17 return ctypes
.cdll
.LoadLibrary(find_library('magic'))
20 _libraries
['magic'] = _init()
22 # Flag constants for open and setflags
24 MAGIC_DEBUG
= DEBUG
= 1
25 MAGIC_SYMLINK
= SYMLINK
= 2
26 MAGIC_COMPRESS
= COMPRESS
= 4
27 MAGIC_DEVICES
= DEVICES
= 8
28 MAGIC_MIME_TYPE
= MIME_TYPE
= 16
29 MAGIC_CONTINUE
= CONTINUE
= 32
30 MAGIC_CHECK
= CHECK
= 64
31 MAGIC_PRESERVE_ATIME
= PRESERVE_ATIME
= 128
33 MAGIC_ERROR
= ERROR
= 512
34 MAGIC_MIME_ENCODING
= MIME_ENCODING
= 1024
35 MAGIC_MIME
= MIME
= 1040
36 MAGIC_APPLE
= APPLE
= 2048
38 MAGIC_NO_CHECK_COMPRESS
= NO_CHECK_COMPRESS
= 4096
39 MAGIC_NO_CHECK_TAR
= NO_CHECK_TAR
= 8192
40 MAGIC_NO_CHECK_SOFT
= NO_CHECK_SOFT
= 16384
41 MAGIC_NO_CHECK_APPTYPE
= NO_CHECK_APPTYPE
= 32768
42 MAGIC_NO_CHECK_ELF
= NO_CHECK_ELF
= 65536
43 MAGIC_NO_CHECK_TEXT
= NO_CHECK_TEXT
= 131072
44 MAGIC_NO_CHECK_CDF
= NO_CHECK_CDF
= 262144
45 MAGIC_NO_CHECK_TOKENS
= NO_CHECK_TOKENS
= 1048576
46 MAGIC_NO_CHECK_ENCODING
= NO_CHECK_ENCODING
= 2097152
48 MAGIC_NO_CHECK_BUILTIN
= NO_CHECK_BUILTIN
= 4173824
51 class magic_set(Structure
):
53 magic_set
._fields
_ = []
54 magic_t
= POINTER(magic_set
)
56 _open
= _libraries
['magic'].magic_open
57 _open
.restype
= magic_t
58 _open
.argtypes
= [c_int
]
60 _close
= _libraries
['magic'].magic_close
62 _close
.argtypes
= [magic_t
]
64 _file
= _libraries
['magic'].magic_file
65 _file
.restype
= c_char_p
66 _file
.argtypes
= [magic_t
, c_char_p
]
68 _descriptor
= _libraries
['magic'].magic_descriptor
69 _descriptor
.restype
= c_char_p
70 _descriptor
.argtypes
= [magic_t
, c_int
]
72 _buffer
= _libraries
['magic'].magic_buffer
73 _buffer
.restype
= c_char_p
74 _buffer
.argtypes
= [magic_t
, c_void_p
, c_size_t
]
76 _error
= _libraries
['magic'].magic_error
77 _error
.restype
= c_char_p
78 _error
.argtypes
= [magic_t
]
80 _setflags
= _libraries
['magic'].magic_setflags
81 _setflags
.restype
= c_int
82 _setflags
.argtypes
= [magic_t
, c_int
]
84 _load
= _libraries
['magic'].magic_load
86 _load
.argtypes
= [magic_t
, c_char_p
]
88 _compile
= _libraries
['magic'].magic_compile
89 _compile
.restype
= c_int
90 _compile
.argtypes
= [magic_t
, c_char_p
]
92 _check
= _libraries
['magic'].magic_check
93 _check
.restype
= c_int
94 _check
.argtypes
= [magic_t
, c_char_p
]
96 _list
= _libraries
['magic'].magic_list
98 _list
.argtypes
= [magic_t
, c_char_p
]
100 _errno
= _libraries
['magic'].magic_errno
101 _errno
.restype
= c_int
102 _errno
.argtypes
= [magic_t
]
106 def __init__(self
, ms
):
111 Closes the magic database and deallocates any resources used.
113 _close(self
._magic
_t
)
115 def file(self
, filename
):
117 Returns a textual description of the contents of the argument passed
118 as a filename or None if an error occurred and the MAGIC_ERROR flag
119 is set. A call to errno() will return the numeric error code.
121 try: # attempt python3 approach first
122 if isinstance(filename
, bytes
):
125 bi
= bytes(filename
, 'utf-8')
126 return str(_file(self
._magic
_t
, bi
), 'utf-8')
128 return _file(self
._magic
_t
, filename
.encode('utf-8'))
130 def descriptor(self
, fd
):
132 Like the file method, but the argument is a file descriptor.
134 return _descriptor(self
._magic
_t
, fd
)
136 def buffer(self
, buf
):
138 Returns a textual description of the contents of the argument passed
139 as a buffer or None if an error occurred and the MAGIC_ERROR flag
140 is set. A call to errno() will return the numeric error code.
142 try: # attempt python3 approach first
143 return str(_buffer(self
._magic
_t
, buf
, len(buf
)), 'utf-8')
145 return _buffer(self
._magic
_t
, buf
, len(buf
))
149 Returns a textual explanation of the last error or None
150 if there was no error.
152 try: # attempt python3 approach first
153 return str(_error(self
._magic
_t
), 'utf-8')
155 return _error(self
._magic
_t
)
157 def setflags(self
, flags
):
159 Set flags on the magic object which determine how magic checking
160 behaves; a bitwise OR of the flags described in libmagic(3), but
161 without the MAGIC_ prefix.
163 Returns -1 on systems that don't support utime(2) or utimes(2)
164 when PRESERVE_ATIME is set.
166 return _setflags(self
._magic
_t
, flags
)
168 def load(self
, filename
=None):
170 Must be called to load entries in the colon separated list of database
171 files passed as argument or the default database file if no argument
172 before any magic queries can be performed.
174 Returns 0 on success and -1 on failure.
176 return _load(self
._magic
_t
, filename
)
178 def compile(self
, dbs
):
180 Compile entries in the colon separated list of database files
181 passed as argument or the default database file if no argument.
182 Returns 0 on success and -1 on failure.
183 The compiled files created are named from the basename(1) of each file
184 argument with ".mgc" appended to it.
186 return _compile(self
._magic
_t
, dbs
)
188 def check(self
, dbs
):
190 Check the validity of entries in the colon separated list of
191 database files passed as argument or the default database file
193 Returns 0 on success and -1 on failure.
195 return _check(self
._magic
_t
, dbs
)
199 Check the validity of entries in the colon separated list of
200 database files passed as argument or the default database file
202 Returns 0 on success and -1 on failure.
204 return _list(self
._magic
_t
, dbs
)
208 Returns a numeric error code. If return value is 0, an internal
209 magic error occurred. If return value is non-zero, the value is
210 an OS error code. Use the errno module or os.strerror() can be used
211 to provide detailed error information.
213 return _errno(self
._magic
_t
)
218 Returns a magic object on success and None on failure.
219 Flags argument as for setflags.
221 return Magic(_open(flags
))