Skip to content

Commit c8d56b3

Browse files
committed
Clean up loader.py
1 parent fc7ebc0 commit c8d56b3

File tree

1 file changed

+51
-31
lines changed

1 file changed

+51
-31
lines changed

magic/loader.py

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,67 @@
44
import glob
55
import os.path
66

7-
def _lib_candidates():
87

9-
yield find_library('magic')
8+
def _lib_candidates_linux():
9+
"""Yield possible libmagic library names on Linux.
10+
11+
This is necessary because alpine is bad
12+
"""
13+
yield "libmagic.so.1"
1014

11-
if sys.platform == 'darwin':
1215

16+
def _lib_candidates_macos():
17+
"""Yield possible libmagic library names on macOS."""
1318
paths = [
14-
'/opt/local/lib',
15-
'/usr/local/lib',
16-
'/opt/homebrew/lib',
17-
] + glob.glob('/usr/local/Cellar/libmagic/*/lib')
19+
"/opt/homebrew/lib",
20+
"/opt/local/lib",
21+
"/usr/local/lib",
22+
] + glob.glob("/usr/local/Cellar/libmagic/*/lib")
1823

19-
for i in paths:
20-
yield os.path.join(i, 'libmagic.dylib')
24+
for path in paths:
25+
yield os.path.join(path, "libmagic.dylib")
2126

22-
elif sys.platform in ('win32', 'cygwin'):
2327

24-
prefixes = ['libmagic', 'magic1', 'magic-1', 'cygmagic-1', 'libmagic-1', 'msys-magic-1']
28+
def _lib_candidates_windows():
29+
"""Yield possible libmagic library names on Windows."""
30+
prefixes = (
31+
"libmagic",
32+
"magic1",
33+
"magic-1",
34+
"cygmagic-1",
35+
"libmagic-1",
36+
"msys-magic-1",
37+
)
2538

26-
for i in prefixes:
27-
# find_library searches in %PATH% but not the current directory,
28-
# so look for both
29-
yield './%s.dll' % (i,)
30-
yield find_library(i)
39+
for prefix in prefixes:
40+
# find_library searches in %PATH% but not the current directory,
41+
# so look for both
42+
yield "./%s.dll" % (prefix,)
43+
yield find_library(prefix)
3144

32-
elif sys.platform == 'linux':
33-
# This is necessary because alpine is bad
34-
yield 'libmagic.so.1'
3545

46+
def _lib_candidates():
47+
yield find_library("magic")
3648

37-
def load_lib():
49+
func = {
50+
"cygwin": _lib_candidates_windows,
51+
"darwin": _lib_candidates_macos,
52+
"linux": _lib_candidates_linux,
53+
"win32": _lib_candidates_windows,
54+
}[sys.platform]
55+
# When we drop legacy Python, we can just `yield from func()`
56+
for path in func():
57+
yield path
3858

39-
for lib in _lib_candidates():
40-
# find_library returns None when lib not found
41-
if lib is None:
42-
continue
43-
try:
44-
return ctypes.CDLL(lib)
45-
except OSError:
46-
pass
47-
else:
48-
# It is better to raise an ImportError since we are importing magic module
49-
raise ImportError('failed to find libmagic. Check your installation')
5059

60+
def load_lib():
61+
for lib in _lib_candidates():
62+
# find_library returns None when lib not found
63+
if lib:
64+
try:
65+
return ctypes.CDLL(lib)
66+
except OSError:
67+
pass
68+
else:
69+
# It is better to raise an ImportError since we are importing magic module
70+
raise ImportError("failed to find libmagic. Check your installation")

0 commit comments

Comments
 (0)