Skip to content

Commit d736dfd

Browse files
authored
fix(models): backwards-compatibility adjustments (#272)
Make the recent models API enhancements non-breaking * restore `DEFAULT_REGISTRY` via `__getattr__` * dictionary style access to file entry properties
1 parent 5e85417 commit d736dfd

1 file changed

Lines changed: 47 additions & 1 deletion

File tree

modflow_devtools/models/__init__.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@
4343

4444

4545
class ModelInputFile(BaseModel):
46-
"""A single file entry in the registry. Can be local or remote."""
46+
"""
47+
A single file entry in the registry. Can be local or remote.
48+
49+
Implements dict-like access for backwards compatibility:
50+
file_entry["hash"], file_entry["path"], file_entry["url"]
51+
"""
4752

4853
url: str | None = Field(None, description="URL (for remote files)")
4954
path: Path | None = Field(None, description="Local file path (original or cached)")
@@ -61,6 +66,36 @@ def check_location(self):
6166
raise ValueError("FileEntry must have either url or path")
6267
return self
6368

69+
# Backwards compatibility: dict-like access
70+
def __getitem__(self, key: str):
71+
"""Allow dict-like access for backwards compatibility."""
72+
if key == "url":
73+
return self.url
74+
elif key == "path":
75+
return self.path
76+
elif key == "hash":
77+
return self.hash
78+
raise KeyError(key)
79+
80+
def get(self, key: str, default=None):
81+
"""Allow dict-like .get() for backwards compatibility."""
82+
try:
83+
return self[key]
84+
except KeyError:
85+
return default
86+
87+
def keys(self):
88+
"""Return available keys for backwards compatibility."""
89+
return ["url", "path", "hash"]
90+
91+
def values(self):
92+
"""Return values for backwards compatibility."""
93+
return [self.url, self.path, self.hash]
94+
95+
def items(self):
96+
"""Return items for backwards compatibility."""
97+
return [("url", self.url), ("path", self.path), ("hash", self.hash)]
98+
6499

65100
class ModelRegistry(BaseModel):
66101
"""
@@ -1311,3 +1346,14 @@ def copy_to(workspace: str | PathLike, model_name: str, verbose: bool = False) -
13111346
The workspace will be created if it does not exist.
13121347
"""
13131348
return get_default_registry().copy_to(workspace, model_name, verbose=verbose)
1349+
1350+
1351+
def __getattr__(name: str):
1352+
"""
1353+
Lazy module attribute access for backwards compatibility.
1354+
1355+
Provides DEFAULT_REGISTRY as a lazily-initialized module attribute.
1356+
"""
1357+
if name == "DEFAULT_REGISTRY":
1358+
return get_default_registry()
1359+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

0 commit comments

Comments
 (0)