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

2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
0.15.10
- ref: migrate from pkg_resources to importlib.resources (#33)
RaghavaAlajangi marked this conversation as resolved.
Show resolved Hide resolved
0.15.9
- build: fix installation directory
- build: disable unsigned installer
Expand Down
18 changes: 7 additions & 11 deletions pyjibe/__main__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
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
from PyQt5.QtCore import QEventLoop

app = QApplication(sys.argv)
# Note:
# 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")

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 15 in pyjibe/__main__.py

View check run for this annotation

Codecov / codecov/patch

pyjibe/__main__.py#L13-L15

Added lines #L13 - L15 were not covered by tests
splash = QSplashScreen(splash_pix)
splash.setMask(splash_pix.mask())
splash.show()
Expand All @@ -28,8 +23,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 28 in pyjibe/__main__.py

View check run for this annotation

Codecov / codecov/patch

pyjibe/__main__.py#L26-L28

Added lines #L26 - L28 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") / "dlg_export_vals.ui"
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
17 changes: 11 additions & 6 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,16 @@
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))
if not os.path.exists(impath):
imdir = pkg_resources.resource_filename("pyjibe", "img")
impath = os.path.join(imdir, name)
pm = QtGui.QPixmap(impath)
impath = cbook._get_data_path('images', name)
pm = None # Initialize pm to None
if os.path.exists(impath):
pm = QtGui.QPixmap(str(impath))
else:
ref = importlib.resources.files("pyjibe.img") / name
with importlib.resources.as_file(ref) as ref_path:
pm = QtGui.QPixmap(str(ref_path))
if pm is None or pm.isNull():
raise ValueError(f"Loaded QPixmap is invalid for: {name}")

Check warning on line 31 in pyjibe/head/custom_widgets/mpl_navigation_toolbar_icons.py

View check run for this annotation

Codecov / codecov/patch

pyjibe/head/custom_widgets/mpl_navigation_toolbar_icons.py#L31

Added line #L31 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

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

Please change the error message to: f"Could not find image '{name}'"

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") / "dlg_tool_convert.ui"
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