@@ -88,13 +88,9 @@ def ATTRS_DATACLASS(element, t):
8888 if attribute .optional :
8989 if attribute .type .startswith ('types.Tuple' ):
9090 if 'types.Integer' in attribute .type :
91- b += [
92- f'{ attribute .name } : list[str] | list[int] | list[{ attribute .type [12 :- 1 ]} ] = None'
93- ]
91+ b += [f'{ attribute .name } : list[str] | list[int] | list[{ attribute .type [12 :- 1 ]} ] = None' ]
9492 elif 'types.Real' in attribute .type :
95- b += [
96- f'{ attribute .name } : list[str] | list[float] | list[{ attribute .type [12 :- 1 ]} ] = None'
97- ]
93+ b += [f'{ attribute .name } : list[str] | list[float] | list[{ attribute .type [12 :- 1 ]} ] = None' ]
9894 else :
9995 b += [f'{ attribute .name } : list[str] | list[{ attribute .type [12 :- 1 ]} ] = None' ]
10096 else :
@@ -107,13 +103,9 @@ def ATTRS_DATACLASS(element, t):
107103 else :
108104 if attribute .type .startswith ('types.Tuple' ):
109105 if 'types.Integer' in attribute .type :
110- a += [
111- f'{ attribute .name } : list[str] | list[int] | list[{ attribute .type [12 :- 1 ]} ]'
112- ]
106+ a += [f'{ attribute .name } : list[str] | list[int] | list[{ attribute .type [12 :- 1 ]} ]' ]
113107 elif 'types.Real' in attribute .type :
114- a += [
115- f'{ attribute .name } : list[str] | list[float] | list[{ attribute .type [12 :- 1 ]} ]'
116- ]
108+ a += [f'{ attribute .name } : list[str] | list[float] | list[{ attribute .type [12 :- 1 ]} ]' ]
117109 else :
118110 a += [f'{ attribute .name } : list[str] | list[{ attribute .type [12 :- 1 ]} ]' ]
119111 else :
@@ -141,7 +133,7 @@ def ATTRS_LIST(element):
141133 o = ''
142134
143135 for attribute in element .attributes :
144- if attribute .name not in {'suffix' , 'designator' }:
136+ if attribute .name not in {'suffix' , 'designator' , 'prefix' }:
145137 o += f'{ attribute .name } , '
146138
147139 return o
@@ -209,27 +201,28 @@ def ATTRS_BUILDER(element, t):
209201 o += f'{ TABS (t )} if isinstance(item, { attribute .type [12 :- 1 ]} ):\n '
210202 o += f'{ TABS (t )} { attribute .name } .append(item)\n '
211203 o += f'{ TABS (t )} elif isinstance(item, int):\n '
212- o += (
213- f'{ TABS (t )} { attribute .name } .append({ attribute .type [12 :- 1 ]} (item))\n '
214- )
204+ o += f'{ TABS (t )} { attribute .name } .append({ attribute .type [12 :- 1 ]} (item))\n '
215205 o += f'{ TABS (t )} elif isinstance(item, str):\n '
216206 o += f'{ TABS (t )} { attribute .name } .append({ attribute .type [12 :- 1 ]} .from_mcnp(item))\n '
217207 elif 'types.Real' in attribute .type :
218208 o += f'{ TABS (t )} if isinstance(item, { attribute .type [12 :- 1 ]} ):\n '
219209 o += f'{ TABS (t )} { attribute .name } .append(item)\n '
220210 o += f'{ TABS (t )} elif isinstance(item, float) or isinstance(item, int):\n '
221- o += (
222- f'{ TABS (t )} { attribute .name } .append({ attribute .type [12 :- 1 ]} (item))\n '
223- )
211+ o += f'{ TABS (t )} { attribute .name } .append({ attribute .type [12 :- 1 ]} (item))\n '
224212 o += f'{ TABS (t )} elif isinstance(item, str):\n '
225213 o += f'{ TABS (t )} { attribute .name } .append({ attribute .type [12 :- 1 ]} .from_mcnp(item))\n '
226- else :
214+ elif 'Option' in attribute . type :
227215 o += f'{ TABS (t )} if isinstance(item, { attribute .type [12 :- 1 ]} ):\n '
228216 o += f'{ TABS (t )} { attribute .name } .append(item)\n '
229217 o += f'{ TABS (t )} elif isinstance(item, str):\n '
230218 o += f'{ TABS (t )} { attribute .name } .append({ attribute .type [12 :- 1 ]} .from_mcnp(item))\n '
231- o += f'{ TABS (t )} else :\n '
219+ o += f'{ TABS (t )} elif isinstance(item, { element . name } { CAMEL ( attribute . type [ 12 + len ( element . name ): - 1 ], "Builder" ) } ) :\n '
232220 o += f'{ TABS (t )} { attribute .name } .append(item.build())\n '
221+ else :
222+ o += f'{ TABS (t )} if isinstance(item, { attribute .type [12 :- 1 ]} ):\n '
223+ o += f'{ TABS (t )} { attribute .name } .append(item)\n '
224+ o += f'{ TABS (t )} elif isinstance(item, str):\n '
225+ o += f'{ TABS (t )} { attribute .name } .append({ attribute .type [12 :- 1 ]} .from_mcnp(item))\n '
233226 o += f'{ TABS (t )} { attribute .name } = types.Tuple({ attribute .name } )\n '
234227 o += f'{ TABS (t )} else:\n '
235228 o += f'{ TABS (t )} { attribute .name } = None\n '
@@ -249,6 +242,13 @@ def ATTRS_BUILDER(element, t):
249242 o += f'{ TABS (t )} { attribute .name } = { attribute .type } (self.{ attribute .name } )\n '
250243 o += f'{ TABS (t )} elif isinstance(self.{ attribute .name } , str):\n '
251244 o += f'{ TABS (t )} { attribute .name } = { attribute .type } .from_mcnp(self.{ attribute .name } )\n '
245+ elif 'Option' in attribute .type :
246+ o += f'{ TABS (t )} if isinstance(self.{ attribute .name } , { attribute .type } ):\n '
247+ o += f'{ TABS (t )} { attribute .name } = self.{ attribute .name } \n '
248+ o += f'{ TABS (t )} elif isinstance(self.{ attribute .name } , str):\n '
249+ o += f'{ TABS (t )} { attribute .name } = { attribute .type } .from_mcnp(self.{ attribute .name } )\n '
250+ o += f'{ TABS (t )} elif isinstance(self.{ attribute .name } , { element .name } { CAMEL (attribute .type [len (element .name ):], "Builder" )} ):\n '
251+ o += f'{ TABS (t )} { attribute .name } = self.{ attribute .name } .build()\n '
252252 else :
253253 o += f'{ TABS (t )} if isinstance(self.{ attribute .name } , { attribute .type } ):\n '
254254 o += f'{ TABS (t )} { attribute .name } = self.{ attribute .name } \n '
@@ -273,10 +273,7 @@ def ATTRS_UNBUILDER(element, t):
273273 o = ''
274274
275275 for attribute in element .attributes :
276- if attribute .type .startswith ('types.' ):
277- o += f'{ TABS (t )} { attribute .name } =copy.deepcopy(ast.{ attribute .name } ),\n '
278- else :
279- o += f'{ TABS (t )} { attribute .name } ={ CAMEL (attribute .type , "Builder" )} .unbuild(ast.{ attribute .name } ),\n '
276+ o += f'{ TABS (t )} { attribute .name } =copy.deepcopy(ast.{ attribute .name } ),\n '
280277
281278 return o .strip ()
282279
@@ -285,12 +282,14 @@ def ATTRS_UNBUILDER(element, t):
285282def INIT (element ):
286283 return f"""
287284from ._option import { CAMEL (element .name , "Option" )}
285+ from ._option import { CAMEL (element .name , "OptionBuilder" )}
288286{ '' .join (f"from . import { SNAKE (option .name )} \n " if option .options else "" for option in element .options )[:- 1 ]}
289287{ '' .join (f'from .{ CAMEL (option .name )} import { CAMEL (option .name )} \n ' for option in element .options )[:- 1 ]}
290288{ '' .join (f'from .{ CAMEL (option .name )} import { CAMEL (option .name , "Builder" )} \n ' for option in element .options )[:- 1 ]}
291289
292290__all__ = [
293291 "{ CAMEL (element .name , "Option" )} ",
292+ "{ CAMEL (element .name , "OptionBuilder" )} ",
294293 { '' .join (f'\t "{ SNAKE (option .name )} ",\n ' if option .options else "" for option in element .options ).strip ()}
295294 { '' .join (f'\t "{ CAMEL (option .name )} ",\n ' for option in element .options ).strip ()}
296295 { '' .join (f'\t "{ CAMEL (option .name , "Builder" )} ",\n ' for option in element .options ).strip ()}
@@ -304,18 +303,24 @@ def OPTION(element, depth):
304303import typing
305304
306305{ '' .join (f"from . import { SNAKE (option .name )} \n " if option .options else "" for option in element .options )}
307- from { "." * (depth - 1 )} _option import Option
306+ from { "." * (depth - 1 )} import _option
308307from { "." * depth } utils import types
309308from { "." * depth } utils import errors
310309from { "." * depth } utils import _parser
311- from { "." * depth } utils import _object
312310
313311
314- class { CAMEL (element .name , "Option" )} (Option):
312+ class { CAMEL (element .name , "Option" )} (_option. Option):
315313 """
316314 Represents generic INP { element .name } options.
317315 """
318316
317+ pass
318+
319+ class { CAMEL (element .name , "OptionBuilder" )} (_option.OptionBuilder):
320+ """
321+ Represents generic INP { element .name } option builders.
322+ """
323+
319324 pass
320325''' [1 :]
321326
@@ -330,15 +335,15 @@ def ELEMENT(element, parent_name, depth):
330335import molmass
331336
332337{ f"from . import { SNAKE (element .name )} " if element .options else "" }
333- { f "from ._option import { CAMEL ( parent_name , 'Option' ) } " if parent_name else "from ._card import Card " }
338+ { "from . import _option " if parent_name else "from . import _card " }
334339from { "." * depth } utils import types
335340from { "." * depth } utils import errors
336341from { "." * depth } utils import _parser
337342from { "." * depth } utils import _elements
338343from { "." * depth } utils import _visualization
339344
340345
341- class { CAMEL (element .name )} ({ CAMEL (parent_name , 'Option' )} ):
346+ class { CAMEL (element .name )} (_option. { CAMEL (parent_name , 'Option' )} ):
342347 """
343348 Represents INP { element .name .split ('_' )[0 ]} { f" variation #{ element .name .split ('_' )[1 ]} " if len (element .name .split ('_' )) - 1 else "" } elements.
344349
@@ -370,7 +375,7 @@ def __init__(self, {ATTRS_PARAM(element)}):
370375
371376 { element .extra .strip ()}
372377@dataclasses.dataclass
373- class { CAMEL (element .name , "Builder" )} :
378+ class { CAMEL (element .name , "Builder" )} (_option. { CAMEL ( parent_name , 'OptionBuilder' ) } ) :
374379 """
375380 Builds ``{ CAMEL (element .name )} ``.
376381
@@ -403,7 +408,7 @@ def unbuild(ast: {CAMEL(element.name)}):
403408 ``{ CAMEL (element .name , "Builder" )} `` for ``{ CAMEL (element .name )} ``.
404409 """
405410
406- return { CAMEL (element .name )} (
411+ return { CAMEL (element .name , "Builder" )} (
407412 { ATTRS_UNBUILDER (element , 3 )}
408413 )
409414''' [1 :]
@@ -433,6 +438,4 @@ def build_element(element, parent_name, path_dir, depth):
433438
434439
435440for card in inp_data .cards .options :
436- build_element (
437- card , 'alsdkjfhalsdkjf' , pathlib .Path (__file__ ).parent .parent / 'src/pymcnp/inp' , 3
438- )
441+ build_element (card , 'alsdkjfhalsdkjf' , pathlib .Path (__file__ ).parent .parent / 'src/pymcnp/inp' , 3 )
0 commit comments