🎨 Various code style / behaviour fixes
- Merged platform sources when possible - Added URLExecutableSource class - Moved replaced_by_schema_key to utils/decorators - Better retryable exception handling in some managers - Split SteamHelper into SteamFileHelper and SteamAPIHelper - Delegated SteamRateLimiter creation to SteamAPIManager init - Using additional_data for appid in SteamAPIManager - Added Windows support for Legendary - Stylistic changed suggested by pylint
This commit is contained in:
@@ -2,6 +2,8 @@ from pathlib import Path
|
||||
from os import PathLike, environ
|
||||
from functools import wraps
|
||||
|
||||
from src import shared
|
||||
|
||||
|
||||
def replaced_by_path(override: PathLike): # Decorator builder
|
||||
"""Replace the method's returned path with the override
|
||||
@@ -36,3 +38,18 @@ def replaced_by_env_path(env_var_name: str, suffix: PathLike | None = None):
|
||||
return wrapper
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
def replaced_by_schema_key(original_method): # Built decorator (closure)
|
||||
"""
|
||||
Replace the original method's value by the path pointed at in the schema
|
||||
by the class' location key (if that override exists)
|
||||
"""
|
||||
|
||||
@wraps(original_method)
|
||||
def wrapper(*args, **kwargs): # func's override
|
||||
source = args[0]
|
||||
override = shared.schema.get_string(source.location_key)
|
||||
return replaced_by_path(override)(original_method)(*args, **kwargs)
|
||||
|
||||
return wrapper
|
||||
|
||||
@@ -4,7 +4,7 @@ import re
|
||||
from typing import TypedDict
|
||||
|
||||
import requests
|
||||
from requests import HTTPError
|
||||
from requests.exceptions import HTTPError
|
||||
|
||||
from src import shared
|
||||
from src.utils.rate_limiter import PickHistory, RateLimiter
|
||||
@@ -27,7 +27,7 @@ class SteamInvalidManifestError(SteamError):
|
||||
|
||||
|
||||
class SteamManifestData(TypedDict):
|
||||
"""Dict returned by SteamHelper.get_manifest_data"""
|
||||
"""Dict returned by SteamFileHelper.get_manifest_data"""
|
||||
|
||||
name: str
|
||||
appid: str
|
||||
@@ -35,7 +35,7 @@ class SteamManifestData(TypedDict):
|
||||
|
||||
|
||||
class SteamAPIData(TypedDict):
|
||||
"""Dict returned by SteamHelper.get_api_data"""
|
||||
"""Dict returned by SteamAPIHelper.get_api_data"""
|
||||
|
||||
developers: str
|
||||
|
||||
@@ -73,34 +73,35 @@ class SteamRateLimiter(RateLimiter):
|
||||
shared.state_schema.set_string("steam-limiter-tokens-history", timestamps_str)
|
||||
|
||||
|
||||
class SteamHelper:
|
||||
"""Helper around the Steam API"""
|
||||
|
||||
base_url = "https://store.steampowered.com/api"
|
||||
rate_limiter: SteamRateLimiter = None
|
||||
|
||||
def __init__(self) -> None:
|
||||
# Instanciate the rate limiter on the class to share across instances
|
||||
# Can't be done at class creation time, schema isn't available yet
|
||||
if self.__class__.rate_limiter is None:
|
||||
self.__class__.rate_limiter = SteamRateLimiter()
|
||||
class SteamFileHelper:
|
||||
"""Helper for steam file formats"""
|
||||
|
||||
def get_manifest_data(self, manifest_path) -> SteamManifestData:
|
||||
"""Get local data for a game from its manifest"""
|
||||
|
||||
with open(manifest_path) as file:
|
||||
with open(manifest_path, "r", encoding="utf-8") as file:
|
||||
contents = file.read()
|
||||
|
||||
data = {}
|
||||
|
||||
for key in SteamManifestData.__required_keys__:
|
||||
regex = f'"{key}"\s+"(.*)"\n'
|
||||
for key in SteamManifestData.__required_keys__: # pylint: disable=no-member
|
||||
regex = f'"{key}"\\s+"(.*)"\n'
|
||||
if (match := re.search(regex, contents, re.IGNORECASE)) is None:
|
||||
raise SteamInvalidManifestError()
|
||||
data[key] = match.group(1)
|
||||
|
||||
return SteamManifestData(**data)
|
||||
|
||||
|
||||
class SteamAPIHelper:
|
||||
"""Helper around the Steam API"""
|
||||
|
||||
base_url = "https://store.steampowered.com/api"
|
||||
rate_limiter: RateLimiter
|
||||
|
||||
def __init__(self, rate_limiter: RateLimiter) -> None:
|
||||
self.rate_limiter = rate_limiter
|
||||
|
||||
def get_api_data(self, appid) -> SteamAPIData:
|
||||
"""
|
||||
Get online data for a game from its appid.
|
||||
|
||||
@@ -3,7 +3,7 @@ from pathlib import Path
|
||||
|
||||
import requests
|
||||
from gi.repository import Gio
|
||||
from requests import HTTPError
|
||||
from requests.exceptions import HTTPError
|
||||
|
||||
from src import shared
|
||||
from src.utils.create_dialog import create_dialog
|
||||
|
||||
Reference in New Issue
Block a user