From e57a2a74df0d47d4d5e14a8cb37d9dd24cf4e542 Mon Sep 17 00:00:00 2001 From: GeoffreyCoulaud Date: Mon, 19 Jun 2023 23:11:55 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20Set=20schema=20on=20location=20r?= =?UTF-8?q?esolve?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/importer/sources/bottles_source.py | 2 +- src/importer/sources/heroic_source.py | 2 +- src/importer/sources/itch_source.py | 2 +- src/importer/sources/legendary_source.py | 2 +- src/importer/sources/location.py | 33 ++++++++++++++++++------ src/importer/sources/lutris_source.py | 4 +-- src/importer/sources/steam_source.py | 2 +- 7 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/importer/sources/bottles_source.py b/src/importer/sources/bottles_source.py index 0823c0a..d8148d4 100644 --- a/src/importer/sources/bottles_source.py +++ b/src/importer/sources/bottles_source.py @@ -86,8 +86,8 @@ class BottlesSource(URLExecutableSource): available_on = set(("linux",)) data_location = Location( + schema_key="bottles-location", candidates=( - lambda: shared.schema.get_string("bottles-location"), "~/.var/app/com.usebottles.bottles/data/bottles/", shared.data_dir / "bottles/", ), diff --git a/src/importer/sources/heroic_source.py b/src/importer/sources/heroic_source.py index 8b828f1..17d5da2 100644 --- a/src/importer/sources/heroic_source.py +++ b/src/importer/sources/heroic_source.py @@ -138,8 +138,8 @@ class HeroicSource(URLExecutableSource): available_on = set(("linux", "win32")) config_location = Location( + schema_key="heroic-location", candidates=( - lambda: shared.schema.get_string("heroic-location"), "~/.var/app/com.heroicgameslauncher.hgl/config/heroic/", shared.config_dir / "heroic/", "~/.config/heroic/", diff --git a/src/importer/sources/itch_source.py b/src/importer/sources/itch_source.py index a36aaa1..df861fd 100644 --- a/src/importer/sources/itch_source.py +++ b/src/importer/sources/itch_source.py @@ -84,8 +84,8 @@ class ItchSource(URLExecutableSource): available_on = set(("linux", "win32")) config_location = Location( + schema_key="itch-location", candidates=( - lambda: shared.schema.get_string("itch-location"), "~/.var/app/io.itch.itch/config/itch/", shared.config_dir / "itch/", "~/.config/itch/", diff --git a/src/importer/sources/legendary_source.py b/src/importer/sources/legendary_source.py index 7fd2f56..988122e 100644 --- a/src/importer/sources/legendary_source.py +++ b/src/importer/sources/legendary_source.py @@ -93,8 +93,8 @@ class LegendarySource(Source): iterator_class = LegendarySourceIterator data_location: Location = Location( + schema_key="legendary-location", candidates=( - lambda: shared.schema.get_string("legendary-location"), shared.config_dir / "legendary/", "~/.config/legendary", ), diff --git a/src/importer/sources/location.py b/src/importer/sources/location.py index 651db7b..545fe08 100644 --- a/src/importer/sources/location.py +++ b/src/importer/sources/location.py @@ -2,6 +2,8 @@ from pathlib import Path from typing import Callable, Mapping, Iterable from os import PathLike +from src import shared + PathSegment = str | PathLike | Path PathSegments = Iterable[PathSegment] Candidate = PathSegments | Callable[[], PathSegments] @@ -16,19 +18,24 @@ class Location: Class representing a filesystem location * A location may have multiple candidate roots - * From its root, multiple subpaths are named and should exist + * The path in the schema is always favored + * From the candidate root, multiple subpaths should exist for it to be valid + * When resolved, the schema is updated with the picked chosen """ + schema_key: str candidates: Iterable[Candidate] paths: Mapping[str, tuple[bool, PathSegments]] root: Path = None def __init__( self, + schema_key: str, candidates: Iterable[Candidate], paths: Mapping[str, tuple[bool, PathSegments]], ) -> None: super().__init__() + self.schema_key = schema_key self.candidates = candidates self.paths = paths @@ -47,16 +54,26 @@ class Location: def resolve(self) -> None: """Choose a root path from the candidates for the location. If none fits, raise a UnresolvableLocationError""" + if self.root is not None: return - for candidate in self.candidates: - if callable(candidate): - candidate = candidate() + + # Get the schema candidate + schema_candidate = shared.schema.get_string(self.schema_key) + + # Find the first matching candidate + for candidate in (schema_candidate, *self.candidates): candidate = Path(candidate).expanduser() - if self.check_candidate(candidate): - self.root = candidate - return - raise UnresolvableLocationError() + if not self.check_candidate(candidate): + continue + self.root = candidate + break + else: + # No good candidate found + raise UnresolvableLocationError() + + # Update the schema with the found candidate + shared.schema.set_string(self.schema_key, str(candidate)) def __getitem__(self, key: str): """Get the computed path from its key for the location""" diff --git a/src/importer/sources/lutris_source.py b/src/importer/sources/lutris_source.py index db05a18..1fa0aa0 100644 --- a/src/importer/sources/lutris_source.py +++ b/src/importer/sources/lutris_source.py @@ -93,8 +93,8 @@ class LutrisSource(URLExecutableSource): # FIXME possible bug: location picks ~/.var... and cache_lcoation picks ~/.local... data_location = Location( + schema_key="lutris-location", candidates=( - lambda: shared.schema.get_string("lutris-location"), "~/.var/app/net.lutris.Lutris/data/lutris/", shared.data_dir / "lutris/", "~/.local/share/lutris/", @@ -105,8 +105,8 @@ class LutrisSource(URLExecutableSource): ) cache_location = Location( + schema_key="lutris-cache-location", candidates=( - lambda: shared.schema.get_string("lutris-cache-location"), "~/.var/app/net.lutris.Lutris/cache/lutris/", shared.cache_dir / "lutris/", "~/.cache/lutris", diff --git a/src/importer/sources/steam_source.py b/src/importer/sources/steam_source.py index 2110692..8adc787 100644 --- a/src/importer/sources/steam_source.py +++ b/src/importer/sources/steam_source.py @@ -118,8 +118,8 @@ class SteamSource(URLExecutableSource): url_format = "steam://rungameid/{game_id}" data_location = Location( + schema_key="steam-location", candidates=( - lambda: shared.schema.get_string("steam-location"), "~/.var/app/com.valvesoftware.Steam/data/Steam/", shared.data_dir / "Steam/", "~/.steam/",