11from __future__ import annotations
22
3- from ctypes .util import find_library
43import ctypes
5- from numpy . ctypeslib import ndpointer
4+ import locale
65import platform
76import sys
87import threading
9- import locale
8+ from ctypes .util import find_library
9+ from typing import TYPE_CHECKING , cast
10+
1011from decouple import config
11- from typing import cast , TYPE_CHECKING
12+ from numpy . ctypeslib import ndpointer
1213
13- from nidaqmx .errors import DaqNotFoundError , DaqNotSupportedError , DaqFunctionNotSupportedError
14+ from nidaqmx .errors import (
15+ DaqFunctionNotSupportedError ,
16+ DaqNotFoundError ,
17+ DaqNotSupportedError ,
18+ )
1419
1520if TYPE_CHECKING :
1621 from typing_extensions import TypeAlias
1722
1823
19- _DAQ_NOT_FOUND_MESSAGE = "Could not find an installation of NI-DAQmx. Please ensure that NI-DAQmx " \
20- "is installed on this machine or contact National Instruments for support."
24+ _DAQ_NOT_FOUND_MESSAGE = (
25+ "Could not find an installation of NI-DAQmx. Please ensure that NI-DAQmx "
26+ "is installed on this machine or contact National Instruments for support."
27+ )
2128
22- _DAQ_NOT_SUPPORTED_MESSAGE = "NI-DAQmx Python is not supported on this platform: {0}. Please " \
23- "direct any questions or feedback to National Instruments."
29+ _DAQ_NOT_SUPPORTED_MESSAGE = (
30+ "NI-DAQmx Python is not supported on this platform: {0}. Please "
31+ "direct any questions or feedback to National Instruments."
32+ )
2433
25- _FUNCTION_NOT_SUPPORTED_MESSAGE = "The NI-DAQmx function \" {0}\" is not supported in this version " \
26- "of NI-DAQmx. Visit ni.com/downloads to upgrade."
34+ _FUNCTION_NOT_SUPPORTED_MESSAGE = (
35+ 'The NI-DAQmx function "{0}" is not supported in this version '
36+ "of NI-DAQmx. Visit ni.com/downloads to upgrade."
37+ )
2738
2839
2940class c_bool32 (ctypes .c_uint ):
@@ -48,9 +59,10 @@ def _setter(self, val):
4859
4960class CtypesByteString :
5061 """
51- Custom argtype that automatically converts unicode strings to encoding
62+ Custom argtype that automatically converts unicode strings to encoding
5263 used by the DAQmx C API DLL in Python 3.
5364 """
65+
5466 @classmethod
5567 def from_param (cls , param ):
5668 if isinstance (param , str ):
@@ -75,8 +87,7 @@ def from_param(cls, obj):
7587 return obj
7688 return base .from_param (obj )
7789
78- return type (base .__name__ , (base ,),
79- {'from_param' : classmethod (from_param )})
90+ return type (base .__name__ , (base ,), {"from_param" : classmethod (from_param )})
8091
8192
8293class DaqFunctionImporter :
@@ -94,9 +105,9 @@ def __init__(self, library):
94105 def __getattr__ (self , function ):
95106 try :
96107 cfunc = getattr (self ._library , function )
97- if not hasattr (cfunc , ' arglock' ):
108+ if not hasattr (cfunc , " arglock" ):
98109 with self ._lib_lock :
99- if not hasattr (cfunc , ' arglock' ):
110+ if not hasattr (cfunc , " arglock" ):
100111 cfunc .arglock = threading .Lock ()
101112 return cfunc
102113 except AttributeError :
@@ -125,7 +136,7 @@ def get_encoding_from_locale() -> str:
125136 Gets the current locale encoding handling cases where it is unset.
126137 """
127138 _ , encoding = locale .getlocale ()
128- return encoding or ' ascii'
139+ return encoding or " ascii"
129140
130141
131142class DaqLibImporter :
@@ -159,7 +170,7 @@ def task_handle(self) -> type:
159170 @property
160171 def cal_handle (self ) -> type :
161172 return CalHandle
162-
173+
163174 @property
164175 def encoding (self ):
165176 if self ._encoding is None :
@@ -178,42 +189,44 @@ def _import_lib(self):
178189 cdll = None
179190 encoding = None
180191
181- if sys .platform .startswith (' win' ):
192+ if sys .platform .startswith (" win" ):
182193
183194 def _load_lib (libname : str ):
184195 windll = ctypes .windll .LoadLibrary (libname )
185196 cdll = ctypes .cdll .LoadLibrary (libname )
186- return windll , cdll
187-
197+ return windll , cdll
198+
188199 # Feature Toggle to load nicaiu.dll or nicai_utf8.dll
189200 # The Feature Toggle can be set in the .env file
190- nidaqmx_c_library = config (' NIDAQMX_C_LIBRARY' , default = None )
191-
201+ nidaqmx_c_library = config (" NIDAQMX_C_LIBRARY" , default = None )
202+
192203 if nidaqmx_c_library is not None :
193- try :
194- if nidaqmx_c_library == "nicaiu" :
204+ try :
205+ if nidaqmx_c_library == "nicaiu" :
195206 windll , cdll = _load_lib ("nicaiu" )
196207 encoding = get_encoding_from_locale ()
197- elif nidaqmx_c_library == "nicai_utf8" :
208+ elif nidaqmx_c_library == "nicai_utf8" :
198209 windll , cdll = _load_lib ("nicai_utf8" )
199- encoding = ' utf-8'
210+ encoding = " utf-8"
200211 else :
201- raise ValueError (f"Unsupported NIDAQMX_C_LIBRARY value: { nidaqmx_c_library } " )
212+ raise ValueError (
213+ f"Unsupported NIDAQMX_C_LIBRARY value: { nidaqmx_c_library } "
214+ )
202215 except OSError as e :
203- raise DaqNotFoundError (_DAQ_NOT_FOUND_MESSAGE ) from e
216+ raise DaqNotFoundError (_DAQ_NOT_FOUND_MESSAGE ) from e
204217 else :
205218 try :
206219 windll , cdll = _load_lib ("nicai_utf8" )
207- encoding = ' utf-8'
220+ encoding = " utf-8"
208221 except OSError :
209222 # Fallback to nicaiu.dll if nicai_utf8.dll cannot be loaded
210223 try :
211224 windll , cdll = _load_lib ("nicaiu" )
212225 encoding = get_encoding_from_locale ()
213226 except OSError as e :
214- raise DaqNotFoundError (_DAQ_NOT_FOUND_MESSAGE ) from e
215- elif sys .platform .startswith (' linux' ):
216- library_path = find_library (' nidaqmx' )
227+ raise DaqNotFoundError (_DAQ_NOT_FOUND_MESSAGE ) from e
228+ elif sys .platform .startswith (" linux" ):
229+ library_path = find_library (" nidaqmx" )
217230 if library_path is not None :
218231 cdll = ctypes .cdll .LoadLibrary (library_path )
219232 windll = cdll
0 commit comments