From 04d0e9e90e1646d0621429d592c876cefce1d0f0 Mon Sep 17 00:00:00 2001 From: GeoffreyCoulaud Date: Wed, 26 Jul 2023 04:43:10 +0200 Subject: [PATCH] Clarified location sub paths --- src/importer/sources/bottles_source.py | 6 +++--- src/importer/sources/flatpak_source.py | 6 +++--- src/importer/sources/heroic_source.py | 6 +++--- src/importer/sources/itch_source.py | 6 ++++-- src/importer/sources/legendary_source.py | 6 +++--- src/importer/sources/location.py | 25 ++++++++++++++---------- src/importer/sources/lutris_source.py | 4 ++-- src/importer/sources/steam_source.py | 6 +++--- 8 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/importer/sources/bottles_source.py b/src/importer/sources/bottles_source.py index 42eebaa..5983c27 100644 --- a/src/importer/sources/bottles_source.py +++ b/src/importer/sources/bottles_source.py @@ -26,7 +26,7 @@ import yaml from src import shared 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 SourceIterable, URLExecutableSource @@ -98,8 +98,8 @@ class BottlesSource(URLExecutableSource): shared.home / ".local" / "share" / "bottles", ), paths={ - "library.yml": (False, "library.yml"), - "data.yml": (False, "data.yml"), + "library.yml": LocationSubPath("library.yml"), + "data.yml": LocationSubPath("data.yml"), }, ) ) diff --git a/src/importer/sources/flatpak_source.py b/src/importer/sources/flatpak_source.py index f29028c..ec8e2a5 100644 --- a/src/importer/sources/flatpak_source.py +++ b/src/importer/sources/flatpak_source.py @@ -25,7 +25,7 @@ from gi.repository import GLib, Gtk from src import shared 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, SourceIterable @@ -132,8 +132,8 @@ class FlatpakSource(Source): shared.data_dir / "flatpak", ), paths={ - "applications": (True, "exports/share/applications"), - "icons": (True, "exports/share/icons"), + "applications": LocationSubPath("exports/share/applications", True), + "icons": LocationSubPath("exports/share/icons", True), }, ) ) diff --git a/src/importer/sources/heroic_source.py b/src/importer/sources/heroic_source.py index 1c28f14..2986aad 100644 --- a/src/importer/sources/heroic_source.py +++ b/src/importer/sources/heroic_source.py @@ -30,7 +30,7 @@ from functools import cached_property from src import shared 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 ( SourceIterable, SourceIterationResult, @@ -374,8 +374,8 @@ class HeroicSource(URLExecutableSource): shared.appdata_dir / "heroic", ), paths={ - "config.json": (False, "config.json"), - "store_config.json": (False, Path("store") / "config.json"), + "config.json": LocationSubPath("config.json"), + "store_config.json": LocationSubPath("store/config.json"), }, ) ) diff --git a/src/importer/sources/itch_source.py b/src/importer/sources/itch_source.py index dc4d4f9..19839cd 100644 --- a/src/importer/sources/itch_source.py +++ b/src/importer/sources/itch_source.py @@ -25,7 +25,7 @@ from typing import NamedTuple from src import shared 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 SourceIterable, URLExecutableSource from src.utils.sqlite import copy_db @@ -94,6 +94,8 @@ class ItchSource(URLExecutableSource): shared.home / ".config" / "itch", shared.appdata_dir / "itch", ), - paths={"butler.db": (False, "db/butler.db")}, + paths={ + "butler.db": LocationSubPath("db/butler.db"), + }, ) ) diff --git a/src/importer/sources/legendary_source.py b/src/importer/sources/legendary_source.py index 529bd8c..e802f51 100644 --- a/src/importer/sources/legendary_source.py +++ b/src/importer/sources/legendary_source.py @@ -25,7 +25,7 @@ from typing import NamedTuple from src import shared 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, SourceIterable @@ -107,8 +107,8 @@ class LegendarySource(Source): shared.home / ".config" / "legendary", ), paths={ - "installed.json": (False, "installed.json"), - "metadata": (True, "metadata"), + "installed.json": LocationSubPath("installed.json"), + "metadata": LocationSubPath("metadata", True), }, ) ) diff --git a/src/importer/sources/location.py b/src/importer/sources/location.py index 8374a20..a884ba0 100644 --- a/src/importer/sources/location.py +++ b/src/importer/sources/location.py @@ -1,13 +1,18 @@ import logging from pathlib import Path -from typing import Callable, Mapping, Iterable +from typing import Mapping, Iterable, NamedTuple from os import PathLike from src import shared PathSegment = str | PathLike | Path PathSegments = Iterable[PathSegment] -Candidate = PathSegments | Callable[[], PathSegments] +Candidate = PathSegments + + +class LocationSubPath(NamedTuple): + segment: PathSegment + is_directory: bool = False class UnresolvableLocationError(Exception): @@ -26,14 +31,14 @@ class Location: schema_key: str candidates: Iterable[Candidate] - paths: Mapping[str, tuple[bool, PathSegments]] + paths: Mapping[str, LocationSubPath] root: Path = None def __init__( self, schema_key: str, candidates: Iterable[Candidate], - paths: Mapping[str, tuple[bool, PathSegments]], + paths: Mapping[str, LocationSubPath], ) -> None: super().__init__() self.schema_key = schema_key @@ -42,13 +47,13 @@ class Location: def check_candidate(self, candidate: Path) -> bool: """Check if a candidate root has the necessary files and directories""" - for type_is_dir, subpath in self.paths.values(): - subpath = Path(candidate) / Path(subpath) - if type_is_dir: - if not subpath.is_dir(): + for segment, is_directory in self.paths.values(): + path = Path(candidate) / segment + if is_directory: + if not path.is_dir(): return False else: - if not subpath.is_file(): + if not path.is_file(): return False return True @@ -81,4 +86,4 @@ class Location: def __getitem__(self, key: str): """Get the computed path from its key for the location""" self.resolve() - return self.root / self.paths[key][1] + return self.root / self.paths[key].segment diff --git a/src/importer/sources/lutris_source.py b/src/importer/sources/lutris_source.py index 5a26fc1..627f896 100644 --- a/src/importer/sources/lutris_source.py +++ b/src/importer/sources/lutris_source.py @@ -24,7 +24,7 @@ from typing import NamedTuple from src import shared 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 SourceIterable, URLExecutableSource from src.utils.sqlite import copy_db @@ -119,7 +119,7 @@ class LutrisSource(URLExecutableSource): shared.home / ".cache" / "lutris", ), paths={ - "coverart": (True, "coverart"), + "coverart": LocationSubPath("coverart", True), }, ), ) diff --git a/src/importer/sources/steam_source.py b/src/importer/sources/steam_source.py index e4e9cfb..39e3c49 100644 --- a/src/importer/sources/steam_source.py +++ b/src/importer/sources/steam_source.py @@ -26,7 +26,7 @@ from typing import Iterable, NamedTuple from src import shared 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 SourceIterable, URLExecutableSource from src.utils.steam import SteamFileHelper, SteamInvalidManifestError @@ -129,8 +129,8 @@ class SteamSource(URLExecutableSource): shared.programfiles32_dir / "Steam", ), paths={ - "libraryfolders.vdf": (False, "steamapps/libraryfolders.vdf"), - "librarycache": (True, "appcache/librarycache"), + "libraryfolders.vdf": LocationSubPath("steamapps/libraryfolders.vdf"), + "librarycache": LocationSubPath("appcache/librarycache", True), }, ) )