Skip to content
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

migrate from pkg_resources to importlib.resources #38

1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
0.15.9
- ref: migrate from pkg_resources to importlib.resources (#33)
RaghavaAlajangi marked this conversation as resolved.
Show resolved Hide resolved
- build: fix installation directory
- build: disable unsigned installer
0.15.8
Expand Down
16 changes: 8 additions & 8 deletions pyjibe/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
def main(splash=True):
import os
import pkg_resources
import importlib.resources

Check warning on line 2 in pyjibe/__main__.py

View check run for this annotation

Codecov / codecov/patch

pyjibe/__main__.py#L2

Added line #L2 was not covered by tests
import sys

from PyQt5.QtWidgets import QApplication
Expand All @@ -10,14 +9,14 @@
# Note:
RaghavaAlajangi marked this conversation as resolved.
Show resolved Hide resolved
# Having the image file *not* in a submodule of PyJibe
# seems to cause the splash to display earlier, because
# presumably `pkg_resources` internally imports modules.
imdir = pkg_resources.resource_filename("pyjibe", "img")
# presumably `importlib.resources` internally imports modules.

if splash:
from PyQt5.QtWidgets import QSplashScreen
from PyQt5.QtGui import QPixmap
splash_path = os.path.join(imdir, "splash.png")
splash_pix = QPixmap(splash_path)
ref = importlib.resources.files("pyjibe.img") / "splash.png"
with importlib.resources.as_file(ref) as splash_path:
splash_pix = QPixmap(str(splash_path))

Check warning on line 19 in pyjibe/__main__.py

View check run for this annotation

Codecov / codecov/patch

pyjibe/__main__.py#L17-L19

Added lines #L17 - L19 were not covered by tests
splash = QSplashScreen(splash_pix)
splash.setMask(splash_pix.mask())
splash.show()
Expand All @@ -28,8 +27,9 @@
from .head import PyJibe

# Set Application Icon
icon_path = os.path.join(imdir, "icon.png")
app.setWindowIcon(QtGui.QIcon(icon_path))
ref = importlib.resources.files("pyjibe.img") / "icon.png"
with importlib.resources.as_file(ref) as icon_path:
app.setWindowIcon(QtGui.QIcon(str(icon_path)))

Check warning on line 32 in pyjibe/__main__.py

View check run for this annotation

Codecov / codecov/patch

pyjibe/__main__.py#L30-L32

Added lines #L30 - L32 were not covered by tests

# Use dots as decimal separators
QtCore.QLocale.setDefault(QtCore.QLocale(QtCore.QLocale.C))
Expand Down
9 changes: 5 additions & 4 deletions pyjibe/fd/dlg_export_vals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pkg_resources
import importlib.resources

from PyQt5 import uic, QtWidgets

Expand All @@ -11,9 +11,10 @@
def __init__(self, parent, fdist_list, identifier, *args, **kwargs):
"""Base class for force-indentation analysis"""
super(ExportDialog, self).__init__(parent=parent, *args, **kwargs)
path_ui = pkg_resources.resource_filename("pyjibe.fd",
"dlg_export_vals.ui")
uic.loadUi(path_ui, self)

ref = importlib.resources.files("pyjibe.fd") / "ana_filter.ui"
RaghavaAlajangi marked this conversation as resolved.
Show resolved Hide resolved
with importlib.resources.as_file(ref) as path_ui:
uic.loadUi(path_ui, self)

Check warning on line 17 in pyjibe/fd/dlg_export_vals.py

View check run for this annotation

Codecov / codecov/patch

pyjibe/fd/dlg_export_vals.py#L15-L17

Added lines #L15 - L17 were not covered by tests

self.fdist_list = fdist_list
self.identifier = identifier
Expand Down
13 changes: 7 additions & 6 deletions pyjibe/fd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import io
import os
import pathlib
import pkg_resources
import importlib.resources
import time

import afmformats.errors
Expand All @@ -22,9 +22,9 @@


# load QWidget from ui file
dlg_autosave_path = pkg_resources.resource_filename("pyjibe.fd",
"dlg_autosave_design.ui")
DlgAutosave = uic.loadUiType(dlg_autosave_path)[0]
dlg_ref = importlib.resources.files("pyjibe.fd") / "dlg_autosave_design.ui"
with importlib.resources.as_file(dlg_ref) as dlg_autosave_path:
DlgAutosave = uic.loadUiType(dlg_autosave_path)[0]


class UiForceDistance(QtWidgets.QWidget):
Expand All @@ -33,8 +33,9 @@ class UiForceDistance(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
"""Base class for force-indentation analysis"""
super(UiForceDistance, self).__init__(*args, **kwargs)
path_ui = pkg_resources.resource_filename("pyjibe.fd", "main.ui")
uic.loadUi(path_ui, self)
ref = importlib.resources.files("pyjibe.fd") / "main.ui"
with importlib.resources.as_file(ref) as path_ui:
uic.loadUi(path_ui, self)

self.settings = QtCore.QSettings()
self.settings.setIniCodec("utf-8")
Expand Down
8 changes: 4 additions & 4 deletions pyjibe/fd/rating_iface.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import os
import pathlib
import pkg_resources
import importlib.resources

from nanite.rate import io as nio
from PyQt5 import uic, QtCore, QtWidgets


# load QWidget from ui file
ui_path = pkg_resources.resource_filename("pyjibe.fd",
"rating_iface.ui")
UiUserRatingBase = uic.loadUiType(ui_path)[0]
ui_ref = importlib.resources.files("pyjibe.fd") / "rating_iface.ui"
with importlib.resources.as_file(ui_ref) as path_ui:
UiUserRatingBase = uic.loadUiType(path_ui)[0]


class Rater(QtWidgets.QWidget, UiUserRatingBase):
Expand Down
8 changes: 4 additions & 4 deletions pyjibe/fd/tab_edelta.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pkg_resources
import importlib.resources

import numpy as np
from PyQt5 import uic, QtCore, QtWidgets
Expand All @@ -10,9 +10,9 @@
class TabEdelta(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super(TabEdelta, self).__init__(*args, **kwargs)
path_ui = pkg_resources.resource_filename("pyjibe.fd",
"tab_edelta.ui")
uic.loadUi(path_ui, self)
ref = importlib.resources.files("pyjibe.fd") / "tab_edelta.ui"
with importlib.resources.as_file(ref) as path_ui:
uic.loadUi(path_ui, self)

self.mpl_edelta_setup()

Expand Down
9 changes: 5 additions & 4 deletions pyjibe/fd/tab_fit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pkg_resources
import importlib.resources

import nanite.model as nmodel
import numpy as np
Expand All @@ -10,9 +10,10 @@
class TabFit(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super(TabFit, self).__init__(*args, **kwargs)
path_ui = pkg_resources.resource_filename("pyjibe.fd",
"tab_fit.ui")
uic.loadUi(path_ui, self)

ref = importlib.resources.files("pyjibe.fd") / "tab_fit.ui"
with importlib.resources.as_file(ref) as path_ui:
uic.loadUi(path_ui, self)

# Setup the fitting tab
id_para = 0
Expand Down
8 changes: 4 additions & 4 deletions pyjibe/fd/tab_info.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numbers
import pkg_resources
import importlib.resources

from afmformats import meta
from nanite import model
Expand All @@ -12,9 +12,9 @@
class TabInfo(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super(TabInfo, self).__init__(*args, **kwargs)
path_ui = pkg_resources.resource_filename("pyjibe.fd",
"tab_info.ui")
uic.loadUi(path_ui, self)
ref = importlib.resources.files("pyjibe.fd") / "tab_info.ui"
with importlib.resources.as_file(ref) as path_ui:
uic.loadUi(path_ui, self)

def update_info(self, fdist):
hr_info = {}
Expand Down
8 changes: 4 additions & 4 deletions pyjibe/fd/tab_preprocess.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pkg_resources
import importlib.resources

from nanite import preproc
from PyQt5 import uic, QtCore, QtWidgets
Expand All @@ -9,9 +9,9 @@
class TabPreprocess(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super(TabPreprocess, self).__init__(*args, **kwargs)
path_ui = pkg_resources.resource_filename("pyjibe.fd",
"tab_preprocess.ui")
uic.loadUi(path_ui, self)
ref = importlib.resources.files("pyjibe.fd") / "tab_preprocess.ui"
with importlib.resources.as_file(ref) as path_ui:
uic.loadUi(path_ui, self)

# Setup everything necessary for the preprocessing tab:
# Get list of preprocessing methods
Expand Down
8 changes: 4 additions & 4 deletions pyjibe/fd/tab_qmap.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pkg_resources
import importlib.resources

import nanite
from PyQt5 import uic, QtCore, QtWidgets
Expand All @@ -23,9 +23,9 @@ def get_qmap(fdist_group):
class TabQMap(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super(TabQMap, self).__init__(*args, **kwargs)
path_ui = pkg_resources.resource_filename("pyjibe.fd",
"tab_qmap.ui")
uic.loadUi(path_ui, self)
ref = importlib.resources.files("pyjibe.fd") / "tab_qmap.ui"
with importlib.resources.as_file(ref) as path_ui:
uic.loadUi(path_ui, self)

# Setup the matplotlib interface for 2D map plotting
self.mpl_qmap = MPLQMap()
Expand Down
10 changes: 5 additions & 5 deletions pyjibe/fd/widget_preprocess_item.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pkg_resources
import importlib.resources

from nanite import preproc
from PyQt5 import QtCore, QtWidgets, uic
Expand All @@ -12,10 +12,10 @@ def __init__(self, identifier, *args, **kwargs):
"""Special widget for preprocessing options"""
self.identifier = identifier
super(WidgetPreprocessItem, self).__init__(*args, **kwargs)

path_ui = pkg_resources.resource_filename("pyjibe.fd",
"widget_preprocess_item.ui")
uic.loadUi(path_ui, self)
ref = (importlib.resources.files("pyjibe.fd") /
"widget_preprocess_item.ui")
with importlib.resources.as_file(ref) as path_ui:
uic.loadUi(path_ui, self)

# set label text
name = preproc.get_name(identifier)
Expand Down
9 changes: 4 additions & 5 deletions pyjibe/head/custom_widgets/mpl_navigation_toolbar_icons.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import pkg_resources
import importlib.resources

from PyQt5 import QtCore, QtGui, QtWidgets

Expand All @@ -19,11 +19,10 @@ def __init__(self, *args, **kwargs):
def _icon(self, name, color=None):
"""Override matplotlibs `_icon` function to get custom icons"""
name = name.replace('.png', '_large.png')
impath = str(cbook._get_data_path('images', name))
impath = cbook._get_data_path('images', name)
if not os.path.exists(impath):
imdir = pkg_resources.resource_filename("pyjibe", "img")
impath = os.path.join(imdir, name)
pm = QtGui.QPixmap(impath)
impath = importlib.resources.files("pyjibe.img") / name
pm = QtGui.QPixmap(str(impath))
RaghavaAlajangi marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ref_path is guaranteed to exist only for the lifetime of the importlib.resources.as_file(ref) context. This means you must load the QPixmap inside the context
This is one of the main difficulties when migrating to importlib.resources.

One option would be to initialize pm with None and then, depending on whether the image path exists and pm is still None, override pm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that means if pm is None, I also have to raise an error. right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, since the method is expected to return a QIcon

pm.setDevicePixelRatio(self.devicePixelRatioF() or 1)
if self.palette().color(self.backgroundRole()).value() < 128:
icon_color = self.palette().color(self.foregroundRole())
Expand Down
8 changes: 4 additions & 4 deletions pyjibe/head/dlg_tool_convert.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import codecs
import hashlib
import pathlib
import pkg_resources
import importlib.resources

import afmformats
import h5py
Expand All @@ -14,9 +14,9 @@
def __init__(self, parent, *args, **kwargs):
"""Data conversion dialog"""
super(ConvertDialog, self).__init__(parent=parent, *args, **kwargs)
path_ui = pkg_resources.resource_filename("pyjibe.head",
"dlg_tool_convert.ui")
uic.loadUi(path_ui, self)
ref = importlib.resources.files("pyjibe.head") / "head.ui"
RaghavaAlajangi marked this conversation as resolved.
Show resolved Hide resolved
with importlib.resources.as_file(ref) as path_ui:
uic.loadUi(path_ui, self)

Check warning on line 19 in pyjibe/head/dlg_tool_convert.py

View check run for this annotation

Codecov / codecov/patch

pyjibe/head/dlg_tool_convert.py#L17-L19

Added lines #L17 - L19 were not covered by tests

self._file_list = []

Expand Down
7 changes: 4 additions & 3 deletions pyjibe/head/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# flake8: noqa: E402 (matplotlib.use has to be right after the import)
import os.path as os_path
import pathlib
import pkg_resources
import importlib.resources
import signal
import sys
import traceback
Expand Down Expand Up @@ -66,8 +66,9 @@ def __init__(self, *args, **kwargs):
self._update_worker = None

# load ui files
path_ui = pkg_resources.resource_filename("pyjibe.head", "main.ui")
uic.loadUi(path_ui, self)
ref = importlib.resources.files("pyjibe.head") / "main.ui"
with importlib.resources.as_file(ref) as path_ui:
uic.loadUi(path_ui, self)

self.setWindowTitle("PyJibe {}".format(__version__))
# Disable native menubar (e.g. on Mac)
Expand Down
8 changes: 4 additions & 4 deletions pyjibe/head/preferences.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os.path as os_path
import pkg_resources
import importlib.resources
import traceback

import nanite
Expand Down Expand Up @@ -35,9 +35,9 @@

def __init__(self, parent, *args, **kwargs):
QtWidgets.QWidget.__init__(self, parent=parent, *args, **kwargs)
path_ui = pkg_resources.resource_filename(
"pyjibe.head", "preferences.ui")
uic.loadUi(path_ui, self)
ref = importlib.resources.files("pyjibe.head") / "preferences.ui"
with importlib.resources.as_file(ref) as path_ui:
uic.loadUi(path_ui, self)

Check warning on line 40 in pyjibe/head/preferences.py

View check run for this annotation

Codecov / codecov/patch

pyjibe/head/preferences.py#L38-L40

Added lines #L38 - L40 were not covered by tests
self.settings = QtCore.QSettings()
self.parent = parent

Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
# Defined by PEP 518:
requires = [
# for version management
"setuptools>=45",
"setuptools_scm[toml]>=6.2",
"setuptools_scm[toml]>=6.2"
]
build-backend = "setuptools.build_meta"

Expand Down
Loading