From 6cd004fcaf2b3da23e285c4b1d682867c7461b57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20Harriet=20Asi=C3=B1ero?= Date: Wed, 9 Oct 2024 02:17:35 +0800 Subject: [PATCH] Added a Message class for Weblate Fedora Messaging schema --- requirements.txt | 1 + weblate_schemas/message.py | 106 ++++++++++++++++++ .../schemas/weblate-messaging.schema.json | 2 +- 3 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 weblate_schemas/message.py diff --git a/requirements.txt b/requirements.txt index 4254446..730b779 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +fedora-messaging fqdn jsonschema rfc3987 diff --git a/weblate_schemas/message.py b/weblate_schemas/message.py new file mode 100644 index 0000000..767f726 --- /dev/null +++ b/weblate_schemas/message.py @@ -0,0 +1,106 @@ +# Copyright © Michal Čihař +# +# SPDX-License-Identifier: MIT + +"""Message class implementation for Weblate Fedora Messaging.""" + +from datetime import datetime + +from weblate_schemas import load_schema +from fedora_messaging import message + + +class BaseMessage(message.Message): + """Inherit the Message class from fedora_messaging.""" + + def __init__(self, **kwargs) -> None: + """Initialize BaseMessage class.""" + super().__init__(**kwargs) + + @property + def app_name(self) -> str: + """Return the app name.""" + return "Weblate" + + @property + def app_icon(self) -> str: + """Return the App icon URL.""" + return "https://weblate.org/static/weblate-128.png" + + +class WeblateMessage(BaseMessage): + """Actual Weblate message class which uses the Messaging schema.""" + + def __init__(self, **kwargs) -> None: + """Initialize the WeblateMessage class with loading of the body_schema.""" + super().__init__(**kwargs) + self.body_schema = load_schema("weblate-messaging.schema.json") + + @property + def agent_name(self) -> str: + """The username who cause the action.""" + return self.body["user"] + + @property + def id(self) -> int: + """Return the change ID.""" + return self.body["id"] + + @property + def action(self) -> str: + """Return the change verbose name.""" + return self.body["action"] + + @property + def timestamp(self) -> datetime: + """Return the timestamp of the change.""" + return self.body["timestamp"] + + @property + def target(self) -> str | list[str]: + """Return the new value of the change.""" + return self.body["target"] + + @property + def old(self) -> str | list[str]: + """Return the old value of the change.""" + return self.body["old"] + + @property + def source(self) -> str | list[str]: + """Return the source string.""" + return self.body["source"] + + @property + def url(self) -> str: + """Return the URL to the related object.""" + return self.body["url"] + + @property + def author(self) -> str: + """Return the author username.""" + return self.body["author"] + + @property + def user(self) -> str: + """Return the acting username.""" + return self.body["user"] + + @property + def project(self) -> str: + """Return the project slug.""" + return self.body["project"] + + @property + def component(self) -> str: + """Return the component slug.""" + return self.body["component"] + + @property + def translation(self) -> str: + """Return the translation language code.""" + return self.body["translation"] + + def __str__(self) -> str: + """Return a human-readable representation of the message.""" + return f"{self.action} {self.timestamp}" diff --git a/weblate_schemas/schemas/weblate-messaging.schema.json b/weblate_schemas/schemas/weblate-messaging.schema.json index 54c14e1..e843999 100644 --- a/weblate_schemas/schemas/weblate-messaging.schema.json +++ b/weblate_schemas/schemas/weblate-messaging.schema.json @@ -75,7 +75,7 @@ "type": "string" }, "translation": { - "title": "Translation", + "title": "Translation Language Code", "type": "string" } }