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

Conversation

RaghavaAlajangi
Copy link
Contributor

@RaghavaAlajangi RaghavaAlajangi commented Dec 5, 2024

This PR aims to implement migration from pkg_resources to importlib.resources as described in issue #33

  • Replace pkg_resources code with importlib.resources and make necessary changes
  • Run tests locally
  • Fix lining issues
  • Update changelog
  • Passed CICD

Copy link

codecov bot commented Dec 5, 2024

Codecov Report

Attention: Patch coverage is 75.00000% with 17 lines in your changes missing coverage. Please review.

Project coverage is 67.37%. Comparing base (60901ee) to head (7a65653).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
pyjibe/__main__.py 0.00% 7 Missing ⚠️
pyjibe/fd/dlg_export_vals.py 25.00% 3 Missing ⚠️
pyjibe/head/dlg_tool_convert.py 25.00% 3 Missing ⚠️
pyjibe/head/preferences.py 25.00% 3 Missing ⚠️
...ead/custom_widgets/mpl_navigation_toolbar_icons.py 90.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #38      +/-   ##
==========================================
+ Coverage   67.31%   67.37%   +0.05%     
==========================================
  Files          36       36              
  Lines        2772     2789      +17     
==========================================
+ Hits         1866     1879      +13     
- Misses        906      910       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@RaghavaAlajangi
Copy link
Contributor Author

Hi @paulmueller
please take a look at these changes.

@RaghavaAlajangi
Copy link
Contributor Author

Hi @paulmueller
all checks were passed. Could you review these changes?

Copy link
Member

@paulmueller paulmueller left a comment

Choose a reason for hiding this comment

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

There are a few copy-paste errors.

Please test the application thoroughly (click on every menu item and open every possible dialog). The current tests do not cover the entire UI.

CHANGELOG Show resolved Hide resolved
pyjibe/__main__.py Outdated Show resolved Hide resolved
pyjibe/fd/dlg_export_vals.py Outdated Show resolved Hide resolved
pyjibe/head/custom_widgets/mpl_navigation_toolbar_icons.py Outdated Show resolved Hide resolved
pyjibe/head/dlg_tool_convert.py Outdated Show resolved Hide resolved
pm = QtGui.QPixmap(impath)
ref = importlib.resources.files("pyjibe.img") / name
with importlib.resources.as_file(ref) as impath:
pm = QtGui.QPixmap(str(impath))
Copy link
Member

Choose a reason for hiding this comment

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

This will not work. If impath exists, then pm is never created. PyCharm should give you a warning here as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I already fixed this. I will push it tomorrow.

@RaghavaAlajangi
Copy link
Contributor Author

Hi Paul
I tested the application thoroughly by clicking every option. It seems working fine. Could you check now?

ref = importlib.resources.files("pyjibe.img") / name
with importlib.resources.as_file(ref) as ref_path:
impath = ref_path
pm = QtGui.QPixmap(str(impath))
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

Comment on lines 23 to 28
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:
impath = ref_path
pm = QtGui.QPixmap(str(impath))
pm = QtGui.QPixmap(str(ref_path))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wrote something like this. Since pyjibe.img.name exists in the repo, Do we need to initialize pm = None?

Copy link
Member

Choose a reason for hiding this comment

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

While the directory pyjibe.img exists, you cannot be certain that an arbitrary file name exists in that directory.
Yes, we have to initialize pm=None and add a check in the end to make sure an image file has been found before using the pixmap.

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, I added this line.

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}")
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}'"

@paulmueller paulmueller merged commit 89166df into AFM-analysis:master Dec 13, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants