@@ -278,6 +278,11 @@ def __getitem__(self, k):
278278 def __repr__ (self ):
279279 return f'#{ self .id } ={ self .type } ({ "," .join (map (str , self .attributes ))} )'
280280
281+ @dataclass
282+ class ParseResult :
283+ header : dict
284+ entities : dict [int , list [entity_instance ]]
285+
281286
282287def create_step_entity (entity_tree ):
283288 t = T (visit_tokens = True ).transform (entity_tree )
@@ -352,7 +357,7 @@ def parse(
352357 with_progress = False ,
353358 with_tree = True ,
354359 only_header = False ,
355- ):
360+ ) -> ParseResult :
356361 if filename :
357362 assert not filecontent
358363 filecontent = builtins .open (filename , encoding = None ).read ()
@@ -388,7 +393,10 @@ def replace_fn(match):
388393
389394 header = dict (map (make_header_ent , header_tree .children [0 ].children ))
390395 validate_header_fields (header )
391- return header
396+ return ParseResult (
397+ header = header ,
398+ entities = defaultdict (list )
399+ )
392400
393401
394402 instance_identifiers = []
@@ -431,7 +439,11 @@ def replace_fn(match):
431439 raise SyntaxError (filecontent , e )
432440
433441 if with_tree :
434- return process_tree (filecontent , ast , with_progress )
442+ header , data = process_tree (filecontent , ast , with_progress )
443+ return ParseResult (
444+ header = header ,
445+ entities = data
446+ )
435447 else :
436448 # process_tree() would take care of duplicate identifiers,
437449 # but we need to do it ourselves now using our rudimentary
@@ -448,8 +460,9 @@ class file:
448460 A somewhat compatible interface (but very limited) to ifcopenshell.file
449461 """
450462
451- def __init__ (self , parse_outcomes ):
452- self .header_ , self .data_ = parse_outcomes
463+ def __init__ (self , result :ParseResult ):
464+ self .header_ = result .header
465+ self .data_ = result .entities
453466
454467 @property
455468 def schema_identifier (self ) -> str :
@@ -536,17 +549,4 @@ def by_type(self, type: str) -> list[entity_instance]:
536549
537550
538551def open (fn , only_header : bool = False ) -> file :
539- if only_header : # Ensure consistent options
540- parse_outcomes = parse (
541- filename = fn ,
542- with_tree = True ,
543- only_header = True ,
544- )
545- return file ((parse_outcomes , defaultdict (list ))) # data section is empty
546- else :
547- parse_outcomes = parse (
548- filename = fn ,
549- with_tree = True ,
550- only_header = False ,
551- )
552- return file (parse_outcomes )
552+ return file (parse (filename = fn , only_header = only_header ))
0 commit comments