maubot/maubot/plugin_base.py

75 lines
2.5 KiB
Python
Raw Normal View History

2018-10-14 19:08:11 +00:00
# maubot - A plugin-based Matrix bot system.
# Copyright (C) 2018 Tulir Asokan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-10-20 15:39:16 +00:00
from typing import Type, Optional, TYPE_CHECKING
2018-10-16 22:30:08 +00:00
from logging import Logger
2018-10-20 15:39:16 +00:00
from abc import ABC, abstractmethod
from asyncio import AbstractEventLoop
from aiohttp import ClientSession
import os.path
2018-10-14 19:08:11 +00:00
from sqlalchemy.engine.base import Engine
import sqlalchemy as sql
2018-10-14 19:08:11 +00:00
if TYPE_CHECKING:
2018-10-15 21:25:23 +00:00
from .client import MaubotMatrixClient
from .command_spec import CommandSpec
from mautrix.util.config import BaseProxyConfig
2018-10-14 19:08:11 +00:00
DatabaseNotConfigured = ValueError("A database for this maubot instance has not been configured.")
2018-10-14 19:08:11 +00:00
class Plugin(ABC):
2018-10-16 22:30:08 +00:00
client: 'MaubotMatrixClient'
id: str
log: Logger
loop: AbstractEventLoop
2018-10-20 15:39:16 +00:00
config: Optional['BaseProxyConfig']
2018-10-16 22:30:08 +00:00
def __init__(self, client: 'MaubotMatrixClient', loop: AbstractEventLoop, http: ClientSession,
plugin_instance_id: str, log: Logger, config: Optional['BaseProxyConfig'],
db_base_path: str) -> None:
2018-10-14 19:08:11 +00:00
self.client = client
self.loop = loop
self.http = http
2018-10-16 13:41:02 +00:00
self.id = plugin_instance_id
2018-10-16 22:30:08 +00:00
self.log = log
2018-10-20 15:39:16 +00:00
self.config = config
self.__db_base_path = db_base_path
2018-10-14 19:08:11 +00:00
def request_db_engine(self) -> Optional[Engine]:
if not self.__db_base_path:
raise DatabaseNotConfigured
return sql.create_engine(f"sqlite:///{os.path.join(self.__db_base_path, self.id)}.db")
2018-10-15 21:25:23 +00:00
def set_command_spec(self, spec: 'CommandSpec') -> None:
2018-10-16 13:41:02 +00:00
self.client.set_command_spec(self.id, spec)
2018-10-15 21:25:23 +00:00
2018-10-20 15:39:16 +00:00
@abstractmethod
2018-10-14 19:08:11 +00:00
async def start(self) -> None:
pass
2018-10-20 15:39:16 +00:00
@abstractmethod
2018-10-14 19:08:11 +00:00
async def stop(self) -> None:
pass
2018-10-20 15:39:16 +00:00
@classmethod
def get_config_class(cls) -> Optional[Type['BaseProxyConfig']]:
return None
def on_external_config_update(self) -> None:
if self.config:
self.config.load_and_update()