forked from ni/nidaqmx-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter_helpers.py
733 lines (622 loc) · 28.3 KB
/
interpreter_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
"""This contains the helper methods used in interpreter generation."""
import re
from copy import deepcopy
from codegen.functions.function import Function
from codegen.utilities.function_helpers import to_param_argtype
from codegen.utilities.helpers import (
AttributeFunctionType,
camel_to_snake_case,
removeprefix,
)
# This custom regex list doesn't split the string before the number.
INTERPRETER_CAMEL_TO_SNAKE_CASE_REGEXES = [
re.compile("([^_\n])([A-Z][a-z]+)"),
re.compile("([a-z])([A-Z])"),
re.compile("([0-9])([^_0-9])"),
]
INTERPRETER_IGNORED_FUNCTIONS = [
"GetExtendedErrorInfo",
# nidaqmx-python uses Get/SetBufferAttribute.
"CfgInputBuffer",
"CfgOutputBuffer",
# nidaqmx-python uses GetTaskAttributeString and splits the comma-delimited string.
"GetNthTaskChannel",
"GetNthTaskDevice",
"GetNthTaskReadChannel",
# nidaqmx-python uses CreateWatchdogTimerTaskEx.
"CreateWatchdogTimerTask",
# AI channel calibration
"GetAIChanCalCalDate",
"GetAIChanCalExpDate",
"SetAIChanCalCalDate",
"SetAIChanCalExpDate",
# Calibration
"DeviceSupportsCal",
"GetCalInfoAttributeBool",
"GetCalInfoAttributeDouble",
"GetCalInfoAttributeString",
"GetCalInfoAttributeUInt32",
"GetSelfCalLastDateAndTime",
"SetCalInfoAttributeBool",
"SetCalInfoAttributeDouble",
"SetCalInfoAttributeString",
"SetCalInfoAttributeUInt32",
# Real-time
"GetRealTimeAttributeBool",
"GetRealTimeAttributeInt32",
"GetRealTimeAttributeUInt32",
"ResetRealTimeAttribute",
"SetRealTimeAttributeBool",
"SetRealTimeAttributeInt32",
"SetRealTimeAttributeUInt32",
"WaitForNextSampleClock",
# Time triggers
"GetArmStartTrigTimestampVal",
"GetArmStartTrigTrigWhen",
"GetFirstSampClkWhen",
"GetFirstSampTimestampVal",
"GetRefTrigTimestampVal",
"GetStartTrigTimestampVal",
"GetStartTrigTrigWhen",
"GetSyncPulseTimeWhen",
"GetTimingAttributeExTimestamp",
"GetTimingAttributeTimestamp",
"GetTrigAttributeTimestamp",
"SetArmStartTrigTrigWhen",
"SetFirstSampClkWhen",
"SetStartTrigTrigWhen",
"SetSyncPulseTimeWhen",
"SetTimingAttributeExTimestamp",
"SetTimingAttributeTimestamp",
"SetTrigAttributeTimestamp",
"WaitForValidTimestamp",
# Deprecated, not working
"GetAnalogPowerUpStates",
]
GRPC_INTERPRETER_IGNORED_FUNCTIONS = [
"get_error_string",
]
LIBRARY_INTERPRETER_IGNORED_FUNCTIONS = [
"get_error_string",
"read_power_binary_i16",
"read_power_f64",
"read_raw",
"write_raw",
]
INCLUDE_SIZE_PARAMETER_IN_SIGNATURE_FUNCTIONS = [
"get_analog_power_up_states_with_output_type",
]
MODIFIED_INTERPRETER_PARAMS = {
"r_0": "r0",
"r_1": "r1",
}
EVENT_UNREGISTER_IGNORED_PARAMS = [
"callback_data",
"callback_function",
"n_samples",
"options",
]
READ_SAMPLES_PARAMETER_NAMES = [
"samps_read",
"samps_per_chan_read",
"num_samps_per_chan",
]
def get_interpreter_functions(metadata):
"""Converts the scrapigen metadata into a list of functions."""
all_functions = deepcopy(metadata["functions"])
functions_metadata = []
for function_name, function_data in all_functions.items():
if function_name in INTERPRETER_IGNORED_FUNCTIONS:
continue
function_data["c_function_name"] = function_name
function_name = camel_to_snake_case(function_name, INTERPRETER_CAMEL_TO_SNAKE_CASE_REGEXES)
function_name = function_name.replace("_u_int", "_uint")
functions_metadata.append(
Function(
function_name,
function_data,
)
)
return sorted(functions_metadata, key=lambda x: x._function_name)
def generate_interpreter_function_call_args(function_metadata):
"""Gets function call arguments."""
function_call_args = []
dateTime_args = []
size_values = {}
interpreter_parameters = get_interpreter_parameters(function_metadata)
for param in interpreter_parameters:
if param.has_explicit_buffer_size:
if param.direction == "in":
size_values[param.size.value] = f"len({param.parameter_name})"
elif param.direction == "out":
if param.size.mechanism == "ivi-dance":
size_values[param.size.value] = "temp_size"
elif (
is_custom_read_write_function(function_metadata)
and param.has_explicit_buffer_size
):
if param.size.mechanism == "passed-in":
size_values[param.size.value] = f"{param.parameter_name}.size"
for param in interpreter_parameters:
if param.parameter_name in size_values:
function_call_args.append(size_values[param.parameter_name])
elif param.parameter_name == "reserved":
function_call_args.append("None")
elif is_event_function(function_metadata) and param.parameter_name == "callback_function":
function_call_args.append("callback_method_ptr")
elif param.type == "CVIAbsoluteTime":
dateTime_args.append(
f"{param.parameter_name} = AbsoluteTime.from_datetime({param.parameter_name})"
)
function_call_args.append(param.parameter_name)
elif param.direction == "out" or (
param.is_pointer and param.parameter_name != "callback_data"
):
if param.has_explicit_buffer_size:
if (
is_numpy_array_datatype(param)
and function_metadata.attribute_function_type == AttributeFunctionType.GET
):
function_call_args.append(
f"{param.parameter_name}.ctypes.data_as(ctypes.c_void_p)"
)
else:
function_call_args.append(param.parameter_name)
else:
function_call_args.append(f"ctypes.byref({param.parameter_name})")
elif param.direction == "in":
if (
param.parameter_name == "value"
and function_metadata.attribute_function_type == AttributeFunctionType.SET
):
function_call_args.append(type_cast_attribute_set_function_parameter(param))
else:
function_call_args.append(param.parameter_name)
return function_call_args, dateTime_args
def get_argument_types(functions_metadata):
"""Gets the 'type' of parameters."""
argtypes = []
interpreter_parameters = get_interpreter_parameters(functions_metadata)
size_params = _get_size_params(interpreter_parameters)
for param in interpreter_parameters:
# Skipping the c arguments of these parameters in attribute functions
# to remove the variadic arguments.
if (
param.parameter_name in ("value", "size")
and functions_metadata.attribute_function_type != AttributeFunctionType.NONE
):
continue
if _is_handle_parameter(functions_metadata, param):
if functions_metadata.handle_parameter.ctypes_data_type != "ctypes.c_char_p":
if param.direction == "in":
argtypes.append(functions_metadata.handle_parameter.ctypes_data_type)
else:
argtypes.append(
f"ctypes.POINTER({functions_metadata.handle_parameter.ctypes_data_type})"
)
else:
argtypes.append("ctypes_byte_str")
elif param.parameter_name in size_params:
if param.direction == "out" or param.is_pointer:
argtypes.append("ctypes.POINTER(ctypes.c_uint)")
else:
argtypes.append("ctypes.c_uint")
else:
if param.is_pointer:
argtypes.append(f"ctypes.POINTER({to_param_argtype(param)})")
else:
argtypes.append(to_param_argtype(param))
return argtypes
def get_interpreter_parameter_signature(is_python_factory, params):
"""Gets parameter signature for function definition."""
params_with_defaults = []
if not is_python_factory:
params_with_defaults.append("self")
for param in params:
if param.type:
params_with_defaults.append(param.parameter_name)
return ", ".join(params_with_defaults)
def get_instantiation_lines_for_output(func):
"""Gets the lines of code for instantiation of output values."""
instantiation_lines = []
if func.is_init_method:
instantiation_lines.append(f"task = lib_importer.task_handle(0)")
for param in get_interpreter_output_params(func):
if param.parameter_name == "task":
continue
elif param.repeating_argument:
instantiation_lines.append(f"{param.parameter_name} = []")
elif param.has_explicit_buffer_size:
if is_custom_read_write_function(func) and param.size.mechanism == "passed-in":
continue
if (
param.size.mechanism == "passed-in" or param.size.mechanism == "passed-in-by-ptr"
) and param.is_list:
instantiation_lines.append(
f"{param.parameter_name} = numpy.zeros({param.size.value}, dtype={param.ctypes_data_type})"
)
elif param.size.mechanism == "custom-code":
instantiation_lines.append(f"size = {param.size.value}")
instantiation_lines.append(
f"{param.parameter_name} = numpy.zeros(size, dtype={param.ctypes_data_type})"
)
else:
instantiation_lines.append(f"{param.parameter_name} = {param.ctypes_data_type}()")
for param in get_interpreter_in_out_params(func):
if param.parameter_name == "reserved" or param.parameter_name == "callback_data":
continue
instantiation_lines.append(
f"{param.parameter_name} = {param.ctypes_data_type}({param.parameter_name})"
)
return instantiation_lines
def get_instantiation_lines_for_varargs(func):
"""Gets instantiation lines for functions with variable arguments."""
instantiation_lines = []
if any(get_varargs_parameters(func)):
for param in func.output_parameters:
instantiation_lines.append(
f"{param.parameter_name}_element = {param.ctypes_data_type}()"
)
instantiation_lines.append(
f"{param.parameter_name}.append({param.parameter_name}_element)"
)
return instantiation_lines
def get_argument_definition_lines_for_varargs(varargs_params):
"""Gets the lines for defining the variable arguments for a function."""
argument_definition_lines = []
for param in varargs_params:
argtype = to_param_argtype(param)
if param.direction == "in":
argument_definition_lines.append(f"args.append({param.parameter_name}[index])")
else:
argument_definition_lines.append(
f"args.append(ctypes.byref({param.parameter_name}_element))"
)
argument_definition_lines.append(f"argtypes.append({argtype})")
argument_definition_lines.append("")
return argument_definition_lines
def get_varargs_parameters(func):
"""Gets variable arguments of a function."""
return [p for p in func.parameters if p.repeating_argument]
def get_params_for_function_signature(func, is_grpc_interpreter=False):
"""Gets interpreter parameters for the function signature."""
interpreter_parameters = []
function_parameters = get_interpreter_parameters(func, is_grpc_interpreter)
size_params = _get_size_params(function_parameters)
for param in function_parameters:
if (
param.parameter_name in size_params or param.parameter_name == "reserved"
) and func.function_name not in INCLUDE_SIZE_PARAMETER_IN_SIGNATURE_FUNCTIONS:
continue
if (
is_event_unregister_function(func)
and param.parameter_name in EVENT_UNREGISTER_IGNORED_PARAMS
):
continue
if param.direction == "in":
interpreter_parameters.append(param)
elif is_custom_read_write_function(func) and param.has_explicit_buffer_size:
if param.size.mechanism == "passed-in":
interpreter_parameters.append(param)
return interpreter_parameters
def get_grpc_interpreter_call_params(func, params):
"""Gets the interpreter parameters for grpc request."""
compound_params = get_input_arguments_for_compound_params(func)
is_read_function = is_custom_read_function(func)
is_write_function = is_custom_write_function(func)
grpc_params = []
has_read_array_parameter = False
for param in params:
if not param.include_in_proto:
continue
if param.parameter_name not in compound_params:
name = param.parameter_name
if param.parameter_name in MODIFIED_INTERPRETER_PARAMS:
name = MODIFIED_INTERPRETER_PARAMS.get(param.parameter_name)
if is_read_function and "read_array" in name:
if has_read_array_parameter:
continue
if is_read_bytes_param(param):
grpc_params.append(
f"{camel_to_snake_case(param.size.value)}={param.parameter_name}.nbytes"
)
else:
grpc_params.append(
f"{camel_to_snake_case(param.size.value)}={param.parameter_name}.size"
)
has_read_array_parameter = True
elif param.is_grpc_enum or (param.is_enum and not param.is_list):
grpc_params.append(f"{name}_raw={param.parameter_name}")
elif param.type == "CVIAbsoluteTime":
grpc_params.append(f"{name}=convert_time_to_timestamp({param.parameter_name})")
else:
if is_write_bytes_param(param):
grpc_params.append(f"{name}={param.parameter_name}.tobytes()")
elif is_write_function:
grpc_params.append(get_write_array_param(param))
else:
grpc_params.append(f"{name}={param.parameter_name}")
if func.is_init_method:
grpc_params.append("initialization_behavior=self._grpc_options.initialization_behavior")
return ", ".join(grpc_params)
def get_output_param_with_ivi_dance_mechanism(func):
"""Gets the output parameters with explicit buffer size."""
output_parameters = get_output_params(func)
explicit_output_params = [p for p in output_parameters if p.has_explicit_buffer_size]
params_with_ivi_dance_mechanism = [
p for p in explicit_output_params if p.size.mechanism == "ivi-dance"
]
if len(params_with_ivi_dance_mechanism) > 1:
raise NotImplementedError(
"There is more than one output parameter with an explicit "
"buffer size that follows ivi dance mechanism."
"This cannot be handled by this template because it "
'calls the C function once with "buffer_size = 0" to get the '
"buffer size from the returned integer, which is normally an "
"error code.\n\n"
"Output parameters with explicit buffer sizes: {}".format(
params_with_ivi_dance_mechanism
)
)
if len(params_with_ivi_dance_mechanism) == 1:
return params_with_ivi_dance_mechanism[0]
return None
def has_parameter_with_ivi_dance_size_mechanism(func):
"""Returns true if the function has a parameter with ivi dance size mechanism."""
parameter_with_size_buffer = get_output_param_with_ivi_dance_mechanism(func)
return parameter_with_size_buffer is not None
def is_custom_read_write_function(func):
"""Returns True if the function is a read or write function."""
return func.python_codegen_method in ("CustomCode_Read", "CustomCode_Write")
def is_custom_read_function(func):
"""Returns True if the function is a read function."""
return func.python_codegen_method == "CustomCode_Read"
def is_custom_write_function(func):
"""Returns True if the function is a write function."""
return func.python_codegen_method == "CustomCode_Write"
def get_interpreter_output_params(func):
"""Gets the output parameters for the functions in interpreter."""
return [p for p in get_interpreter_parameters(func) if p.direction == "out"]
def get_output_params(func):
"""Gets output parameters for the function."""
return [p for p in func.base_parameters if p.direction == "out"]
def get_interpreter_in_out_params(func):
"""Gets the input parameters that are also pointers for the function."""
return [p for p in get_interpreter_parameters(func) if p.direction == "in" and p.is_pointer]
def get_return_values(func):
"""Gets the values to add to return statement of the function."""
return_values = []
for param in get_interpreter_output_params(func):
is_read_write_function = is_custom_read_write_function(func)
if param.repeating_argument:
return_values.append(
f"[{param.parameter_name}_element.value for {param.parameter_name}_element in {param.parameter_name}]"
)
elif param.ctypes_data_type == "ctypes.c_char_p":
return_values.append(f"{param.parameter_name}.value.decode('ascii')")
elif param.is_list:
if is_read_write_function:
return_values.append(param.parameter_name)
else:
return_values.append(f"{param.parameter_name}.tolist()")
elif param.type == "TaskHandle":
return_values.append(param.parameter_name)
else:
return_values.append(f"{param.parameter_name}.value")
if func.is_init_method:
return_values.append("new_session_initialized")
return return_values
def get_c_function_call_template(func):
"""Gets the template to use for generating the logic of calling the c functions."""
if is_event_function(func):
return "/event_function_call.py.mako"
elif any(get_varargs_parameters(func)):
return "/exec_cdecl_c_function_call.py.mako"
elif has_parameter_with_ivi_dance_size_mechanism(func):
return "/double_c_function_call.py.mako"
return "/default_c_function_call.py.mako"
def get_grpc_function_call_template(func):
"""Gets the template to use for generating the logic of calling the grpc functions."""
if func.stream_response:
return "/event_function_call.py.mako"
else:
return "/default_grpc_function_call.py.mako"
def get_callback_func_param(func):
"""Gets the callback_function parameter."""
return next(p for p in func.base_parameters if p.parameter_name == "callback_function")
def get_callback_data_param(func):
"""Gets the callback_data parameter."""
return next(p for p in func.base_parameters if p.parameter_name == "callback_data")
def get_callback_function_call_args(func):
"""Gets the parameters used in the callback function call."""
callback_func_param = get_callback_func_param(func)
callback_func_args = []
for param in callback_func_param.callback_params:
name = camel_to_snake_case(param["name"])
if name == "task":
callback_func_args.append(f"{name}")
elif "enum" in param:
callback_func_args.append(f"response.{name}_raw")
else:
callback_func_args.append(f"response.{name}")
callback_func_args.append("callback_data")
return callback_func_args
def get_callback_param_data_types(func):
"""Gets the data types for call back function parameters."""
callback_func_param = get_callback_func_param(func)
callback_data_param = get_callback_data_param(func)
# callback_param_types: [result_type, [**ctypes_data_type** of callback_params],
# **ctypes_data_type** of callback_data_param]
return (
["ctypes.c_int32"]
+ [p["ctypes_data_type"] for p in callback_func_param.callback_params]
+ [callback_data_param.ctypes_data_type]
)
def is_event_function(func):
"""Returns True if this is an event register/unregister function, False otherwise."""
return is_event_register_function(func) or is_event_unregister_function(func)
def is_event_register_function(func):
"""Returns True if this is an event register function, False otherwise."""
return func.function_name.startswith("register_")
def is_event_unregister_function(func):
"""Returns True if this is an event unregister function, False otherwise."""
return func.function_name.startswith("unregister_")
def get_event_name(func):
"""Gets the event name for an event register/unregister function."""
if is_event_register_function(func):
return removeprefix(func.function_name, "register_")
elif is_event_unregister_function(func):
return removeprefix(func.function_name, "unregister_")
else:
raise ValueError(f"{func.function_name} is not an event function.")
def get_compound_parameter(params):
"""Returns the compound parameter associated with the given function."""
return next((x for x in params if x.is_compound_type), None)
def get_input_arguments_for_compound_params(func):
"""Returns a list of input parameter for creating the compound parameter."""
compound_params = []
if any(x for x in func.base_parameters if x.is_compound_type):
for parameter in func.base_parameters:
if parameter.direction == "in" and parameter.repeating_argument:
compound_params.append(parameter.parameter_name)
return compound_params
def create_compound_parameter_request(func):
"""Gets the input parameters for createing the compound type parameter."""
parameters = []
compound_parameter_type = ""
for parameter in func.base_parameters:
if parameter.direction == "in" and parameter.repeated_var_args:
compound_parameter_type = parameter.grpc_type.replace("repeated ", "")
break
for parameter in get_input_arguments_for_compound_params(func):
parameters.append(f"{parameter}={parameter}[index]")
return f"grpc_types.{compound_parameter_type}(" + ", ".join(parameters) + ")"
def get_response_parameters(func):
"""Gets the list of parameters in grpc response."""
output_parameters = get_output_params(func)
is_read_method = check_if_parameters_contain_read_array(func.base_parameters)
response_parameters = []
output_parameters = get_output_params(func)
for parameter in output_parameters:
if not parameter.repeating_argument:
name = parameter.parameter_name
if parameter.parameter_name in MODIFIED_INTERPRETER_PARAMS:
name = MODIFIED_INTERPRETER_PARAMS.get(parameter.parameter_name)
if is_read_method and "read_array" in parameter.parameter_name:
response_parameters.append(f"{name}")
elif parameter.is_grpc_enum:
response_parameters.append(f"response.{name}_raw")
elif parameter.is_list:
response_parameters.append(f"list(response.{name})")
else:
response_parameters.append(f"response.{name}")
return ", ".join(response_parameters)
def get_samps_per_chan_read_or_write_param(func_params):
"""Gets samps per read/ samps per write parameter."""
for param in func_params:
if param.parameter_name == "samps_per_chan_read":
return f"samps_per_chan_read={param.parameter_name}"
if param.parameter_name in ("samps_per_chan_written", "num_samps_per_chan_written"):
return f"samps_per_chan_written={param.parameter_name}"
return None
def get_samps_per_chan_read_param(func):
"""Gets samps per read parameter."""
output_parameters = get_output_params(func)
for param in output_parameters:
if param.parameter_name in READ_SAMPLES_PARAMETER_NAMES:
return param.parameter_name
return None
def get_interpreter_parameters(func, is_grpc_interpreter=False):
"""Gets the parameters used in the interpreter functions."""
size_params = _get_size_params(func.base_parameters)
interpreter_parameters = []
for parameter in func.base_parameters:
# Repeated variable argument parameters are not used
# as an interpreter parameter in nidaqmx-python
if (
(
parameter.is_used_in_python_api
and not parameter.is_proto_only
and (not parameter.repeated_var_args or is_grpc_interpreter)
)
or parameter.parameter_name in size_params
or _is_handle_parameter(func, parameter)
or (is_grpc_interpreter and parameter.is_compound_type)
):
interpreter_parameters.append(parameter)
return interpreter_parameters
def _get_size_params(function_parameters):
size_params = []
for param in function_parameters:
if param.has_explicit_buffer_size:
if param.size.mechanism != "custom-code":
size_params.append(param.size.value)
return list(set(size_params))
def _is_handle_parameter(func, param):
if func.handle_parameter is not None:
parameter_name = "task_handle" if param.parameter_name == "task" else param.parameter_name
return parameter_name == camel_to_snake_case(func.handle_parameter.cvi_name)
return False
def check_if_parameters_contain_read_array(params):
"""Checks if the list of parameters contains read array parameter."""
return any(x for x in params if "read_array" in x.parameter_name)
def get_read_array_parameters(func):
"""Gets the list of array parameters."""
response = []
for param in func.base_parameters:
if param.direction == "out" and "read_array" in param.parameter_name:
response.append(camel_to_snake_case(param.parameter_name))
return response
def type_cast_attribute_set_function_parameter(param):
"""Type casting of attribute set parameter during c call."""
if param.ctypes_data_type == "ctypes.c_char_p":
return f"{param.parameter_name}.encode('ascii')"
if is_numpy_array_datatype(param):
return f"{param.parameter_name}.ctypes.data_as(ctypes.c_void_p)"
return f"{param.ctypes_data_type}({param.parameter_name})"
def is_numpy_array_datatype(param):
"""Checks if datatype is a numpy array or not."""
if param.ctypes_data_type and param.ctypes_data_type.startswith("numpy."):
return True
return False
def is_read_bytes_param(param):
"""Returns true if parameter reads bytes."""
if param.is_list and param.ctypes_data_type in ("numpy.bool", "numpy.uint8"):
return True
# This is a special case for 'ReadRaw' function.
# since its metadata is incorrect in daqmxAPISharp.json file.
elif param.parameter_name == "read_array" and param.ctypes_data_type == "numpy.generic":
return True
else:
return False
def is_write_bytes_param(param):
"""Returns true if parameter writes bytes."""
if param.is_list and param.ctypes_data_type in ("numpy.bool", "numpy.uint8"):
return True
# This is a special case for 'WriteRaw' function.
# since its metadata is incorrect in daqmxAPISharp.json file.
elif param.parameter_name == "write_array" and param.ctypes_data_type == "numpy.generic":
return True
else:
return False
def get_numpy_array_params(func):
"""Returns a dictionary of numpy data type parameters."""
numpy_params = {}
for param in func.base_parameters:
if is_numpy_array_datatype(param):
if param.ctypes_data_type == "numpy.bool":
numpy_params[param.parameter_name] = "bool"
else:
numpy_params[param.parameter_name] = param.ctypes_data_type
# This is a special case for these functions.
# since its metadata is incorrect in daqmxAPISharp.json file.
if func.function_name == "read_power_f64" and "read_array" in param.parameter_name:
numpy_params[param.parameter_name] = "numpy.float64"
if func.function_name == "read_power_binary_i16" and "read_array" in param.parameter_name:
numpy_params[param.parameter_name] = "numpy.int16"
return numpy_params
def get_write_array_param(param):
"""Assigns the numpy array to a flattened numpy array."""
if is_numpy_array_datatype(param):
return f"{param.parameter_name}={param.parameter_name}.flat"
return f"{param.parameter_name}={param.parameter_name}"