diff --git a/matrix_alertbot/alertmanager.py b/matrix_alertbot/alertmanager.py index 2f813fd..27226e2 100644 --- a/matrix_alertbot/alertmanager.py +++ b/matrix_alertbot/alertmanager.py @@ -1,7 +1,7 @@ from __future__ import annotations import datetime -from typing import Any, Dict, List +from typing import Dict, List import aiohttp import pytimeparse @@ -32,7 +32,7 @@ class AlertmanagerClient: return await response.json() except ClientError as e: raise AlertmanagerServerError( - f"Cannot fetch alerts from Alertmanager" + "Cannot fetch alerts from Alertmanager" ) from e async def get_alert(self, fingerprint: str) -> Dict: diff --git a/matrix_alertbot/callback.py b/matrix_alertbot/callback.py index a099593..5bbe899 100644 --- a/matrix_alertbot/callback.py +++ b/matrix_alertbot/callback.py @@ -13,8 +13,8 @@ from nio import ( ) from matrix_alertbot.alertmanager import AlertmanagerClient -from matrix_alertbot.command import Command from matrix_alertbot.chat_functions import make_pill, send_text_to_room, strip_fallback +from matrix_alertbot.command import Command from matrix_alertbot.config import Config logger = logging.getLogger(__name__) diff --git a/matrix_alertbot/chat_functions.py b/matrix_alertbot/chat_functions.py index bf0530d..86e5481 100644 --- a/matrix_alertbot/chat_functions.py +++ b/matrix_alertbot/chat_functions.py @@ -1,11 +1,24 @@ import logging -from typing import Optional, Union +from typing import Dict, Optional, TypedDict, Union from nio import AsyncClient, ErrorResponse, Response, RoomSendResponse, SendRetryError +from typing_extensions import NotRequired logger = logging.getLogger(__name__) +ContentEventDict = TypedDict( + "ContentEventDict", + { + "msgtype": str, + "format": str, + "body": str, + "formatted_body": NotRequired[str], + "m.relates_to": NotRequired[Dict], + }, +) + + async def send_text_to_room( client: AsyncClient, room_id: str, @@ -37,7 +50,7 @@ async def send_text_to_room( # Determine whether to ping room members or not msgtype = "m.notice" if notice else "m.text" - content = { + content: ContentEventDict = { "msgtype": msgtype, "format": "org.matrix.custom.html", "body": plaintext, diff --git a/mypy.ini b/mypy.ini index 5ae56f9..cdd031b 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2,4 +2,7 @@ ignore_missing_imports = True disallow_untyped_defs = True disallow_untyped_calls = True -plugins = sqlalchemy.ext.mypy.plugin +exclude = + env + .env + .venv diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8831777 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,10 @@ +coverage>=6.4.1 +black>=22.6.0 +flake8>=4.0.1 +flake8-comprehensions>=3.10.0 +isort>=5.10.1 +mypy>=0.961 +pytest>=7.1.2 +pytest-asyncio>=0.18.3 +types-PyYAML>=6.0.9 +types-setuptools>=62.6.0 diff --git a/setup.cfg b/setup.cfg index 4702d91..2001691 100644 --- a/setup.cfg +++ b/setup.cfg @@ -7,6 +7,10 @@ # E731: do not assign a lambda expression, use a def # E501: Line too long (black enforces this for us) ignore=W503,W504,E203,E731,E501 +exclude = + env + .venv + .env [isort] line_length = 88 diff --git a/setup.py b/setup.py index 21246d3..c39d945 100644 --- a/setup.py +++ b/setup.py @@ -1,18 +1,11 @@ #!/usr/bin/env python3 import os -from typing import Dict, Tuple, Any +from typing import Tuple +import pkg_resources from setuptools import find_packages, setup -def exec_file(path_segments: Tuple) -> Dict[str, Any]: - """Execute a single python file to get the variables defined in it""" - result = {} - code = read_file(path_segments) - exec(code, result) - return result - - def read_file(path_segments: Tuple) -> str: """Read a file from the package. Takes a list of strings to join to make the path""" @@ -21,7 +14,7 @@ def read_file(path_segments: Tuple) -> str: return f.read() -version = exec_file(("matrix_alertbot", "__init__.py"))["__version__"] +version = pkg_resources.require("matrix_alertbot")[0].version long_description = read_file(("README.md",)) @@ -32,26 +25,23 @@ setup( description="A matrix bot to do amazing things!", packages=find_packages(exclude=["tests", "tests.*"]), install_requires=[ - "matrix-nio[e2e]>=0.10.0", - "Markdown>=3.1.1", - "PyYAML>=5.1.2", + "matrix-nio>=0.19.0", + "Markdown>=3.3.7", + "PyYAML>=6.0", + "aiohttp-prometheus-exporter>=0.2.4", + "aiotools>=1.5.9", + "aiohttp>=3.8.1", + "diskcache>=5.4.0", + "pytimeparse>=1.1.8", ], extras_require={ - "postgres": ["psycopg2>=2.8.5"], - "dev": [ - "isort==5.0.4", - "flake8==3.8.3", - "flake8-comprehensions==3.2.3", - "black==19.10b0", - ], + "e2e": ["matrix-nio[e2e]>=0.19.0"], }, classifiers=[ "License :: OSI Approved :: Apache Software License", "Programming Language :: Python :: 3 :: Only", - "Programming Language :: Python :: 3.5", - "Programming Language :: Python :: 3.6", - "Programming Language :: Python :: 3.7", - "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", ], long_description=long_description, long_description_content_type="text/markdown", diff --git a/tests/test_alertmanager.py b/tests/test_alertmanager.py index ff1a41f..b8b5dcf 100644 --- a/tests/test_alertmanager.py +++ b/tests/test_alertmanager.py @@ -3,7 +3,7 @@ from __future__ import annotations import json import unittest from typing import Any -from unittest.mock import MagicMock, Mock, patch +from unittest.mock import MagicMock, Mock import aiohttp import aiohttp.test_utils @@ -280,14 +280,14 @@ class AlertmanagerClientTestCase(unittest.IsolatedAsyncioTestCase): await alertmanager.delete_silences("fingerprint2") async def test_find_alert_happy(self) -> None: - alertmanager = AlertmanagerClient(f"http://localhost", self.fake_cache) + alertmanager = AlertmanagerClient("http://localhost", self.fake_cache) alert = alertmanager._find_alert( "fingerprint1", [{"fingerprint": "fingerprint1"}] ) self.assertEqual({"fingerprint": "fingerprint1"}, alert) async def test_find_alert_raise_alert_not_found(self) -> None: - alertmanager = AlertmanagerClient(f"http://localhost", self.fake_cache) + alertmanager = AlertmanagerClient("http://localhost", self.fake_cache) with self.assertRaises(AlertNotFoundError): alertmanager._find_alert("fingerprint2", [{"fingerprint": "fingerprint1"}])