Convert to new importer format

This commit is contained in:
Rilic
2023-08-03 20:32:28 +01:00
parent 436a54ba5b
commit 0440eee5d4

View File

@@ -19,27 +19,28 @@
from pathlib import Path from pathlib import Path
from time import time from time import time
from typing import NamedTuple
from src import shared from src import shared
from src.game import Game from src.game import Game
from src.importer.sources.location import Location from src.importer.sources.location import Location, LocationSubPath
from src.importer.sources.source import Source, SourceIterationResult, SourceIterator from src.importer.sources.source import Source, SourceIterable
from src.utils.dolphin_cache_reader import DolphinCacheReader from src.utils.dolphin_cache_reader import DolphinCacheReader
class DolphinIterator(SourceIterator): class DolphinSourceIterable(SourceIterable):
source: "DolphinSource" source: "DolphinSource"
def generator_builder(self) -> SourceIterationResult: def __iter__(self):
added_time = int(time()) added_time = int(time())
cache_reader = DolphinCacheReader(self.source.cache_location["cache_file"]) cache_reader = DolphinCacheReader(self.source.locations.cache["cache_file"])
games_data = cache_reader.get_games() games_data = cache_reader.get_games()
for game_data in games_data: for game_data in games_data:
# Build game # Build game
values = { values = {
"source": self.source.id, "source": self.source.source_id,
"added": added_time, "added": added_time,
"name": Path(game_data["file_name"]).stem, "name": Path(game_data["file_name"]).stem,
"game_id": self.source.game_id_format.format( "game_id": self.source.game_id_format.format(
@@ -53,31 +54,45 @@ class DolphinIterator(SourceIterator):
game = Game(values) game = Game(values)
image_path = Path( image_path = Path(
self.source.cache_location["covers"] / (game_data["game_id"] + ".png") self.source.locations.cache["covers"] / (game_data["game_id"] + ".png")
) )
additional_data = {"local_image_path": image_path} additional_data = {"local_image_path": image_path}
yield (game, additional_data) yield (game, additional_data)
class DolphinSource(Source): class DolphinLocations(NamedTuple):
name = "Dolphin" cache: Location
available_on = {"linux"}
iterator_class = DolphinIterator
cache_location = Location(
schema_key="dolphin-cache-location", class DolphinSource(Source):
candidates=( name = _("Dolphin")
shared.flatpak_dir / "org.DolphinEmu.dolphin-emu" / "cache" / "dolphin-emu", source_id = "dolphin"
shared.home / ".cache" / "dolphin-emu", available_on = {"linux"}
), iterable_class = DolphinSourceIterable
paths={"cache_file": (False, "gamelist.cache"), "covers": (True, "GameCovers")},
locations = DolphinLocations(
Location(
schema_key="dolphin-cache-location",
candidates=[
shared.flatpak_dir
/ "org.DolphinEmu.dolphin-emu"
/ "cache"
/ "dolphin-emu",
shared.home / ".cache" / "dolphin-emu",
],
paths={
"cache_file": LocationSubPath("gamelist.cache"),
"covers": LocationSubPath("GameCovers", True),
},
invalid_subtitle=Location.CACHE_INVALID_SUBTITLE,
)
) )
@property @property
def executable_format(self): def executable_format(self):
self.cache_location.resolve() self.locations.cache.resolve()
is_flatpak = self.cache_location.root.is_relative_to(shared.flatpak_dir) is_flatpak = self.locations.cache.root.is_relative_to(shared.flatpak_dir)
base = "flatpak run org.DolphinEmu.dolphin-emu" if is_flatpak else "dolphin-emu" base = "flatpak run org.DolphinEmu.dolphin-emu" if is_flatpak else "dolphin-emu"
args = '-b -e "{rom_path}"' args = '-b -e "{rom_path}"'
return f"{base} {args}" return f"{base} {args}"