Skip to content

Commit 88fb949

Browse files
authored
Add set_runtime_environment functionality to niswitch (#1976)
* Update niswitch metadata to add set_runtime_environment functionality * Update CHANGELOG
1 parent aad5dfa commit 88fb949

File tree

9 files changed

+110
-20
lines changed

9 files changed

+110
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ All notable changes to this project will be documented in this file.
6969
* #### Removed
7070
* ### `niswitch` (NI-SWITCH)
7171
* #### Added
72+
* Pass Python interpreter information if the driver runtime version supports it. This is used by NI in order to better understand client usage.
7273
* #### Changed
7374
* #### Removed
7475
* ### `nise` (NI Switch Executive)

generated/niswitch/niswitch/_grpc_stub_interpreter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ def set_path(self, path_list): # noqa: N802
276276
grpc_types.SetPathRequest(vi=self._vi, path_list=path_list),
277277
)
278278

279+
def set_runtime_environment(self, environment, environment_version, reserved1, reserved2): # noqa: N802
280+
raise NotImplementedError('set_runtime_environment is not supported over gRPC')
281+
279282
def unlock(self): # noqa: N802
280283
self._lock.release()
281284

generated/niswitch/niswitch/_library.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def __init__(self, ctypes_library):
5151
self.niSwitch_SetAttributeViReal64_cfunc = None
5252
self.niSwitch_SetAttributeViString_cfunc = None
5353
self.niSwitch_SetPath_cfunc = None
54+
self.niSwitch_SetRuntimeEnvironment_cfunc = None
5455
self.niSwitch_UnlockSession_cfunc = None
5556
self.niSwitch_WaitForDebounce_cfunc = None
5657
self.niSwitch_WaitForScanComplete_cfunc = None
@@ -178,13 +179,13 @@ def niSwitch_GetChannelName(self, vi, index, buffer_size, channel_name_buffer):
178179
self.niSwitch_GetChannelName_cfunc.restype = ViStatus # noqa: F405
179180
return self.niSwitch_GetChannelName_cfunc(vi, index, buffer_size, channel_name_buffer)
180181

181-
def niSwitch_GetError(self, vi, code, buffer_size, description): # noqa: N802
182+
def niSwitch_GetError(self, vi, error_code, buffer_size, description): # noqa: N802
182183
with self._func_lock:
183184
if self.niSwitch_GetError_cfunc is None:
184185
self.niSwitch_GetError_cfunc = self._get_library_function('niSwitch_GetError')
185186
self.niSwitch_GetError_cfunc.argtypes = [ViSession, ctypes.POINTER(ViStatus), ViInt32, ctypes.POINTER(ViChar)] # noqa: F405
186187
self.niSwitch_GetError_cfunc.restype = ViStatus # noqa: F405
187-
return self.niSwitch_GetError_cfunc(vi, code, buffer_size, description)
188+
return self.niSwitch_GetError_cfunc(vi, error_code, buffer_size, description)
188189

189190
def niSwitch_GetPath(self, vi, channel1, channel2, buffer_size, path): # noqa: N802
190191
with self._func_lock:
@@ -322,6 +323,14 @@ def niSwitch_SetPath(self, vi, path_list): # noqa: N802
322323
self.niSwitch_SetPath_cfunc.restype = ViStatus # noqa: F405
323324
return self.niSwitch_SetPath_cfunc(vi, path_list)
324325

326+
def niSwitch_SetRuntimeEnvironment(self, environment, environment_version, reserved1, reserved2): # noqa: N802
327+
with self._func_lock:
328+
if self.niSwitch_SetRuntimeEnvironment_cfunc is None:
329+
self.niSwitch_SetRuntimeEnvironment_cfunc = self._get_library_function('niSwitch_SetRuntimeEnvironment')
330+
self.niSwitch_SetRuntimeEnvironment_cfunc.argtypes = [ctypes.POINTER(ViChar), ctypes.POINTER(ViChar), ctypes.POINTER(ViChar), ctypes.POINTER(ViChar)] # noqa: F405
331+
self.niSwitch_SetRuntimeEnvironment_cfunc.restype = ViStatus # noqa: F405
332+
return self.niSwitch_SetRuntimeEnvironment_cfunc(environment, environment_version, reserved1, reserved2)
333+
325334
def niSwitch_UnlockSession(self, vi, caller_has_lock): # noqa: N802
326335
with self._func_lock:
327336
if self.niSwitch_UnlockSession_cfunc is None:

