Skip to content

Commit e142df7

Browse files
authored
feat: model mapping (#192)
expose the model mapping so consumers can ask which models are available without using dir()
1 parent 1d787a4 commit e142df7

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

autotest/test_models.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,22 @@ def temp_cache_dir(tmpdir, monkeypatch):
2929
return temp_dir
3030

3131

32-
def test_registry_loaded():
32+
def test_registry():
3333
assert models.FETCHER.registry is not None, "Registry was not loaded"
3434
assert len(models.FETCHER.registry) > 0, "Registry is empty"
3535

3636

37+
def test_model_map(models_toml):
38+
assert models.model_map()
39+
for model_name, files in models_toml.items():
40+
assert model_name in models.model_map().keys(), (
41+
f"Model {model_name} not found in model map"
42+
)
43+
assert files == models.model_map()[model_name], (
44+
f"Files for model {model_name} do not match"
45+
)
46+
47+
3748
def test_generated_functions_exist(models_toml):
3849
for model_name in models_toml.keys():
3950
assert hasattr(models, model_name), (

modflow_devtools/models.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
def _generate_function(model_name, files) -> callable:
3939
def model_function():
40-
return [FETCHER.fetch(file) for file in files]
40+
return [Path(FETCHER.fetch(file)) for file in files]
4141

4242
model_function.__name__ = model_name
4343
return model_function
@@ -49,10 +49,15 @@ def _attach_functions(models):
4949
else:
5050
with Path(models).open("rb") as f:
5151
models = tomli.load(f)
52+
globals()["_models"] = models
5253
for name, files in models.items():
5354
globals()[name] = _generate_function(name, files)
5455

5556

57+
def model_map() -> dict[str, list[Path]]:
58+
return globals().get("_models", {})
59+
60+
5661
try:
5762
with pkg_resources.open_binary(DATA_ANCHOR, MODELMAP_NAME) as f:
5863
_attach_functions(f)

0 commit comments

Comments
 (0)