Skip to content

Commit

Permalink
Updated implementation and requirements listing
Browse files Browse the repository at this point in the history
  • Loading branch information
harriebird committed Oct 14, 2024
1 parent 8126012 commit b0b129e
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 45 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Changelog

2024.2
------

* Added schema for Weblate Fedora Messaging.

2024.1
Expand Down
2 changes: 2 additions & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-r requirements.txt
build==1.2.2.post1
fedora-messaging
jsonschema[format]
pytest
pytest-cov
twine==5.1.1
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
fedora-messaging
fqdn
jsonschema[format]
jsonschema
rfc3987
strict-rfc3339
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 1 addition & 10 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
43 changes: 26 additions & 17 deletions weblate_schemas/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

"""Message class implementation for Weblate Fedora Messaging."""

from __future__ import annotations
from datetime import datetime

from . import load_schema
Expand Down Expand Up @@ -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", "")

Check warning on line 43 in weblate_schemas/messages.py

View check run for this annotation

Codecov / codecov/patch

weblate_schemas/messages.py#L43

Added line #L43 was not covered by tests

@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")

Check warning on line 68 in weblate_schemas/messages.py

View check run for this annotation

Codecov / codecov/patch

weblate_schemas/messages.py#L68

Added line #L68 was not covered by tests

@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."""
Expand Down
4 changes: 2 additions & 2 deletions weblate_schemas/schemas/weblate-messaging.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 7 additions & 7 deletions weblate_schemas/tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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"]
Expand All @@ -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."
)
14 changes: 7 additions & 7 deletions weblate_schemas/tests/test_valid.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,15 @@ 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/",
"component": "test",
}

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",
Expand All @@ -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",
Expand All @@ -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/",
Expand All @@ -338,15 +338,15 @@ def test_component():
}

removal_body = {
"id": 9,
"change_id": 9,
"action": "Removed project",
"timestamp": "2019-10-17T15:57:08.559420+00:00",
"target": "test",
"user": "testuser",
}

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",
Expand All @@ -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/",
Expand Down

0 comments on commit b0b129e

Please sign in to comment.