Skip to content

Commit

Permalink
do not use pkg_resources for entrypoints, towards fixing #42
Browse files Browse the repository at this point in the history
  • Loading branch information
jensens committed Mar 12, 2024
1 parent 8b9c52d commit b56eaee
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 21 deletions.
6 changes: 1 addition & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,12 @@ dependencies = []
dynamic = ["readme"]

[project.optional-dependencies]
mypy = [
"types-setuptools",
"types-pkg-resources",
]
mypy = []
test = [
"pytest",
"pytest-cov",
"pytest-mock",
"httpretty",
"types-setuptools",
]

[project.urls]
Expand Down
2 changes: 1 addition & 1 deletion src/mxdev/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(
if line:
self.ignore_keys.append(line)

def is_ns_member(name):
def is_ns_member(name) -> bool:
for hook in hooks:
if name.startswith(hook.namespace):
return True
Expand Down
27 changes: 27 additions & 0 deletions src/mxdev/entry_points.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# this is a helper to load entrypoints with importlib, since pkg_resources
# is deprecated. In Python 3.12 an API incompatible change was introduced,
# so this code is that ugly now.
from importlib.metadata import entry_points


try:
# do we have Python 3.12+?
from importlib.metadata import EntryPoints # type: ignore # noqa: F401

HAS_IMPORTLIB_ENTRYPOINTS = True
except ImportError:
HAS_IMPORTLIB_ENTRYPOINTS = False


def load_eps_by_group(group: str) -> list:
if HAS_IMPORTLIB_ENTRYPOINTS:
eps = entry_points(group=group) # type: ignore
else:
eps_base = entry_points()
if group not in eps_base:
return []
eps = eps_base[group]
# XXX: for some reasons entry points are loaded twice. not sure if this
# is a glitch when installing with uv or something related to
# importlib.metadata.entry_points
return list(eps) # type: ignore
14 changes: 12 additions & 2 deletions src/mxdev/hooks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
from .entry_points import load_eps_by_group
from .state import State
from pkg_resources import iter_entry_points

import typing


try:
# do we have Python 3.12+
from importlib.metadata import EntryPoints # type: ignore # noqa: F401

HAS_IMPORTLIB_ENTRYPOINTS = True
except ImportError:
HAS_IMPORTLIB_ENTRYPOINTS = False


class Hook:
"""Entry point for hooking into mxdev."""

Expand All @@ -18,11 +27,12 @@ def write(self, state: State) -> None:


def load_hooks() -> list:
return [ep.load()() for ep in iter_entry_points("mxdev") if ep.name == "hook"]
return [ep.load()() for ep in load_eps_by_group("mxdev") if ep.name == "hook"]


def read_hooks(state: State, hooks: typing.List[Hook]) -> None:
for hook in hooks:
breakpoint()
hook.read(state)


Expand Down
3 changes: 1 addition & 2 deletions src/mxdev/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ def test_WorkingCopies_process(mocker, caplog):
def test_WorkingCopies_checkout(mocker, caplog, tmpdir):
caplog.set_level(logging.INFO)

class SysExit(Exception):
...
class SysExit(Exception): ...

class Exit:
def __call__(self, code):
Expand Down
19 changes: 8 additions & 11 deletions src/mxdev/vcs/common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from ..entry_points import load_eps_by_group

import abc
import logging
import os
import pkg_resources
import platform
import queue
import re
Expand Down Expand Up @@ -93,20 +94,16 @@ def should_update(self, **kwargs) -> bool:
return update

@abc.abstractmethod
def checkout(self, **kwargs) -> typing.Union[str, None]:
...
def checkout(self, **kwargs) -> typing.Union[str, None]: ...

@abc.abstractmethod
def status(self, **kwargs) -> typing.Union[typing.Tuple[str, str], str]:
...
def status(self, **kwargs) -> typing.Union[typing.Tuple[str, str], str]: ...

@abc.abstractmethod
def matches(self) -> bool:
...
def matches(self) -> bool: ...

@abc.abstractmethod
def update(self, **kwargs) -> typing.Union[str, None]:
...
def update(self, **kwargs) -> typing.Union[str, None]: ...


def yesno(
Expand Down Expand Up @@ -151,12 +148,12 @@ def get_workingcopytypes() -> typing.Dict[str, typing.Type[BaseWorkingCopy]]:
return _workingcopytypes
group = "mxdev.workingcopytypes"
addons = {}
for entrypoint in pkg_resources.iter_entry_points(group=group):
for entrypoint in load_eps_by_group(group):
key = entrypoint.name
workingcopytype = entrypoint.load()
if not entrypoint.dist:
continue
if entrypoint.dist.project_name == "mxdev":
if entrypoint.dist.name == "mxdev":
_workingcopytypes[key] = workingcopytype
continue
if key in addons:
Expand Down

0 comments on commit b56eaee

Please sign in to comment.