-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: prefer pathlib when possible #183
base: master
Are you sure you want to change the base?
Changes from all commits
7cc7b8e
27c12c0
694095b
4d963e7
de0138d
6818b6b
4493863
87a9b84
325242b
3215ea1
52a9352
5d5dbe0
e1baf78
ac892cc
32d7d45
3a24d9f
47cc736
0bd33ac
e52d935
45c5dd2
427ea03
efe0915
95b96fa
2a5128e
7ec6ed8
2492ddf
aae2e64
5647852
b84fa75
ab03aaf
290c6d7
c1bb4a9
ae0265f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,42 +42,45 @@ def get_game_id() -> str: | |
def get_game_name() -> str: | ||
"""Trys to return the game name from environment variables""" | ||
pfx = os.environ.get('WINEPREFIX') or protonmain.g_session.env.get('WINEPREFIX') | ||
script_dir = os.path.dirname(os.path.abspath(__file__)) | ||
script_dir = os.path.dirname(os.path.abspath(__file__)) # noqa: PTH100, PTH120 | ||
|
||
if os.environ.get('UMU_ID'): | ||
|
||
if os.path.isfile(f'{pfx}/game_title'): | ||
with open(f'{pfx}/game_title', encoding='utf-8') as file: | ||
if os.path.isfile(f'{pfx}/game_title'): # noqa: PTH113 | ||
with open(f'{pfx}/game_title', encoding='utf-8') as file: # noqa: PTH123 | ||
return file.readline() | ||
|
||
umu_id = os.environ['UMU_ID'] | ||
store = os.getenv('STORE', 'none') | ||
csv_file_path = os.path.join(script_dir, 'umu-database.csv') | ||
csv_file_path = os.path.join(script_dir, 'umu-database.csv') # noqa: PTH118 | ||
|
||
try: | ||
with open(csv_file_path, newline='', encoding='utf-8') as csvfile: | ||
with open(csv_file_path, newline='', encoding='utf-8') as csvfile: # noqa: PTH123 | ||
csvreader = csv.reader(csvfile) | ||
for row in csvreader: | ||
# Check if the row has enough columns and matches both UMU_ID and STORE | ||
if len(row) > 3 and row[3] == umu_id and row[1] == store: | ||
title = row[0] # Title is the first entry | ||
with open(os.path.join(script_dir, 'game_title'), 'w', encoding='utf-8') as file: | ||
with open( # noqa: PTH123 | ||
os.path.join(script_dir, 'game_title'), # noqa: PTH118 | ||
'w', | ||
encoding='utf-8', | ||
) as file: | ||
file.write(title) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be a bug. If I'm understanding correctly, we're not writing the title in the user's prefix. Instead, the title's is in the protonfixes directory. |
||
return title | ||
except FileNotFoundError: | ||
log.warn(f"CSV file not found: {csv_file_path}") | ||
log.warn(f'CSV file not found: {csv_file_path}') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just style changes applied from ruff format. |
||
except Exception as ex: | ||
log.debug(f"Error reading CSV file: {ex}") | ||
log.debug(f'Error reading CSV file: {ex}') | ||
|
||
log.warn("Game title not found in CSV") | ||
log.warn('Game title not found in CSV') | ||
return 'UNKNOWN' | ||
|
||
try: | ||
log.debug('UMU_ID is not in environment') | ||
game_library = re.findall(r'.*/steamapps', os.environ['PWD'], re.IGNORECASE)[-1] | ||
game_manifest = os.path.join(game_library, f'appmanifest_{get_game_id()}.acf') | ||
game_manifest = os.path.join(game_library, f'appmanifest_{get_game_id()}.acf') # noqa: PTH118 | ||
|
||
with open(game_manifest, encoding='utf-8') as appmanifest: | ||
with open(game_manifest, encoding='utf-8') as appmanifest: # noqa: PTH123 | ||
for xline in appmanifest.readlines(): | ||
if 'name' in xline.strip(): | ||
name = re.findall(r'"[^"]+"', xline, re.UNICODE)[-1] | ||
|
@@ -129,16 +132,16 @@ def get_module_name(game_id: str, default: bool = False, local: bool = False) -> | |
|
||
def _run_fix_local(game_id: str, default: bool = False) -> bool: | ||
"""Check if a local gamefix is available first and run it""" | ||
localpath = os.path.expanduser('~/.config/protonfixes/localfixes') | ||
localpath = os.path.expanduser('~/.config/protonfixes/localfixes') # noqa: PTH111 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactoring this line and below as pathlib.Path objects should be safe. |
||
module_name = game_id if not default else 'default' | ||
|
||
# Check if local gamefix exists | ||
if not os.path.isfile(os.path.join(localpath, module_name + '.py')): | ||
if not os.path.isfile(os.path.join(localpath, module_name + '.py')): # noqa: PTH113, PTH118 | ||
return False | ||
|
||
# Ensure local gamefixes are importable as modules via PATH | ||
with open(os.path.join(localpath, '__init__.py'), 'a', encoding='utf-8'): | ||
sys.path.append(os.path.expanduser('~/.config/protonfixes')) | ||
with open(os.path.join(localpath, '__init__.py'), 'a', encoding='utf-8'): # noqa: PTH118, PTH123 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we even have to open the file here? |
||
sys.path.append(os.path.expanduser('~/.config/protonfixes')) # noqa: PTH111 | ||
|
||
# Run fix | ||
return _run_fix(game_id, default, True) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CSV is a database for us so we should handle the CSV by assuming it's very large.
TODO: Refactor to prefer memory mapping the CSV in a future PR.