11"""
22magic is a wrapper around the libmagic file identification library.
33
4- See README for more information.
5-
64Usage:
75
86>>> import magic
1412'PDF document, version 1.2'
1513>>>
1614
17-
1815"""
1916
2017import sys
2421
2522from ctypes import c_char_p , c_int , c_size_t , c_void_p
2623
27- class MagicException (Exception ): pass
24+ class MagicException (Exception ):
25+ pass
2826
2927class Magic :
3028 """
3129 Magic is a wrapper around the libmagic C library.
32-
3330 """
3431
3532 def __init__ (self , mime = False , magic_file = None , mime_encoding = False ):
@@ -39,8 +36,8 @@ def __init__(self, mime=False, magic_file=None, mime_encoding=False):
3936 mime - if True, mimetypes are returned instead of textual descriptions
4037 mime_encoding - if True, codec is returned
4138 magic_file - use a mime database other than the system default
42-
4339 """
40+
4441 flags = MAGIC_NONE
4542 if mime :
4643 flags |= MAGIC_MIME
@@ -56,6 +53,7 @@ def from_buffer(self, buf):
5653 """
5754 Identify the contents of `buf`
5855 """
56+
5957 return magic_buffer (self .cookie , buf )
6058
6159 def from_file (self , filename ):
@@ -104,8 +102,6 @@ def from_buffer(buffer, mime=False):
104102 m = _get_magic_type (mime )
105103 return m .from_buffer (buffer )
106104
107-
108-
109105try :
110106 libmagic = None
111107 # Let's try to find magic or magic1
@@ -178,7 +174,6 @@ def magic_file(cookie, filename):
178174 def magic_buffer (cookie , buf ):
179175 return _magic_buffer (cookie , buf , len (buf ))
180176
181-
182177 _magic_load = libmagic .magic_load
183178 _magic_load .restype = c_int
184179 _magic_load .argtypes = [magic_t , c_char_p ]
@@ -198,48 +193,28 @@ def magic_load(cookie, filename):
198193 magic_compile = libmagic .magic_compile
199194 magic_compile .restype = c_int
200195 magic_compile .argtypes = [magic_t , c_char_p ]
196+
201197except ImportError :
202198 pass
203199
204-
205200MAGIC_NONE = 0x000000 # No flags
206-
207201MAGIC_DEBUG = 0x000001 # Turn on debugging
208-
209202MAGIC_SYMLINK = 0x000002 # Follow symlinks
210-
211203MAGIC_COMPRESS = 0x000004 # Check inside compressed files
212-
213204MAGIC_DEVICES = 0x000008 # Look at the contents of devices
214-
215205MAGIC_MIME = 0x000010 # Return a mime string
216-
217206MAGIC_MIME_ENCODING = 0x000400 # Return the MIME encoding
218-
219207MAGIC_CONTINUE = 0x000020 # Return all matches
220-
221208MAGIC_CHECK = 0x000040 # Print warnings to stderr
222-
223209MAGIC_PRESERVE_ATIME = 0x000080 # Restore access time on exit
224-
225210MAGIC_RAW = 0x000100 # Don't translate unprintable chars
226-
227211MAGIC_ERROR = 0x000200 # Handle ENOENT etc as real errors
228-
229212MAGIC_NO_CHECK_COMPRESS = 0x001000 # Don't check for compressed files
230-
231213MAGIC_NO_CHECK_TAR = 0x002000 # Don't check for tar files
232-
233214MAGIC_NO_CHECK_SOFT = 0x004000 # Don't check magic entries
234-
235215MAGIC_NO_CHECK_APPTYPE = 0x008000 # Don't check application type
236-
237216MAGIC_NO_CHECK_ELF = 0x010000 # Don't check for elf details
238-
239217MAGIC_NO_CHECK_ASCII = 0x020000 # Don't check for ascii files
240-
241218MAGIC_NO_CHECK_TROFF = 0x040000 # Don't check ascii/troff
242-
243219MAGIC_NO_CHECK_FORTRAN = 0x080000 # Don't check ascii/fortran
244-
245220MAGIC_NO_CHECK_TOKENS = 0x100000 # Don't check ascii/tokens
0 commit comments