♻️ Restructured sources and managers

This commit is contained in:
GeoffreyCoulaud
2023-05-23 17:00:47 +02:00
parent 95524563bb
commit a11569014d
10 changed files with 17 additions and 24 deletions

View File

@@ -1,12 +1,12 @@
from abc import abstractmethod
from functools import lru_cache
from pathlib import Path
from sqlite3 import connect
from time import time
from pathlib import Path
import src.shared as shared
from src.game import Game
from src.importer.source import Source, SourceIterator
from src.importer.sources.source import Source, SourceIterator
from src.utils.decorators import replaced_by_path, replaced_by_schema_key
from src.utils.save_cover import resize_cover, save_cover

View File

@@ -0,0 +1,75 @@
from abc import abstractmethod
from collections.abc import Iterable, Iterator, Sized
from typing import Optional
from src.game import Game
class SourceIterator(Iterator, Sized):
"""Data producer for a source of games"""
source: "Source" = None
def __init__(self, source: "Source") -> None:
super().__init__()
self.source = source
def __iter__(self) -> "SourceIterator":
return self
@abstractmethod
def __len__(self) -> int:
"""Get a rough estimate of the number of games produced by the source"""
@abstractmethod
def __next__(self) -> Optional[Game]:
"""Get the next generated game from the source.
Raises StopIteration when exhausted.
May raise any other exception signifying an error on this specific game.
May return None when a game has been skipped without an error."""
class Source(Iterable):
"""Source of games. E.g an installed app with a config file that lists game directories"""
name: str
variant: str
@property
def full_name(self) -> str:
"""The source's full name"""
full_name_ = self.name
if self.variant is not None:
full_name_ += f" ({self.variant})"
return full_name_
@property
def id(self) -> str: # pylint: disable=invalid-name
"""The source's identifier"""
id_ = self.name.lower()
if self.variant is not None:
id_ += f"_{self.variant.lower()}"
return id_
@property
def game_id_format(self) -> str:
"""The string format used to construct game IDs"""
format_ = self.name.lower()
if self.variant is not None:
format_ += f"_{self.variant.lower()}"
format_ += "_{game_id}"
return format_
@property
@abstractmethod
def executable_format(self) -> str:
"""The executable format used to construct game executables"""
@property
@abstractmethod
def is_installed(self) -> bool:
"""Whether the source is detected as installed"""
@abstractmethod
def __iter__(self) -> SourceIterator:
"""Get the source's iterator, to use in for loops"""

View File

@@ -4,22 +4,15 @@ from pathlib import Path
from time import time
from typing import Iterator
from requests import HTTPError, JSONDecodeError
from src.game import Game
from src.importer.source import Source, SourceIterator
from src.importer.sources.source import Source, SourceIterator
from src.utils.decorators import (
replaced_by_env_path,
replaced_by_path,
replaced_by_schema_key,
)
from src.utils.save_cover import resize_cover, save_cover
from src.utils.steam import (
SteamGameNotFoundError,
SteamHelper,
SteamInvalidManifestError,
SteamNotAGameError,
)
from src.utils.steam import SteamHelper, SteamInvalidManifestError
class SteamSourceIterator(SourceIterator):