Clarified location sub paths

This commit is contained in:
GeoffreyCoulaud
2023-07-26 04:43:10 +02:00
parent 0677eae0a2
commit 04d0e9e90e
8 changed files with 36 additions and 29 deletions

View File

@@ -26,7 +26,7 @@ import yaml
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 SourceIterable, URLExecutableSource from src.importer.sources.source import SourceIterable, URLExecutableSource
@@ -98,8 +98,8 @@ class BottlesSource(URLExecutableSource):
shared.home / ".local" / "share" / "bottles", shared.home / ".local" / "share" / "bottles",
), ),
paths={ paths={
"library.yml": (False, "library.yml"), "library.yml": LocationSubPath("library.yml"),
"data.yml": (False, "data.yml"), "data.yml": LocationSubPath("data.yml"),
}, },
) )
) )

View File

@@ -25,7 +25,7 @@ from gi.repository import GLib, Gtk
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, SourceIterable from src.importer.sources.source import Source, SourceIterable
@@ -132,8 +132,8 @@ class FlatpakSource(Source):
shared.data_dir / "flatpak", shared.data_dir / "flatpak",
), ),
paths={ paths={
"applications": (True, "exports/share/applications"), "applications": LocationSubPath("exports/share/applications", True),
"icons": (True, "exports/share/icons"), "icons": LocationSubPath("exports/share/icons", True),
}, },
) )
) )

View File

@@ -30,7 +30,7 @@ from functools import cached_property
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 ( from src.importer.sources.source import (
SourceIterable, SourceIterable,
SourceIterationResult, SourceIterationResult,
@@ -374,8 +374,8 @@ class HeroicSource(URLExecutableSource):
shared.appdata_dir / "heroic", shared.appdata_dir / "heroic",
), ),
paths={ paths={
"config.json": (False, "config.json"), "config.json": LocationSubPath("config.json"),
"store_config.json": (False, Path("store") / "config.json"), "store_config.json": LocationSubPath("store/config.json"),
}, },
) )
) )

View File

@@ -25,7 +25,7 @@ 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 SourceIterable, URLExecutableSource from src.importer.sources.source import SourceIterable, URLExecutableSource
from src.utils.sqlite import copy_db from src.utils.sqlite import copy_db
@@ -94,6 +94,8 @@ class ItchSource(URLExecutableSource):
shared.home / ".config" / "itch", shared.home / ".config" / "itch",
shared.appdata_dir / "itch", shared.appdata_dir / "itch",
), ),
paths={"butler.db": (False, "db/butler.db")}, paths={
"butler.db": LocationSubPath("db/butler.db"),
},
) )
) )

View File

@@ -25,7 +25,7 @@ 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, SourceIterable from src.importer.sources.source import Source, SourceIterationResult, SourceIterable
@@ -107,8 +107,8 @@ class LegendarySource(Source):
shared.home / ".config" / "legendary", shared.home / ".config" / "legendary",
), ),
paths={ paths={
"installed.json": (False, "installed.json"), "installed.json": LocationSubPath("installed.json"),
"metadata": (True, "metadata"), "metadata": LocationSubPath("metadata", True),
}, },
) )
) )

View File

@@ -1,13 +1,18 @@
import logging import logging
from pathlib import Path from pathlib import Path
from typing import Callable, Mapping, Iterable from typing import Mapping, Iterable, NamedTuple
from os import PathLike from os import PathLike
from src import shared from src import shared
PathSegment = str | PathLike | Path PathSegment = str | PathLike | Path
PathSegments = Iterable[PathSegment] PathSegments = Iterable[PathSegment]
Candidate = PathSegments | Callable[[], PathSegments] Candidate = PathSegments
class LocationSubPath(NamedTuple):
segment: PathSegment
is_directory: bool = False
class UnresolvableLocationError(Exception): class UnresolvableLocationError(Exception):
@@ -26,14 +31,14 @@ class Location:
schema_key: str schema_key: str
candidates: Iterable[Candidate] candidates: Iterable[Candidate]
paths: Mapping[str, tuple[bool, PathSegments]] paths: Mapping[str, LocationSubPath]
root: Path = None root: Path = None
def __init__( def __init__(
self, self,
schema_key: str, schema_key: str,
candidates: Iterable[Candidate], candidates: Iterable[Candidate],
paths: Mapping[str, tuple[bool, PathSegments]], paths: Mapping[str, LocationSubPath],
) -> None: ) -> None:
super().__init__() super().__init__()
self.schema_key = schema_key self.schema_key = schema_key
@@ -42,13 +47,13 @@ class Location:
def check_candidate(self, candidate: Path) -> bool: def check_candidate(self, candidate: Path) -> bool:
"""Check if a candidate root has the necessary files and directories""" """Check if a candidate root has the necessary files and directories"""
for type_is_dir, subpath in self.paths.values(): for segment, is_directory in self.paths.values():
subpath = Path(candidate) / Path(subpath) path = Path(candidate) / segment
if type_is_dir: if is_directory:
if not subpath.is_dir(): if not path.is_dir():
return False return False
else: else:
if not subpath.is_file(): if not path.is_file():
return False return False
return True return True
@@ -81,4 +86,4 @@ class Location:
def __getitem__(self, key: str): def __getitem__(self, key: str):
"""Get the computed path from its key for the location""" """Get the computed path from its key for the location"""
self.resolve() self.resolve()
return self.root / self.paths[key][1] return self.root / self.paths[key].segment

View File

@@ -24,7 +24,7 @@ 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 SourceIterable, URLExecutableSource from src.importer.sources.source import SourceIterable, URLExecutableSource
from src.utils.sqlite import copy_db from src.utils.sqlite import copy_db
@@ -119,7 +119,7 @@ class LutrisSource(URLExecutableSource):
shared.home / ".cache" / "lutris", shared.home / ".cache" / "lutris",
), ),
paths={ paths={
"coverart": (True, "coverart"), "coverart": LocationSubPath("coverart", True),
}, },
), ),
) )

View File

@@ -26,7 +26,7 @@ from typing import Iterable, 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 SourceIterable, URLExecutableSource from src.importer.sources.source import SourceIterable, URLExecutableSource
from src.utils.steam import SteamFileHelper, SteamInvalidManifestError from src.utils.steam import SteamFileHelper, SteamInvalidManifestError
@@ -129,8 +129,8 @@ class SteamSource(URLExecutableSource):
shared.programfiles32_dir / "Steam", shared.programfiles32_dir / "Steam",
), ),
paths={ paths={
"libraryfolders.vdf": (False, "steamapps/libraryfolders.vdf"), "libraryfolders.vdf": LocationSubPath("steamapps/libraryfolders.vdf"),
"librarycache": (True, "appcache/librarycache"), "librarycache": LocationSubPath("appcache/librarycache", True),
}, },
) )
) )