🚧 Set schema on location resolve

This commit is contained in:
GeoffreyCoulaud
2023-06-19 23:11:55 +02:00
parent f9000be272
commit e57a2a74df
7 changed files with 32 additions and 15 deletions

View File

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

View File

@@ -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/",

View File

@@ -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/",

View File

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

View File

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

View File

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

View File

@@ -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/",