-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from simonsobs/type
Clean code
- Loading branch information
Showing
7 changed files
with
312 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
__all__ = ["PluginManager"] | ||
|
||
from .manager import PluginManager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import asyncio | ||
from collections.abc import Callable | ||
from typing import Any, Coroutine | ||
|
||
from pluggy import HookCaller | ||
from pluggy import PluginManager as PluginManager_ | ||
|
||
|
||
class AHook: | ||
def __init__(self, pm: PluginManager_) -> None: | ||
self.pm = pm | ||
|
||
def __getattr__(self, name: str) -> Callable[..., Coroutine[Any, Any, list]]: | ||
async def call(*args: Any, **kwargs: Any) -> list: | ||
hook: HookCaller = getattr(self.pm.hook, name) | ||
coros: list[asyncio.Future] = hook(*args, **kwargs) | ||
return await asyncio.gather(*coros) | ||
|
||
return call |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import contextlib | ||
from collections.abc import AsyncIterator, Callable | ||
from typing import Any, AsyncContextManager | ||
|
||
from pluggy import HookCaller | ||
from pluggy import PluginManager as PluginManager_ | ||
|
||
|
||
class AWith: | ||
def __init__(self, pm: PluginManager_) -> None: | ||
self.pm = pm | ||
|
||
def __getattr__(self, name: str) -> Callable[..., AsyncContextManager]: | ||
hook: HookCaller = getattr(self.pm.hook, name) | ||
call = _Call(hook) | ||
return call | ||
|
||
|
||
def _Call( | ||
hook: Callable[..., list[AsyncContextManager]] | ||
) -> Callable[..., AsyncContextManager]: | ||
@contextlib.asynccontextmanager | ||
async def call(*args: Any, **kwargs: Any) -> AsyncIterator[list]: | ||
ctxs = hook(*args, **kwargs) | ||
async with contextlib.AsyncExitStack() as stack: | ||
yields = [await stack.enter_async_context(ctx) for ctx in ctxs] | ||
|
||
# TODO: Consider entering the contexts asynchronously as in the | ||
# following commented out code. | ||
|
||
# yields = await asyncio.gather( | ||
# *[stack.enter_async_context(ctx) for ctx in ctxs] | ||
# ) | ||
|
||
yield yields | ||
|
||
# TODO: The following commented out code is an attempt to support | ||
# `asend()` through the `gen` attribute. It only works for | ||
# simple cases. It doesn't work with starlette.lifespan(). | ||
# When starlette is shutting down, an exception is raised | ||
# `RuntimeError: generator didn't stop after athrow()`. | ||
|
||
# stop = False | ||
# while not stop: | ||
# sent = yield yields | ||
# try: | ||
# yields = await asyncio.gather( | ||
# *[ctx.gen.asend(sent) for ctx in ctxs] | ||
# ) | ||
# except StopAsyncIteration: | ||
# stop = True | ||
|
||
return call |
Oops, something went wrong.