WIP location (to be discarded)
This commit is contained in:
@@ -1,20 +1,35 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from functools import cached_property
|
from functools import cached_property
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from os import PathLike
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Location():
|
class Location():
|
||||||
"""Abstraction for a location that can be overriden by a schema key"""
|
"""Abstraction for a location that has multiple candidate paths"""
|
||||||
|
|
||||||
win = None
|
candidates: list[PathLike] = None
|
||||||
|
|
||||||
default: str = None
|
def __init__(self, *candidates):
|
||||||
key: str = None
|
self.candidates = list()
|
||||||
|
self.candidates.extend(candidates)
|
||||||
|
return self
|
||||||
|
|
||||||
|
def add(self, canddiate):
|
||||||
|
"""Add a candidate (last evaluated)"""
|
||||||
|
self.candidates.append(canddiate)
|
||||||
|
return self
|
||||||
|
|
||||||
|
def add_override(self, candidate):
|
||||||
|
"""Add a canddiate (first evaluated)"""
|
||||||
|
self.candidates.insert(0, candidate)
|
||||||
|
return self
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def path(self):
|
def path(self):
|
||||||
override = Path(self.win.schema.get_string(self.path_override_key))
|
"""Chosen path depending on availability on the disk."""
|
||||||
if override.exists():
|
for candidate in self.candidates:
|
||||||
return override
|
p = Path(candidate).expanduser()
|
||||||
return self.path_default
|
if p.exists:
|
||||||
|
return p
|
||||||
|
return None
|
||||||
@@ -27,16 +27,20 @@ class Source(Iterable):
|
|||||||
"""Source of games. Can be a program location on disk with a config file that points to game for example"""
|
"""Source of games. Can be a program location on disk with a config file that points to game for example"""
|
||||||
|
|
||||||
win = None
|
win = None
|
||||||
name: str = "GenericSource"
|
schema_keys: dict
|
||||||
variant: str = None
|
|
||||||
|
|
||||||
# Format to construct the executable command for a game.
|
name: str
|
||||||
# Available field names depend on the implementation
|
variant: str
|
||||||
executable_format: str
|
executable_format: str
|
||||||
|
|
||||||
def __init__(self, win) -> None:
|
def __init__(self, win) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.win = win
|
self.win = win
|
||||||
|
self.__init_schema_keys__()
|
||||||
|
|
||||||
|
def __init_schema_keys__(self):
|
||||||
|
"""Initialize schema keys needed by the source if necessary"""
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def full_name(self):
|
def full_name(self):
|
||||||
@@ -57,4 +61,9 @@ class Source(Iterable):
|
|||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
"""Get the source's iterator, to use in for loops"""
|
"""Get the source's iterator, to use in for loops"""
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def __init_locations__(self):
|
||||||
|
"""Initialize locations needed by the source.
|
||||||
|
Extended and called by **final** children."""
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
@@ -4,6 +4,7 @@ from sqlite3 import connect
|
|||||||
|
|
||||||
from cartridges.game2 import Game
|
from cartridges.game2 import Game
|
||||||
from cartridges.importer.source import Source, SourceIterator
|
from cartridges.importer.source import Source, SourceIterator
|
||||||
|
from cartridges.importer.location import Location
|
||||||
|
|
||||||
|
|
||||||
class LutrisSourceIterator(SourceIterator):
|
class LutrisSourceIterator(SourceIterator):
|
||||||
@@ -72,44 +73,50 @@ class LutrisSource(Source):
|
|||||||
|
|
||||||
name = "Lutris"
|
name = "Lutris"
|
||||||
executable_format = "xdg-open lutris:rungameid/{game_id}"
|
executable_format = "xdg-open lutris:rungameid/{game_id}"
|
||||||
|
schema_keys = {
|
||||||
|
"location": None,
|
||||||
|
"cache_location": None
|
||||||
|
}
|
||||||
|
|
||||||
location = None
|
def __init__(self, win) -> None:
|
||||||
cache_location = None
|
super().__init__(win)
|
||||||
|
self.location = Location()
|
||||||
|
self.cache_location = Location()
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return LutrisSourceIterator(source=self)
|
return LutrisSourceIterator(source=self)
|
||||||
|
|
||||||
# TODO find a way to no duplicate this code
|
def __init_locations__(self):
|
||||||
# Ideas:
|
super().__init_locations__()
|
||||||
# - Location class (verbose, set in __init__)
|
self.location.add_override(self.win.schema.get_string(self.schema_keys["location"]))
|
||||||
# - Schema key override decorator ()
|
self.cache_location.add_override(self.win.schema.get_string(self.schema_keys["cache_location"]))
|
||||||
|
|
||||||
# Lutris location property
|
|
||||||
@cached_property
|
|
||||||
def location(self):
|
|
||||||
ovr = Path(self.win.schema.get_string(self.location_key))
|
|
||||||
if ovr.exists(): return ovr
|
|
||||||
return self.location_default
|
|
||||||
|
|
||||||
# Lutris cache location property
|
|
||||||
@cached_property
|
|
||||||
def cache_location(self):
|
|
||||||
ovr = Path(self.win.schema.get_string(self.cache_location_key))
|
|
||||||
if ovr.exists(): return ovr
|
|
||||||
return self.cache_location_default
|
|
||||||
|
|
||||||
|
|
||||||
class LutrisNativeSource(LutrisSource):
|
class LutrisNativeSource(LutrisSource):
|
||||||
|
"""Class representing an installation of Lutris using native packaging"""
|
||||||
|
|
||||||
variant = "native"
|
variant = "native"
|
||||||
location_key = "lutris-location"
|
schema_keys = {
|
||||||
location_default = Path("~/.local/share/lutris/").expanduser()
|
"location": "lutris-location",
|
||||||
cache_location_key = "lutris-cache-location"
|
"cache_location": "lutris-cache-location"
|
||||||
cache_location_default = location_default / "covers"
|
}
|
||||||
|
|
||||||
|
def __init_locations__(self):
|
||||||
|
super().__init_locations__()
|
||||||
|
self.location.add("~/.local/share/lutris/")
|
||||||
|
self.cache_location.add("~/.local/share/lutris/covers")
|
||||||
|
|
||||||
|
|
||||||
class LutrisFlatpakSource(LutrisSource):
|
class LutrisFlatpakSource(LutrisSource):
|
||||||
|
"""Class representing an installation of Lutris using flatpak"""
|
||||||
|
|
||||||
variant = "flatpak"
|
variant = "flatpak"
|
||||||
location_key = "lutris-flatpak-location"
|
schema_keys = {
|
||||||
location_default = Path("~/.var/app/net.lutris.Lutris/data/lutris").expanduser()
|
"location": "lutris-flatpak-location",
|
||||||
cache_location_key = "lutris-flatpak-cache-location"
|
"cache_location": "lutris-flatpak-cache-location"
|
||||||
cache_location_default = location_default / "covers"
|
}
|
||||||
|
|
||||||
|
def __init_locations__(self):
|
||||||
|
super().__init_locations__()
|
||||||
|
self.location.add("~/.var/app/net.lutris.Lutris/data/lutris")
|
||||||
|
self.cache_location.add("~/.var/app/net.lutris.Lutris/data/lutris/covers")
|
||||||
Reference in New Issue
Block a user