From b0b129e56407f98467ce9c4fde0f73874f342a62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20Harriet=20Asi=C3=B1ero?= Date: Tue, 15 Oct 2024 00:13:46 +0800 Subject: [PATCH] Updated implementation and requirements listing --- .github/workflows/test.yml | 1 + CHANGES.rst | 1 + requirements-test.txt | 2 + requirements.txt | 3 +- setup.cfg | 4 ++ setup.py | 11 +---- weblate_schemas/messages.py | 43 +++++++++++-------- .../schemas/weblate-messaging.schema.json | 4 +- weblate_schemas/tests/test_message.py | 14 +++--- weblate_schemas/tests/test_valid.py | 14 +++--- 10 files changed, 52 insertions(+), 45 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f3de571..96321d1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -31,6 +31,7 @@ jobs: run: | python -m pip install --upgrade pip wheel pip install -r requirements.txt -r requirements-test.txt + pip install -e . - name: Test run: | py.test --cov=weblate_schemas weblate_schemas diff --git a/CHANGES.rst b/CHANGES.rst index 62fb91d..3dcfec6 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,6 +3,7 @@ Changelog 2024.2 ------ + * Added schema for Weblate Fedora Messaging. 2024.1 diff --git a/requirements-test.txt b/requirements-test.txt index 33993ac..d2a866c 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1,5 +1,7 @@ -r requirements.txt build==1.2.2.post1 +fedora-messaging +jsonschema[format] pytest pytest-cov twine==5.1.1 diff --git a/requirements.txt b/requirements.txt index 30c032c..4254446 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ -fedora-messaging fqdn -jsonschema[format] +jsonschema rfc3987 strict-rfc3339 diff --git a/setup.cfg b/setup.cfg index 44d22a1..c592ae7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -36,6 +36,10 @@ python_requires = >=3.9 include_package_data = 1 zip_safe = 0 +[options.entry_points] +fedora.messages = + weblate.message.v1 = weblate_schemas.messages:WeblateV1Message + [flake8] max-complexity = 16 extend-select = E,W1,W2,W3,W504,W505,W6,B diff --git a/setup.py b/setup.py index 96a6f16..28e310b 100755 --- a/setup.py +++ b/setup.py @@ -11,13 +11,4 @@ with open("requirements.txt") as handle: REQUIRES = list(handle.read().splitlines()) -setup( - name="weblate_schemas", - entry_points={ - "fedora.messages": [ - "base.message=weblate_schemas.messages:BaseMessage", - "weblate.message=weblate_schemas.messages:WeblateV1Message", - ] - }, - install_requires=REQUIRES, -) +setup(install_requires=REQUIRES) diff --git a/weblate_schemas/messages.py b/weblate_schemas/messages.py index 1068a8a..0d7ef7f 100644 --- a/weblate_schemas/messages.py +++ b/weblate_schemas/messages.py @@ -4,6 +4,7 @@ """Message class implementation for Weblate Fedora Messaging.""" +from __future__ import annotations from datetime import datetime from . import load_schema @@ -39,78 +40,86 @@ def __init__(self, **kwargs) -> None: @property def agent_name(self) -> str: """The username who cause the action.""" - return self.body["user"] + return self.body.get("user", "") @property - def id(self) -> int: + def change_id(self) -> int: """Return the change ID.""" - return self.body["id"] + return self.body.get("change_id") @property def action(self) -> str: """Return the change verbose name.""" - return self.body["action"] + return self.body.get("action") @property def timestamp(self) -> datetime: """Return the timestamp of the change.""" - return self.body["timestamp"] + return self.body.get("timestamp") @property def target(self) -> str | list[str]: """Return the new value of the change.""" - return self.body["target"] + return self.body.get("target") @property def old(self) -> str | list[str]: """Return the old value of the change.""" - return self.body["old"] + return self.body.get("old") @property def source(self) -> str | list[str]: """Return the source string.""" - return self.body["source"] + return self.body.get("source") @property def url(self) -> str: """Return the URL to the related object.""" - return self.body["url"] + return self.body.get("url") @property def author(self) -> str: """Return the author username.""" - return self.body["author"] + return self.body.get("author", "") @property def user(self) -> str: """Return the acting username.""" - return self.body["user"] + return self.body.get("user", "") @property def project(self) -> str: """Return the project slug.""" - return self.body["project"] + return self.body.get("project", "") @property def component(self) -> str: """Return the component slug.""" - return self.body["component"] + return self.body.get("component", "") @property def translation(self) -> str: """Return the translation language code.""" - return self.body["translation"] + return self.body.get("translation", "") @property def summary(self) -> str: """Return the message summary.""" - return f"{self.body['action']} event occurred in {self.body['timestamp']}" + return "{} event{} occurred in {}.".format( + self.body.get("action"), + " done by {}".format(self.body.get("user")) + if self.body.get("user", None) + else "", + self.body.get("timestamp"), + ) @property def usernames(self): """Return the usernames involved.""" - users = {self.body["author"], self.body["user"]} - return list(users) + users = [] + users += [self.author] if self.author else [] + users += [self.user] if self.user else [] + return sorted(set(users)) def __str__(self) -> str: """Return a human-readable representation of the message.""" diff --git a/weblate_schemas/schemas/weblate-messaging.schema.json b/weblate_schemas/schemas/weblate-messaging.schema.json index e843999..b0d297f 100644 --- a/weblate_schemas/schemas/weblate-messaging.schema.json +++ b/weblate_schemas/schemas/weblate-messaging.schema.json @@ -7,12 +7,12 @@ "definitions": {}, "type": "object", "required": [ - "id", + "change_id", "action", "timestamp" ], "properties": { - "id": { + "change_id": { "title": "Change ID", "type": "integer", "description": "Numerical ID of change" diff --git a/weblate_schemas/tests/test_message.py b/weblate_schemas/tests/test_message.py index 5721f81..18c40a4 100644 --- a/weblate_schemas/tests/test_message.py +++ b/weblate_schemas/tests/test_message.py @@ -24,21 +24,21 @@ def test_weblate_merge_message() -> None: weblate_message = WeblateV1Message(topic="test.topic", body=merge_body) assert weblate_message.app_name == "Weblate" assert weblate_message.app_icon == "https://weblate.org/static/weblate-128.png" - assert weblate_message.id == merge_body["id"] + assert weblate_message.change_id == merge_body["change_id"] assert weblate_message.action == merge_body["action"] assert weblate_message.timestamp == merge_body["timestamp"] assert weblate_message.url == merge_body["url"] assert weblate_message.component == merge_body["component"] assert ( weblate_message.summary - == "Merged repository event occurred in 2017-06-15T11:30:47.325000+00:00" + == "Merged repository event occurred in 2017-06-15T11:30:47.325000+00:00." ) def test_weblate_new_string_message() -> None: """Test WeblateV1Message class with a new string event message body.""" weblate_message = WeblateV1Message(topic="test.topic", body=new_string_body) - assert weblate_message.id == new_string_body["id"] + assert weblate_message.change_id == new_string_body["change_id"] assert weblate_message.action == new_string_body["action"] assert weblate_message.timestamp == new_string_body["timestamp"] assert weblate_message.url == new_string_body["url"] @@ -47,14 +47,14 @@ def test_weblate_new_string_message() -> None: assert weblate_message.source == new_string_body["source"] assert ( weblate_message.summary - == "New source string event occurred in 2017-06-15T11:30:47.372000+00:00" + == "New source string event occurred in 2017-06-15T11:30:47.372000+00:00." ) def test_weblate_new_translation_message() -> None: """Test WeblateV1Message class with a new translation event message body.""" weblate_message = WeblateV1Message(topic="test.topic", body=new_translation_body) - assert weblate_message.id == new_translation_body["id"] + assert weblate_message.change_id == new_translation_body["change_id"] assert weblate_message.action == new_translation_body["action"] assert weblate_message.timestamp == new_translation_body["timestamp"] assert weblate_message.url == new_translation_body["url"] @@ -67,6 +67,6 @@ def test_weblate_new_translation_message() -> None: assert weblate_message.source == new_translation_body["source"] assert weblate_message.usernames == ["testuser"] assert ( - weblate_message.summary - == "New translation event occurred in 2019-10-17T15:57:08.772591+00:00" + weblate_message.summary == "New translation event done by testuser occurred " + "in 2019-10-17T15:57:08.772591+00:00." ) diff --git a/weblate_schemas/tests/test_valid.py b/weblate_schemas/tests/test_valid.py index c4bf38b..bbce385 100644 --- a/weblate_schemas/tests/test_valid.py +++ b/weblate_schemas/tests/test_valid.py @@ -296,7 +296,7 @@ def test_component(): merge_body = { - "id": 1, + "change_id": 1, "action": "Merged repository", "timestamp": "2017-06-15T11:30:47.325000+00:00", "url": "https://example.com/projects/test/test/", @@ -304,7 +304,7 @@ def test_component(): } new_string_body = { - "id": 2, + "change_id": 2, "action": "New source string", "timestamp": "2017-06-15T11:30:47.372000+00:00", "url": "https://example.com/translate/test/test/cs/?checksum=6412684aaf018e8e", @@ -314,7 +314,7 @@ def test_component(): } new_translation_body = { - "id": 12, + "change_id": 12, "action": "New translation", "timestamp": "2019-10-17T15:57:08.772591+00:00", "url": "https://example.com/translate/test/test/cs/?checksum=6412684aaf018e8e", @@ -328,7 +328,7 @@ def test_component(): } resource_update_body = { - "id": 6, + "change_id": 6, "action": "Resource update", "timestamp": "2017-06-15T11:30:47.410000+00:00", "url": "https://example.com/projects/test/test/cs/", @@ -338,7 +338,7 @@ def test_component(): } removal_body = { - "id": 9, + "change_id": 9, "action": "Removed project", "timestamp": "2019-10-17T15:57:08.559420+00:00", "target": "test", @@ -346,7 +346,7 @@ def test_component(): } new_contributor_body = { - "id": 11, + "change_id": 11, "action": "New contributor", "timestamp": "2019-10-17T15:57:08.759960+00:00", "url": "https://example.com/translate/test/test/cs/?checksum=6412684aaf018e8e", @@ -359,7 +359,7 @@ def test_component(): } invalid_body = { - "id": 1, + "change_id": 1, "action": "Merged repository", "timestamp": "invalid datetime", "url": "https://example.com/projects/test/test/",