Skip to content

Commit 19cbf6d

Browse files
committed
ifcfm - move preset searching logic from ifcm to bonsai
So anyone using 'python -m ifcfm' will get more readable error for invalid presets.
1 parent 9821c34 commit 19cbf6d

2 files changed

Lines changed: 25 additions & 13 deletions

File tree

src/bonsai/bonsai/bim/module/fm/data.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,9 @@ def load(cls):
3636
cls.data["engine"] = cls.engine()
3737

3838
@classmethod
39-
def engine(cls):
39+
def engine(cls) -> list[tuple[str, str, str]]:
4040
results = []
41-
fm_dir = os.path.dirname(ifcfm.__file__)
42-
for f in os.listdir(fm_dir):
43-
if f.endswith(".py") and not f.startswith("_"):
44-
preset = os.path.splitext(f)[0]
45-
module = importlib.import_module(f"ifcfm.{preset}")
46-
config = getattr(module, "config")
47-
results.append((preset, config["name"], config["description"]))
41+
presets = ifcfm.get_presets_configs()
42+
for preset, config in presets.items():
43+
results.append((preset, config["name"], config["description"]))
4844
return results

src/ifcfm/ifcfm/__init__.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,21 @@
4949

5050

5151
ParserPreset = 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

5469
class 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

Comments
 (0)