generated/niswitch/niswitch/_library_interpreter.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
import array
55
import ctypes
66
import hightime # noqa: F401
7+
import platform
8+
79
import niswitch._library_singleton as _library_singleton
810
import niswitch._visatype as _visatype
911
import niswitch.enums as enums # noqa: F401
1012
import niswitch.errors as errors
1113

1214

15+
_was_runtime_environment_set = None
16+
17+
1318
# Helper functions for creating ctypes needed for calling into the driver DLL
1419
def _get_ctypes_pointer_for_buffer(value=None, library_type=None, size=None):
1520
if isinstance(value, array.array):
@@ -56,6 +61,21 @@ class LibraryInterpreter(object):
5661
def __init__(self, encoding):
5762
self._encoding = encoding
5863
self._library = _library_singleton.get()
64+
global _was_runtime_environment_set
65+
if _was_runtime_environment_set is None:
66+
try:
67+
runtime_env = platform.python_implementation()
68+
version = platform.python_version()
69+
self.set_runtime_environment(
70+
runtime_env,
71+
version,
72+
'',
73+
''
74+
)
75+
except errors.DriverTooOldError:
76+
pass
77+
finally:
78+
_was_runtime_environment_set = True
5979
# Initialize _vi to 0 for now.
6080
# Session will directly update it once the driver runtime init function has been called and
6181
# we have a valid session handle.
@@ -210,16 +230,16 @@ def get_channel_name(self, index): # noqa: N802
210230

211231
def get_error(self): # noqa: N802
212232
vi_ctype = _visatype.ViSession(self._vi) # case S110
213-
code_ctype = _visatype.ViStatus() # case S220
233+
error_code_ctype = _visatype.ViStatus() # case S220
214234
buffer_size_ctype = _visatype.ViInt32() # case S170
215235
description_ctype = None # case C050
216-
error_code = self._library.niSwitch_GetError(vi_ctype, None if code_ctype is None else (ctypes.pointer(code_ctype)), buffer_size_ctype, description_ctype)
236+
error_code = self._library.niSwitch_GetError(vi_ctype, None if error_code_ctype is None else (ctypes.pointer(error_code_ctype)), buffer_size_ctype, description_ctype)
217237
errors.handle_error(self, error_code, ignore_warnings=True, is_error_handling=True)
218238
buffer_size_ctype = _visatype.ViInt32(error_code) # case S180
219239
description_ctype = (_visatype.ViChar * buffer_size_ctype.value)() # case C060
220-
error_code = self._library.niSwitch_GetError(vi_ctype, None if code_ctype is None else (ctypes.pointer(code_ctype)), buffer_size_ctype, description_ctype)
240+
error_code = self._library.niSwitch_GetError(vi_ctype, None if error_code_ctype is None else (ctypes.pointer(error_code_ctype)), buffer_size_ctype, description_ctype)
221241
errors.handle_error(self, error_code, ignore_warnings=False, is_error_handling=True)
222-
return int(code_ctype.value), description_ctype.value.decode(self._encoding)
242+
return int(error_code_ctype.value), description_ctype.value.decode(self._encoding)
223243

224244
def get_path(self, channel1, channel2): # noqa: N802
225245
vi_ctype = _visatype.ViSession(self._vi) # case S110
@@ -368,6 +388,15 @@ def set_path(self, path_list): # noqa: N802
368388
errors.handle_error(self, error_code, ignore_warnings=False, is_error_handling=False)
369389
return
370390

