Skip to content

Commit

Permalink
fully type the caching system
Browse files Browse the repository at this point in the history
  • Loading branch information
TabulateJarl8 committed Nov 15, 2024
1 parent c6fdc00 commit c21b57f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "vapor-steam"
version = "1.5.7"
version = "1.5.8"
description = "TUI program to check the ProtonDB compatibility of all the games of a Steam user."
authors = ["TabulateJarl8 <[email protected]>"]
license = "GPLv3"
Expand Down
24 changes: 16 additions & 8 deletions vapor/cache_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
import json
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Optional, Tuple, Union
from typing import Dict, List, Optional, Tuple

from typing_extensions import Self, override

from vapor.data_structures import CONFIG_DIR, AntiCheatData, AntiCheatStatus, Game
from vapor.data_structures import (
CONFIG_DIR,
AntiCheatData,
AntiCheatStatus,
CacheFile,
Game,
SerializedAnticheatData,
SerializedGameData,
)

CACHE_PATH = CONFIG_DIR / 'cache.json'
"""The path to the cache file."""
Expand Down Expand Up @@ -37,11 +45,11 @@ def __repr__(self) -> str:
"""Return the string representation of the Cache object."""
return f'Cache({self.__dict__!r})'

def _serialize_game_data(self) -> Dict[str, Dict[str, str]]:
def _serialize_game_data(self) -> Dict[str, SerializedGameData]:
"""Serialize the game data into a valid JSON dict.
Returns:
Dict[str, Dict[str, str]]: Valid JSON dict.
Dict[str, SerializedGameData]: Valid JSON dict.
"""
return {
app_id: {
Expand All @@ -52,11 +60,11 @@ def _serialize_game_data(self) -> Dict[str, Dict[str, str]]:
for app_id, game in self._games_data.items()
}

def _serialize_anti_cheat_data(self) -> Dict[str, Union[str, Dict[str, str]]]:
def _serialize_anti_cheat_data(self) -> SerializedAnticheatData:
"""Serialize the anticheat data into a valid JSON dict.
Returns:
Dict[str, Union[str, Dict[str, str]]]: Valid JSON dict.
SerializedAnticheatData: Valid JSON dict.
"""
return {
'data': {
Expand Down Expand Up @@ -120,7 +128,7 @@ def load_cache(self, prune: Optional[bool] = True) -> Self:
self.prune_cache()

try:
data = json.loads(self.cache_path.read_text())
data: CacheFile = json.loads(self.cache_path.read_text())
except Exception:
return self

Expand Down Expand Up @@ -199,7 +207,7 @@ def prune_cache(self) -> Self:
Self: self.
"""
try:
data = json.loads(self.cache_path.read_text())
data: CacheFile = json.loads(self.cache_path.read_text())
except Exception:
return self

Expand Down
39 changes: 39 additions & 0 deletions vapor/data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Dict, List, NamedTuple, TypedDict

from platformdirs import user_config_path
from typing_extensions import NotRequired

CONFIG_DIR = user_config_path(appname='vapor', appauthor='tabulate', ensure_exists=True)
"""The config directory used to write files such as config and cache."""
Expand Down Expand Up @@ -261,3 +262,41 @@ class SteamAPIUserDataResponse(TypedDict):
"""

response: _SteamAPIUserGameList


class SerializedGameData(TypedDict):
"""Serialized game data for caching.
Attributes:
name (str): Name of the game
rating (str): Game's ProtonDB rating
timestamp (str): Last updated
"""

name: str
rating: str
timestamp: str


class SerializedAnticheatData(TypedDict):
"""Serialized anticheat data for caching.
Attributes:
data (Dict[str, str]): Dictionary of app_id: anticheat_status
timestamp (str): Last updated
"""

data: Dict[str, str]
timestamp: str


class CacheFile(TypedDict):
"""Fully aggregated cache file as JSON.
Attributes:
game_cache (NotRequired[Dict[str, SerializedGameData]]): Optional game cache
anticheat_cache (NotRequired[SerializedAnticheatData]): Optional anticheat cache
"""

game_cache: NotRequired[Dict[str, SerializedGameData]]
anticheat_cache: NotRequired[SerializedAnticheatData]

0 comments on commit c21b57f

Please sign in to comment.