diff --git a/.gitignore b/.gitignore index 9edda3c..bd13aaa 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .mypy_cache coverage.xml htmlcov +**__pycache__ diff --git a/matrix_webhook/formatters/__pycache__/github.cpython-310.pyc b/matrix_webhook/formatters/__pycache__/github.cpython-310.pyc index ac481ee..cc3038e 100644 Binary files a/matrix_webhook/formatters/__pycache__/github.cpython-310.pyc and b/matrix_webhook/formatters/__pycache__/github.cpython-310.pyc differ diff --git a/matrix_webhook/formatters/github.py b/matrix_webhook/formatters/github.py index 9f6226e..920a9b5 100644 --- a/matrix_webhook/formatters/github.py +++ b/matrix_webhook/formatters/github.py @@ -1,8 +1,4 @@ -import plugins - - -@plugins.register -def github(data, headers): +def formatter(data, headers): """Pretty-print a github notification.""" # TODO: Write nice useful formatters. This is only an example. if headers["X-GitHub-Event"] == "push": diff --git a/matrix_webhook/formatters/grafana.py b/matrix_webhook/formatters/grafana.py index e78344f..0117e63 100644 --- a/matrix_webhook/formatters/grafana.py +++ b/matrix_webhook/formatters/grafana.py @@ -1,8 +1,4 @@ -import plugins - - -@plugins.register -def grafana(data, headers): +def formatter(data, headers): """Pretty-print a grafana notification.""" text = "" if "title" in data: diff --git a/matrix_webhook/formatters/pingdom.py b/matrix_webhook/formatters/pingdom.py index 1bffb2f..ee8fada 100644 --- a/matrix_webhook/formatters/pingdom.py +++ b/matrix_webhook/formatters/pingdom.py @@ -1,8 +1,7 @@ -import plugins +from datetime import datetime -@plugin.register -def pingdom(data, headers): +def formatter(data, headers): """Pretty-print a pingdom notification.""" #JSON data formatting was obtained from https://www.pingdom.com/resources/webhooks/ check_id = data["check_id"] diff --git a/matrix_webhook/handler.py b/matrix_webhook/handler.py index b3c81df..fbf94e1 100644 --- a/matrix_webhook/handler.py +++ b/matrix_webhook/handler.py @@ -4,11 +4,11 @@ import json import logging from http import HTTPStatus from hmac import HMAC -import plugins +import importlib from markdown import markdown -from . import conf, formatters, utils +from . import conf, utils LOGGER = logging.getLogger("matrix_webhook.handler") @@ -19,7 +19,6 @@ async def matrix_webhook(request): This one handles a POST, checks its content, and forwards it to the matrix room. """ - formatters = plugins.names_factory(__package__) LOGGER.debug(f"Handling {request=}") data_b = await request.read() @@ -38,9 +37,12 @@ async def matrix_webhook(request): if "formatter" in request.rel_url.query: try: - data = getattr(formatters, request.rel_url.query["formatter"])( - data, request.headers - ) +# data = getattr(formatters, request.rel_url.query["formatter"])( +# data, request.headers +# ) + format = request.rel_url.query["formatter"] + plugin = importlib.import_module(f"matrix_webhook.formatters.{format}", "formatter") + data = plugin.formatter(data, request.headers) except AttributeError: return utils.create_json_response( HTTPStatus.BAD_REQUEST, "Unknown formatter"