-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
release 0.17.0, add derive and more utilles in dispatcher interface
- Loading branch information
1 parent
d63a7ef
commit c1ece2a
Showing
7 changed files
with
252 additions
and
180 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,10 @@ | ||
{ | ||
"python.linting.enabled": false, | ||
"python.linting.pylintEnabled": true, | ||
"python.linting.banditEnabled": false, | ||
"maven.view": "hierarchical", | ||
"python.formatting.provider": "black", | ||
"python.pythonPath": "C:\\Users\\Chenw\\AppData\\Local\\pypoetry\\Cache\\virtualenvs\\graia-broadcast-kkIP7ti5-py3.8", | ||
"jupyter.jupyterServerType": "local", | ||
"cSpell.words": [ | ||
"oplog", | ||
"Unexisted", | ||
"utilles" | ||
] | ||
} | ||
"python.linting.enabled": false, | ||
"python.linting.pylintEnabled": true, | ||
"python.linting.banditEnabled": false, | ||
"maven.view": "hierarchical", | ||
"python.formatting.provider": "black", | ||
"python.pythonPath": "C:\\Users\\Chenw\\AppData\\Local\\pypoetry\\Cache\\virtualenvs\\graia-broadcast-kkIP7ti5-py3.8", | ||
"jupyter.jupyterServerType": "local", | ||
"cSpell.words": ["Dispatchable", "oplog", "Unexisted", "utilles"] | ||
} |
Large diffs are not rendered by default.
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
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 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,31 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Protocol, TypeVar | ||
|
||
from graia.broadcast.entities.dispatcher import BaseDispatcher | ||
from graia.broadcast.interfaces.dispatcher import DispatcherInterface | ||
|
||
try: | ||
from typing import get_args | ||
except ImportError: | ||
from typing_extensions import get_args | ||
|
||
|
||
T = TypeVar("T") | ||
|
||
|
||
class Derive(Protocol[T]): | ||
async def __call__(self, value: T, dispatcher_interface: DispatcherInterface) -> T: | ||
... | ||
|
||
|
||
class DeriveDispatcher(BaseDispatcher): | ||
async def catch(self, interface: DispatcherInterface): | ||
if not interface.is_annotated: | ||
return | ||
args = get_args(interface.annotation) | ||
origin_arg, meta = args[0], args[1:] | ||
result = await interface.lookup_param(interface.name, origin_arg, interface.default) | ||
for i in meta: | ||
result = await i(result, interface) | ||
return result |
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 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,35 @@ | ||
import asyncio | ||
from typing import Annotated | ||
|
||
from graia.broadcast import Broadcast, Dispatchable | ||
from graia.broadcast.entities.dispatcher import BaseDispatcher | ||
from graia.broadcast.interfaces.dispatcher import DispatcherInterface | ||
|
||
|
||
class ExampleEvent(Dispatchable): | ||
class Dispatcher(BaseDispatcher): | ||
@staticmethod | ||
async def catch(interface: "DispatcherInterface"): | ||
if interface.annotation is str: | ||
return "ok, i'm." | ||
|
||
|
||
loop = asyncio.get_event_loop() | ||
broadcast = Broadcast(loop=loop) | ||
|
||
|
||
async def test_derive_1(v: str, dii: DispatcherInterface): | ||
print("in derive 1", v) | ||
return v[1:] | ||
|
||
|
||
@broadcast.receiver("ExampleEvent") # or just receiver(ExampleEvent) | ||
async def event_listener(maybe_you_are_str: Annotated[str, test_derive_1, test_derive_1]): | ||
print(maybe_you_are_str) # <<< ok, i'm | ||
|
||
|
||
async def main(): | ||
await broadcast.postEvent(ExampleEvent()) # sync call is allowed. | ||
|
||
|
||
loop.run_until_complete(main()) |