Skip to content

Apply fix from #446 to replace pkg_resources with importlib #485

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions pysteps/io/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

get_method
"""
import importlib
from importlib.metadata import entry_points

from pysteps.decorators import postprocess_import
from pysteps.io import importers, exporters, interface
Expand Down Expand Up @@ -46,17 +46,11 @@ def discover_importers():
The importers found are added to the `pysteps.io.interface_importer_methods`
dictionary containing the available importers.
"""
# The pkg resources needs to be reload to detect new packages installed during
# the execution of the python application. For example, when the plugins are
# installed during the tests
import pkg_resources

importlib.reload(pkg_resources)
# Backward compatibility with previous entry point 'pysteps.plugins.importers' next to 'pysteps.plugins.importer'
for entry_point in list(
pkg_resources.iter_entry_points(group="pysteps.plugins.importer", name=None)
entry_points(group="pysteps.plugins.importer")
) + list(
pkg_resources.iter_entry_points(group="pysteps.plugins.importers", name=None)
entry_points(group="pysteps.plugins.importers")
):
_importer = entry_point.load()

Expand All @@ -71,14 +65,14 @@ def discover_importers():
RuntimeWarning(
f"The importer identifier '{importer_short_name}' is already available in"
"'pysteps.io.interface._importer_methods'.\n"
f"Skipping {entry_point.module_name}:{entry_point.attrs}"
f"Skipping {entry_point.module}:{entry_point.attr}"
)

if hasattr(importers, importer_function_name):
RuntimeWarning(
f"The importer function '{importer_function_name}' is already an attribute"
"of 'pysteps.io.importers`.\n"
f"Skipping {entry_point.module_name}:{entry_point.attrs}"
f"Skipping {entry_point.module}:{entry_point.attr}"
)
else:
setattr(importers, importer_function_name, _importer)
Expand Down
16 changes: 4 additions & 12 deletions pysteps/postprocessing/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
get_method
"""
import importlib
from importlib.metadata import entry_points

from pysteps.postprocessing import diagnostics, ensemblestats
from pprint import pprint
Expand Down Expand Up @@ -82,28 +83,19 @@ def discover_postprocessors():
dictionary in 'pysteps.postprocessing.interface' containing the available postprocessors.
"""

# The pkg resources needs to be reloaded to detect new packages installed during
# the execution of the python application. For example, when the plugins are
# installed during the tests
import pkg_resources

importlib.reload(pkg_resources)

# Discover the postprocessors available in the plugins
for plugintype in ["diagnostic", "ensemblestat"]:
for entry_point in pkg_resources.iter_entry_points(
group=f"pysteps.plugins.{plugintype}", name=None
):
for entry_point in entry_points(group=f"pysteps.plugins.{plugintype}"):
_postprocessors = entry_point.load()

postprocessors_function_name = _postprocessors.__name__

if plugintype in entry_point.module_name:
if plugintype in entry_point.module:
add_postprocessor(
postprocessors_function_name,
_postprocessors,
f"{plugintype}s",
entry_point.attrs,
entry_point.attr,
)


Expand Down
Loading