Skip to content

Commit

Permalink
Improved type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
Root-Core committed Dec 17, 2024
1 parent a2c80f7 commit fd8b058
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
5 changes: 3 additions & 2 deletions fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import csv
from functools import lru_cache
from importlib import import_module
from typing import Optional

try:
from . import config
Expand Down Expand Up @@ -88,7 +89,7 @@ def get_game_name() -> str:
return 'UNKNOWN'


def get_store_name(store: str) -> str:
def get_store_name(store: str) -> Optional[str]:
"""Mapping for store identifier to store name"""
return {
'amazon': 'Amazon',
Expand All @@ -110,7 +111,7 @@ def get_module_name(game_id: str, default: bool = False, local: bool = False) ->
if game_id.isnumeric():
store = 'steam'
elif os.environ.get('STORE'):
store = os.environ.get('STORE').lower()
store = os.environ.get('STORE', '').lower()

if store != 'steam':
log.info(f'Non-steam game {get_game_name()} ({game_id})')
Expand Down
27 changes: 15 additions & 12 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import urllib.request
import functools
from socket import socket, AF_INET, SOCK_DGRAM
from typing import Literal, Any, Callable, Union
from typing import Literal, Any, Callable, Union, Optional
from collections.abc import Mapping, Generator

try:
Expand All @@ -28,8 +28,11 @@
except ImportError:
log.warn('Unable to hook into Proton main script environment')

# TypeAliases
BasePathType = Literal['user', 'game']

def which(appname: str) -> Union[str, None]:

def which(appname: str) -> Optional[str]:
"""Returns the full path of an executable in $PATH"""
for path in os.environ['PATH'].split(os.pathsep):
fullpath = os.path.join(path, appname)
Expand All @@ -50,7 +53,7 @@ def protonprefix() -> str:
return os.path.join(os.environ['STEAM_COMPAT_DATA_PATH'], 'pfx/')


def protonnameversion() -> Union[str, None]:
def protonnameversion() -> Optional[str]:
"""Returns the version of proton from sys.argv[0]"""
version = re.search('Proton ([0-9]*\\.[0-9]*)', sys.argv[0])
if version:
Expand All @@ -73,16 +76,16 @@ def protontimeversion() -> int:
return 0


def protonversion(timestamp: bool = False) -> Union[str, None, int]:
def protonversion(timestamp: bool = False) -> Optional[Union[str, int]]:
"""Returns the version of proton"""
if timestamp:
return protontimeversion()
return protonnameversion()


def once(
func: Union[Callable, None] = None, retry: bool = False
) -> Union[None, Callable[..., Any]]:
func: Optional[Callable] = None, retry: bool = False
) -> Callable[..., Any]:
"""Decorator to use on functions which should only run once in a prefix.
Error handling:
Expand Down Expand Up @@ -292,9 +295,9 @@ def protontricks(verb: str) -> bool:

def regedit_add(
folder: str,
name: Union[str, None] = None,
typ: Union[str, None] = None,
value: Union[str, None] = None,
name: Optional[str] = None,
typ: Optional[str] = None,
value: Optional[str] = None,
arch: bool = False,
) -> None:
"""Add regedit keys"""
Expand Down Expand Up @@ -643,7 +646,7 @@ def _get_case_insensitive_name(path: str) -> str:
return root


def _get_config_full_path(cfile: str, base_path: str) -> Union[str, None]:
def _get_config_full_path(cfile: str, base_path: BasePathType) -> Optional[str]:
"""Find game's config file"""
# Start from 'user'/'game' directories or absolute path
if base_path == 'user':
Expand Down Expand Up @@ -674,7 +677,7 @@ def create_backup_config(cfg_path: str) -> None:


def set_ini_options(
ini_opts: str, cfile: str, encoding: str, base_path: str = 'user'
ini_opts: str, cfile: str, encoding: str, base_path: BasePathType = 'user'
) -> bool:
"""Edit game's INI config file"""
cfg_path = _get_config_full_path(cfile, base_path)
Expand All @@ -700,7 +703,7 @@ def set_ini_options(


def set_xml_options(
base_attibutte: str, xml_line: str, cfile: str, base_path: str = 'user'
base_attibutte: str, xml_line: str, cfile: str, base_path: BasePathType = 'user'
) -> bool:
"""Edit game's XML config file"""
xml_path = _get_config_full_path(cfile, base_path)
Expand Down

0 comments on commit fd8b058

Please sign in to comment.