Explorar o código

Add Config, Request and Respond injectors, fix types

Kirk Trombley %!s(int64=4) %!d(string=hai) anos
pai
achega
87b1614e78
Modificáronse 1 ficheiros con 29 adicións e 13 borrados
  1. 29 13
      lib/rollbot/injection/util.py

+ 29 - 13
lib/rollbot/injection/util.py

@@ -1,7 +1,10 @@
 from collections.abc import Callable, Coroutine
-from typing import TypeVar
+from typing import TypeVar, Any
+from datetime import datetime
 
-from ..types import Message, Context
+from aiohttp import ClientSession
+
+from ..types import Message, Context, Attachment, Command
 from .base import Injector, InjectorWithCleanup
 
 __all__ = [
@@ -17,6 +20,9 @@ __all__ = [
     "Text",
     "Attachments",
     "CommandInjector",
+    "Request",
+    "Respond",
+    "Config",
 ]
 
 
@@ -31,17 +37,27 @@ class Simple(Injector[Dep]):
         return self.extract(message, context)
 
 
-MessageInjector = Simple(lambda m, c: m)
-ContextInjector = Simple(lambda m, c: c)
-Origin = Simple(lambda m, c: m.origin_id)
-Channel = Simple(lambda m, c: m.channel_id)
-Sender = Simple(lambda m, c: m.sender_id)
-Timestamp = Simple(lambda m, c: m.timestamp)
-OriginAdmin = Simple(lambda m, c: m.origin_admin)
-ChannelAdmin = Simple(lambda m, c: m.channel_admin)
-Text = Simple(lambda m, c: m.text)
-Attachments = Simple(lambda m, c: m.attachments)
-CommandInjector = Simple(lambda m, c: m.command)
+MessageInjector = Simple[Message](lambda m, c: m)
+ContextInjector = Simple[Context](lambda m, c: c)
+Origin = Simple[str](lambda m, c: m.origin_id)
+Channel = Simple[str](lambda m, c: m.channel_id)
+Sender = Simple[str](lambda m, c: m.sender_id)
+Timestamp = Simple[datetime](lambda m, c: m.timestamp)
+OriginAdmin = Simple[bool](lambda m, c: m.origin_admin)
+ChannelAdmin = Simple[bool](lambda m, c: m.channel_admin)
+Text = Simple[str](lambda m, c: m.text)
+Attachments = Simple[list[Attachment]](lambda m, c: m.attachments)
+CommandInjector = Simple[Command](lambda m, c: m.command)
+Request = Simple[ClientSession](lambda m, c: c.request)
+Respond = Simple[Callable[[], Coroutine[None, None, None]]](lambda m, c: c.respond)
+
+
+class Config(Injector[Any]):
+    def __init__(self, key: str):
+        self.key = key
+
+    async def inject(self, message: Message, context: Context) -> Any:
+        return context.config(self.key)
 
 
 class Lazy(InjectorWithCleanup[Callable[[], Coroutine[None, None, Dep]]]):