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.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"),
},
)
)

View File

@@ -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),
},
)
)

View File

@@ -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"),
},
)
)

View File

@@ -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"),
},
)
)

View File

@@ -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),
},
)
)

View File

@@ -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

View File

@@ -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),
},
),
)

View File

@@ -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),
},
)
)