♻️ Restructured sources and managers
This commit is contained in:
16
src/store/managers/display_manager.py
Normal file
16
src/store/managers/display_manager.py
Normal 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()
|
||||
14
src/store/managers/file_manager.py
Normal file
14
src/store/managers/file_manager.py
Normal 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()
|
||||
49
src/store/managers/manager.py
Normal file
49
src/store/managers/manager.py
Normal 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
|
||||
22
src/store/managers/sgdb_manager.py
Normal file
22
src/store/managers/sgdb_manager.py
Normal 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
|
||||
27
src/store/managers/steam_api_manager.py
Normal file
27
src/store/managers/steam_api_manager.py
Normal 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)
|
||||
Reference in New Issue
Block a user