diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..cab3bdf --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,29 @@ +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.4.0 + hooks: + - id: check-added-large-files + - id: check-ast + - id: check-merge-conflict + - id: check-symlinks + - id: check-toml + - id: check-yaml + - id: debug-statements + - id: detect-private-key + - id: end-of-file-fixer + - id: flake8 + - id: mixed-line-ending + - id: trailing-whitespace +- repo: https://github.com/pre-commit/mirrors-yapf + rev: v0.29.0 + hooks: + - id: yapf +- repo: https://github.com/PyCQA/pydocstyle + rev: 5.0.1 + hooks: + - id: pydocstyle +- repo: https://gitlab.com/smop/pre-commit-hooks + rev: v1.0.0 + hooks: + - id: check-poetry + - id: check-gitlab-ci diff --git a/matrix_webhook.py b/matrix_webhook.py index ce02bae..67b60b2 100755 --- a/matrix_webhook.py +++ b/matrix_webhook.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 """ -Matrix Webhook +Matrix Webhook. + Post a message to a matrix room with a simple HTTP POST v1: matrix-client & http.server v2: matrix-nio & aiohttp @@ -13,7 +14,6 @@ from signal import SIGINT, SIGTERM from aiohttp import web from nio import AsyncClient -from nio.rooms import MatrixRoom SERVER_ADDRESS = ('', int(os.environ.get('PORT', 4785))) MATRIX_URL = os.environ.get('MATRIX_URL', 'https://matrix.org') @@ -26,6 +26,7 @@ CLIENT = AsyncClient(MATRIX_URL, MATRIX_ID) async def handler(request): """ Coroutine given to the server, st. it knows what to do with an HTTP request. + This one handles a POST, checks its content, and forwards it to the matrix room. """ data = await request.read() @@ -49,11 +50,10 @@ async def handler(request): async def main(event): """ - Main coroutine + Launch main coroutine. matrix client login & start web server """ - await CLIENT.login(MATRIX_PW) server = web.Server(handler) @@ -71,6 +71,7 @@ async def main(event): def terminate(event, signal): + """Close handling stuff.""" event.set() loop = asyncio.get_event_loop() loop.remove_signal_handler(signal)