diff --git a/CHANGELOG.md b/CHANGELOG.md index 16856480..97bf88e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 * ... diff --git a/generated/nidaqmx/_lib.py b/generated/nidaqmx/_lib.py index 04551638..02bd13a9 100644 --- a/generated/nidaqmx/_lib.py +++ b/generated/nidaqmx/_lib.py @@ -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): @@ -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: @@ -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) @@ -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 diff --git a/generated/nidaqmx/errors.py b/generated/nidaqmx/errors.py index 8c068ddc..02b17145 100644 --- a/generated/nidaqmx/errors.py +++ b/generated/nidaqmx/errors.py @@ -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. diff --git a/src/handwritten/_lib.py b/src/handwritten/_lib.py index 04551638..02bd13a9 100644 --- a/src/handwritten/_lib.py +++ b/src/handwritten/_lib.py @@ -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): @@ -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: @@ -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) @@ -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 diff --git a/src/handwritten/errors.py b/src/handwritten/errors.py index 8c068ddc..02b17145 100644 --- a/src/handwritten/errors.py +++ b/src/handwritten/errors.py @@ -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.