| 1 |
"""Loads libdmtx. |
| 2 |
""" |
| 3 |
import platform |
| 4 |
import sys |
| 5 |
|
| 6 |
from ctypes import cdll |
| 7 |
from ctypes.util import find_library |
| 8 |
from pathlib import Path |
| 9 |
|
| 10 |
__all__ = ['load'] |
| 11 |
|
| 12 |
|
| 13 |
def _windows_fname(): |
| 14 |
"""For convenience during development and to aid debugging, the DLL name is |
| 15 |
specific to the bit depth of interpreter. |
| 16 |
|
| 17 |
This logic has its own function to make testing easier |
| 18 |
""" |
| 19 |
return 'libdmtx-64.dll' if sys.maxsize > 2**32 else 'libdmtx-32.dll' |
| 20 |
|
| 21 |
|
| 22 |
def load(): |
| 23 |
"""Loads the libdmtx shared library. |
| 24 |
""" |
| 25 |
if 'Windows' == platform.system(): |
| 26 |
# Possible scenarios here |
| 27 |
# 1. Run from source, DLLs are in pylibdmtx directory |
| 28 |
# cdll.LoadLibrary() imports DLLs in repo root directory |
| 29 |
# 2. Wheel install into CPython installation |
| 30 |
# cdll.LoadLibrary() imports DLLs in package directory |
| 31 |
# 3. Wheel install into virtualenv |
| 32 |
# cdll.LoadLibrary() imports DLLs in package directory |
| 33 |
# 4. Frozen |
| 34 |
# cdll.LoadLibrary() imports DLLs alongside executable |
| 35 |
|
| 36 |
fname = _windows_fname() |
| 37 |
try: |
| 38 |
libdmtx = cdll.LoadLibrary(fname) |
| 39 |
except OSError: |
| 40 |
libdmtx = cdll.LoadLibrary( |
| 41 |
str(Path(__file__).parent.joinpath(fname)) |
| 42 |
) |
| 43 |
else: |
| 44 |
# Assume a shared library on the path |
| 45 |
path = find_library('dmtx') |
| 46 |
if not path: |
| 47 |
raise ImportError('Unable to find dmtx shared library') |
| 48 |
libdmtx = cdll.LoadLibrary(path) |
| 49 |
|
| 50 |
return libdmtx |