1414'PDF document, version 1.2'
1515>>>
1616
17-
1817"""
1918
2019import sys
@@ -35,11 +34,10 @@ def __init__(self, message):
3534class Magic :
3635 """
3736 Magic is a wrapper around the libmagic C library.
38-
3937 """
4038
4139 def __init__ (self , mime = False , magic_file = None , mime_encoding = False ,
42- keep_going = False , uncompress = False ):
40+ keep_going = False , uncompress = False , raw = False ):
4341 """
4442 Create a new libmagic wrapper.
4543
@@ -48,6 +46,7 @@ def __init__(self, mime=False, magic_file=None, mime_encoding=False,
4846 magic_file - use a mime database other than the system default
4947 keep_going - don't stop at the first match, keep going
5048 uncompress - Try to look inside compressed files.
49+ raw - Do not try to decode "non-printable" chars.
5150 """
5251 self .flags = MAGIC_NONE
5352 if mime :
@@ -56,9 +55,10 @@ def __init__(self, mime=False, magic_file=None, mime_encoding=False,
5655 self .flags |= MAGIC_MIME_ENCODING
5756 if keep_going :
5857 self .flags |= MAGIC_CONTINUE
59-
6058 if uncompress :
6159 self .flags |= MAGIC_COMPRESS
60+ if raw :
61+ self .flags |= MAGIC_RAW
6262
6363 self .cookie = magic_open (self .flags )
6464 self .lock = threading .Lock ()
@@ -190,13 +190,15 @@ def from_buffer(buffer, mime=False):
190190
191191magic_t = ctypes .c_void_p
192192
193+
193194def errorcheck_null (result , func , args ):
194195 if result is None :
195196 err = magic_error (args [0 ])
196197 raise MagicException (err )
197198 else :
198199 return result
199200
201+
200202def errorcheck_negative_one (result , func , args ):
201203 if result is - 1 :
202204 err = magic_error (args [0 ])
@@ -213,6 +215,7 @@ def maybe_decode(s):
213215 else :
214216 return s .decode ('utf-8' )
215217
218+
216219def coerce_filename (filename ):
217220 if filename is None :
218221 return None
@@ -230,6 +233,7 @@ def coerce_filename(filename):
230233 else :
231234 return filename
232235
236+
233237magic_open = libmagic .magic_open
234238magic_open .restype = magic_t
235239magic_open .argtypes = [c_int ]
@@ -251,6 +255,7 @@ def coerce_filename(filename):
251255_magic_file .argtypes = [magic_t , c_char_p ]
252256_magic_file .errcheck = errorcheck_null
253257
258+
254259def magic_file (cookie , filename ):
255260 return _magic_file (cookie , coerce_filename (filename ))
256261
@@ -259,6 +264,7 @@ def magic_file(cookie, filename):
259264_magic_buffer .argtypes = [magic_t , c_void_p , c_size_t ]
260265_magic_buffer .errcheck = errorcheck_null
261266
267+
262268def magic_buffer (cookie , buf ):
263269 return _magic_buffer (cookie , buf , len (buf ))
264270
@@ -268,6 +274,7 @@ def magic_buffer(cookie, buf):
268274_magic_load .argtypes = [magic_t , c_char_p ]
269275_magic_load .errcheck = errorcheck_negative_one
270276
277+
271278def magic_load (cookie , filename ):
272279 return _magic_load (cookie , coerce_filename (filename ))
273280
@@ -288,6 +295,7 @@ def magic_load(cookie, filename):
288295_magic_setparam .argtypes = [magic_t , c_int , POINTER (c_size_t )]
289296_magic_setparam .errcheck = errorcheck_negative_one
290297
298+
291299def magic_setparam (cookie , param , val ):
292300 v = c_size_t (val )
293301 return _magic_setparam (cookie , param , byref (v ))
@@ -297,11 +305,13 @@ def magic_setparam(cookie, param, val):
297305_magic_getparam .argtypes = [magic_t , c_int , POINTER (c_size_t )]
298306_magic_getparam .errcheck = errorcheck_negative_one
299307
308+
300309def magic_getparam (cookie , param ):
301310 val = c_size_t ()
302311 _magic_getparam (cookie , param , byref (val ))
303312 return val .value
304313
314+
305315MAGIC_NONE = 0x000000 # No flags
306316MAGIC_DEBUG = 0x000001 # Turn on debugging
307317MAGIC_SYMLINK = 0x000002 # Follow symlinks
0 commit comments