4949
5050
5151ParserPreset = Literal ["basic" , "cobie24" , "cobie24legacy" ]
52+ _parser_presets_configs = {}
53+
54+
55+ def get_presets_configs () -> dict [ParserPreset , dict [str , Any ]]:
56+ global _parser_presets_configs
57+ if not _parser_presets_configs :
58+ fm_dir = Path (__file__ ).parent
59+ for f in fm_dir .iterdir ():
60+ if not f .suffix == ".py" or f .name .startswith ("_" ):
61+ continue
62+ preset = f .stem
63+ module = importlib .import_module (f"ifcfm.{ preset } " )
64+ config = getattr (module , "config" )
65+ _parser_presets_configs [preset ] = config
66+ return _parser_presets_configs
5267
5368
5469class Parser :
@@ -59,16 +74,18 @@ class Parser:
5974 ]
6075 duplicate_keys : list [tuple [dict [str , Any ], dict [str , Any ]]]
6176
62- def __init__ (self , preset : Union [ParserPreset , dict [str , Any ]] = "basic" ):
77+ def __init__ (self , preset : Union [str , ParserPreset , dict [str , Any ]] = "basic" ):
6378 self .file = None
6479 self .preset = preset
6580 self .categories = defaultdict (dict )
6681 self .get_custom_element_data = {}
6782 self .duplicate_keys = []
6883
6984 if isinstance (preset , str ):
70- module = importlib .import_module (f"ifcfm.{ preset } " )
71- self .config = getattr (module , "config" )
85+ presets = get_presets_configs ()
86+ if preset not in presets :
87+ raise Exception (f"Invalid preset '{ preset } '. Available presets: { ',' .join (presets .keys ())} ." )
88+ self .config = get_presets_configs ()[preset ]
7289 else :
7390 self .config = preset
7491
@@ -134,8 +151,7 @@ class Writer:
134151 def __init__ (self , parser : Parser ):
135152 self .parser = parser
136153 if isinstance (self .parser .preset , str ):
137- module = importlib .import_module (f"ifcfm.{ self .parser .preset } " )
138- self .config = getattr (module , "config" )
154+ self .config = get_presets_configs ()[self .parser .preset ]
139155 elif isinstance (self .parser .preset , dict ):
140156 self .config = self .parser .preset ["config" ]
141157 else :
0 commit comments