Make it run
This commit is contained in:
parent
eef052b1e9
commit
dce2771588
@ -20,17 +20,22 @@ import argparse
|
||||
import asyncio
|
||||
import copy
|
||||
import sys
|
||||
import os
|
||||
|
||||
from .config import Config
|
||||
from .db import Base, init as init_db
|
||||
from .server import MaubotServer
|
||||
from .client import Client, init as init_client
|
||||
from .loader import ZippedPluginLoader, MaubotZipImportError
|
||||
from .__meta__ import __version__
|
||||
|
||||
parser = argparse.ArgumentParser(description="A plugin-based Matrix bot system.",
|
||||
prog="python -m maubot")
|
||||
parser.add_argument("-c", "--config", type=str, default="config.yaml",
|
||||
metavar="<path>", help="the path to your config file")
|
||||
parser.add_argument("-b", "--base-config", type=str, default="example-config.yaml",
|
||||
metavar="<path>", help="the path to the example config "
|
||||
"(for automatic config updates)")
|
||||
args = parser.parse_args()
|
||||
|
||||
config = Config(args.config, args.base_config)
|
||||
@ -38,13 +43,14 @@ config.load()
|
||||
config.update()
|
||||
|
||||
logging.config.dictConfig(copy.deepcopy(config["logging"]))
|
||||
log = logging.getLogger("maubot")
|
||||
log = logging.getLogger("maubot.init")
|
||||
log.debug(f"Initializing maubot {__version__}")
|
||||
|
||||
db_engine: sql.engine.Engine = sql.create_engine(config["database"])
|
||||
db_factory = orm.sessionmaker(bind=db_engine)
|
||||
db_session = orm.scoping.scoped_session(db_factory)
|
||||
Base.metadata.bind=db_engine
|
||||
Base.metadata.bind = db_engine
|
||||
Base.metadata.create_all()
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
@ -52,8 +58,22 @@ init_db(db_session)
|
||||
init_client(loop)
|
||||
server = MaubotServer(config, loop)
|
||||
|
||||
loader_log = logging.getLogger("maubot.loader.zip")
|
||||
loader_log.debug("Preloading plugins...")
|
||||
for directory in config["plugin_directories"]:
|
||||
for file in os.listdir(directory):
|
||||
if not file.endswith(".mbp"):
|
||||
continue
|
||||
path = os.path.join(directory, file)
|
||||
try:
|
||||
loader = ZippedPluginLoader.get(path)
|
||||
loader_log.debug(f"Preloaded plugin {loader.id} from {loader.path}.")
|
||||
except MaubotZipImportError:
|
||||
loader_log.exception(f"Failed to load plugin at {path}.")
|
||||
|
||||
try:
|
||||
loop.run_until_complete(server.start())
|
||||
log.debug("Startup actions complete, running forever.")
|
||||
loop.run_forever()
|
||||
except KeyboardInterrupt:
|
||||
log.debug("Keyboard interrupt received, stopping...")
|
||||
|
@ -1 +1 @@
|
||||
__version__ = "0.1.0+dev"
|
||||
__version__ = "0.1.0.dev1"
|
||||
|
@ -13,14 +13,28 @@
|
||||
#
|
||||
# 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/>.
|
||||
import random
|
||||
import string
|
||||
|
||||
from mautrix.util import BaseConfig
|
||||
|
||||
|
||||
class Config(BaseConfig):
|
||||
@staticmethod
|
||||
def _new_token() -> str:
|
||||
return "".join(random.choice(string.ascii_lowercase + string.digits) for _ in range(64))
|
||||
|
||||
def update(self):
|
||||
base, copy, copy_dict = self._pre_update()
|
||||
copy("database")
|
||||
copy("plugin_directories")
|
||||
copy("server.hostname")
|
||||
copy("server.port")
|
||||
copy("server.listen")
|
||||
copy("server.base_path")
|
||||
shared_secret = self["server.shared_secret"]
|
||||
if shared_secret is None or shared_secret == "generate":
|
||||
base["server.shared_secret"] = self._new_token()
|
||||
else:
|
||||
base["server.shared_secret"] = shared_secret
|
||||
copy("logging")
|
||||
|
@ -60,7 +60,7 @@ class DBPlugin(Base):
|
||||
nullable=False)
|
||||
|
||||
|
||||
class DBClient(ClientStore, Base):
|
||||
class DBClient(Base):
|
||||
query: Query
|
||||
__tablename__ = "client"
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
from .abc import PluginLoader, PluginClass
|
||||
from .zip import ZippedPluginLoader
|
||||
from .zip import ZippedPluginLoader, MaubotZipImportError
|
||||
|
@ -21,12 +21,21 @@ from ..plugin_base import Plugin
|
||||
PluginClass = TypeVar("PluginClass", bound=Plugin)
|
||||
|
||||
|
||||
class IDConflictError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class PluginLoader(ABC):
|
||||
id_cache: Dict[str, 'PluginLoader'] = {}
|
||||
|
||||
id: str
|
||||
version: str
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def source(self) -> str:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def load(self) -> Type[PluginClass]:
|
||||
pass
|
||||
|
@ -20,7 +20,7 @@ import configparser
|
||||
|
||||
from ..lib.zipimport import zipimporter, ZipImportError
|
||||
from ..plugin_base import Plugin
|
||||
from .abc import PluginLoader, PluginClass
|
||||
from .abc import PluginLoader, PluginClass, IDConflictError
|
||||
|
||||
|
||||
class MaubotZipImportError(Exception):
|
||||
@ -36,35 +36,50 @@ class ZippedPluginLoader(PluginLoader):
|
||||
modules: List[str]
|
||||
main_class: str
|
||||
main_module: str
|
||||
loaded: bool
|
||||
_loaded: Type[PluginClass]
|
||||
_importer: zipimporter
|
||||
|
||||
def __init__(self, path: str) -> None:
|
||||
self.path = path
|
||||
self.id = None
|
||||
self.loaded = False
|
||||
self._loaded = None
|
||||
self._load_meta()
|
||||
self._run_preload_checks(self._get_importer())
|
||||
try:
|
||||
existing = self.id_cache[self.id]
|
||||
raise IDConflictError(f"Plugin with id {self.id} already loaded from {existing.source}")
|
||||
except KeyError:
|
||||
pass
|
||||
self.path_cache[self.path] = self
|
||||
self.id_cache[self.id] = self
|
||||
|
||||
@classmethod
|
||||
def get(cls, path: str) -> 'ZippedPluginLoader':
|
||||
try:
|
||||
return cls.path_cache[path]
|
||||
except KeyError:
|
||||
return cls(path)
|
||||
|
||||
@property
|
||||
def source(self) -> str:
|
||||
return self.path
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return ("<ZippedPlugin "
|
||||
f"path='{self.path}' "
|
||||
f"id='{self.id}' "
|
||||
f"loaded={self.loaded}>")
|
||||
f"loaded={self._loaded}>")
|
||||
|
||||
def _load_meta(self) -> None:
|
||||
try:
|
||||
file = ZipFile(self.path)
|
||||
data = file.read("maubot.ini")
|
||||
except FileNotFoundError as e:
|
||||
raise MaubotZipImportError(f"Maubot plugin not found at {self.path}") from e
|
||||
raise MaubotZipImportError("Maubot plugin not found") from e
|
||||
except BadZipFile as e:
|
||||
raise MaubotZipImportError(f"File at {self.path} is not a maubot plugin") from e
|
||||
raise MaubotZipImportError("File is not a maubot plugin") from e
|
||||
except KeyError as e:
|
||||
raise MaubotZipImportError(
|
||||
"File at {path} does not contain a maubot plugin definition") from e
|
||||
raise MaubotZipImportError("File does not contain a maubot plugin definition") from e
|
||||
config = configparser.ConfigParser()
|
||||
try:
|
||||
config.read_string(data.decode("utf-8"), source=f"{self.path}/maubot.ini")
|
||||
@ -77,8 +92,7 @@ class ZippedPluginLoader(PluginLoader):
|
||||
if "/" in main_class:
|
||||
main_module, main_class = main_class.split("/")[:2]
|
||||
except (configparser.Error, KeyError, IndexError, ValueError) as e:
|
||||
raise MaubotZipImportError(
|
||||
f"Maubot plugin definition in file at {self.path} is invalid") from e
|
||||
raise MaubotZipImportError("Maubot plugin definition in file is invalid") from e
|
||||
if self.id and meta_id != self.id:
|
||||
raise MaubotZipImportError("Maubot plugin ID changed during reload")
|
||||
self.id, self.version, self.modules = meta_id, version, modules
|
||||
@ -91,8 +105,7 @@ class ZippedPluginLoader(PluginLoader):
|
||||
importer.reset_cache()
|
||||
return importer
|
||||
except ZipImportError as e:
|
||||
raise MaubotZipImportError(
|
||||
f"File at {self.path} not found or not a maubot plugin") from e
|
||||
raise MaubotZipImportError("File not found or not a maubot plugin") from e
|
||||
|
||||
def _run_preload_checks(self, importer: zipimporter) -> None:
|
||||
try:
|
||||
@ -102,29 +115,30 @@ class ZippedPluginLoader(PluginLoader):
|
||||
f"Main class {self.main_class} not in {self.main_module}")
|
||||
except ZipImportError as e:
|
||||
raise MaubotZipImportError(
|
||||
f"Main module {self.main_module} not found in {self.path}") from e
|
||||
f"Main module {self.main_module} not found in file") from e
|
||||
for module in self.modules:
|
||||
try:
|
||||
importer.find_module(module)
|
||||
except ZipImportError as e:
|
||||
raise MaubotZipImportError(f"Module {module} not found in {self.path}") from e
|
||||
raise MaubotZipImportError(f"Module {module} not found in file") from e
|
||||
|
||||
def load(self) -> Type[PluginClass]:
|
||||
importer = self._get_importer(reset_cache=self.loaded)
|
||||
def load(self, reset_cache: bool = False) -> Type[PluginClass]:
|
||||
if self._loaded is not None and not reset_cache:
|
||||
return self._loaded
|
||||
importer = self._get_importer(reset_cache=reset_cache)
|
||||
self._run_preload_checks(importer)
|
||||
for module in self.modules:
|
||||
importer.load_module(module)
|
||||
self.loaded = True
|
||||
main_mod = sys.modules[self.main_module]
|
||||
plugin = getattr(main_mod, self.main_class)
|
||||
if not issubclass(plugin, Plugin):
|
||||
raise MaubotZipImportError(
|
||||
f"Main class of plugin at {self.path} does not extend maubot.Plugin")
|
||||
raise MaubotZipImportError("Main class of plugin does not extend maubot.Plugin")
|
||||
self._loaded = plugin
|
||||
return plugin
|
||||
|
||||
def reload(self) -> Type[PluginClass]:
|
||||
self.unload()
|
||||
return self.load()
|
||||
return self.load(reset_cache=True)
|
||||
|
||||
def unload(self) -> None:
|
||||
for name, mod in list(sys.modules.items()):
|
||||
|
@ -16,7 +16,7 @@
|
||||
from aiohttp import web
|
||||
import asyncio
|
||||
|
||||
from mautrix.api import PathBuilder
|
||||
from mautrix.api import PathBuilder, Method
|
||||
|
||||
from .config import Config
|
||||
from .__meta__ import __version__
|
||||
@ -29,13 +29,16 @@ class MaubotServer:
|
||||
self.config = config
|
||||
|
||||
path = PathBuilder(config["server.base_path"])
|
||||
self.app.router.add_get(path.version, self.version)
|
||||
self.add_route(Method.GET, path.version, self.version)
|
||||
|
||||
as_path = PathBuilder(config["server.appservice_base_path"])
|
||||
self.app.router.add_put(as_path.transactions, self.handle_transaction)
|
||||
self.add_route(Method.PUT, as_path.transactions, self.handle_transaction)
|
||||
|
||||
self.runner = web.AppRunner(self.app)
|
||||
|
||||
def add_route(self, method: Method, path: PathBuilder, handler) -> None:
|
||||
self.app.router.add_route(method.value, str(path), handler)
|
||||
|
||||
async def start(self) -> None:
|
||||
await self.runner.setup()
|
||||
site = web.TCPSite(self.runner, self.config["server.hostname"], self.config["server.port"])
|
||||
|
Loading…
Reference in New Issue
Block a user