Skip to content

Commit

Permalink
managers, meta: migrate off of pkg_resources to importlib_metadata
Browse files Browse the repository at this point in the history
This fixes installing `sopel` (which depends on this package) from
scratch on a new environment that does not ship with `pkg_resources` as
part of the Python stdlib (py3.12+) and also omits `setuptools` (some
distros, fresh venvs, probably others).

Includes slight cleanup of comments, log messages, and imports, but I
think not enough to be worth a separate commit or pull request.
  • Loading branch information
dgw committed Oct 19, 2024
1 parent 4f139c9 commit 8636756
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ classifiers = [
requires-python = ">=3.8"
dependencies = [
"sopel>=7.1",
# sopel also requires both of the below, but it's best to be explicit
"requests",
"importlib_metadata>=3.6",
]

[project.urls]
Expand Down
24 changes: 15 additions & 9 deletions sopel_help/managers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""Sopel Help Managers."""

import pkg_resources
# TODO: use stdlib importlib.metadata when possible, after dropping py3.9.
# Stdlib does not support `entry_points(group='filter')` until py3.10, but
# fallback logic is more trouble than it's worth when e.g. clean Ubuntu
# py3.10 envs include old versions of this backport.
import importlib_metadata

PROVIDERS_ENTRY_POINT = 'sopel_help.providers'

Expand All @@ -18,8 +22,8 @@ def provider(self):
def provider_names(self):
"""Names of the available providers."""
if self._provider_list is None:
entry_points = pkg_resources.iter_entry_points(
PROVIDERS_ENTRY_POINT)
entry_points = importlib_metadata.entry_points(
group=PROVIDERS_ENTRY_POINT)
self._provider_list = [
entry_point.name
for entry_point in entry_points
Expand All @@ -37,16 +41,18 @@ def load_provider(self, name):
:return: a provider instance
:rtype: :class:`sopel_help.providers.AbstractProvider`
The provider will be loaded from an entry point and then instanciated
The provider will be loaded from an entry point and then instantiated
to be returned as is (no setup, no configure).
"""
entry_points = pkg_resources.iter_entry_points(
PROVIDERS_ENTRY_POINT, name)
# 1. get EntryPoints matching the group and name
entry_points = importlib_metadata.entry_points(
group=PROVIDERS_ENTRY_POINT, name=name)

# 2. get just the EntryPoint matching `name`
try:
entry_point = next(entry_points)
except StopIteration as err:
raise RuntimeError('Cannot found help provider %s' % name) from err
entry_point = entry_points[name]
except KeyError:
raise RuntimeError('Cannot find help provider %r' % name) from None

# 3. load the entry point
provider_maker = entry_point.load()
Expand Down
1 change: 1 addition & 0 deletions sopel_help/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import urllib

import requests

from sopel.tools import get_logger

from sopel_help import mixins
Expand Down

0 comments on commit 8636756

Please sign in to comment.