@@ -60,6 +60,53 @@ def _REGEX(element):
60
60
return o
61
61
62
62
63
+ def ATTRS_DATACLASS (element , t ):
64
+ a = []
65
+ b = []
66
+
67
+ for attribute in element .attributes :
68
+ if attribute .optional :
69
+ if attribute .type .startswith ('types.Tuple' ):
70
+ if 'types.Integer' in attribute .type :
71
+ b += [
72
+ f'{ attribute .name } : list[str] | list[int] | list[{ attribute .type [12 :- 1 ]} ] = None'
73
+ ]
74
+ elif 'types.Real' in attribute .type :
75
+ b += [
76
+ f'{ attribute .name } : list[str] | list[float] | list[{ attribute .type [12 :- 1 ]} ] = None'
77
+ ]
78
+ else :
79
+ b += [f'{ attribute .name } : list[str] | list[{ attribute .type [12 :- 1 ]} ] = None' ]
80
+ else :
81
+ if 'types.Integer' in attribute .type :
82
+ b += [f'{ attribute .name } : str | int | { attribute .type } = None' ]
83
+ elif 'types.Real' in attribute .type :
84
+ b += [f'{ attribute .name } : str | float | { attribute .type } = None' ]
85
+ else :
86
+ b += [f'{ attribute .name } : str | { attribute .type } = None' ]
87
+ else :
88
+ if attribute .type .startswith ('types.Tuple' ):
89
+ if 'types.Integer' in attribute .type :
90
+ a += [
91
+ f'{ attribute .name } : list[str] | list[int] | list[{ attribute .type [12 :- 1 ]} ]'
92
+ ]
93
+ elif 'types.Real' in attribute .type :
94
+ a += [
95
+ f'{ attribute .name } : list[str] | list[float] | list[{ attribute .type [12 :- 1 ]} ]'
96
+ ]
97
+ else :
98
+ a += [f'{ attribute .name } : list[str] | list[{ attribute .type [12 :- 1 ]} ]' ]
99
+ else :
100
+ if 'types.Integer' in attribute .type :
101
+ a += [f'{ attribute .name } : str | int | { attribute .type } ' ]
102
+ elif 'types.Real' in attribute .type :
103
+ a += [f'{ attribute .name } : str | float | { attribute .type } ' ]
104
+ else :
105
+ a += [f'{ attribute .name } : str | { attribute .type } ' ]
106
+
107
+ return f'\n { TABS (t )} ' .join (a + b ).strip ()
108
+
109
+
63
110
def ATTRS_DICT (element ):
64
111
a = ''
65
112
b = ''
@@ -129,17 +176,88 @@ def ATTRS_COMMENT(element, t):
129
176
return o .strip ()
130
177
131
178
179
+ def ATTRS_BUILDER (element , t ):
180
+ o = ''
181
+
182
+ for attribute in element .attributes :
183
+ if attribute .type .startswith ('types.Tuple' ):
184
+ o += f'{ TABS (t )} { attribute .name } = []\n '
185
+ o += f'{ TABS (t )} for item in self.{ attribute .name } :\n '
186
+
187
+ if 'types.Integer' in attribute .type :
188
+ o += f'{ TABS (t )} if isinstance(item, { attribute .type [12 :- 1 ]} ):\n '
189
+ o += f'{ TABS (t )} { attribute .name } .append(item)\n '
190
+ o += f'{ TABS (t )} elif isinstance(item, int):\n '
191
+ o += f'{ TABS (t )} { attribute .name } .append({ attribute .type [12 :- 1 ]} (item))\n '
192
+ o += f'{ TABS (t )} elif isinstance(item, str):\n '
193
+ o += f'{ TABS (t )} { attribute .name } .append({ attribute .type [12 :- 1 ]} .from_mcnp(item))\n '
194
+ elif 'types.Real' in attribute .type :
195
+ o += f'{ TABS (t )} if isinstance(item, { attribute .type [12 :- 1 ]} ):\n '
196
+ o += f'{ TABS (t )} { attribute .name } .append(item)\n '
197
+ o += f'{ TABS (t )} elif isinstance(item, float) or isinstance(item, int):\n '
198
+ o += f'{ TABS (t )} { attribute .name } .append({ attribute .type [12 :- 1 ]} (item))\n '
199
+ o += f'{ TABS (t )} elif isinstance(item, str):\n '
200
+ o += f'{ TABS (t )} { attribute .name } .append({ attribute .type [12 :- 1 ]} .from_mcnp(item))\n '
201
+ else :
202
+ o += f'{ TABS (t )} if isinstance(item, { attribute .type [12 :- 1 ]} ):\n '
203
+ o += f'{ TABS (t )} { attribute .name } .append(item)\n '
204
+ o += f'{ TABS (t )} elif isinstance(item, str):\n '
205
+ o += f'{ TABS (t )} { attribute .name } .append({ attribute .type [12 :- 1 ]} .from_mcnp(item))\n '
206
+ o += f'{ TABS (t )} else:\n '
207
+ o += f'{ TABS (t )} { attribute .name } .append(item.build())\n '
208
+
209
+ o += f'{ TABS (t )} { attribute .name } = types.Tuple({ attribute .name } )\n '
210
+ else :
211
+ if attribute .optional :
212
+ o += f'{ TABS (t )} { attribute .name } = None\n '
213
+
214
+ if 'types.Integer' in attribute .type :
215
+ o += f'{ TABS (t )} if isinstance(self.{ attribute .name } , types.Integer):\n '
216
+ o += f'{ TABS (t )} { attribute .name } = self.{ attribute .name } \n '
217
+ o += f'{ TABS (t )} elif isinstance(self.{ attribute .name } , int):\n '
218
+ o += f'{ TABS (t )} { attribute .name } = { attribute .type } (self.{ attribute .name } )\n '
219
+ o += f'{ TABS (t )} elif isinstance(self.{ attribute .name } , str):\n '
220
+ o += f'{ TABS (t )} { attribute .name } = { attribute .type } .from_mcnp(self.{ attribute .name } )\n '
221
+ elif 'types.Real' in attribute .type :
222
+ o += f'{ TABS (t )} if isinstance(self.{ attribute .name } , types.Real):\n '
223
+ o += f'{ TABS (t )} { attribute .name } = self.{ attribute .name } \n '
224
+ o += f'{ TABS (t )} elif isinstance(self.{ attribute .name } , float) or isinstance(self.{ attribute .name } , int):\n '
225
+ o += f'{ TABS (t )} { attribute .name } = { attribute .type } (self.{ attribute .name } )\n '
226
+ o += f'{ TABS (t )} elif isinstance(self.{ attribute .name } , str):\n '
227
+ o += f'{ TABS (t )} { attribute .name } = { attribute .type } .from_mcnp(self.{ attribute .name } )\n '
228
+ else :
229
+ o += f'{ TABS (t )} if isinstance(self.{ attribute .name } , { attribute .type } ):\n '
230
+ o += f'{ TABS (t )} { attribute .name } = self.{ attribute .name } \n '
231
+ o += f'{ TABS (t )} elif isinstance(self.{ attribute .name } , str):\n '
232
+ o += f'{ TABS (t )} { attribute .name } = { attribute .type } .from_mcnp(self.{ attribute .name } )\n '
233
+
234
+ o += '\n '
235
+
236
+ return o .strip ()
237
+
238
+
239
+ def ATTRS_ASSIGN (element , t ):
240
+ o = ''
241
+
242
+ for attribute in element .attributes :
243
+ o += f'{ TABS (t )} { attribute .name } ={ attribute .name } ,\n '
244
+
245
+ return o .strip ()
246
+
247
+
132
248
# ELEMENT #
133
249
def INIT (element ):
134
250
return f"""
135
251
from .option_ import { CAMEL (element .name )} Option_
136
252
{ '' .join (f"from . import { SNAKE (option .name )} \n " if option .options else "" for option in element .options )[:- 1 ]}
137
253
{ '' .join (f'from .{ CAMEL (option .name )} import { CAMEL (option .name )} \n ' for option in element .options )[:- 1 ]}
254
+ { '' .join (f'from .{ CAMEL (option .name )} import { CAMEL (option .name ).split ('_' )[0 ]} Builder{ f"_{ CAMEL (option .name ).split ('_' )[1 ]} " if len (CAMEL (option .name ).split ('_' )) - 1 else "" } \n ' for option in element .options )[:- 1 ]}
138
255
139
256
__all__ = [
140
257
"{ CAMEL (element .name )} Option_",
141
258
{ '' .join (f'\t "{ SNAKE (option .name )} ",\n ' if option .options else "" for option in element .options ).strip ()}
142
259
{ '' .join (f'\t "{ CAMEL (option .name )} ",\n ' for option in element .options ).strip ()}
260
+ { '' .join (f'\t "{ CAMEL (option .name ).split ('_' )[0 ]} Builder{ f"_{ CAMEL (option .name ).split ('_' )[1 ]} " if len (CAMEL (option .name ).split ('_' )) - 1 else "" } ",\n ' for option in element .options )[:- 1 ].strip ()}
143
261
]
144
262
""" [1 :- 1 ]
145
263
@@ -185,6 +303,7 @@ def ELEMENT(element, parent_name, depth):
185
303
return f'''
186
304
import re
187
305
import typing
306
+ import dataclasses
188
307
189
308
import molmass
190
309
@@ -199,10 +318,10 @@ def ELEMENT(element, parent_name, depth):
199
318
200
319
class { CAMEL (element .name )} ({ f"{ CAMEL (parent_name )} Option_, keyword='{ element .mnemonic } '" if parent_name else "Card_" } ):
201
320
"""
202
- Represents INP { element .name } elements.
321
+ Represents INP { element .name . split ( '_' )[ 0 ] } { f" variation # { element . name . split ( '_' )[ 1 ] } " if len ( element . name . split ( '_' )) - 1 else "" } elements.
203
322
204
323
Attributes:
205
- InpError: { element . error } .
324
+ { ATTRS_COMMENT ( element , 2 ) }
206
325
"""
207
326
208
327
_ATTRS = {{{ ATTRS_DICT (element )} }}
@@ -225,7 +344,31 @@ def __init__(self, {ATTRS_PARAM(element)}):
225
344
self.value: typing.Final[types.Tuple] = types.Tuple([{ ATTRS_LIST (element )} ])\n
226
345
{ ATTRS_STORE (element , 2 )}
227
346
228
- { element .extra }
347
+ { element .extra .strip ()}
348
+ @dataclasses.dataclass
349
+ class { CAMEL (element .name ).split ('_' )[0 ]} Builder{ f"_{ CAMEL (element .name ).split ('_' )[1 ]} " if len (CAMEL (element .name ).split ('_' )) - 1 else "" } :
350
+ """
351
+ Builds ``{ CAMEL (element .name )} ``.
352
+
353
+ Attributes:
354
+ { ATTRS_COMMENT (element , 2 )}
355
+ """
356
+
357
+ { ATTRS_DATACLASS (element , 1 )}
358
+
359
+ def build(self):
360
+ """
361
+ Builds ``{ CAMEL (element .name ).split ('_' )[0 ]} Builder{ f"_{ CAMEL (element .name ).split ('_' )[1 ]} " if len (CAMEL (element .name ).split ('_' )) - 1 else "" } `` into ``{ CAMEL (element .name )} ``.
362
+
363
+ Returns:
364
+ ``{ CAMEL (element .name )} `` for ``{ CAMEL (element .name ).split ('_' )[0 ]} Builder{ f"_{ CAMEL (element .name ).split ('_' )[1 ]} " if len (CAMEL (element .name ).split ('_' )) - 1 else "" } ``.
365
+ """
366
+
367
+ { ATTRS_BUILDER (element , 2 )}
368
+
369
+ return { CAMEL (element .name )} (
370
+ { ATTRS_ASSIGN (element , 3 )}
371
+ )
229
372
''' [1 :]
230
373
231
374
@@ -246,11 +389,13 @@ def build_element(element, parent_name, path_dir, depth):
246
389
247
390
build_element (option , element .name , path_subdir , depth + 1 )
248
391
249
- if element . name not in { 'cell' , 'surface' , 'comment' } and parent_name != '' :
392
+ if parent_name != 'alsdkjfhalsdkjf ' :
250
393
path_file = path_dir / f'{ CAMEL (element .name )} .py'
251
394
with path_file .open ('w' ) as file :
252
395
file .write (ELEMENT (element , parent_name , depth - 1 ))
253
396
254
397
255
398
for card in inp_data .cards .options :
256
- build_element (card , '' , pathlib .Path (__file__ ).parent .parent / 'src/pymcnp/inp' , 3 )
399
+ build_element (
400
+ card , 'alsdkjfhalsdkjf' , pathlib .Path (__file__ ).parent .parent / 'src/pymcnp/inp' , 3
401
+ )
0 commit comments