Skip to content

Commit b720db4

Browse files
db version logging
1 parent f40ae12 commit b720db4

3 files changed

Lines changed: 59 additions & 2 deletions

File tree

mp_api/client/core/settings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ class MAPIClientSettings(BaseSettings):
7272
description="Angle tolerance for structure matching in degrees.",
7373
)
7474

75+
LOG_FILE : Path = Field(
76+
Path("~/.mprester.log.yaml").expanduser(),
77+
description = "Path for storing last accessed database version."
78+
)
79+
7580
LOCAL_DATASET_CACHE: Path = Field(
7681
Path("~/mp_datasets").expanduser(),
7782
description="Target directory for downloading full datasets",

mp_api/client/mprester.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def __init__(
211211
)
212212

213213
if notify_db_version:
214-
raise NotImplementedError("This has not yet been implemented.")
214+
self._db_version_check()
215215

216216
# Dynamically set rester attributes.
217217
# First, materials and molecules top level resters are set.
@@ -298,6 +298,10 @@ def __dir__(self):
298298
+ [r.split("/", 1)[0] for r in TOP_LEVEL_RESTERS if not r.startswith("_")]
299299
)
300300

301+
def __repr__(self) -> str:
302+
db_version = self.get_database_version()
303+
return f"MPRester({'v' + db_version if db_version else "unknown version"})"
304+
301305
def get_task_ids_associated_with_material_id(
302306
self, material_id: str, calc_types: list[CalcType] | None = None
303307
) -> list[str]:
@@ -369,7 +373,7 @@ def get_database_version(self) -> str | None:
369373
where "_DD" may be optional. An additional numerical suffix
370374
might be added if multiple releases happen on the same day.
371375
372-
Returns: database version as a string
376+
Returns: database version as a string if accessible, None otherwise
373377
"""
374378
if (get_resp := get(url=self.endpoint + "heartbeat")).status_code == 403:
375379
_emit_status_warning()
@@ -1615,3 +1619,33 @@ def get_oxygen_evolution(
16151619
phase_diagram,
16161620
unique_composition,
16171621
)
1622+
1623+
def _db_version_check(self) -> None:
1624+
"""Check if the database version has drifted."""
1625+
1626+
import yaml
1627+
db_version = self.get_database_version()
1628+
old_db_version = None
1629+
if MAPI_CLIENT_SETTINGS.LOG_FILE.exists():
1630+
old_db_version = (
1631+
yaml.safe_load(
1632+
MAPI_CLIENT_SETTINGS.LOG_FILE.read_text()
1633+
) or {}
1634+
).get("MAPI_DB_VERSION",None)
1635+
1636+
# Handle legacy pymatgen behavior
1637+
if not isinstance(old_db_version,str):
1638+
old_db_version = None
1639+
1640+
if old_db_version != db_version:
1641+
MAPI_CLIENT_SETTINGS.LOG_FILE.write_text(
1642+
yaml.safe_dump({"MAPI_DB_VERSION": db_version})
1643+
)
1644+
1645+
if old_db_version:
1646+
warnings.warn(
1647+
"Materials Project database version has changed "
1648+
f"from v{old_db_version} to v{db_version}.",
1649+
category=MPRestWarning,
1650+
stacklevel=2,
1651+
)

tests/client/test_mprester.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,3 +625,21 @@ def test_oxygen_evolution_bad_input(self, mpr):
625625
def test_monty_decode_warning(self):
626626
with pytest.warns(MPRestWarning, match="Ignoring `monty_decode`"):
627627
MPRester(monty_decode=False)
628+
629+
def test_db_warning(self, monkeypatch: pytest.MonkeyPatch):
630+
631+
from pathlib import Path
632+
import yaml
633+
from mp_api.client.core.settings import MAPI_CLIENT_SETTINGS
634+
635+
with NamedTemporaryFile(suffix=".yaml") as tmp_log:
636+
monkeypatch.setattr(MAPI_CLIENT_SETTINGS,"LOG_FILE",Path(tmp_log.name))
637+
638+
with MPRester(notify_db_version = True) as mpr:
639+
db_version = mpr.get_database_version()
640+
641+
parsed_db_ver = yaml.safe_load(
642+
Path(tmp_log.name).read_text()
643+
).get("MAPI_DB_VERSION")
644+
assert parsed_db_ver == db_version
645+
assert isinstance(parsed_db_ver,str)

0 commit comments

Comments
 (0)