391+
def set_runtime_environment(self, environment, environment_version, reserved1, reserved2): # noqa: N802
392+
environment_ctype = ctypes.create_string_buffer(environment.encode(self._encoding)) # case C020
393+
environment_version_ctype = ctypes.create_string_buffer(environment_version.encode(self._encoding)) # case C020
394+
reserved1_ctype = ctypes.create_string_buffer(reserved1.encode(self._encoding)) # case C020
395+
reserved2_ctype = ctypes.create_string_buffer(reserved2.encode(self._encoding)) # case C020
396+
error_code = self._library.niSwitch_SetRuntimeEnvironment(environment_ctype, environment_version_ctype, reserved1_ctype, reserved2_ctype)
397+
errors.handle_error(self, error_code, ignore_warnings=False, is_error_handling=False)
398+
return
399+
371400
def unlock(self): # noqa: N802
372401
vi_ctype = _visatype.ViSession(self._vi) # case S110
373402
error_code = self._library.niSwitch_UnlockSession(vi_ctype, None)

generated/niswitch/niswitch/unit_tests/_mock_helper.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(self):
5252
self._defaults['GetChannelName']['channelNameBuffer'] = None
5353
self._defaults['GetError'] = {}
5454
self._defaults['GetError']['return'] = 0
55-
self._defaults['GetError']['code'] = None
55+
self._defaults['GetError']['errorCode'] = None
5656
self._defaults['GetError']['description'] = None
5757
self._defaults['GetPath'] = {}
5858
self._defaults['GetPath']['return'] = 0
@@ -94,6 +94,8 @@ def __init__(self):
9494
self._defaults['SetAttributeViString']['return'] = 0
9595
self._defaults['SetPath'] = {}
9696
self._defaults['SetPath']['return'] = 0
97+
self._defaults['SetRuntimeEnvironment'] = {}
98+
self._defaults['SetRuntimeEnvironment']['return'] = 0
9799
self._defaults['UnlockSession'] = {}
98100
self._defaults['UnlockSession']['return'] = 0
99101
self._defaults['UnlockSession']['callerHasLock'] = None
@@ -221,14 +223,14 @@ def niSwitch_GetChannelName(self, vi, index, buffer_size, channel_name_buffer):
221223
channel_name_buffer.value = self._defaults['GetChannelName']['channelNameBuffer'].encode('ascii')
222224
return self._defaults['GetChannelName']['return']
223225

224-
def niSwitch_GetError(self, vi, code, buffer_size, description): # noqa: N802
226+
def niSwitch_GetError(self, vi, error_code, buffer_size, description): # noqa: N802
225227
if self._defaults['GetError']['return'] != 0:
226228
return self._defaults['GetError']['return']
227-
# code
228-
if self._defaults['GetError']['code'] is None:
229-
raise MockFunctionCallError("niSwitch_GetError", param='code')
230-
if code is not None:
231-
code.contents.value = self._defaults['GetError']['code']
229+
# error_code
230+
if self._defaults['GetError']['errorCode'] is None:
231+
raise MockFunctionCallError("niSwitch_GetError", param='errorCode')
232+
if error_code is not None:
233+
error_code.contents.value = self._defaults['GetError']['errorCode']
232234
# description
233235
if self._defaults['GetError']['description'] is None:
234236
raise MockFunctionCallError("niSwitch_GetError", param='description')
@@ -354,6 +356,11 @@ def niSwitch_SetPath(self, vi, path_list): # noqa: N802
354356
return self._defaults['SetPath']['return']
355357
return self._defaults['SetPath']['return']
356358

359+
def niSwitch_SetRuntimeEnvironment(self, environment, environment_version, reserved1, reserved2): # noqa: N802
360+
if self._defaults['SetRuntimeEnvironment']['return'] != 0:
361+
return self._defaults['SetRuntimeEnvironment']['return']
362+
return self._defaults['SetRuntimeEnvironment']['return']
363+
357364
def niSwitch_UnlockSession(self, vi, caller_has_lock): # noqa: N802
358365
if self._defaults['UnlockSession']['return'] != 0:
359366
return self._defaults['UnlockSession']['return']
@@ -483,6 +490,8 @@ def set_side_effects_and_return_values(self, mock_library):
483490
mock_library.niSwitch_SetAttributeViString.return_value = 0
484491
mock_library.niSwitch_SetPath.side_effect = MockFunctionCallError("niSwitch_SetPath")
485492
mock_library.niSwitch_SetPath.return_value = 0
493+
mock_library.niSwitch_SetRuntimeEnvironment.side_effect = MockFunctionCallError("niSwitch_SetRuntimeEnvironment")
494+
mock_library.niSwitch_SetRuntimeEnvironment.return_value = 0
486495
mock_library.niSwitch_UnlockSession.side_effect = MockFunctionCallError("niSwitch_UnlockSession")
487496
mock_library.niSwitch_UnlockSession.return_value = 0
488497
mock_library.niSwitch_WaitForDebounce.side_effect = MockFunctionCallError("niSwitch_WaitForDebounce")

