Skip to content

Commit

Permalink
feat: model mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
wpbonelli committed Mar 7, 2025
1 parent 1d787a4 commit 4657ece
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
13 changes: 12 additions & 1 deletion autotest/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,22 @@ def temp_cache_dir(tmpdir, monkeypatch):
return temp_dir


def test_registry_loaded():
def test_registry():
assert models.FETCHER.registry is not None, "Registry was not loaded"
assert len(models.FETCHER.registry) > 0, "Registry is empty"


def test_model_map(models_toml):
assert models.model_map()
for model_name, files in models_toml.items():
assert model_name in models.model_map().keys(), (
f"Model {model_name} not found in model map"
)
assert files == models.model_map()[model_name], (
f"Files for model {model_name} do not match"
)


def test_generated_functions_exist(models_toml):
for model_name in models_toml.keys():
assert hasattr(models, model_name), (
Expand Down
7 changes: 6 additions & 1 deletion modflow_devtools/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

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

model_function.__name__ = model_name
return model_function
Expand All @@ -49,10 +49,15 @@ def _attach_functions(models):
else:
with Path(models).open("rb") as f:
models = tomli.load(f)
globals()["_models"] = models
for name, files in models.items():
globals()[name] = _generate_function(name, files)


def model_map() -> dict[str, list[Path]]:
return globals().get("_models", {})


try:
with pkg_resources.open_binary(DATA_ANCHOR, MODELMAP_NAME) as f:
_attach_functions(f)
Expand Down

0 comments on commit 4657ece

Please sign in to comment.