Skip to content

Commit 48cbf16

Browse files
authored
refactor(LocalRegistry): rework index method prefix parameters (#215)
The parameter prefix is a string to prepend to model names. But the routine to search for model dirs also has a prefix param to filter the results. Switch the prefix parameter's semantics to match this, and pass it through, and add a new model_name_prefix parameter for model name modification.
1 parent 087b063 commit 48cbf16

2 files changed

Lines changed: 27 additions & 13 deletions

File tree

modflow_devtools/misc.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,14 @@ def get_model_paths(
279279
Find model directories recursively in the given location.
280280
A model directory is any directory containing one or more
281281
namefiles. Model directories can be filtered or excluded,
282-
by prefix, pattern, namefile name, or packages used. The
283-
directories are returned in order within scenario folders
282+
by prefix, pattern, namefile name, or packages used. This
283+
function attempts to sort model subdirectories of a shared
284+
parent directory by the order in which the models must run,
284285
such that groundwater flow model workspaces precede other
285-
model types. This allows models which depend on the flow
286-
model's outputs to consume its head or budget, and models
287-
should successfully run in the sequence returned provided
288-
input files (e.g. FMI) refer to output via relative paths.
286+
model types, if the model directory names contain standard
287+
model abbreviates (e.g. "gwf", "gwt", "gwe"). This allows
288+
transport or other models to consume a flow model's head
289+
or budget results.
289290
"""
290291

291292
def keyfunc(v):

modflow_devtools/models.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,10 @@ def __init__(self) -> None:
105105
def index(
106106
self,
107107
path: str | PathLike,
108-
prefix: str = "",
108+
prefix: str | None = None,
109109
namefile: str = "mfsim.nam",
110+
excluded: list[str] | None = None,
111+
model_name_prefix: str = "",
110112
):
111113
"""
112114
Add models found under the given path to the registry.
@@ -116,22 +118,33 @@ def index(
116118
is idempotent and may be used to reload the registry e.g. if model
117119
files have changed since the registry was created.
118120
119-
The `path` may consist of model subdirectories
120-
at arbitrary depth. Model input subdirectories
121-
are identified by the presence of a namefile
122-
matching `namefile_pattern`.
121+
The `path` may consist of model subdirectories at arbitrary depth.
122+
Model subdirectories are identified by the presence of a namefile
123+
matching `namefile_pattern`. Model subdirectories may be filtered
124+
inclusively by `prefix` or exclusively by `excluded`
125+
126+
A `model_name_prefix` may be specified to avoid collisions with
127+
models indexed from other directories. This prefix will be added
128+
to the model name, which is derived from the relative path of the
129+
model subdirectory under `path`.
123130
"""
124131

125132
path = Path(path).expanduser().resolve().absolute()
126133
if not path.is_dir():
127134
raise NotADirectoryError(f"Directory path not found: {path}")
128135
self._paths.add(path)
129136

130-
model_paths = get_model_paths(path, namefile=namefile)
137+
model_paths = get_model_paths(
138+
path, prefix=prefix, namefile=namefile, excluded=excluded
139+
)
131140
for model_path in model_paths:
132141
model_path = model_path.expanduser().resolve().absolute()
133142
rel_path = model_path.relative_to(path)
134-
parts = [prefix, *list(rel_path.parts)] if prefix else list(rel_path.parts)
143+
parts = (
144+
[model_name_prefix, *list(rel_path.parts)]
145+
if model_name_prefix
146+
else list(rel_path.parts)
147+
)
135148
model_name = "/".join(parts)
136149
self._models[model_name] = []
137150
if len(rel_path.parts) > 1:

0 commit comments

Comments
 (0)