🚧 Set schema on location resolve
This commit is contained in:
@@ -86,8 +86,8 @@ class BottlesSource(URLExecutableSource):
|
|||||||
available_on = set(("linux",))
|
available_on = set(("linux",))
|
||||||
|
|
||||||
data_location = Location(
|
data_location = Location(
|
||||||
|
schema_key="bottles-location",
|
||||||
candidates=(
|
candidates=(
|
||||||
lambda: shared.schema.get_string("bottles-location"),
|
|
||||||
"~/.var/app/com.usebottles.bottles/data/bottles/",
|
"~/.var/app/com.usebottles.bottles/data/bottles/",
|
||||||
shared.data_dir / "bottles/",
|
shared.data_dir / "bottles/",
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -138,8 +138,8 @@ class HeroicSource(URLExecutableSource):
|
|||||||
available_on = set(("linux", "win32"))
|
available_on = set(("linux", "win32"))
|
||||||
|
|
||||||
config_location = Location(
|
config_location = Location(
|
||||||
|
schema_key="heroic-location",
|
||||||
candidates=(
|
candidates=(
|
||||||
lambda: shared.schema.get_string("heroic-location"),
|
|
||||||
"~/.var/app/com.heroicgameslauncher.hgl/config/heroic/",
|
"~/.var/app/com.heroicgameslauncher.hgl/config/heroic/",
|
||||||
shared.config_dir / "heroic/",
|
shared.config_dir / "heroic/",
|
||||||
"~/.config/heroic/",
|
"~/.config/heroic/",
|
||||||
|
|||||||
@@ -84,8 +84,8 @@ class ItchSource(URLExecutableSource):
|
|||||||
available_on = set(("linux", "win32"))
|
available_on = set(("linux", "win32"))
|
||||||
|
|
||||||
config_location = Location(
|
config_location = Location(
|
||||||
|
schema_key="itch-location",
|
||||||
candidates=(
|
candidates=(
|
||||||
lambda: shared.schema.get_string("itch-location"),
|
|
||||||
"~/.var/app/io.itch.itch/config/itch/",
|
"~/.var/app/io.itch.itch/config/itch/",
|
||||||
shared.config_dir / "itch/",
|
shared.config_dir / "itch/",
|
||||||
"~/.config/itch/",
|
"~/.config/itch/",
|
||||||
|
|||||||
@@ -93,8 +93,8 @@ class LegendarySource(Source):
|
|||||||
|
|
||||||
iterator_class = LegendarySourceIterator
|
iterator_class = LegendarySourceIterator
|
||||||
data_location: Location = Location(
|
data_location: Location = Location(
|
||||||
|
schema_key="legendary-location",
|
||||||
candidates=(
|
candidates=(
|
||||||
lambda: shared.schema.get_string("legendary-location"),
|
|
||||||
shared.config_dir / "legendary/",
|
shared.config_dir / "legendary/",
|
||||||
"~/.config/legendary",
|
"~/.config/legendary",
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ from pathlib import Path
|
|||||||
from typing import Callable, Mapping, Iterable
|
from typing import Callable, Mapping, Iterable
|
||||||
from os import PathLike
|
from os import PathLike
|
||||||
|
|
||||||
|
from src import shared
|
||||||
|
|
||||||
PathSegment = str | PathLike | Path
|
PathSegment = str | PathLike | Path
|
||||||
PathSegments = Iterable[PathSegment]
|
PathSegments = Iterable[PathSegment]
|
||||||
Candidate = PathSegments | Callable[[], PathSegments]
|
Candidate = PathSegments | Callable[[], PathSegments]
|
||||||
@@ -16,19 +18,24 @@ class Location:
|
|||||||
Class representing a filesystem location
|
Class representing a filesystem location
|
||||||
|
|
||||||
* A location may have multiple candidate roots
|
* 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]
|
candidates: Iterable[Candidate]
|
||||||
paths: Mapping[str, tuple[bool, PathSegments]]
|
paths: Mapping[str, tuple[bool, PathSegments]]
|
||||||
root: Path = None
|
root: Path = None
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
schema_key: str,
|
||||||
candidates: Iterable[Candidate],
|
candidates: Iterable[Candidate],
|
||||||
paths: Mapping[str, tuple[bool, PathSegments]],
|
paths: Mapping[str, tuple[bool, PathSegments]],
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
self.schema_key = schema_key
|
||||||
self.candidates = candidates
|
self.candidates = candidates
|
||||||
self.paths = paths
|
self.paths = paths
|
||||||
|
|
||||||
@@ -47,16 +54,26 @@ class Location:
|
|||||||
def resolve(self) -> None:
|
def resolve(self) -> None:
|
||||||
"""Choose a root path from the candidates for the location.
|
"""Choose a root path from the candidates for the location.
|
||||||
If none fits, raise a UnresolvableLocationError"""
|
If none fits, raise a UnresolvableLocationError"""
|
||||||
|
|
||||||
if self.root is not None:
|
if self.root is not None:
|
||||||
return
|
return
|
||||||
for candidate in self.candidates:
|
|
||||||
if callable(candidate):
|
# Get the schema candidate
|
||||||
candidate = 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()
|
candidate = Path(candidate).expanduser()
|
||||||
if self.check_candidate(candidate):
|
if not self.check_candidate(candidate):
|
||||||
self.root = candidate
|
continue
|
||||||
return
|
self.root = candidate
|
||||||
raise UnresolvableLocationError()
|
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):
|
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"""
|
||||||
|
|||||||
@@ -93,8 +93,8 @@ class LutrisSource(URLExecutableSource):
|
|||||||
# FIXME possible bug: location picks ~/.var... and cache_lcoation picks ~/.local...
|
# FIXME possible bug: location picks ~/.var... and cache_lcoation picks ~/.local...
|
||||||
|
|
||||||
data_location = Location(
|
data_location = Location(
|
||||||
|
schema_key="lutris-location",
|
||||||
candidates=(
|
candidates=(
|
||||||
lambda: shared.schema.get_string("lutris-location"),
|
|
||||||
"~/.var/app/net.lutris.Lutris/data/lutris/",
|
"~/.var/app/net.lutris.Lutris/data/lutris/",
|
||||||
shared.data_dir / "lutris/",
|
shared.data_dir / "lutris/",
|
||||||
"~/.local/share/lutris/",
|
"~/.local/share/lutris/",
|
||||||
@@ -105,8 +105,8 @@ class LutrisSource(URLExecutableSource):
|
|||||||
)
|
)
|
||||||
|
|
||||||
cache_location = Location(
|
cache_location = Location(
|
||||||
|
schema_key="lutris-cache-location",
|
||||||
candidates=(
|
candidates=(
|
||||||
lambda: shared.schema.get_string("lutris-cache-location"),
|
|
||||||
"~/.var/app/net.lutris.Lutris/cache/lutris/",
|
"~/.var/app/net.lutris.Lutris/cache/lutris/",
|
||||||
shared.cache_dir / "lutris/",
|
shared.cache_dir / "lutris/",
|
||||||
"~/.cache/lutris",
|
"~/.cache/lutris",
|
||||||
|
|||||||
@@ -118,8 +118,8 @@ class SteamSource(URLExecutableSource):
|
|||||||
url_format = "steam://rungameid/{game_id}"
|
url_format = "steam://rungameid/{game_id}"
|
||||||
|
|
||||||
data_location = Location(
|
data_location = Location(
|
||||||
|
schema_key="steam-location",
|
||||||
candidates=(
|
candidates=(
|
||||||
lambda: shared.schema.get_string("steam-location"),
|
|
||||||
"~/.var/app/com.valvesoftware.Steam/data/Steam/",
|
"~/.var/app/com.valvesoftware.Steam/data/Steam/",
|
||||||
shared.data_dir / "Steam/",
|
shared.data_dir / "Steam/",
|
||||||
"~/.steam/",
|
"~/.steam/",
|
||||||
|
|||||||
Reference in New Issue
Block a user