Skip to content

Commit 6f9e769

Browse files
committed
Migrate to importlib.resources
`pkg_resources` is set to be deprecated and removed in the future. Use the `importlib.resources` standard library module instead to retrieve resources such as the placeholder icon and script templates.
1 parent c1664e5 commit 6f9e769

File tree

8 files changed

+36
-42
lines changed

8 files changed

+36
-42
lines changed

setup.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ package_dir =
2828
= src
2929
include_package_data = True
3030
install_requires =
31-
setuptools # Required for pkg_resources
3231
vdf>=3.2
3332
Pillow
3433
setup_requires =

src/protontricks/cli/desktop_install.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pathlib import Path
44
from subprocess import run
55

6-
import pkg_resources
6+
import importlib.resources
77

88
from .util import CustomArgumentParser
99

@@ -20,16 +20,19 @@ def install_desktop_entries():
2020
applications_dir = Path.home() / ".local" / "share" / "applications"
2121
applications_dir.mkdir(parents=True, exist_ok=True)
2222

23-
run([
24-
"desktop-file-install", "--dir", str(applications_dir),
25-
pkg_resources.resource_filename(
26-
"protontricks", "data/share/applications/protontricks.desktop"
27-
),
28-
pkg_resources.resource_filename(
29-
"protontricks",
30-
"data/share/applications/protontricks-launch.desktop"
31-
)
32-
], check=True)
23+
desktop_path_resolver = importlib.resources.path(
24+
"protontricks.data.share.applications", "protontricks.desktop"
25+
)
26+
launch_path_resolver = importlib.resources.path(
27+
"protontricks.data.share.applications", "protontricks-launch.desktop"
28+
)
29+
30+
with desktop_path_resolver as desktop_path, \
31+
launch_path_resolver as launch_path:
32+
run([
33+
"desktop-file-install", "--dir", str(applications_dir),
34+
str(desktop_path), str(launch_path)
35+
], check=True)
3336

3437
return applications_dir
3538

src/protontricks/data/__init__.py

Whitespace-only changes.

src/protontricks/data/data/__init__.py

Whitespace-only changes.

src/protontricks/data/scripts/__init__.py

Whitespace-only changes.

src/protontricks/data/share/applications/__init__.py

Whitespace-only changes.

src/protontricks/gui.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import functools
2+
import importlib.resources
23
import itertools
34
import json
45
import logging
@@ -9,13 +10,12 @@
910
from pathlib import Path
1011
from subprocess import PIPE, CalledProcessError, run
1112

12-
import pkg_resources
1313
from PIL import Image
1414

1515
from .config import get_config
1616
from .flatpak import get_inaccessible_paths
17-
from .util import get_cache_dir
1817
from .steam import SNAP_STEAM_DIRS
18+
from .util import get_cache_dir
1919

2020
APP_ICON_SIZE = (32, 32)
2121

@@ -60,15 +60,16 @@ def _get_appid2icon(steam_apps):
6060
Get icons for Steam apps to show in the app selection dialog.
6161
Return a {appid: icon_path} dict.
6262
"""
63-
placeholder_path = Path(
64-
pkg_resources.resource_filename(
65-
"protontricks", "data/data/icon_placeholder.png"
66-
)
67-
)
68-
6963
protontricks_icon_dir = get_cache_dir() / "app_icons"
7064
protontricks_icon_dir.mkdir(exist_ok=True)
7165

66+
# Write the placeholder from Python package into a more persistent
67+
# cache directory
68+
with importlib.resources.path(
69+
"protontricks.data.data", "icon_placeholder.png") as path:
70+
placeholder_path = protontricks_icon_dir / "icon_placeholder.png"
71+
placeholder_path.write_bytes(path.read_bytes())
72+
7273
appid2icon = {}
7374

7475
for app in steam_apps:

src/protontricks/util.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import importlib.resources
12
import itertools
23
import locale
34
import logging
@@ -9,8 +10,6 @@
910
from pathlib import Path
1011
from subprocess import DEVNULL, PIPE, Popen, TimeoutExpired, check_output, run
1112

12-
import pkg_resources
13-
1413
__all__ = (
1514
"SUPPORTED_STEAM_RUNTIMES", "OS_RELEASE_PATHS", "lower_dict",
1615
"is_steam_deck", "is_steamos", "get_legacy_runtime_library_paths",
@@ -173,26 +172,18 @@ def find_runtime_app_root(runtime_app):
173172
])
174173

175174

176-
WINE_SCRIPT_TEMPLATE = Path(
177-
pkg_resources.resource_filename(
178-
"protontricks", "data/scripts/wine_launch.sh"
179-
)
180-
).read_text(encoding="utf-8")
181-
WINESERVER_KEEPALIVE_SH_SCRIPT = Path(
182-
pkg_resources.resource_filename(
183-
"protontricks", "data/scripts/wineserver_keepalive.sh"
184-
)
185-
).read_text(encoding="utf-8")
186-
WINESERVER_KEEPALIVE_BATCH_SCRIPT = Path(
187-
pkg_resources.resource_filename(
188-
"protontricks", "data/scripts/wineserver_keepalive.bat"
189-
)
190-
).read_text(encoding="utf-8")
191-
BWRAP_LAUNCHER_SH_SCRIPT = Path(
192-
pkg_resources.resource_filename(
193-
"protontricks", "data/scripts/bwrap_launcher.sh"
194-
)
195-
).read_text(encoding="utf-8")
175+
WINE_SCRIPT_TEMPLATE = importlib.resources.read_text(
176+
"protontricks.data.scripts", "wine_launch.sh", encoding="utf-8"
177+
)
178+
WINESERVER_KEEPALIVE_SH_SCRIPT = importlib.resources.read_text(
179+
"protontricks.data.scripts", "wineserver_keepalive.sh", encoding="utf-8"
180+
)
181+
WINESERVER_KEEPALIVE_BATCH_SCRIPT = importlib.resources.read_text(
182+
"protontricks.data.scripts", "wineserver_keepalive.bat", encoding="utf-8"
183+
)
184+
BWRAP_LAUNCHER_SH_SCRIPT = importlib.resources.read_text(
185+
"protontricks.data.scripts", "bwrap_launcher.sh", encoding="utf-8"
186+
)
196187

197188

198189
def get_cache_dir():

0 commit comments

Comments
 (0)