🎨 Moved things to managers

This commit is contained in:
GeoffreyCoulaud
2023-05-23 16:49:37 +02:00
parent abe41635fd
commit 95524563bb
5 changed files with 34 additions and 77 deletions

View File

@@ -22,33 +22,21 @@ class Importer:
n_games_added = 0 n_games_added = 0
n_source_tasks_created = 0 n_source_tasks_created = 0
n_source_tasks_done = 0 n_source_tasks_done = 0
n_sgdb_tasks_created = 0
n_sgdb_tasks_done = 0
sgdb_cancellable = None
sgdb_error = None
def __init__(self): def __init__(self):
self.sources = set() self.sources = set()
@property
def n_tasks_created(self):
return self.n_source_tasks_created + self.n_sgdb_tasks_created
@property
def n_tasks_done(self):
return self.n_source_tasks_done + self.n_sgdb_tasks_done
@property @property
def progress(self): def progress(self):
try: try:
progress = self.n_tasks_done / self.n_tasks_created progress = self.n_source_tasks_done / self.n_source_tasks_created
except ZeroDivisionError: except ZeroDivisionError:
progress = 1 progress = 1
return progress return progress
@property @property
def finished(self): def finished(self):
return self.n_tasks_created == self.n_tasks_done return self.n_source_tasks_created == self.n_source_tasks_done
def add_source(self, source): def add_source(self, source):
self.sources.add(source) self.sources.add(source)
@@ -123,15 +111,6 @@ class Importer:
shared.store.add_game(game) shared.store.add_game(game)
self.n_games_added += 1 self.n_games_added += 1
# Start sgdb lookup for game
# TODO move to its own manager
task = Task.new(
None, self.sgdb_cancellable, self.sgdb_task_callback, (game,)
)
self.n_sgdb_tasks_created += 1
task.set_task_data((game,))
task.run_in_thread(self.sgdb_task_thread_func)
def source_task_callback(self, _obj, _result, data): def source_task_callback(self, _obj, _result, data):
"""Source import callback""" """Source import callback"""
source, *_rest = data source, *_rest = data
@@ -141,38 +120,14 @@ class Importer:
if self.finished: if self.finished:
self.import_callback() self.import_callback()
def sgdb_task_thread_func(self, _task, _obj, data, cancellable):
"""SGDB query code"""
game, *_rest = data
game.set_loading(1)
sgdb = SGDBHelper()
try:
sgdb.conditionaly_update_cover(game)
except SGDBAuthError as error:
cancellable.cancel()
self.sgdb_error = error
except (HTTPError, SGDBError) as _error:
# TODO handle other SGDB errors
pass
def sgdb_task_callback(self, _obj, _result, data):
"""SGDB query callback"""
game, *_rest = data
logging.debug("SGDB import done for game %s", game.name)
game.set_loading(-1)
self.n_sgdb_tasks_done += 1
self.update_progressbar()
if self.finished:
self.import_callback()
def import_callback(self): def import_callback(self):
"""Callback called when importing has finished""" """Callback called when importing has finished"""
logging.info("Import done") logging.info("Import done")
self.import_dialog.close() self.import_dialog.close()
# TODO replace by summary if necessary
self.create_summary_toast() self.create_summary_toast()
# TODO create a summary of errors/warnings/tips popup (eg. SGDB, Steam libraries) # TODO create a summary of errors/warnings/tips popup (eg. SGDB, Steam libraries)
if self.sgdb_error is not None: # Get the error data from shared.store.managers)
self.create_sgdb_error_dialog()
def create_summary_toast(self): def create_summary_toast(self):
"""N games imported toast""" """N games imported toast"""
@@ -199,16 +154,6 @@ class Importer:
shared.win.toast_overlay.add_toast(toast) shared.win.toast_overlay.add_toast(toast)
def create_sgdb_error_dialog(self):
"""SGDB error dialog"""
create_dialog(
shared.win,
_("Couldn't Connect to SteamGridDB"),
str(self.sgdb_error),
"open_preferences",
_("Preferences"),
).connect("response", self.dialog_response_callback, "sgdb")
def open_preferences(self, page=None, expander_row=None): def open_preferences(self, page=None, expander_row=None):
shared.win.get_application().on_preferences_action( shared.win.get_application().on_preferences_action(
page_name=page, expander_row=expander_row page_name=page, expander_row=expander_row

View File

@@ -96,18 +96,6 @@ class SteamSourceIterator(SourceIterator):
if cover_path.is_file(): if cover_path.is_file():
save_cover(game.game_id, resize_cover(cover_path)) save_cover(game.game_id, resize_cover(cover_path))
# Get online metadata
# TODO move to its own manager
try:
online_data = steam.get_api_data(appid=appid)
except (HTTPError, JSONDecodeError):
pass
except (SteamNotAGameError, SteamGameNotFoundError):
game.update_values({"blacklisted": True})
else:
game.update_values(online_data)
return game
class SteamSource(Source): class SteamSource(Source):
name = "Steam" name = "Steam"

View File

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

View File

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

View File

@@ -1,11 +1,27 @@
from src.store.manager import Manager from requests import HTTPError, JSONDecodeError
from src.game import Game from src.game import Game
from src.utils.steam import SteamHelper from src.store.manager import Manager
from src.utils.steam import SteamGameNotFoundError, SteamHelper, SteamNotAGameError
class SteamAPIManager(Manager): class SteamAPIManager(Manager):
"""Manager in charge of completing a game's data from the Steam API""" """Manager in charge of completing a game's data from the Steam API"""
def run(self, game: Game) -> None: def run(self, game: Game) -> None:
# TODO # Skip non-steam games
pass 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)