Skip to content

Commit 7d116c9

Browse files
Template changes for rep caps
1 parent 88d867e commit 7d116c9

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed

build/helper/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
from build.helper.metadata_filters import filter_library_functions # noqa: F401
5252
from build.helper.metadata_filters import filter_parameters # noqa: F401
5353
from build.helper.metadata_filters import filter_public_functions # noqa: F401
54+
from build.helper.metadata_filters import filter_rep_cap_supported_attributes
5455

5556
from build.helper.metadata_find import find_custom_type # noqa: F401
5657
from build.helper.metadata_find import find_session_handle_parameter # noqa: F401

build/helper/metadata_add_all.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .helper import get_python_type_for_api_type
1313
from .metadata_filters import filter_codegen_attributes
1414
from .metadata_filters import filter_codegen_functions
15+
from .metadata_filters import filter_rep_cap_supported_attributes
1516
from .metadata_find import find_custom_type
1617
from .metadata_find import find_size_parameter
1718
from .metadata_merge_dicts import merge_helper
@@ -721,6 +722,9 @@ def add_all_config_metadata(config):
721722
if 'uses_nitclk' not in config:
722723
config['uses_nitclk'] = False
723724

725+
if 'rep_cap_expansion' not in config:
726+
config['rep_cap_expansion'] = 'session'
727+
724728
return config
725729

726730

build/helper/metadata_filters.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,4 +450,14 @@ def filter_codegen_enums(enums):
450450
'''Returns enum metadata only for those enums to be included in codegen'''
451451
return {k: v for k, v in enums.items() if v['codegen_method'] != 'no'}
452452

453+
def filter_rep_cap_supported_attributes(attributes, rep_cap_name):
454+
'''Returns attribute metadata only for those attributes that support the specified repeated capability.
453455
456+
Args:
457+
attributes: Dictionary of attribute metadata.
458+
rep_cap_name: The name of the repeated capability to filter by.
459+
460+
Returns:
461+
Dictionary of attributes that support the specified repeated capability.
462+
'''
463+
return {k: v for k, v in attributes.items() if rep_cap_name in v.get('supported_rep_caps', [])}

build/templates/session.py.mako

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,85 @@ class _Lock(object):
9191

9292
% endif
9393
% if len(config['repeated_capabilities']) > 0:
94+
95+
% if config['rep_cap_expansion'] == 'custom':
96+
# Dynamically handle repeated capabilities
97+
% for rep_cap in config['repeated_capabilities']:
98+
99+
class _RepCap${rep_cap['python_name'].capitalize()}(object):
100+
% for attribute in helper.sorted_attrs(helper.filter_rep_cap_supported_attributes(attributes, rep_cap['python_name'])):
101+
<%
102+
helper.add_attribute_rep_cap_tip(attributes[attribute], config)
103+
%>\
104+
% if attributes[attribute]['enum']:
105+
% if helper.enum_uses_converter(enums[attributes[attribute]['enum']]):
106+
${attributes[attribute]['python_name']} = _attributes.AttributeEnumWithConverter(_attributes.AttributeEnum(_attributes.Attribute${attributes[attribute]['type']}, enums.${enums[attributes[attribute]['enum']]['python_name']}, ${attribute}), _converters.${enums[attributes[attribute]['enum']]['enum_to_converted_value_function_name']}, _converters.${enums[attributes[attribute]['enum']]['converted_value_to_enum_function_name']})
107+
% else:
108+
${attributes[attribute]['python_name']} = _attributes.AttributeEnum(_attributes.Attribute${attributes[attribute]['type']}, enums.${enums[attributes[attribute]['enum']]['python_name']}, ${attribute})
109+
% endif
110+
% else:
111+
${attributes[attribute]['python_name']} = _attributes.${attributes[attribute]['attribute_class']}(${attribute})
112+
% endif
113+
% if 'documentation' in attributes[attribute] and len(helper.get_documentation_for_node_docstring(attributes[attribute], config, indent=4).strip()) > 0:
114+
'''Type: ${attributes[attribute]['type_in_documentation']}
115+
116+
${helper.get_documentation_for_node_docstring(attributes[attribute], config, indent=4)}
117+
'''
118+
% endif
119+
% endfor
120+
def __init__(self, session, repeated_capability_list):
121+
object.__setattr__(self, '_session', session)
122+
object.__setattr__(self, '_repeated_capability_list', repeated_capability_list)
123+
object.__setattr__(self, '_prefix', '${rep_cap["prefix"]}')
124+
object.__setattr__(self, '_current_repeated_capability_list', repeated_capability_list if len(repeated_capability_list) > 0 else [''])
125+
object.__setattr__(self, '_separator', '')
126+
127+
def __setattr__(self, key, value):
128+
if key not in dir(self):
129+
raise AttributeError("'{0}' object has no attribute '{1}'".format(type(self).__name__, key))
130+
object.__setattr__(self, key, value)
131+
132+
def __getitem__(self, repeated_capability):
133+
'''Set/get properties or call methods with a repeated capability (i.e. channels)'''
134+
rep_caps_list = _converters.convert_repeated_capabilities(repeated_capability, self._prefix)
135+
complete_rep_cap_list = [
136+
current_rep_cap + self._separator + rep_cap
137+
for current_rep_cap in self._current_repeated_capability_list
138+
for rep_cap in rep_caps_list
139+
]
140+
object.__setattr__(self, '_current_repeated_capability_list', complete_rep_cap_list)
141+
self._current_repeated_capability_list = complete_rep_cap_list
142+
143+
return self
144+
145+
def _get_attribute_vi_real64(self, attribute):
146+
repeated_capability = ','.join(self._current_repeated_capability_list)
147+
value = self._session._interpreter.get_attribute_vi_real64(repeated_capability, attribute)
148+
return value
149+
150+
def _set_attribute_vi_real64(self, attribute, value):
151+
repeated_capability = ','.join(self._current_repeated_capability_list)
152+
self._session._interpreter.set_attribute_vi_real64(repeated_capability, attribute, value)
153+
154+
def _get_attribute_vi_int32(self, attribute):
155+
repeated_capability = ','.join(self._current_repeated_capability_list)
156+
value = self._session._interpreter.get_attribute_vi_int32(repeated_capability, attribute)
157+
return value
158+
159+
def _set_attribute_vi_int32(self, attribute, value):
160+
repeated_capability = ','.join(self._current_repeated_capability_list)
161+
self._session._interpreter.set_attribute_vi_int32(repeated_capability, attribute, value)
162+
163+
def _get_attribute_vi_string(self, attribute):
164+
repeated_capability = ','.join(self._current_repeated_capability_list)
165+
value = self._session._interpreter.get_attribute_vi_string(repeated_capability, attribute)
166+
return value
167+
168+
def _set_attribute_vi_string(self, attribute, value):
169+
repeated_capability = ','.join(self._current_repeated_capability_list)
170+
self._session._interpreter.set_attribute_vi_string(repeated_capability, attribute, value)
171+
% endfor
172+
% else:
94173
class _RepeatedCapabilities(object):
95174
def __init__(self, session, prefix, current_repeated_capability_list):
96175
self._session = session

src/nirfsg/metadata/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
}
4040
},
4141
'module_name': 'nirfsg',
42+
'rep_cap_expansion': 'custom',
4243
'repeated_capabilities': [
4344
{
4445
'prefix': 'marker',

0 commit comments

Comments
 (0)