Skip to content

Commit 1a8f008

Browse files
committed
Merge remote-tracking branch 'origin/pr/190'
* origin/pr/190: Remove deprecated pkg_resources and replace it with importlib.resources
2 parents e4c97a1 + 18c291e commit 1a8f008

File tree

10 files changed

+89
-51
lines changed

10 files changed

+89
-51
lines changed

qubes_config/global_config/global_config.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import sys
2424
from typing import Dict, Optional, List, Union
2525
from html import escape
26-
import pkg_resources
26+
import importlib.resources
2727
import logging
2828

2929
import qubesadmin
@@ -252,18 +252,19 @@ def perform_setup(self):
252252
self.progress_bar_dialog.update_progress(0)
253253

254254
self.builder = Gtk.Builder()
255-
self.builder.add_from_file(pkg_resources.resource_filename(
256-
'qubes_config', 'global_config.glade'))
255+
glade_ref = (importlib.resources.files('qubes_config') /
256+
'global_config.glade')
257+
with importlib.resources.as_file(glade_ref) as path:
258+
self.builder.add_from_file(str(path))
257259

258260
self.main_window: Gtk.Window = self.builder.get_object('main_window')
259261
self.main_notebook: Gtk.Notebook = \
260262
self.builder.get_object('main_notebook')
261263

262264
load_theme(widget=self.main_window,
263-
light_theme_path=pkg_resources.resource_filename(
264-
'qubes_config', 'qubes-global-config-light.css'),
265-
dark_theme_path=pkg_resources.resource_filename(
266-
'qubes_config', 'qubes-global-config-dark.css'))
265+
package_name='qubes_config',
266+
light_file_name='qubes-global-config-light.css',
267+
dark_file_name='qubes-global-config-dark.css')
267268

268269
self.apply_button: Gtk.Button = self.builder.get_object('apply_button')
269270
self.cancel_button: Gtk.Button = \

qubes_config/new_qube/new_qube_app.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import subprocess
2525
import sys
2626
from typing import Optional, Dict, Any
27-
import pkg_resources
27+
import importlib.resources
2828
import logging
2929

3030
import qubesadmin
@@ -93,19 +93,20 @@ def perform_setup(self):
9393
self.progress_bar_dialog.update_progress(0.1)
9494

9595
self.builder = Gtk.Builder()
96-
self.builder.add_from_file(pkg_resources.resource_filename(
97-
'qubes_config', 'new_qube.glade'))
96+
glade_ref = (importlib.resources.files('qubes_config') /
97+
'new_qube.glade')
98+
with importlib.resources.as_file(glade_ref) as path:
99+
self.builder.add_from_file(str(path))
98100

99101
self.main_window = self.builder.get_object('main_window')
100102
self.qube_name: Gtk.Entry = self.builder.get_object('qube_name')
101103
self.qube_label_combo: Gtk.ComboBox = \
102104
self.builder.get_object('qube_label')
103105

104106
load_theme(widget=self.main_window,
105-
light_theme_path=pkg_resources.resource_filename(
106-
'qubes_config', 'qubes-new-qube-light.css'),
107-
dark_theme_path=pkg_resources.resource_filename(
108-
'qubes_config', 'qubes-new-qube-dark.css'))
107+
package_name='qubes_config',
108+
light_file_name='qubes-new-qube-light.css',
109+
dark_file_name='qubes-new-qube-dark.css')
109110

110111
self.progress_bar_dialog.update_progress(0.1)
111112

qubes_config/policy_editor/policy_editor.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from typing import Callable, Optional, Dict, Tuple
2525

2626
import gi
27-
import pkg_resources
27+
import importlib.resources
2828

2929
import qubesadmin
3030
from qrexec.policy.admin_client import PolicyClient
@@ -171,8 +171,11 @@ def perform_setup(self):
171171
Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
172172

173173
self.builder = Gtk.Builder()
174-
self.builder.add_from_file(pkg_resources.resource_filename(
175-
'qubes_config', 'policy_editor.glade'))
174+
175+
glade_ref = (importlib.resources.files('qubes_config') /
176+
'policy_editor.glade')
177+
with importlib.resources.as_file(glade_ref) as path:
178+
self.builder.add_from_file(str(path))
176179

