Pass asyncio event loop and http session to plugin instances

This commit is contained in:
Tulir Asokan 2018-11-01 11:58:58 +02:00
parent 9e066478a9
commit 767885cec7
3 changed files with 16 additions and 7 deletions

View File

@ -32,8 +32,8 @@ log = logging.getLogger("maubot.client")
class Client: class Client:
log: logging.Logger log: logging.Logger = None
loop: asyncio.AbstractEventLoop loop: asyncio.AbstractEventLoop = None
cache: Dict[UserID, 'Client'] = {} cache: Dict[UserID, 'Client'] = {}
http_client: ClientSession = None http_client: ClientSession = None

View File

@ -17,6 +17,7 @@ from typing import Dict, List, Optional
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from ruamel.yaml.comments import CommentedMap from ruamel.yaml.comments import CommentedMap
from ruamel.yaml import YAML from ruamel.yaml import YAML
from asyncio import AbstractEventLoop
import logging import logging
import io import io
@ -38,6 +39,7 @@ yaml.indent(4)
class PluginInstance: class PluginInstance:
db: Session = None db: Session = None
mb_config: Config = None mb_config: Config = None
loop: AbstractEventLoop = None
cache: Dict[str, 'PluginInstance'] = {} cache: Dict[str, 'PluginInstance'] = {}
plugin_directories: List[str] = [] plugin_directories: List[str] = []
@ -109,8 +111,8 @@ class PluginInstance:
except (FileNotFoundError, KeyError): except (FileNotFoundError, KeyError):
base_file = None base_file = None
self.config = config_class(self.load_config, lambda: base_file, self.save_config) self.config = config_class(self.load_config, lambda: base_file, self.save_config)
self.plugin = cls(self.client.client, self.id, self.log, self.config, self.plugin = cls(self.client.client, self.loop, self.client.http_client, self.id,
self.mb_config["plugin_directories.db"]) self.log, self.config, self.mb_config["plugin_directories.db"])
try: try:
await self.plugin.start() await self.plugin.start()
except Exception: except Exception:
@ -178,6 +180,7 @@ class PluginInstance:
# endregion # endregion
def init(db: Session, config: Config): def init(db: Session, config: Config, loop: AbstractEventLoop):
PluginInstance.db = db PluginInstance.db = db
PluginInstance.mb_config = config PluginInstance.mb_config = config
PluginInstance.loop = loop

View File

@ -16,6 +16,8 @@
from typing import Type, Optional, TYPE_CHECKING from typing import Type, Optional, TYPE_CHECKING
from logging import Logger from logging import Logger
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from asyncio import AbstractEventLoop
from aiohttp import ClientSession
import os.path import os.path
from sqlalchemy.engine.base import Engine from sqlalchemy.engine.base import Engine
@ -34,11 +36,15 @@ class Plugin(ABC):
client: 'MaubotMatrixClient' client: 'MaubotMatrixClient'
id: str id: str
log: Logger log: Logger
loop: AbstractEventLoop
config: Optional['BaseProxyConfig'] config: Optional['BaseProxyConfig']
def __init__(self, client: 'MaubotMatrixClient', plugin_instance_id: str, log: Logger, def __init__(self, client: 'MaubotMatrixClient', loop: AbstractEventLoop, http: ClientSession,
config: Optional['BaseProxyConfig'], db_base_path: str) -> None: plugin_instance_id: str, log: Logger, config: Optional['BaseProxyConfig'],
db_base_path: str) -> None:
self.client = client self.client = client
self.loop = loop
self.http = http
self.id = plugin_instance_id self.id = plugin_instance_id
self.log = log self.log = log
self.config = config self.config = config