src/niswitch/metadata/attributes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
# This file is generated from NI-SWITCH API metadata version 23.0.0f167
2+
# This file is generated from NI-SWITCH API metadata version 23.5.0d149
33
attributes = {
44
1050005: {
55
'access': 'read-write',

src/niswitch/metadata/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
2-
# This file is generated from NI-SWITCH API metadata version 23.0.0f167
2+
# This file is generated from NI-SWITCH API metadata version 23.5.0d149
33
config = {
4-
'api_version': '23.0.0f167',
4+
'api_version': '23.5.0d149',
55
'c_function_prefix': 'niSwitch_',
66
'close_function': 'close',
77
'context_manager_name': {

src/niswitch/metadata/enums.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
# This file is generated from NI-SWITCH API metadata version 23.0.0f167
2+
# This file is generated from NI-SWITCH API metadata version 23.5.0d149
33
enums = {
44
'CabledModuleScanAdvancedBus': {
55
'values': [

src/niswitch/metadata/functions.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
# This file is generated from NI-SWITCH API metadata version 23.0.0f167
2+
# This file is generated from NI-SWITCH API metadata version 23.5.0d149
33
functions = {
44
'AbortScan': {
55
'documentation': {
@@ -482,7 +482,8 @@
482482
'documentation': {
483483
'description': '\nReturns the error code for the session or execution thread. If you pass\n0 for the Buffer Size, you can pass VI_NULL for this parameter.\n'
484484
},
485-
'name': 'code',
485+
'grpc_name': 'code',
486+
'name': 'errorCode',
486487
'type': 'ViStatus'
487488
},
488489
{
@@ -1115,7 +1116,7 @@
11151116
},
11161117
'grpc_name': 'attribute_value_raw',
11171118
'name': 'attributeValue',
1118-
'type': 'ViString'
1119+
'type': 'ViConstString'
11191120
}
11201121
],
11211122
'returns': 'ViStatus'
@@ -1145,6 +1146,44 @@
11451146
],
11461147
'returns': 'ViStatus'
11471148
},
1149+
'SetRuntimeEnvironment': {
1150+
'codegen_method': 'private',
1151+
'documentation': {
1152+
'description': 'TBD'
1153+
},
1154+
'included_in_proto': False,
1155+
'method_templates': [
1156+
{
1157+
'documentation_filename': 'none',
1158+
'library_interpreter_filename': 'default_method',
1159+
'method_python_name_suffix': '',
1160+
'session_filename': 'none'
1161+
}
1162+
],
1163+
'parameters': [
1164+
{
1165+
'direction': 'in',
1166+
'name': 'environment',
1167+
'type': 'ViConstString'
1168+
},
1169+
{
1170+
'direction': 'in',
1171+
'name': 'environmentVersion',
1172+
'type': 'ViConstString'
1173+
},
1174+
{
1175+
'direction': 'in',
1176+
'name': 'reserved1',
1177+
'type': 'ViConstString'
1178+
},
1179+
{
1180+
'direction': 'in',
1181+
'name': 'reserved2',
1182+
'type': 'ViConstString'
1183+
}
1184+
],
1185+
'returns': 'ViStatus'
1186+
},
11481187
'UnlockSession': {
11491188
'documentation': {
11501189
'description': '\nThis function releases a lock that you acquired on an instrument session\nusing niSwitch_LockSession. Refer to niSwitch_LockSession for\nadditional information on session locks.\n'

0 commit comments

Comments
 (0)