177180
self.file_select_handler = OpenDialogHandler(self.builder,
178181
self.policy_client,
@@ -218,16 +221,14 @@ def perform_setup(self):
218221
self.setup_actions()
219222
self.setup_menu()
220223

221-
load_theme(widget=self.main_window,
222-
light_theme_path=pkg_resources.resource_filename(
223-
'qubes_config', 'qubes-policy-editor-light.css'),
224-
dark_theme_path=pkg_resources.resource_filename(
225-
'qubes_config', 'qubes-policy-editor-dark.css'))
224+
load_theme(widget=self.main_window, package_name='qubes_config',
225+
light_file_name='qubes-policy-editor-light.css',
226+
dark_file_name='qubes-policy-editor-dark.css')
226227

227228
self.setup_source()
228-
229-
help_text = pkg_resources.resource_string(
230-
'qubes_config', 'policy_editor/policy_help.txt').decode()
229+
help_text = importlib.resources.files(
230+
'qubes_config').joinpath(
231+
'policy_editor/policy_help.txt').read_text()
231232
self.builder.get_object("help_label").set_markup(help_text)
232233

233234
self.open_policy_file(self.filename)

qubes_config/tests/conftest.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"""Conftest helper pytest file: fixtures container here are
2121
reachable by all tests"""
2222
import pytest
23-
import pkg_resources
23+
import importlib.resources
2424
import subprocess
2525
from typing import Mapping, Union, Tuple, List
2626
from qubesadmin.tests import QubesTest
@@ -436,8 +436,10 @@ def test_builder():
436436
SIGNALS_REGISTERED = True
437437
# test glade file contains very simple setup with correctly named widgets
438438
builder = Gtk.Builder()
439-
builder.add_from_file(pkg_resources.resource_filename(
440-
__name__, 'test.glade'))
439+
glade_ref = (importlib.resources.files('qubes_config') /
440+
'tests/test.glade')
441+
with importlib.resources.as_file(glade_ref) as path:
442+
builder.add_from_file(str(path))
441443
return builder
442444

443445
@pytest.fixture
@@ -450,8 +452,10 @@ def real_builder():
450452
SIGNALS_REGISTERED = True
451453
# test glade file contains very simple setup with correctly named widgets
452454
builder = Gtk.Builder()
453-
builder.add_from_file(pkg_resources.resource_filename(
454-
'qubes_config', 'global_config.glade'))
455+
glade_ref = (importlib.resources.files('qubes_config') /
456+
'global_config.glade')
457+
with importlib.resources.as_file(glade_ref) as path:
458+
builder.add_from_file(str(path))
455459
return builder
456460

457461

@@ -468,8 +472,10 @@ def new_qube_builder():
468472
NEW_QUBE_SIGNALS_REGISTERED = True
469473
# test glade file contains very simple setup with correctly named widgets
470474
builder = Gtk.Builder()
471-
builder.add_from_file(pkg_resources.resource_filename(
472-
'qubes_config', 'new_qube.glade'))
475+
glade_ref = (importlib.resources.files('qubes_config') /
476+
'new_qube.glade')
477+
with importlib.resources.as_file(glade_ref) as path:
478+
builder.add_from_file(str(path))
473479
return builder
474480

475481

qubes_config/widgets/gtk_utils.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# with this program; if not, see <http://www.gnu.org/licenses/>.
2020
"""Utility functions using Gtk"""
2121
import contextlib
22+
import importlib.resources
2223
import os
2324

2425
import fcntl
@@ -187,14 +188,31 @@ def show_dialog(
187188
return dialog
188189

189190

190-
def load_theme(widget: Gtk.Widget, light_theme_path: str, dark_theme_path: str):
191+
def load_theme(widget: Gtk.Widget, light_theme_path: Optional[str] = None,
192+
dark_theme_path: Optional[str] = None,
193+
package_name: Optional[str] = None,
194+
light_file_name: Optional[str] = None,
195+
dark_file_name: Optional[str] = None):
191196
"""
192197
Load a dark or light theme to current screen, based on widget's
193198
current (system) defaults.
194199
:param widget: Gtk.Widget, preferably main window
195200
:param light_theme_path: path to file with light theme css
196201
:param dark_theme_path: path to file with dark theme css
202+
Alternatively, you can provide:
203+
:param package_name: name of the package
204+
:param light_file_name: name of the css file with light theme
205+
:param dark_file_name: name of the css file with dark theme
197206
"""
207+
if not light_theme_path and light_file_name:
208+
css_path = importlib.resources.files(package_name) / light_file_name
209+
with importlib.resources.as_file(css_path) as resource_path:
210+
light_theme_path = str(resource_path)
211+
if not dark_theme_path and dark_file_name:
212+
css_path = importlib.resources.files(package_name) / dark_file_name
213+
with importlib.resources.as_file(css_path) as resource_path:
214+
dark_theme_path = str(resource_path)
215+
198216
path = light_theme_path if is_theme_light(widget) else dark_theme_path
199217

200218
screen = Gdk.Screen.get_default()

qui/devices/device_widget.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import asyncio
2222
import sys
2323

24-
import pkg_resources
24+
import importlib.resources
2525

2626
import qubesadmin
2727
import qubesadmin.exc
@@ -281,8 +281,11 @@ def load_css(widget) -> str:
281281
theme = 'light' if is_theme_light(widget) else 'dark'
282282
screen = Gdk.Screen.get_default()
283283
provider = Gtk.CssProvider()
284-
provider.load_from_path(pkg_resources.resource_filename(
285-
'qui', f'qubes-devices-{theme}.css'))
284+
css_file_ref = (importlib.resources.files('qui') /
285+
f'qubes-devices-{theme}.css')
286+
with importlib.resources.as_file(css_file_ref) as css_file:
287+
provider.load_from_path(str(css_file))
288+
286289
Gtk.StyleContext.add_provider_for_screen(
287290
screen, provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
288291

qui/updater/tests/conftest.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"""Conftest helper pytest file: fixtures container here are
2121
reachable by all tests"""
2222
import pytest
23-
import pkg_resources
23+
import importlib.resources
2424

2525
from qubes_config.tests.conftest import add_dom0_vm_property, \
2626
add_dom0_text_property, add_dom0_feature, add_expected_vm, \
@@ -141,8 +141,10 @@ def real_builder():
141141
"""Gtk builder with actual config glade file registered"""
142142
builder = Gtk.Builder()
143143
builder.set_translation_domain("desktop-linux-manager")
144-
builder.add_from_file(pkg_resources.resource_filename(
145-
'qui', 'updater.glade'))
144+
glade_ref = (importlib.resources.files('qui') /
145+
'updater.glade')
146+
with importlib.resources.as_file(glade_ref) as path:
147+
self.builder.add_from_file(str(path))
146148
return builder
147149

148150

qui/updater/updater.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import logging
66
import time
77

8-
import pkg_resources
8+
import importlib.resources
99
import gi # isort:skip
1010

1111
from qubes_config.widgets.gtk_utils import load_icon_at_gtk_size, load_theme, \
@@ -78,8 +78,11 @@ def perform_setup(self, *_args, **_kwargs):
7878
# pylint: disable=attribute-defined-outside-init
7979
self.builder = Gtk.Builder()
8080
self.builder.set_translation_domain("desktop-linux-manager")
81-
self.builder.add_from_file(pkg_resources.resource_filename(
82-
'qui', 'updater.glade'))
81+
82+
glade_ref = (importlib.resources.files('qui') /
83+
'updater.glade')
84+
with importlib.resources.as_file(glade_ref) as path:
85+
self.builder.add_from_file(str(path))
8386

8487
self.main_window: Gtk.Window = self.builder.get_object("main_window")
8588
self.next_button: Gtk.Button = self.builder.get_object("button_next")
@@ -89,10 +92,9 @@ def perform_setup(self, *_args, **_kwargs):
8992
self.cancel_button.connect("clicked", self.cancel_clicked)
9093

9194
load_theme(widget=self.main_window,
92-
light_theme_path=pkg_resources.resource_filename(
93-
'qui', 'qubes-updater-light.css'),
94-
dark_theme_path=pkg_resources.resource_filename(
95-
'qui', 'qubes-updater-dark.css'))
95+
package_name='qui',
96+
light_file_name='qubes-updater-light.css',
97+
dark_file_name='qubes-updater-dark.css')
9698

9799
self.header_label: Gtk.Label = self.builder.get_object("header_label")
98100

qui/updater/updater_settings.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import dataclasses
2222
from typing import Optional, Union, Callable
2323

24-
import pkg_resources
24+
import importlib.resources
2525
import gi
2626

2727
gi.require_version('Gtk', '3.0') # isort:skip
@@ -68,8 +68,11 @@ def __init__(
6868

6969
self.builder = Gtk.Builder()
7070
self.builder.set_translation_domain("desktop-linux-manager")
71-
self.builder.add_from_file(pkg_resources.resource_filename(
72-
'qui', 'updater_settings.glade'))
71+
72+
glade_ref = (importlib.resources.files('qui') /
73+
'updater_settings.glade')
74+
with importlib.resources.as_file(glade_ref) as path:
75+
self.builder.add_from_file(str(path))
7376

7477
self.settings_window: Gtk.Window = self.builder.get_object(
7578
"main_window")

qui/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
import gettext
2929

30-
import pkg_resources
30+
import importlib.resources
3131
from datetime import datetime
3232

3333
t = gettext.translation("desktop-linux-manager", fallback=True)
@@ -37,7 +37,8 @@
3737
gi.require_version('Gtk', '3.0') # isort:skip
3838
from gi.repository import Gtk # isort:skip
3939

40-
EOL_DATES = json.load(pkg_resources.resource_stream(__name__, 'eol.json'))
40+
with importlib.resources.files('qui').joinpath('eol.json').open() as stream:
41+
EOL_DATES = json.load(stream)
4142
# remove the following suffixes when checking for EOL
4243
SUFFIXES = ['-minimal', '-xfce']
4344

0 commit comments

Comments
 (0)