Skip to content

Commit

Permalink
feat: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
klen committed Jul 31, 2024
1 parent 98e434e commit c948ede
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
21 changes: 10 additions & 11 deletions muffin/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
from __future__ import annotations

from abc import ABC
from typing import TYPE_CHECKING, Any, Callable, ClassVar, Mapping, Optional
from typing import TYPE_CHECKING, Any, Awaitable, Callable, ClassVar, Mapping, Optional

from asgi_tools.utils import to_awaitable
from modconfig import Config

from muffin.errors import MuffinError
Expand Down Expand Up @@ -33,13 +32,13 @@ class BasePlugin(ABC):
defaults: ClassVar[Mapping[str, Any]] = {"disabled": False}

# Optional middleware method
middleware: Optional[Callable] = None
middleware: Callable[..., Awaitable]

# Optional startup method
startup: Optional[Callable] = None
startup: Callable[..., Awaitable]

# Optional shutdown method
shutdown: Optional[Callable] = None
shutdown: Callable[..., Awaitable]

# Optional conftest method
conftest: Optional[Callable[[], _AsyncGeneratorContextManager]] = None
Expand All @@ -65,11 +64,11 @@ def __repr__(self) -> str:
return f"<muffin.Plugin: { self.name }>"

async def __aenter__(self):
if self.startup is not None:
if hasattr(self, "startup"):
await self.startup()

async def __aexit__(self, exc_type, exc, tb):
if self.shutdown is not None:
if hasattr(self, "shutdown"):
await self.shutdown()

@property
Expand Down Expand Up @@ -100,15 +99,15 @@ def setup(self, app: Application, *, name: Optional[str] = None, **options) -> A
self.__app__ = app

# Register a middleware
if self.middleware:
app.middleware(to_awaitable(self.middleware))
if hasattr(self, "middleware"):
app.middleware(self.middleware)

# Bind startup
if self.startup:
if hasattr(self, "startup"):
app.on_startup(self.startup)

# Bind shutdown
if self.shutdown:
if hasattr(self, "shutdown"):
app.on_shutdown(self.shutdown)

return True
3 changes: 0 additions & 3 deletions tests/common/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ async def test_plugin(app, client):

start = mock.MagicMock()
finish = mock.MagicMock()
assert BasePlugin.middleware is None
assert BasePlugin.startup is None
assert BasePlugin.shutdown is None

class Plugin(BasePlugin):
name = "plugin"
Expand Down

0 comments on commit c948ede

Please sign in to comment.