Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion modflow_devtools/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@


class ModelInputFile(BaseModel):
"""A single file entry in the registry. Can be local or remote."""
"""
A single file entry in the registry. Can be local or remote.

Implements dict-like access for backwards compatibility:
file_entry["hash"], file_entry["path"], file_entry["url"]
"""

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

# Backwards compatibility: dict-like access
def __getitem__(self, key: str):
"""Allow dict-like access for backwards compatibility."""
if key == "url":
return self.url
elif key == "path":
return self.path
elif key == "hash":
return self.hash
raise KeyError(key)

def get(self, key: str, default=None):
"""Allow dict-like .get() for backwards compatibility."""
try:
return self[key]
except KeyError:
return default

def keys(self):
"""Return available keys for backwards compatibility."""
return ["url", "path", "hash"]

def values(self):
"""Return values for backwards compatibility."""
return [self.url, self.path, self.hash]

def items(self):
"""Return items for backwards compatibility."""
return [("url", self.url), ("path", self.path), ("hash", self.hash)]


class ModelRegistry(BaseModel):
"""
Expand Down Expand Up @@ -1311,3 +1346,14 @@ def copy_to(workspace: str | PathLike, model_name: str, verbose: bool = False) -
The workspace will be created if it does not exist.
"""
return get_default_registry().copy_to(workspace, model_name, verbose=verbose)


def __getattr__(name: str):
"""
Lazy module attribute access for backwards compatibility.

Provides DEFAULT_REGISTRY as a lazily-initialized module attribute.
"""
if name == "DEFAULT_REGISTRY":
return get_default_registry()
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")