From 9bc4d0348d0ffaa0458467f1723cb7bde19883ab Mon Sep 17 00:00:00 2001 From: kku Date: Wed, 1 Jan 2025 22:35:17 +0100 Subject: [PATCH 1/4] WIP: Matrix support (with SMBL) --- pyproject.toml | 1 + requirements/matrix.txt | 1 + src/libbot/matrix/__init__.py | 2 + src/libbot/matrix/classes/__init__.py | 1 + src/libbot/matrix/classes/bot.py | 63 +++++++++++++++++++++++++++ 5 files changed, 68 insertions(+) create mode 100644 requirements/matrix.txt create mode 100644 src/libbot/matrix/__init__.py create mode 100644 src/libbot/matrix/classes/__init__.py create mode 100644 src/libbot/matrix/classes/bot.py diff --git a/pyproject.toml b/pyproject.toml index 5748f8d..b9f673f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,7 @@ dependencies = { file = "requirements/_.txt" } [tool.setuptools.dynamic.optional-dependencies] dev = { file = "requirements/dev.txt" } +matrix = { file = "requirements/matrix.txt" } pycord = { file = "requirements/pycord.txt" } pyrogram = { file = "requirements/pyrogram.txt" } speed = { file = "requirements/speed.txt" } diff --git a/requirements/matrix.txt b/requirements/matrix.txt new file mode 100644 index 0000000..244adad --- /dev/null +++ b/requirements/matrix.txt @@ -0,0 +1 @@ +simplematrixbotlib~=2.12.1 \ No newline at end of file diff --git a/src/libbot/matrix/__init__.py b/src/libbot/matrix/__init__.py new file mode 100644 index 0000000..a64b6f3 --- /dev/null +++ b/src/libbot/matrix/__init__.py @@ -0,0 +1,2 @@ +# This file is left empty on purpose +# Adding imports here will cause import errors when libbot[matrix] is not installed diff --git a/src/libbot/matrix/classes/__init__.py b/src/libbot/matrix/classes/__init__.py new file mode 100644 index 0000000..39a0ed6 --- /dev/null +++ b/src/libbot/matrix/classes/__init__.py @@ -0,0 +1 @@ +from .bot import MatrixBot diff --git a/src/libbot/matrix/classes/bot.py b/src/libbot/matrix/classes/bot.py new file mode 100644 index 0000000..4004e40 --- /dev/null +++ b/src/libbot/matrix/classes/bot.py @@ -0,0 +1,63 @@ +import logging +from logging import Logger +from pathlib import Path +from typing import Dict, Any + +from typing_extensions import override + +from ...i18n.classes import BotLocale +from ...utils import json_read + +try: + from apscheduler.schedulers.asyncio import AsyncIOScheduler + from apscheduler.schedulers.background import BackgroundScheduler + from simplematrixbotlib import Bot, Creds, Config +except ImportError as exc: + raise ImportError("You need to install libbot[matrix] in order to use this class.") from exc + +logger: Logger = logging.getLogger(__name__) + + +class MatrixBot(Bot): + @override + def __init__( + self, + config: Dict[str, Any] | None = None, + config_path: str | Path = Path("config.json"), + locales_root: str | Path | None = None, + scheduler: AsyncIOScheduler | BackgroundScheduler | None = None, + smbl_creds: Creds = None, + smbl_config: Config = None, + ): + self.config: Dict[str, Any] = config if config is not None else json_read(config_path) + + super().__init__( + creds=smbl_creds if smbl_creds is not None else Creds(homeserver=self.config["homeserver"], + username=self.config["username"], + password=self.config["password"], + device_name=self.config["device_name"]), + config=smbl_config, + ) + + self.bot_locale: BotLocale = BotLocale( + default_locale=self.config["locale"], + locales_root=(Path("locale") if locales_root is None else locales_root), + ) + self.default_locale: str = self.bot_locale.default + self.locales: Dict[str, Any] = self.bot_locale.locales + + self._ = self.bot_locale._ + self.in_all_locales = self.bot_locale.in_all_locales + self.in_every_locale = self.bot_locale.in_every_locale + + self.scheduler: AsyncIOScheduler | BackgroundScheduler | None = scheduler + + @override + def run(self, scheduler_start: bool = True, scheduler_shutdown: bool = True, scheduler_wait: bool = True) -> None: + if self.scheduler is not None and scheduler_start: + self.scheduler.start() + + super().run() + + if self.scheduler is not None and scheduler_shutdown: + self.scheduler.shutdown(scheduler_wait) -- 2.39.5 From 5a244f603d18ae2a672dfdff581692dd62a4893d Mon Sep 17 00:00:00 2001 From: kku Date: Thu, 2 Jan 2025 13:51:36 +0100 Subject: [PATCH 2/4] TEST: Publishing Action --- .gitea/workflows/publish.yml | 90 ++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 .gitea/workflows/publish.yml diff --git a/.gitea/workflows/publish.yml b/.gitea/workflows/publish.yml new file mode 100644 index 0000000..5809a53 --- /dev/null +++ b/.gitea/workflows/publish.yml @@ -0,0 +1,90 @@ +name: Upload Python Package + +on: + release: + types: [ published ] + +permissions: + contents: read + +jobs: + release-build: + runs-on: ubuntu-latest + container: catthehacker/ubuntu:act-latest + + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v5 + with: + python-version: "3.x" + + - name: Build release distributions + run: | + python -m pip install build + python -m build + + - name: Upload distributions + uses: christopherhx/gitea-upload-artifact@v4 + with: + name: release-dists + path: dist/ + + gitea-publish: + runs-on: ubuntu-latest + container: catthehacker/ubuntu:act-latest + + needs: + - release-build + + permissions: + id-token: write + + environment: + name: gitea + url: https://git.end-play.xyz/profitroll/-/packages/pypi/libbot + + env: + GITHUB_WORKFLOW_REF: ${{ gitea.workflow_ref }} + INPUT_REPOSITORY_URL: https://git.end-play.xyz/api/packages/profitroll/pypi + + steps: + - name: Retrieve release distributions + uses: christopherhx/gitea-download-artifact@v4 + with: + name: release-dists + path: dist/ + + - name: Publish package distributions to TestPyPI + uses: pypa/gh-action-pypi-publish@release/v1 + with: + password: ${{ secrets.PYPI_GITEA_API_TOKEN }} + repository-url: https://git.end-play.xyz/api/packages/profitroll/pypi + + pypi-publish: + runs-on: ubuntu-latest + container: catthehacker/ubuntu:act-latest + + needs: + - release-build + + permissions: + id-token: write + + environment: + name: pypi + + env: + GITHUB_WORKFLOW_REF: ${{ gitea.workflow_ref }} + + steps: + - name: Retrieve release distributions + uses: christopherhx/gitea-download-artifact@v4 + with: + name: release-dists + path: dist/ + + - name: Publish package distributions to TestPyPI + uses: pypa/gh-action-pypi-publish@release/v1 + with: + password: ${{ secrets.PYPI_PYPI_API_TOKEN }} \ No newline at end of file -- 2.39.5 From b76f7272634c508b10becfcd0c18ccf35cd677ad Mon Sep 17 00:00:00 2001 From: profitroll Date: Sat, 4 Jan 2025 17:49:44 +0100 Subject: [PATCH 3/4] Replaced MatrixBot.config with MatrixBot.bot_config because .config is already used by smbl --- src/libbot/matrix/classes/bot.py | 36 +++++++++++++++++++------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/libbot/matrix/classes/bot.py b/src/libbot/matrix/classes/bot.py index 4004e40..85d79f7 100644 --- a/src/libbot/matrix/classes/bot.py +++ b/src/libbot/matrix/classes/bot.py @@ -21,26 +21,32 @@ logger: Logger = logging.getLogger(__name__) class MatrixBot(Bot): @override def __init__( - self, - config: Dict[str, Any] | None = None, - config_path: str | Path = Path("config.json"), - locales_root: str | Path | None = None, - scheduler: AsyncIOScheduler | BackgroundScheduler | None = None, - smbl_creds: Creds = None, - smbl_config: Config = None, + self, + config: Dict[str, Any] | None = None, + config_path: str | Path = Path("config.json"), + locales_root: str | Path | None = None, + scheduler: AsyncIOScheduler | BackgroundScheduler | None = None, + smbl_creds: Creds = None, + smbl_config: Config = None, ): - self.config: Dict[str, Any] = config if config is not None else json_read(config_path) + self.bot_config: Dict[str, Any] = config if config is not None else json_read(config_path) super().__init__( - creds=smbl_creds if smbl_creds is not None else Creds(homeserver=self.config["homeserver"], - username=self.config["username"], - password=self.config["password"], - device_name=self.config["device_name"]), + creds=( + smbl_creds + if smbl_creds is not None + else Creds( + homeserver=self.bot_config["bot"]["homeserver"], + username=self.bot_config["bot"]["username"], + password=self.bot_config["bot"]["password"], + device_name=self.bot_config["bot"]["device_name"], + ) + ), config=smbl_config, ) self.bot_locale: BotLocale = BotLocale( - default_locale=self.config["locale"], + default_locale=self.bot_config["locale"], locales_root=(Path("locale") if locales_root is None else locales_root), ) self.default_locale: str = self.bot_locale.default @@ -53,7 +59,9 @@ class MatrixBot(Bot): self.scheduler: AsyncIOScheduler | BackgroundScheduler | None = scheduler @override - def run(self, scheduler_start: bool = True, scheduler_shutdown: bool = True, scheduler_wait: bool = True) -> None: + def run( + self, scheduler_start: bool = True, scheduler_shutdown: bool = True, scheduler_wait: bool = True + ) -> None: if self.scheduler is not None and scheduler_start: self.scheduler.start() -- 2.39.5 From a458788841b5301139fdfeda387f31dd65350529 Mon Sep 17 00:00:00 2001 From: profitroll Date: Sat, 4 Jan 2025 18:01:49 +0100 Subject: [PATCH 4/4] Added device info and bot prefix --- src/libbot/matrix/classes/bot.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/libbot/matrix/classes/bot.py b/src/libbot/matrix/classes/bot.py index 85d79f7..512097f 100644 --- a/src/libbot/matrix/classes/bot.py +++ b/src/libbot/matrix/classes/bot.py @@ -5,6 +5,7 @@ from typing import Dict, Any from typing_extensions import override +from ... import __version__ as __libbot_version__ from ...i18n.classes import BotLocale from ...utils import json_read @@ -39,12 +40,20 @@ class MatrixBot(Bot): homeserver=self.bot_config["bot"]["homeserver"], username=self.bot_config["bot"]["username"], password=self.bot_config["bot"]["password"], - device_name=self.bot_config["bot"]["device_name"], + device_name=( + f"LibBotUniversal v{__libbot_version__}" + if "device_name" not in self.bot_config["bot"] + else self.bot_config["bot"]["device_name"] + ), ) ), config=smbl_config, ) + self.bot_prefix: str = ( + "!" if "prefix" not in self.bot_config["bot"] else self.bot_config["bot"]["prefix"] + ) + self.bot_locale: BotLocale = BotLocale( default_locale=self.bot_config["locale"], locales_root=(Path("locale") if locales_root is None else locales_root), -- 2.39.5