-
Notifications
You must be signed in to change notification settings - Fork 450
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
replacing pkg_resources with importlib #3749
Conversation
There are some more places where |
nikola/nikola.py
Outdated
@@ -1031,7 +1031,7 @@ def init_plugins(self, commands_only=False, load_all=False): | |||
extra_plugins_dirs = self.config['EXTRA_PLUGINS_DIRS'] | |||
self._loading_commands_only = commands_only | |||
self._plugin_places = [ | |||
resource_filename('nikola', 'plugins'), | |||
resources.files('nikola').joinpath('plugins'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resources.files()
has been added in Python 3.9 (https://docs.python.org/3/library/importlib.resources.html#importlib.resources.files), while this repo still supports Python 3.8 (https://github.com/getnikola/nikola/blob/master/setup.py#L142).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resources.files()
has been added in Python 3.9 (https://docs.python.org/3/library/importlib.resources.html#importlib.resources.files), while this repo still supports Python 3.8 (https://github.com/getnikola/nikola/blob/master/setup.py#L142).
should we use importlib_resources instead of importlib.resources ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. Please use the built-in importlib.resources
, but only the functions available in Python 3.7 3.8: https://docs.python.org/3.8/library/importlib.html#module-importlib.resources
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. Please use the built-in
importlib.resources
, but only the functions available in Python3.73.8: https://docs.python.org/3.8/library/importlib.html#module-importlib.resources
I can't, with importlib.resources you can choose only between deprecated or newer methods! (all the available methods in 3.8 are not working with newer python releases)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can always make code depend on sys.version_info
.
not in nikola.py ? |
Yes, there are 6 other files. |
how to deal with python 3.8 as felixfontein said ? |
I would probably create a helper function (in |
Another alternative might be using https://docs.python.org/3/library/pkgutil.html#pkgutil.get_data instead. |
will "pkgutil" be widely compatible ? |
According to the docs it's been available since at least Python 2.6. |
hoping this works. |
nikola/nikola.py
Outdated
@@ -1031,7 +1031,7 @@ def init_plugins(self, commands_only=False, load_all=False): | |||
extra_plugins_dirs = self.config['EXTRA_PLUGINS_DIRS'] | |||
self._loading_commands_only = commands_only | |||
self._plugin_places = [ | |||
resource_filename('nikola', 'plugins'), | |||
str(resources.path('nikola', 'plugins')) if sys.version_info.minor == 8 else str(resources.files('nikola').joinpath('plugins')), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Instead of repeating the same version check everywhere, please instead make a helper function in
utils.py
. - Please make it so your version check won’t break for Python 4.8, and consider making it
<=
instead of==
while doing that. - What do the functions return so that they need
str
? If it’s apathlib.Path
, it would be desirable to switch other things toPath
s instead of working with strings.
nikola/nikola.py
Outdated
builtin_sc_dir = resource_filename( | ||
'nikola', | ||
os.path.join('data', 'shortcodes', utils.get_template_engine(self.THEMES))) | ||
builtin_sc_dir = str(resources.path('nikola')) if sys.version_info.minor == 8 else str(resources.files('nikola').joinpath(os.path.join('data', 'shortcodes', utils.get_template_engine(self.THEMES)))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The two things are not equivalent, this is why a function in utils would be a better choice.
Please fix the failing tests and linting errors. |
I have no idea why resources.path() is not working! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like I forgot to send this review. Here it is.
To understand the 3.8 test failures, you can try running the tests locally, but first changing the code so that the 3.8 version runs on all Python versions (or you could install 3.8, but it’s probably not worth it). |
I tried every method in https://docs.python.org/3.8/library/importlib.html#module-importlib.resources , but nothing is working on 3.8 ! |
The documentation of
(Emphasis is mine.) So it returns a context manager, not directly a path. Your code is treating it as a function which returns a path. That doesn't work. Instead of with resources.path(package, resource) as path:
return str(path) This only works for resources that are not part of an archive. For this to work with resources in an archive, you need to implement a caching solution. Which might be easier if not all uses of I guess one question is how much complexity we want to add for Python 3.8. @Kwpolska what do you think? |
What’s an archive? We don’t support If the |
If we don't support zipfiles or directly running from eggs, then yeah, just doing a |
I tried the "with" on path() but the test is still failing. |
I found a solution that works for all 3.8 and 3.9+ (at least for me), just adding some init.py in data/ and removing os.path.join . |
Please fix the test failures. |
Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Chris Warrick <kwpolska@gmail.com>
Co-authored-by: Chris Warrick <kwpolska@gmail.com>
Thanks for your contribution! |
Pull Request Checklist
Description
Sorry for the old error.
I updated the file with this PR instead.