From 0d92c378c45e23be40db5efc066380931b26a84c Mon Sep 17 00:00:00 2001 From: Alex Kelly Date: Wed, 3 Nov 2021 17:02:22 -0400 Subject: [PATCH] feat: breakout formatters into their own plugin-like module to ease additonal formatters --- .gitignore | 1 + .../__pycache__/github.cpython-310.pyc | Bin 939 -> 939 bytes matrix_webhook/formatters/github.py | 6 +----- matrix_webhook/formatters/grafana.py | 6 +----- matrix_webhook/formatters/pingdom.py | 5 ++--- matrix_webhook/handler.py | 14 ++++++++------ 6 files changed, 13 insertions(+), 19 deletions(-) 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 ac481eee08159fabc91e44dfa5cece6e09fc400a..cc3038e13982fa28fb03d1913b176977db8b6822 100644 GIT binary patch delta 19 acmZ3@zM7pYpO=@5fq{X+;NwQFh0FjeWCVf$ delta 19 acmZ3@zM7pYpO=@5fq{Wx(u<8;3z-2eg9O_E 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"