24
24
import typing
25
25
import warnings
26
26
27
+ from ansys .dyna .core .lib .field import Flag
27
28
from ansys .dyna .core .lib .parameters import ParameterSet
28
29
29
30
@@ -83,11 +84,19 @@ def buffer_to_lines(buf: typing.TextIO, max_num_lines: int = -1) -> typing.List[
83
84
return data_lines
84
85
85
86
87
+ def _is_flag (item_type : typing .Union [type , Flag ]):
88
+ if isinstance (item_type , Flag ):
89
+ return True
90
+ return False
91
+
92
+
86
93
def _expand_spec (spec : typing .List [tuple ]) -> typing .List [tuple ]:
87
94
specs = []
88
95
for item in spec :
89
96
position , width , item_type = item
90
- if dataclasses .is_dataclass (item_type ):
97
+ if _is_flag (item_type ):
98
+ specs .append (item )
99
+ elif dataclasses .is_dataclass (item_type ):
91
100
offset = position
92
101
for field in dataclasses .fields (item_type ):
93
102
item_spec = (offset , width , field .type )
@@ -104,7 +113,9 @@ def _contract_data(spec: typing.List[tuple], data: typing.List) -> typing.Iterab
104
113
while True :
105
114
try :
106
115
_ , _ , item_type = next (iterspec )
107
- if dataclasses .is_dataclass (item_type ):
116
+ if _is_flag (item_type ):
117
+ yield next (iterdata )
118
+ elif dataclasses .is_dataclass (item_type ):
108
119
args = [next (iterdata ) for f in dataclasses .fields (item_type )]
109
120
yield item_type (* args )
110
121
else :
@@ -116,6 +127,7 @@ def _contract_data(spec: typing.List[tuple], data: typing.List) -> typing.Iterab
116
127
def load_dataline (spec : typing .List [tuple ], line_data : str , parameter_set : ParameterSet = None ) -> typing .List :
117
128
"""loads a keyword card line with fixed column offsets and width from string
118
129
spec: list of tuples representing the (offset, width, type) of each field
130
+ type can be a Flag which represents the True and False value
119
131
line_data: string with keyword data
120
132
example:
121
133
>>> load_dataline([(0,10, int),(10,10, str)], ' 1 hello')
@@ -144,6 +156,14 @@ def has_value(text_block: str) -> bool:
144
156
def get_none_value (item_type ):
145
157
if item_type is float :
146
158
return float ("nan" )
159
+ if _is_flag (item_type ):
160
+ if len (item_type .false_value ) == 0 :
161
+ return False
162
+ if len (item_type .true_value ) == 0 :
163
+ return True
164
+ raise Exception (
165
+ "No input data for flag. Expected true or false value because neither uses `no input` as a value!"
166
+ )
147
167
return None
148
168
149
169
def has_parameter (text_block : str ) -> bool :
@@ -171,6 +191,21 @@ def get_parameter(text_block: str, item_type: type) -> typing.Any:
171
191
end_position , text_block = seek_text_block (line_data , position , width )
172
192
if not has_value (text_block ):
173
193
value = get_none_value (item_type )
194
+ elif _is_flag (item_type ):
195
+ flag : Flag = item_type
196
+ true_value = flag .true_value
197
+ false_value = flag .false_value
198
+ # check the true value first, empty text may be false but not true
199
+ if true_value in text_block :
200
+ value = True
201
+ else :
202
+ if len (false_value ) == 0 :
203
+ warnings .warn ("value detected in field where false value was an empty string" )
204
+ value = False
205
+ else :
206
+ if false_value in text_block :
207
+ value = False
208
+ raise Exception ("Failed to find true or false value in flag" )
174
209
elif has_parameter (text_block ):
175
210
assert parameter_set != None
176
211
value = get_parameter (text_block , item_type )
@@ -187,6 +222,5 @@ def get_parameter(text_block: str, item_type: type) -> typing.Any:
187
222
if end_position < len (line_data ):
188
223
warning_message = f'Detected out of bound card characters:\n "{ line_data [end_position :]} "\n "Ignoring.'
189
224
warnings .warn (warning_message )
190
-
191
225
data = list (_contract_data (spec , data ))
192
226
return tuple (data )
0 commit comments