@@ -38,12 +38,27 @@ class Magic:
38
38
Magic is a wrapper around the libmagic C library.
39
39
"""
40
40
41
- def __init__(self, mime=False, magic_file=None, mime_encoding=False,
42
- keep_going=False, uncompress=False, raw=False, extension=False,
43
- follow_symlinks=False, check_tar=True, check_soft=True,
44
- check_apptype=True, check_elf=True, check_text=True,
45
- check_cdf=True, check_csv=True, check_encoding=True,
46
- check_json=True, check_simh=True):
41
+ def __init__(
42
+ self,
43
+ mime=False,
44
+ magic_file=None,
45
+ mime_encoding=False,
46
+ keep_going=False,
47
+ uncompress=False,
48
+ raw=False,
49
+ extension=False,
50
+ follow_symlinks=False,
51
+ check_tar=True,
52
+ check_soft=True,
53
+ check_apptype=True,
54
+ check_elf=True,
55
+ check_text=True,
56
+ check_cdf=True,
57
+ check_csv=True,
58
+ check_encoding=True,
59
+ check_json=True,
60
+ check_simh=True,
61
+ ):
47
62
"""
48
63
Create a new libmagic wrapper.
49
64
@@ -101,7 +116,9 @@ def __init__(self, mime=False, magic_file=None, mime_encoding=False,
101
116
# MAGIC_EXTENSION was added in 523 or 524, so bail if
102
117
# it doesn't appear to be available
103
118
if extension and (not _has_version or version() < 524):
104
- raise NotImplementedError('MAGIC_EXTENSION is not supported in this version of libmagic')
119
+ raise NotImplementedError(
120
+ "MAGIC_EXTENSION is not supported in this version of libmagic"
121
+ )
105
122
106
123
# For https://github.com/ahupp/python-magic/issues/190
107
124
# libmagic has fixed internal limits that some files exceed, causing
@@ -128,7 +145,7 @@ def from_buffer(self, buf):
128
145
# which is not what libmagic expects
129
146
# NEXTBREAK: only take bytes
130
147
if type(buf) == str and str != bytes:
131
- buf = buf.encode(' utf-8' , errors=' replace' )
148
+ buf = buf.encode(" utf-8" , errors=" replace" )
132
149
return maybe_decode(magic_buffer(self.cookie, buf))
133
150
except MagicException as e:
134
151
return self._handle509Bug(e)
@@ -176,7 +193,7 @@ def __del__(self):
176
193
# incorrect fix for a threading problem, however I'm leaving
177
194
# it in because it's harmless and I'm slightly afraid to
178
195
# remove it.
179
- if hasattr(self, ' cookie' ) and self.cookie and magic_close:
196
+ if hasattr(self, " cookie" ) and self.cookie and magic_close:
180
197
magic_close(self.cookie)
181
198
self.cookie = None
182
199
@@ -192,7 +209,7 @@ def _get_magic_type(mime):
192
209
193
210
194
211
def from_file(filename, mime=False):
195
- """"
212
+ """
196
213
Accepts a filename and returns the detected filetype. Return
197
214
value is the mimetype if mime=True, otherwise a human readable
198
215
name.
@@ -230,7 +247,9 @@ def from_descriptor(fd, mime=False):
230
247
m = _get_magic_type(mime)
231
248
return m.from_descriptor(fd)
232
249
250
+
233
251
from . import loader
252
+
234
253
libmagic = loader.load_lib()
235
254
236
255
magic_t = ctypes.c_void_p
@@ -261,20 +280,24 @@ def maybe_decode(s):
261
280
else:
262
281
# backslashreplace here because sometimes libmagic will return metadata in the charset
263
282
# of the file, which is unknown to us (e.g the title of a Word doc)
264
- return s.decode(' utf-8', ' backslashreplace' )
283
+ return s.decode(" utf-8", " backslashreplace" )
265
284
266
285
267
286
try:
268
287
from os import PathLike
288
+
269
289
def unpath(filename):
270
290
if isinstance(filename, PathLike):
271
291
return filename.__fspath__()
272
292
else:
273
293
return filename
294
+
274
295
except ImportError:
296
+
275
297
def unpath(filename):
276
298
return filename
277
299
300
+
278
301
def coerce_filename(filename):
279
302
if filename is None:
280
303
return None
@@ -286,12 +309,11 @@ def coerce_filename(filename):
286
309
# then you'll get inconsistent behavior (crashes) depending on the user's
287
310
# LANG environment variable
288
311
# NEXTBREAK: remove
289
- is_unicode = (sys.version_info[0] <= 2 and
290
- isinstance(filename, unicode)) or \
291
- (sys.version_info[0] >= 3 and
292
- isinstance(filename, str))
312
+ is_unicode = (sys.version_info[0] <= 2 and isinstance(filename, unicode)) or (
313
+ sys.version_info[0] >= 3 and isinstance(filename, str)
314
+ )
293
315
if is_unicode:
294
- return filename.encode(' utf-8', ' surrogateescape' )
316
+ return filename.encode(" utf-8", " surrogateescape" )
295
317
else:
296
318
return filename
297
319
@@ -370,7 +392,7 @@ def magic_load(cookie, filename):
370
392
magic_compile.argtypes = [magic_t, c_char_p]
371
393
372
394
_has_param = False
373
- if hasattr(libmagic, ' magic_setparam' ) and hasattr(libmagic, ' magic_getparam' ):
395
+ if hasattr(libmagic, " magic_setparam" ) and hasattr(libmagic, " magic_getparam" ):
374
396
_has_param = True
375
397
_magic_setparam = libmagic.magic_setparam
376
398
_magic_setparam.restype = c_int
@@ -443,8 +465,8 @@ def version():
443
465
MAGIC_NO_CHECK_CDF = 0x0040000 # Don't check for CDF files
444
466
MAGIC_NO_CHECK_CSV = 0x0080000 # Don't check for CSV files
445
467
MAGIC_NO_CHECK_ENCODING = 0x0200000 # Don't check text encodings
446
- MAGIC_NO_CHECK_JSON = 0x0400000 # Don't check for JSON files
447
- MAGIC_NO_CHECK_SIMH = 0x0800000 # Don't check for SIMH tape files
468
+ MAGIC_NO_CHECK_JSON = 0x0400000 # Don't check for JSON files
469
+ MAGIC_NO_CHECK_SIMH = 0x0800000 # Don't check for SIMH tape files
448
470
449
471
MAGIC_PARAM_INDIR_MAX = 0 # Recursion limit for indirect magic
450
472
MAGIC_PARAM_NAME_MAX = 1 # Use count limit for name/use magic
@@ -468,22 +490,20 @@ def _(*args, **kwargs):
468
490
warnings.warn(
469
491
"Using compatibility mode with libmagic's python binding. "
470
492
"See https://github.com/ahupp/python-magic/blob/master/COMPAT.md for details.",
471
- PendingDeprecationWarning)
493
+ PendingDeprecationWarning,
494
+ )
472
495
473
496
return fn(*args, **kwargs)
474
497
475
498
return _
476
499
477
- fn = ['detect_from_filename',
478
- 'detect_from_content',
479
- 'detect_from_fobj',
480
- 'open']
500
+ fn = ["detect_from_filename", "detect_from_content", "detect_from_fobj", "open"]
481
501
for fname in fn:
482
502
to_module[fname] = deprecation_wrapper(compat.__dict__[fname])
483
503
484
504
# copy constants over, ensuring there's no conflicts
485
505
is_const_re = re.compile("^[A-Z_]+$")
486
- allowed_inconsistent = set([' MAGIC_MIME' ])
506
+ allowed_inconsistent = set([" MAGIC_MIME" ])
487
507
for name, value in compat.__dict__.items():
488
508
if is_const_re.match(name):
489
509
if name in to_module:
0 commit comments