nonebot2/nonebot/drivers/__init__.py

105 lines
2.4 KiB
Python
Raw Normal View History

2020-07-04 22:51:10 +08:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
2020-07-18 18:18:43 +08:00
import abc
2020-07-04 22:51:10 +08:00
2020-08-14 17:41:24 +08:00
from nonebot.log import logger
2020-08-10 13:06:02 +08:00
from nonebot.config import Env, Config
2020-08-18 17:24:49 +08:00
from nonebot.typing import Bot, Dict, Type, Union, Optional, Callable
2020-07-11 17:32:03 +08:00
2020-07-04 22:51:10 +08:00
2020-07-18 18:18:43 +08:00
class BaseDriver(abc.ABC):
2020-08-13 15:23:04 +08:00
_adapters: Dict[str, Type[Bot]] = {}
2020-07-04 22:51:10 +08:00
2020-07-18 18:18:43 +08:00
@abc.abstractmethod
2020-08-10 13:06:02 +08:00
def __init__(self, env: Env, config: Config):
self.env = env.environment
2020-08-07 17:51:57 +08:00
self.config = config
2020-08-13 15:23:04 +08:00
self._clients: Dict[str, Bot] = {}
@classmethod
def register_adapter(cls, name: str, adapter: Type[Bot]):
cls._adapters[name] = adapter
2020-08-27 13:27:42 +08:00
logger.opt(
colors=True).debug(f'Succeeded to load adapter "<y>{name}</y>"')
2020-07-05 20:39:34 +08:00
2020-08-13 15:56:09 +08:00
@property
@abc.abstractmethod
def type(self):
raise NotImplementedError
2020-07-04 22:51:10 +08:00
@property
2020-07-18 18:18:43 +08:00
@abc.abstractmethod
2020-07-04 22:51:10 +08:00
def server_app(self):
raise NotImplementedError
@property
2020-07-18 18:18:43 +08:00
@abc.abstractmethod
2020-07-04 22:51:10 +08:00
def asgi(self):
raise NotImplementedError
@property
2020-07-18 18:18:43 +08:00
@abc.abstractmethod
2020-07-04 22:51:10 +08:00
def logger(self):
raise NotImplementedError
2020-08-07 17:51:57 +08:00
@property
2020-08-13 15:23:04 +08:00
def bots(self) -> Dict[str, Bot]:
2020-08-07 17:51:57 +08:00
return self._clients
2020-08-11 10:44:05 +08:00
@abc.abstractmethod
def on_startup(self, func: Callable) -> Callable:
raise NotImplementedError
@abc.abstractmethod
def on_shutdown(self, func: Callable) -> Callable:
raise NotImplementedError
2020-07-18 18:18:43 +08:00
@abc.abstractmethod
2020-07-04 22:51:10 +08:00
def run(self,
2020-08-18 17:24:49 +08:00
host: Optional[str] = None,
2020-07-04 22:51:10 +08:00
port: Optional[int] = None,
*args,
**kwargs):
raise NotImplementedError
2020-07-05 20:39:34 +08:00
2020-07-18 18:18:43 +08:00
@abc.abstractmethod
2020-07-05 20:39:34 +08:00
async def _handle_http(self):
raise NotImplementedError
2020-07-18 18:18:43 +08:00
@abc.abstractmethod
2020-07-05 20:39:34 +08:00
async def _handle_ws_reverse(self):
raise NotImplementedError
2020-07-11 17:32:03 +08:00
class BaseWebSocket(object):
2020-07-18 18:18:43 +08:00
@abc.abstractmethod
def __init__(self, websocket):
self._websocket = websocket
@property
def websocket(self):
return self._websocket
@property
2020-07-18 18:18:43 +08:00
@abc.abstractmethod
def closed(self):
raise NotImplementedError
2020-07-18 18:18:43 +08:00
@abc.abstractmethod
async def accept(self):
raise NotImplementedError
2020-07-18 18:18:43 +08:00
@abc.abstractmethod
2020-08-01 22:03:40 +08:00
async def close(self, code: int):
raise NotImplementedError
2020-07-18 18:18:43 +08:00
@abc.abstractmethod
async def receive(self) -> dict:
raise NotImplementedError
2020-07-18 18:18:43 +08:00
@abc.abstractmethod
async def send(self, data: dict):
raise NotImplementedError