|
1 | 1 | """Wrapper for libedfapi.so.""" |
2 | 2 |
|
| 3 | +import os |
3 | 4 | import struct |
4 | 5 | import sys |
5 | 6 | from ctypes import ( |
|
17 | 18 | c_ushort, |
18 | 19 | util, |
19 | 20 | ) |
| 21 | +from pathlib import Path |
| 22 | + |
20 | 23 |
|
21 | 24 | # find and load the library |
22 | | -if sys.platform.startswith('win') and (8 * struct.calcsize("P") == 64): |
23 | | - name = 'edfapi64' # on windows you can install both |
| 25 | +def find_installed_library(): |
| 26 | + """Try to find edfapi that is installed on the users system.""" |
| 27 | + if sys.platform.startswith('win') and (8 * struct.calcsize("P") == 64): |
| 28 | + name = 'edfapi64' # on windows you can install both |
| 29 | + else: |
| 30 | + name = 'edfapi' |
| 31 | + fname = util.find_library(name) |
| 32 | + if fname is None: |
| 33 | + raise OSError('edfapi not found') |
| 34 | + return fname |
| 35 | + |
| 36 | + |
| 37 | +def get_lib_path(): |
| 38 | + """Get the path to the edfapi shared library that we ship with eyelinkio.""" |
| 39 | + lib_path = Path(__file__).parent.parent.parent / "libedfapi" |
| 40 | + if sys.platform.startswith('win'): |
| 41 | + if (8 * struct.calcsize("P") == 64): # 64 bit |
| 42 | + lib_path = lib_path / "win64" / "edfapi64.dll" |
| 43 | + else: # 32 bit |
| 44 | + lib_path = lib_path / "win32" / "edfapi.dll" |
| 45 | + elif sys.platform.startswith('darwin'): |
| 46 | + lib_path = (lib_path / |
| 47 | + "macos" / |
| 48 | + "edfapi.framework" / |
| 49 | + "Versions" / |
| 50 | + "Current" / |
| 51 | + "edfapi" |
| 52 | + ) |
| 53 | + elif sys.platform.startswith('linux'): |
| 54 | + lib_path = lib_path / "linux" / "libedfapi.so" |
| 55 | + else: |
| 56 | + raise OSError('Unsupported platform') |
| 57 | + assert lib_path.exists(), f"libedfapi.so not found at {lib_path}" |
| 58 | + return lib_path.resolve() |
| 59 | + |
| 60 | + |
| 61 | +if os.environ.get("EYELINKIO_USE_INSTALLED_EDFAPI") == "true": |
| 62 | + # Then we will use the edfapi that is installed on the users system |
| 63 | + fname = find_installed_library() |
24 | 64 | else: |
25 | | - name = 'edfapi' |
26 | | -fname = util.find_library(name) |
27 | | -if fname is None: |
28 | | - raise OSError('edfapi not found') |
| 65 | + # Otherwise we will use the edfapi that we ship with eyelinkio |
| 66 | + fname = get_lib_path() |
29 | 67 | edfapi = CDLL(fname) |
30 | 68 |
|
31 | 69 |
|
|
0 commit comments