Skip to content

Commit abec244

Browse files
authored
refactor(dfns): DfnRegistry.spec as method (#330)
1 parent 5fde75e commit abec244

3 files changed

Lines changed: 17 additions & 19 deletions

File tree

autotest/dfns/test_registry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def test_local_dfn_registry(dfn_dir):
152152
registry = LocalDfnRegistry(path=dfn_dir)
153153
assert registry.path == dfn_dir.resolve()
154154

155-
spec = registry.spec
155+
spec = registry.spec()
156156
assert spec.schema_version == "2.0.0.dev2"
157157
assert len(spec.components) > 100
158158
assert "gwf-chd" in spec.components
@@ -194,7 +194,7 @@ def test_remote_dfn_registry_sync():
194194
path = registry.get_path("gwf-chd")
195195
assert path.exists()
196196

197-
spec = registry.spec
197+
spec = registry.spec()
198198
assert "gwf-chd" in spec.components
199199
assert "sim-nam" in spec.components
200200

modflow_devtools/dfns/registry.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
import tomli_w
1212
from pydantic import BaseModel, Field, PrivateAttr
1313

14-
from modflow_devtools.dfns.schema import Dfns
14+
from modflow_devtools.dfn import schema as dfn
15+
from modflow_devtools.dfns import schema as dfns
1516

1617
__all__ = [
1718
"DfnRegistry",
@@ -27,10 +28,7 @@ class DfnRegistry(BaseModel):
2728

2829
model_config = {"arbitrary_types_allowed": True}
2930

30-
_spec: Dfns | None = PrivateAttr(default=None, init=False)
31-
32-
@property
33-
def spec(self) -> Dfns:
31+
def spec(self, schema_version: str = dfns.CURRENT_SCHEMA_VERSION) -> dfns.Dfns | dfn.Dfns:
3432
raise NotImplementedError
3533

3634
def get_path(self, component: str) -> Path:
@@ -45,11 +43,10 @@ class LocalDfnRegistry(DfnRegistry):
4543
def model_post_init(self, _) -> None:
4644
object.__setattr__(self, "path", Path(self.path).expanduser().resolve())
4745

48-
@property
49-
def spec(self) -> Dfns:
50-
if self._spec is None:
51-
self._spec = Dfns.load(self.path)
52-
return self._spec
46+
def spec(self, schema_version: str = dfns.CURRENT_SCHEMA_VERSION) -> dfns.Dfns | dfn.Dfns:
47+
if schema_version == dfns.CURRENT_SCHEMA_VERSION:
48+
return dfns.Dfns.load(self.path)
49+
return dfn.Dfn.load_all(self.path, schema_version=schema_version) # type: ignore
5350

5451
def get_path(self, component: str) -> Path:
5552
for ext in [".dfn", ".toml"]:
@@ -163,13 +160,12 @@ def cache_path(self) -> Path:
163160
repo, _ = self.release_id.split("@")
164161
return RemoteDfnRegistry.base_cache_path() / repo / self.latest_tag()
165162

166-
@property
167-
def spec(self) -> Dfns:
168-
if self._spec is None:
169-
if not self.cache_path.exists() or not any(self.cache_path.iterdir()):
170-
self.sync()
171-
self._spec = Dfns.load(self.cache_path)
172-
return self._spec
163+
def spec(self, schema_version: str = dfns.CURRENT_SCHEMA_VERSION) -> dfns.Dfns | dfn.Dfns:
164+
if not self.cache_path.exists() or not any(self.cache_path.iterdir()):
165+
self.sync()
166+
if schema_version == dfns.CURRENT_SCHEMA_VERSION:
167+
return dfns.Dfns.load(self.cache_path)
168+
return dfn.Dfn.load_all(self.cache_path, schema_version=schema_version) # type: ignore
173169

174170
def sync(self, force: bool = False) -> None:
175171
"""Download and extract DFN files for this release to the local cache."""

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,11 @@ mf = "modflow_devtools.cli:main"
166166

167167
[tool.hatch.build.targets.sdist]
168168
only-include = ["modflow_devtools"]
169+
core-metadata-version = "2.4"
169170

170171
[tool.hatch.build.targets.wheel]
171172
packages = ["modflow_devtools"]
173+
core-metadata-version = "2.4"
172174

173175
[tool.hatch.build]
174176
include = [

0 commit comments

Comments
 (0)