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

Replace pkg_resources #31

Merged
merged 7 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions news/4126.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replace `pkg_resources` with `importlib.metadata`/`importlib.resources` @gforcada
58 changes: 32 additions & 26 deletions src/plone/autoinclude/loader.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from importlib.metadata import distribution
from pkg_resources import iter_entry_points
from pkg_resources import resource_filename
from pkg_resources import working_set
from importlib.metadata import distributions
from importlib.metadata import entry_points
from importlib.resources import files
from zope.configuration.xmlconfig import include
from zope.configuration.xmlconfig import includeOverrides

Expand Down Expand Up @@ -59,16 +59,16 @@ def load_z3c_packages(target=""):
This returns a dictionary of package names and packages.
"""
dists = {}
for ep in iter_entry_points(group="z3c.autoinclude.plugin"):
for ep in entry_points().select(group="z3c.autoinclude.plugin"):
# If we look for target 'plone' then only consider entry points
# that are registered for this target (module name).
# But if the entry point is not registered for a specific target,
# we can include it.
if target and ep.module_name != target:
if target and ep.name == "target" and ep.value != target:
continue
# We should always be able to get the distribution.
# Otherwise: how could we have an entry point?
module_name = _get_module_name_from_project_name(ep.dist.project_name)
module_name = _get_module_name_from_project_name(ep.dist.name)
if module_name not in _known_module_names:
try:
module = importlib.import_module(module_name)
Expand Down Expand Up @@ -121,27 +121,33 @@ def load_own_packages(target=""):
and it must have a value.
"""
dists = {}
for wsdist in working_set:
eps = wsdist.get_entry_map("plone.autoinclude.plugin")
if not bool(eps):
for dist_obj in distributions():
eps = dist_obj.entry_points.select(group="plone.autoinclude.plugin")
if not eps:
continue
# If we look for target 'plone' then only consider entry points
# that are registered for this target (module name).
# But if the entry point is not registered for a specific target,
# we can include it. The biggest reason for doing this,
# is that I first thought you could not specify both
# target and module at the same time.
module_name = None
if "target" in eps:
if target and eps["target"].module_name != target:
# entry point defines target X but we only want target Y.
continue
# We should always be able to get the distribution.
# Otherwise: how could we have an entry point?
module_name = _get_module_name_from_project_name(wsdist.project_name)
if "module" in eps:
# We could load the dist with ep.load(), but we do it differently.
module_name = eps["module"].module_name
this_target = True
for ep in eps:
# If we look for target 'plone' then only consider entry points
# that are registered for this target (module name).
# But if the entry point is not registered for a specific target,
# we can include it. The biggest reason for doing this,
# is that I first thought you could not specify both
# target and module at the same time.
if ep.name == "target":
if target and ep.value != target:
# entry point defines target X but we only want target Y.
this_target = False
break
# We should always be able to get the distribution.
# Otherwise: how could we have an entry point?
if module_name is None:
module_name = _get_module_name_from_project_name(dist_obj.name)
if ep.name == "module":
# We could load the dist with ep.load(), but we do it differently.
module_name = ep.value
if not this_target:
continue
if module_name is None: # pragma: no cover
# We could log a warning, but really this is an error.
raise ValueError(
Expand Down Expand Up @@ -171,7 +177,7 @@ def load_packages(target=""):

def get_zcml_file(module_name, zcml="configure.zcml"):
try:
filename = resource_filename(module_name, zcml)
filename = str(files(module_name) / zcml)
except ModuleNotFoundError:
# Note: this may happen a lot, at least for z3c.autoinclude,
# because the project name may not be the same as the package/module.
Expand Down
Loading