♻️ Restructured sources and managers

This commit is contained in:
GeoffreyCoulaud
2023-05-23 17:00:47 +02:00
parent 95524563bb
commit a11569014d
10 changed files with 17 additions and 24 deletions

View File

@@ -0,0 +1,16 @@
import src.shared as shared
from src.game import Game
from src.store.managers.manager import Manager
from src.store.managers.sgdb_manager import SGDBManager
from src.store.managers.steam_api_manager import SteamAPIManager
class DisplayManager(Manager):
"""Manager in charge of adding a game to the UI"""
run_after = set((SteamAPIManager, SGDBManager))
def run(self, game: Game) -> None:
# TODO decouple a game from its widget
shared.win.games[game.game_id] = game
game.update()

View File

@@ -0,0 +1,14 @@
from src.game import Game
from src.store.managers.manager import Manager
from src.store.managers.sgdb_manager import SGDBManager
from src.store.managers.steam_api_manager import SteamAPIManager
class FileManager(Manager):
"""Manager in charge of saving a game to a file"""
run_after = set((SteamAPIManager, SGDBManager))
def run(self, game: Game) -> None:
# TODO make game.save (disk) not trigger game.update (UI)
game.save()

View File

@@ -0,0 +1,49 @@
from abc import abstractmethod
from gi.repository import Gio
from src.game import Game
class Manager:
"""Class in charge of handling a post creation action for games.
* May connect to signals on the game to handle them.
* May cancel its running tasks on critical error,
in that case a new cancellable must be generated for new tasks to run.
"""
run_after: set[type["Manager"]]
cancellable: Gio.Cancellable
errors: list[Exception]
def __init__(self) -> None:
super().__init__()
self.cancellable = Gio.Cancellable()
self.errors = list()
def cancel_tasks(self):
"""Cancel all tasks for this manager"""
self.cancellable.cancel()
def reset_cancellable(self):
"""Reset the cancellable for this manager.
Alreadyn scheduled Tasks will no longer be cancellable."""
self.cancellable = Gio.Cancellable()
def report_error(self, error: Exception):
"""Report an error that happened in of run"""
self.errors.append(error)
def collect_errors(self) -> list[Exception]:
"""Get the errors produced by the manager and remove them from self.errors"""
errors = list(self.errors)
self.errors.clear()
return errors
@abstractmethod
def run(self, game: Game) -> None:
"""Pass the game through the manager.
May block its thread.
May not raise exceptions, as they will be silently ignored."""
pass

View File

@@ -0,0 +1,22 @@
from requests import HTTPError
from src.game import Game
from src.store.managers.manager import Manager
from src.utils.steamgriddb import SGDBAuthError, SGDBError, SGDBHelper
class SGDBManager(Manager):
"""Manager in charge of downloading a game's cover from steamgriddb"""
def run(self, game: Game) -> None:
try:
sgdb = SGDBHelper()
sgdb.conditionaly_update_cover(game)
except SGDBAuthError as error:
# If invalid auth, cancel all SGDBManager tasks
self.cancellable.cancel()
self.report_error(error)
except (HTTPError, SGDBError) as error:
# On other error, just report it
self.report_error(error)
pass

View File

@@ -0,0 +1,27 @@
from requests import HTTPError, JSONDecodeError
from src.game import Game
from src.store.managers.manager import Manager
from src.utils.steam import SteamGameNotFoundError, SteamHelper, SteamNotAGameError
class SteamAPIManager(Manager):
"""Manager in charge of completing a game's data from the Steam API"""
def run(self, game: Game) -> None:
# Skip non-steam games
if not game.source.startswith("steam_"):
return
# Get online metadata
appid = str(game.game_id).split("_")[-1]
steam = SteamHelper()
try:
online_data = steam.get_api_data(appid=appid)
except (HTTPError, JSONDecodeError) as error:
# On minor error, just report it
self.report_error(error)
except (SteamNotAGameError, SteamGameNotFoundError):
game.update_values({"blacklisted": True})
else:
game.update_values(online_data)