Skip to content

Commit

Permalink
rework plugins discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
mosquito committed Jul 20, 2023
1 parent 9db7197 commit 14342d2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
35 changes: 34 additions & 1 deletion aiomisc/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import socket
import sys
from typing import Optional
from typing import Any, Iterator, Optional

from ._context_vars import EVENT_LOOP

Expand All @@ -29,6 +29,36 @@ def time_ns() -> int:
from typing_extensions import ParamSpec


if sys.version_info >= (3, 8):
from typing import Protocol
else:
from typing_extensions import Protocol


class EntrypointProtocol(Protocol):
name: str

def load(self) -> Any:
...


# noinspection PyUnresolvedReferences
try:
from importlib.metadata import Distribution, EntryPoint

def entry_pont_iterator(entry_point: str) -> Iterator[EntrypointProtocol]:
ep: EntryPoint
for dist in Distribution.discover():
for ep in dist.entry_points:
if ep.group == entry_point:
yield ep # type: ignore
except ImportError:
import pkg_resources

def entry_pont_iterator(entry_point: str) -> Iterator[EntrypointProtocol]:
yield from pkg_resources.iter_entry_points(entry_point)


class EventLoopMixin:
__slots__ = "_loop",

Expand Down Expand Up @@ -81,8 +111,11 @@ def sock_set_reuseport(sock: socket.socket, reuse_port: bool) -> None:
get_current_loop = EVENT_LOOP.get

__all__ = (
"EntrypointProtocol",
"EventLoopMixin",
"ParamSpec",
"Protocol",
"entry_pont_iterator",
"event_loop_policy",
"final",
"get_current_loop",
Expand Down
22 changes: 14 additions & 8 deletions aiomisc/plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import logging
import os
from itertools import chain
from types import MappingProxyType
from typing import Callable, Mapping

from aiomisc.compat import entry_pont_iterator


def setup_plugins() -> Mapping[str, Callable]:
if os.getenv("AIOMISC_NO_PLUGINS"):
return MappingProxyType({})

import pkg_resources

plugins = {}
logger = logging.getLogger(__name__)

for entry_point in pkg_resources.iter_entry_points("aiomisc.plugins"):
plugins[entry_point.name] = entry_point.load()

for entry_point in pkg_resources.iter_entry_points("aiomisc"):
plugins[entry_point.name] = entry_point.load()
for entry_point in chain(
entry_pont_iterator("aiomisc.plugins"),
entry_pont_iterator("aiomisc"),
):
try:
plugins[entry_point.name] = entry_point.load()
except: # noqa
logger.exception(
"Failed to load entrypoint %r", entry_point,
)

logger = logging.getLogger(__name__)
for name, plugin in plugins.items():
try:
logger.debug("Trying to load %r %r", name, plugin)
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "aiomisc"
# This is a dummy version which will be rewritten with poem-plugins
version = "17.2.23"
version = "17.3.0"
description = "aiomisc - miscellaneous utils for asyncio"
authors = ["Dmitry Orlov <me@mosquito.su>"]
readme = "README.rst"
Expand Down Expand Up @@ -55,7 +55,6 @@ packages = [
"Documentation" = "https://aiomisc.readthedocs.io/en/latest/"

[tool.poetry.dependencies]
python = "^3.7"
aiocarbon = { version = "^0.15", optional = true }
aiohttp = { version = ">3", optional = true }
aiohttp-asgi = { version = "^0.5.2", optional = true }
Expand All @@ -64,8 +63,10 @@ croniter = { version = "^1.3.8", optional = true }
grpcio = { version = "^1.56.0", optional = true }
grpcio-tools = { version = "^1.56.0", optional = true }
logging-journald = [{ version = '*', platform = 'linux' }]
python = "^3.7"
raven = { version = "*", optional = true }
rich = { version = "*", optional = true }
setuptools = [{ version = '*', python = "< 3.8" }]
typing_extensions = [{ version = '*', python = "< 3.10" }]
uvloop = { version = ">=0.14, <1", optional = true }

Expand Down

0 comments on commit 14342d2

Please sign in to comment.