Skip to content

Commit

Permalink
chore(add-ons): use set to store list of events
Browse files Browse the repository at this point in the history
This is more straightforward to handle as these are actually
used as sets.
  • Loading branch information
nijel committed Nov 26, 2024
1 parent 0e41982 commit 2548dcd
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 40 deletions.
5 changes: 4 additions & 1 deletion weblate/addons/autotranslate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@


class AutoTranslateAddon(BaseAddon):
events = (AddonEvent.EVENT_COMPONENT_UPDATE, AddonEvent.EVENT_DAILY)
events: set[AddonEvent] = {
AddonEvent.EVENT_COMPONENT_UPDATE,
AddonEvent.EVENT_DAILY,
}
name = "weblate.autotranslate.autotranslate"
verbose = gettext_lazy("Automatic translation")
description = gettext_lazy(
Expand Down
14 changes: 10 additions & 4 deletions weblate/addons/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from django.utils.functional import cached_property
from django.utils.translation import gettext

from weblate.addons.events import AddonEvent
from weblate.addons.events import POST_CONFIGURE_EVENTS, AddonEvent
from weblate.trans.exceptions import FileParseError
from weblate.trans.models import Component
from weblate.trans.util import get_clean_env
Expand Down Expand Up @@ -43,7 +43,7 @@ class CompatDict(TypedDict, total=False):
class BaseAddon:
"""Base class for Weblate add-ons."""

events: tuple[AddonEvent, ...] = ()
events: set[AddonEvent] = set()
settings_form: type[BaseAddonForm] | None = None
name = ""
compat: CompatDict = {}
Expand Down Expand Up @@ -185,6 +185,8 @@ def post_configure_run(self) -> None:
def post_configure_run_component(self, component) -> None:
# Trigger post configure event for a VCS component
previous = component.repository.last_revision
if not (POST_CONFIGURE_EVENTS & self.events):
return

if AddonEvent.EVENT_POST_COMMIT in self.events:
component.log_debug("running post_commit add-on: %s", self.name)
Expand Down Expand Up @@ -421,7 +423,9 @@ class UpdateBaseAddon(BaseAddon):
It hooks to post update and commits all changed translations.
"""

events: tuple[AddonEvent, ...] = (AddonEvent.EVENT_POST_UPDATE,)
events: set[AddonEvent] = {
AddonEvent.EVENT_POST_UPDATE,
}

@staticmethod
def iterate_translations(component: Component):
Expand All @@ -445,5 +449,7 @@ def post_update(
class StoreBaseAddon(BaseAddon):
"""Base class for add-ons tweaking store."""

events: tuple[AddonEvent, ...] = (AddonEvent.EVENT_STORE_POST_LOAD,)
events: set[AddonEvent] = {
AddonEvent.EVENT_STORE_POST_LOAD,
}
icon = "wrench.svg"
4 changes: 2 additions & 2 deletions weblate/addons/cdn.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@


class CDNJSAddon(BaseAddon):
events = (
events = {
AddonEvent.EVENT_DAILY,
AddonEvent.EVENT_POST_COMMIT,
AddonEvent.EVENT_POST_UPDATE,
)
}
name = "weblate.cdn.cdnjs"
verbose = gettext_lazy("JavaScript localization CDN")
description = gettext_lazy(
Expand Down
10 changes: 8 additions & 2 deletions weblate/addons/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ class CleanupAddon(BaseCleanupAddon):
"no longer present in the base file."
)
icon = "eraser.svg"
events = (AddonEvent.EVENT_PRE_COMMIT, AddonEvent.EVENT_POST_UPDATE)
events: set[AddonEvent] = {
AddonEvent.EVENT_PRE_COMMIT,
AddonEvent.EVENT_POST_UPDATE,
}

@classmethod
def can_install_format(cls, component: Component) -> bool:
Expand Down Expand Up @@ -76,7 +79,10 @@ class RemoveBlankAddon(BaseCleanupAddon):
description = gettext_lazy(
"Removes strings without a translation from translation files."
)
events = (AddonEvent.EVENT_POST_COMMIT, AddonEvent.EVENT_POST_UPDATE)
events: set[AddonEvent] = {
AddonEvent.EVENT_POST_COMMIT,
AddonEvent.EVENT_POST_UPDATE,
}
icon = "eraser.svg"

def update_translations(self, component: Component, previous_head: str) -> None:
Expand Down
2 changes: 1 addition & 1 deletion weblate/addons/consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


class LanguageConsistencyAddon(BaseAddon):
events = (AddonEvent.EVENT_DAILY, AddonEvent.EVENT_POST_ADD)
events: set[AddonEvent] = {AddonEvent.EVENT_DAILY, AddonEvent.EVENT_POST_ADD}
name = "weblate.consistency.languages"
verbose = gettext_lazy("Add missing languages")
description = gettext_lazy(
Expand Down
4 changes: 3 additions & 1 deletion weblate/addons/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@


class DiscoveryAddon(BaseAddon):
events = (AddonEvent.EVENT_POST_UPDATE,)
events: set[AddonEvent] = {
AddonEvent.EVENT_POST_UPDATE,
}
name = "weblate.discovery.discovery"
verbose = gettext_lazy("Component discovery")
description = gettext_lazy(
Expand Down
33 changes: 21 additions & 12 deletions weblate/addons/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@


class AddonEvent(IntegerChoices):
EVENT_POST_PUSH = 1, "repository post-push"
EVENT_POST_UPDATE = 2, "repository post-update"
EVENT_PRE_COMMIT = 3, "repository pre-commit"
EVENT_POST_COMMIT = 4, "repository post-commit"
EVENT_POST_ADD = 5, "repository post-add"
EVENT_UNIT_PRE_CREATE = 6, "unit post-create"
EVENT_STORE_POST_LOAD = 7, "storage post-load"
EVENT_UNIT_POST_SAVE = 8, "unit post-save"
EVENT_PRE_UPDATE = 9, "repository pre-update"
EVENT_PRE_PUSH = 10, "repository pre-push"
EVENT_DAILY = 11, "daily"
EVENT_COMPONENT_UPDATE = 12, "component update"
EVENT_POST_PUSH = 1, "Repository post-push"
EVENT_POST_UPDATE = 2, "Repository post-update"
EVENT_PRE_COMMIT = 3, "Repository pre-commit"
EVENT_POST_COMMIT = 4, "Repository post-commit"
EVENT_POST_ADD = 5, "Repository post-add"
EVENT_UNIT_PRE_CREATE = 6, "Unit pre-create"
EVENT_STORE_POST_LOAD = 7, "Storage post-load"
EVENT_UNIT_POST_SAVE = 8, "Unit post-save"
EVENT_PRE_UPDATE = 9, "Repository pre-update"
EVENT_PRE_PUSH = 10, "Repository pre-push"
EVENT_DAILY = 11, "Daily"
EVENT_COMPONENT_UPDATE = 12, "Component update"


POST_CONFIGURE_EVENTS = {
AddonEvent.EVENT_POST_COMMIT,
AddonEvent.EVENT_POST_UPDATE,
AddonEvent.EVENT_COMPONENT_UPDATE,
AddonEvent.EVENT_POST_PUSH,
AddonEvent.EVENT_DAILY,
}
4 changes: 3 additions & 1 deletion weblate/addons/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class ExampleAddon(BaseAddon):
# matched against property of component
compat = {"file_format": {"po", "po-mono"}}
# List of events add-on should receive
events = (AddonEvent.EVENT_PRE_COMMIT,)
events: set[AddonEvent] = {
AddonEvent.EVENT_PRE_COMMIT,
}
# Add-on unique identifier
name = "weblate.example.example"
# Verbose name shown in the user interface
Expand Down
4 changes: 3 additions & 1 deletion weblate/addons/example_pre.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

class ExamplePreAddon(BaseScriptAddon):
# Event used to trigger the script
events = (AddonEvent.EVENT_PRE_COMMIT,)
events: set[AddonEvent] = {
AddonEvent.EVENT_PRE_COMMIT,
}
# Name of the addon, has to be unique
name = "weblate.example.pre"
# Verbose name and long description
Expand Down
8 changes: 6 additions & 2 deletions weblate/addons/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@


class FlagBase(BaseAddon):
events = (AddonEvent.EVENT_UNIT_PRE_CREATE,)
events: set[AddonEvent] = {
AddonEvent.EVENT_UNIT_PRE_CREATE,
}
icon = "flag.svg"

@classmethod
Expand Down Expand Up @@ -91,7 +93,9 @@ def unit_pre_create(self, unit) -> None:


class BulkEditAddon(BaseAddon):
events = (AddonEvent.EVENT_COMPONENT_UPDATE,)
events: set[AddonEvent] = {
AddonEvent.EVENT_COMPONENT_UPDATE,
}
name = "weblate.flags.bulk"
verbose = gettext_lazy("Bulk edit")
description = gettext_lazy("Bulk edit flags, labels, or states of strings.")
Expand Down
9 changes: 7 additions & 2 deletions weblate/addons/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@


class GenerateFileAddon(BaseAddon):
events = (AddonEvent.EVENT_PRE_COMMIT,)
events: set[AddonEvent] = {
AddonEvent.EVENT_PRE_COMMIT,
}
name = "weblate.generate.generate"
verbose = gettext_lazy("Statistics generator")
description = gettext_lazy(
Expand Down Expand Up @@ -59,7 +61,10 @@ def pre_commit(self, translation, author: str, store_hash: bool) -> None:


class LocaleGenerateAddonBase(BaseAddon):
events = (AddonEvent.EVENT_COMPONENT_UPDATE, AddonEvent.EVENT_DAILY)
events: set[AddonEvent] = {
AddonEvent.EVENT_COMPONENT_UPDATE,
AddonEvent.EVENT_DAILY,
}
multiple = True
icon = "language.svg"

Expand Down
12 changes: 8 additions & 4 deletions weblate/addons/gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class GettextBaseAddon(BaseAddon):


class GenerateMoAddon(GettextBaseAddon):
events = (AddonEvent.EVENT_PRE_COMMIT,)
events: set[AddonEvent] = {
AddonEvent.EVENT_PRE_COMMIT,
}
name = "weblate.gettext.mo"
verbose = gettext_lazy("Generate MO files")
description = gettext_lazy(
Expand Down Expand Up @@ -61,7 +63,7 @@ def pre_commit(self, translation, author: str, store_hash: bool) -> None:


class UpdateLinguasAddon(GettextBaseAddon):
events = (AddonEvent.EVENT_POST_ADD, AddonEvent.EVENT_DAILY)
events: set[AddonEvent] = {AddonEvent.EVENT_POST_ADD, AddonEvent.EVENT_DAILY}
name = "weblate.gettext.linguas"
verbose = gettext_lazy("Update LINGUAS file")
description = gettext_lazy(
Expand Down Expand Up @@ -154,7 +156,7 @@ def daily(self, component) -> None:


class UpdateConfigureAddon(GettextBaseAddon):
events = (AddonEvent.EVENT_POST_ADD, AddonEvent.EVENT_DAILY)
events: set[AddonEvent] = {AddonEvent.EVENT_POST_ADD, AddonEvent.EVENT_DAILY}
name = "weblate.gettext.configure"
verbose = gettext_lazy('Update ALL_LINGUAS variable in the "configure" file')
description = gettext_lazy(
Expand Down Expand Up @@ -343,7 +345,9 @@ def get_msgmerge_args(self, component: Component):


class GettextAuthorComments(GettextBaseAddon):
events = (AddonEvent.EVENT_PRE_COMMIT,)
events: set[AddonEvent] = {
AddonEvent.EVENT_PRE_COMMIT,
}
name = "weblate.gettext.authors"
verbose = gettext_lazy("Contributors in comment")
description = gettext_lazy(
Expand Down
10 changes: 6 additions & 4 deletions weblate/addons/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from collections import defaultdict
from itertools import chain
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, cast

from django.utils.translation import gettext_lazy

Expand All @@ -14,11 +14,11 @@
from weblate.addons.forms import GitSquashForm
from weblate.utils.errors import report_error
from weblate.vcs.base import RepositoryError
from weblate.vcs.git import GitRepository
from weblate.vcs.models import VCS_REGISTRY

if TYPE_CHECKING:
from weblate.trans.models import Component
from weblate.vcs.git import GitRepository


class GitSquashAddon(BaseAddon):
Expand All @@ -29,7 +29,9 @@ class GitSquashAddon(BaseAddon):
compat = {
"vcs": VCS_REGISTRY.git_based,
}
events = (AddonEvent.EVENT_POST_COMMIT,)
events: set[AddonEvent] = {
AddonEvent.EVENT_POST_COMMIT,
}
icon = "compress.svg"
repo_scope = True

Expand Down Expand Up @@ -237,7 +239,7 @@ def squash_author(self, component: Component, repository: GitRepository) -> None
repository.delete_branch(tmp)

def post_commit(self, component: Component, store_hash: bool) -> None:
repository = component.repository
repository = cast(GitRepository, component.repository)
branch_updated = False
with repository.lock:
# Ensure repository is rebased on current remote prior to squash, otherwise
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright © Michal Čihař <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later

# Generated by Django 5.1.3 on 2024-11-26 13:51

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("addons", "0003_addonactivitylog"),
]

operations = [
migrations.AlterField(
model_name="addonactivitylog",
name="event",
field=models.IntegerField(
choices=[
(1, "Repository post-push"),
(2, "Repository post-update"),
(3, "Repository pre-commit"),
(4, "Repository post-commit"),
(5, "Repository post-add"),
(6, "Unit pre-create"),
(7, "Storage post-load"),
(8, "Unit post-save"),
(9, "Repository pre-update"),
(10, "Repository pre-push"),
(11, "Daily"),
(12, "Component update"),
]
),
),
migrations.AlterField(
model_name="event",
name="event",
field=models.IntegerField(
choices=[
(1, "Repository post-push"),
(2, "Repository post-update"),
(3, "Repository pre-commit"),
(4, "Repository post-commit"),
(5, "Repository post-add"),
(6, "Unit pre-create"),
(7, "Storage post-load"),
(8, "Unit post-save"),
(9, "Repository pre-update"),
(10, "Repository pre-push"),
(11, "Daily"),
(12, "Component update"),
]
),
),
]
2 changes: 1 addition & 1 deletion weblate/addons/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def store_change(self, action) -> None:
details=self.configuration,
)

def configure_events(self, events) -> None:
def configure_events(self, events: set[AddonEvent]) -> None:
for event in events:
Event.objects.get_or_create(addon=self, event=event)
self.event_set.exclude(event__in=events).delete()
Expand Down
4 changes: 3 additions & 1 deletion weblate/addons/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ def format_file(filename: str, case_sensitive: bool) -> bool:


class PropertiesSortAddon(BaseAddon):
events = (AddonEvent.EVENT_PRE_COMMIT,)
events: set[AddonEvent] = {
AddonEvent.EVENT_PRE_COMMIT,
}
name = "weblate.properties.sort"
verbose = gettext_lazy("Format the Java properties file")
description = gettext_lazy("Formats and sorts the Java properties file.")
Expand Down
4 changes: 3 additions & 1 deletion weblate/addons/removal.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

class RemovalAddon(BaseAddon):
project_scope = True
events = (AddonEvent.EVENT_DAILY,)
events: set[AddonEvent] = {
AddonEvent.EVENT_DAILY,
}
settings_form = RemoveForm
icon = "delete.svg"

Expand Down

0 comments on commit 2548dcd

Please sign in to comment.