Skip to content

Commit

Permalink
🔀 Merge pull request #69 from davep/data-version-check
Browse files Browse the repository at this point in the history
Add a local data version check
  • Loading branch information
davep authored Jan 10, 2025
2 parents 8c4c4b6 + da3ad9b commit 2b83efb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/braindrop/app/data/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from datetime import datetime
from json import dumps, loads
from pathlib import Path
from typing import Any, Callable
from typing import Any, Callable, Final

##############################################################################
# pytz imports.
Expand Down Expand Up @@ -43,6 +43,9 @@ def local_data_file() -> Path:
class LocalData:
"""Holds and manages the local copy of the Raindrop data."""

VERSION: Final[int] = 0
"""The version of the format of the local data."""

def __init__(self, api: API) -> None:
"""Initialise the object.
Expand All @@ -63,12 +66,19 @@ def __init__(self, api: API) -> None:
"""An index of all of the Raindrops we know about."""
self._last_downloaded: datetime | None = None
"""The time the data was last downloaded from the server."""
self._version: int | None = None
"""The version of the format of the data."""

@property
def last_downloaded(self) -> datetime | None:
"""The time the data was downloaded, or `None` if not yet."""
return self._last_downloaded

@property
def outdated_format(self) -> bool:
"""Is the format of the local data outdated?"""
return self._version is None or self._version < self.VERSION

@property
def user(self) -> User | None:
"""The user that the data relates to."""
Expand Down Expand Up @@ -252,6 +262,7 @@ def _local_json(self) -> dict[str, Any]:
"last_downloaded": None
if self._last_downloaded is None
else self._last_downloaded.isoformat(),
"version": self.VERSION,
"user": None if self._user is None else self._user.raw,
"all": [raindrop.raw for raindrop in self._all],
"trash": [raindrop.raw for raindrop in self._trash],
Expand All @@ -277,6 +288,11 @@ def load(self) -> Self:
"""
if local_data_file().exists():
data = loads(local_data_file().read_text(encoding="utf-8"))
self._version = data.get("version")
if self.outdated_format:
# The version is unknown, or older than we're expecting, so
# let's pretend it doesn't exist.
return self
self._last_downloaded = get_time(data, "last_downloaded")
self._user = User.from_json(data.get("user", {}))
self._all.set_to(
Expand Down
4 changes: 3 additions & 1 deletion src/braindrop/app/screens/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ async def maybe_redownload(self) -> None:
)
return

if self._data.last_downloaded is None:
if self._data.outdated_format:
self.notify("Local file format has changed; rebuilding from the server.")
elif self._data.last_downloaded is None:
self.notify("No local data found; checking in with the server.")
elif (
self._user.last_update is not None
Expand Down

0 comments on commit 2b83efb

Please sign in to comment.