Skip to content

Commit

Permalink
Consistently return DaqNotFoundError when NI-DAQmx is not installed o…
Browse files Browse the repository at this point in the history
…n all platforms (#433)
  • Loading branch information
zhindes authored Aug 2, 2023
1 parent 4956702 commit f9e1a48
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 59 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ All notable changes to this project will be documented in this file.
* ### Merged Pull Requests
* ...
* ### Major Changes
* ...
* `nidaqmx.errors.DaqNotFoundError`, `nidaqmx.errors.DaqNotSupportedError`, and
`nidaqmx.errors.DaqFunctionNotSupportedError` are now public exceptions.
* Consistently return `nidaqmx.errors.DaqNotFoundError` on all platforms when the NI-DAQmx
driver is not installed.
* ### Known Issues
* ...

Expand Down
49 changes: 20 additions & 29 deletions generated/nidaqmx/_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@
import threading
from typing import cast

from nidaqmx.errors import Error
from nidaqmx.errors import DaqNotFoundError, DaqNotSupportedError, DaqFunctionNotSupportedError


class DaqNotFoundError(Error):
pass
_DAQ_NOT_FOUND_MESSAGE = "Could not find an installation of NI-DAQmx. Please ensure that NI-DAQmx " \
"is installed on this machine or contact National Instruments for support."

_DAQ_NOT_SUPPORTED_MESSAGE = "NI-DAQmx Python is not supported on this platform: {0}. Please " \
"direct any questions or feedback to National Instruments."

class DaqFunctionNotSupportedError(Error):
pass


class InvalidHandleError(Error):
pass
_FUNCTION_NOT_SUPPORTED_MESSAGE = "The NI-DAQmx function \"{0}\" is not supported in this version " \
"of NI-DAQmx. Visit ni.com/downloads to upgrade."


class c_bool32(ctypes.c_uint):
Expand Down Expand Up @@ -95,10 +93,7 @@ def __getattr__(self, function):
cfunc.arglock = threading.Lock()
return cfunc
except AttributeError:
raise DaqFunctionNotSupportedError(
'The NI-DAQmx function "{}" is not supported in this '
'version of NI-DAQmx. Visit ni.com/downloads to upgrade your '
'version of NI-DAQmx.'.format(function))
raise DaqFunctionNotSupportedError(_FUNCTION_NOT_SUPPORTED_MESSAGE.format(function))


class DaqLibImporter:
Expand Down Expand Up @@ -147,28 +142,24 @@ def _import_lib(self):
cdll = None

if sys.platform.startswith('win') or sys.platform.startswith('cli'):
if 'iron' in platform.python_implementation().lower():
windll = ctypes.windll.nicaiu
cdll = ctypes.cdll.nicaiu
else:
windll = ctypes.windll.LoadLibrary('nicaiu')
cdll = ctypes.cdll.LoadLibrary('nicaiu')

try:
if 'iron' in platform.python_implementation().lower():
windll = ctypes.windll.nicaiu
cdll = ctypes.cdll.nicaiu
else:
windll = ctypes.windll.LoadLibrary('nicaiu')
cdll = ctypes.cdll.LoadLibrary('nicaiu')
except (OSError, WindowsError) as e:
raise DaqNotFoundError(_DAQ_NOT_FOUND_MESSAGE) from e
elif sys.platform.startswith('linux'):
# On linux you can use the command find_library('nidaqmx')
if find_library('nidaqmx') is not None:
cdll = ctypes.cdll.LoadLibrary(find_library('nidaqmx'))
windll = cdll
else:
raise DaqNotFoundError(
'Could not find an installation of NI-DAQmx. Please '
'ensure that NI-DAQmx is installed on this machine or '
'contact National Instruments for support.')
raise DaqNotFoundError(_DAQ_NOT_FOUND_MESSAGE)
else:
raise DaqNotFoundError(
'NI-DAQmx Python is not supported on this platform: {}. '
'Please direct any questions or feedback to National '
'Instruments.'.format(sys.platform))
raise DaqNotSupportedError(_DAQ_NOT_SUPPORTED_MESSAGE.format(sys.platform))

self._windll = DaqFunctionImporter(windll)
self._cdll = DaqFunctionImporter(cdll)
Expand All @@ -184,7 +175,7 @@ def _get_task_handle_type(driver_version):

def _parse_typedefs(self):
"""
Determines the ctypes data types of the Task and Cal handles
Determines the ctypes data types of the Task and Cal handles
based on the version of the NI-DAQmx driver installed.
"""
from nidaqmx.system.system import System
Expand Down
22 changes: 22 additions & 0 deletions generated/nidaqmx/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@ class Error(Exception):
pass


class DaqNotFoundError(Error):
"""
Error raised when NI-DAQmx driver is not installed.
"""
pass


class DaqNotSupportedError(Error):
"""
Error raised when DAQmx is not supported on this platform.
"""
pass


class DaqFunctionNotSupportedError(Error):
"""
Error raised when a specific function isn't supported by the installed
version of the NI-DAQmx driver.
"""
pass


class DaqError(Error):
"""
Error raised by any DAQmx method.
Expand Down
49 changes: 20 additions & 29 deletions src/handwritten/_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@
import threading
from typing import cast

from nidaqmx.errors import Error
from nidaqmx.errors import DaqNotFoundError, DaqNotSupportedError, DaqFunctionNotSupportedError


class DaqNotFoundError(Error):
pass
_DAQ_NOT_FOUND_MESSAGE = "Could not find an installation of NI-DAQmx. Please ensure that NI-DAQmx " \
"is installed on this machine or contact National Instruments for support."

_DAQ_NOT_SUPPORTED_MESSAGE = "NI-DAQmx Python is not supported on this platform: {0}. Please " \
"direct any questions or feedback to National Instruments."

class DaqFunctionNotSupportedError(Error):
pass


class InvalidHandleError(Error):
pass
_FUNCTION_NOT_SUPPORTED_MESSAGE = "The NI-DAQmx function \"{0}\" is not supported in this version " \
"of NI-DAQmx. Visit ni.com/downloads to upgrade."


class c_bool32(ctypes.c_uint):
Expand Down Expand Up @@ -95,10 +93,7 @@ def __getattr__(self, function):
cfunc.arglock = threading.Lock()
return cfunc
except AttributeError:
raise DaqFunctionNotSupportedError(
'The NI-DAQmx function "{}" is not supported in this '
'version of NI-DAQmx. Visit ni.com/downloads to upgrade your '
'version of NI-DAQmx.'.format(function))
raise DaqFunctionNotSupportedError(_FUNCTION_NOT_SUPPORTED_MESSAGE.format(function))


class DaqLibImporter:
Expand Down Expand Up @@ -147,28 +142,24 @@ def _import_lib(self):
cdll = None

if sys.platform.startswith('win') or sys.platform.startswith('cli'):
if 'iron' in platform.python_implementation().lower():
windll = ctypes.windll.nicaiu
cdll = ctypes.cdll.nicaiu
else:
windll = ctypes.windll.LoadLibrary('nicaiu')
cdll = ctypes.cdll.LoadLibrary('nicaiu')

try:
if 'iron' in platform.python_implementation().lower():
windll = ctypes.windll.nicaiu
cdll = ctypes.cdll.nicaiu
else:
windll = ctypes.windll.LoadLibrary('nicaiu')
cdll = ctypes.cdll.LoadLibrary('nicaiu')
except (OSError, WindowsError) as e:
raise DaqNotFoundError(_DAQ_NOT_FOUND_MESSAGE) from e
elif sys.platform.startswith('linux'):
# On linux you can use the command find_library('nidaqmx')
if find_library('nidaqmx') is not None:
cdll = ctypes.cdll.LoadLibrary(find_library('nidaqmx'))
windll = cdll
else:
raise DaqNotFoundError(
'Could not find an installation of NI-DAQmx. Please '
'ensure that NI-DAQmx is installed on this machine or '
'contact National Instruments for support.')
raise DaqNotFoundError(_DAQ_NOT_FOUND_MESSAGE)
else:
raise DaqNotFoundError(
'NI-DAQmx Python is not supported on this platform: {}. '
'Please direct any questions or feedback to National '
'Instruments.'.format(sys.platform))
raise DaqNotSupportedError(_DAQ_NOT_SUPPORTED_MESSAGE.format(sys.platform))

self._windll = DaqFunctionImporter(windll)
self._cdll = DaqFunctionImporter(cdll)
Expand All @@ -184,7 +175,7 @@ def _get_task_handle_type(driver_version):

def _parse_typedefs(self):
"""
Determines the ctypes data types of the Task and Cal handles
Determines the ctypes data types of the Task and Cal handles
based on the version of the NI-DAQmx driver installed.
"""
from nidaqmx.system.system import System
Expand Down
22 changes: 22 additions & 0 deletions src/handwritten/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@ class Error(Exception):
pass


class DaqNotFoundError(Error):
"""
Error raised when NI-DAQmx driver is not installed.
"""
pass


class DaqNotSupportedError(Error):
"""
Error raised when DAQmx is not supported on this platform.
"""
pass


class DaqFunctionNotSupportedError(Error):
"""
Error raised when a specific function isn't supported by the installed
version of the NI-DAQmx driver.
"""
pass


class DaqError(Error):
"""
Error raised by any DAQmx method.
Expand Down

0 comments on commit f9e1a48

Please sign in to comment.