3131import ctypes
3232import glob
3333import os
34+ import warnings
3435
3536from commoncode import command
3637from commoncode .system import on_windows
37- import warnings
3838
3939"""
4040magic2 is minimal and specialized wrapper around a vendored libmagic file
@@ -85,6 +85,24 @@ def logger_debug(*args):
8585TYPECODE_LIBMAGIC_PATH_ENVVAR = 'TYPECODE_LIBMAGIC_PATH'
8686TYPECODE_LIBMAGIC_DB_PATH_ENVVAR = 'TYPECODE_LIBMAGIC_DB_PATH'
8787
88+ if TRACE :
89+
90+ def file_type (location ):
91+ return _detect (location , DETECT_TYPE )
92+
93+ else :
94+
95+ def file_type (location ):
96+ """"
97+ Return the detected filetype for file at `location` or an empty string if
98+ nothing found or an error occurred.
99+ """
100+ try :
101+ return _detect (location , DETECT_TYPE )
102+ except :
103+ # TODO: log errors
104+ return ''
105+
88106
89107class NoMagicLibError (Exception ):
90108 """
@@ -150,6 +168,7 @@ def load_lib():
150168 Return the libmagic shared library object loaded from either:
151169 - an environment variable ``TYPECODE_LIBMAGIC_PATH``
152170 - a plugin-provided path,
171+ - well known system names and locations,
153172 - the system PATH.
154173 Raise an NoMagicLibError if no libmagic can be found.
155174 """
@@ -259,25 +278,6 @@ def get_magicdb_location(_cache=[]):
259278 return magicdb_loc
260279
261280
262- if TRACE :
263-
264- def file_type (location ):
265- return _detect (location , DETECT_TYPE )
266-
267- else :
268-
269- def file_type (location ):
270- """"
271- Return the detected filetype for file at `location` or an empty string if
272- nothing found or an error occurred.
273- """
274- try :
275- return _detect (location , DETECT_TYPE )
276- except :
277- # TODO: log errors
278- return ''
279-
280-
281281def mime_type (location ):
282282 """"
283283 Return the detected mimetype for file at `location` or an empty string if
@@ -377,6 +377,10 @@ def __del__(self):
377377libmagic = load_lib ()
378378
379379
380+ def libmagic_version ():
381+ return _magic_version ()
382+
383+
380384def check_error (result , func , args ): # NOQA
381385 """
382386 ctypes error handler/checker: Check for errors and raise an exception or
@@ -385,11 +389,14 @@ def check_error(result, func, args): # NOQA
385389 is_int = isinstance (result , int )
386390 is_bytes = isinstance (result , bytes )
387391 is_text = isinstance (result , str )
388-
389- if (result is None
390- or (is_int and result < 0 )
391- or (is_bytes and str (result , encoding = 'utf-8' ).startswith ('cannot open' ))
392- or (is_text and result .startswith ('cannot open' ))):
392+ if (
393+ result is None
394+ or (is_int and result < 0 )
395+ or (
396+ is_bytes
397+ and str (result , encoding = 'utf-8' , errors = 'ignore' ).startswith ('cannot open' ))
398+ or (is_text and result .startswith ('cannot open' ))
399+ ):
393400 err = _magic_error (args [0 ])
394401 raise MagicException (err )
395402 else :
@@ -427,5 +434,3 @@ def check_error(result, func, args): # NOQA
427434_magic_version = libmagic .magic_version
428435_magic_version .restype = ctypes .c_int
429436_magic_version .argtypes = []
430-
431- libmagic_version = _magic_version ()
0 commit comments