Skip to content

Commit

Permalink
autofix: expose related checks
Browse files Browse the repository at this point in the history
Used to get list of ignore flags so that these are allowed even if the
corresponding check is disabled.

Fixes #10880
  • Loading branch information
nijel committed Jan 29, 2024
1 parent b599e43 commit a6197e6
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
5 changes: 4 additions & 1 deletion weblate/checks/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
single_value_flag,
)
from weblate.fonts.utils import get_font_weight
from weblate.trans.autofixes import AUTOFIXES
from weblate.trans.defines import VARIANT_KEY_LENGTH

PLAIN_FLAGS = {
Expand Down Expand Up @@ -73,7 +74,9 @@
TYPED_FLAGS["fluent-type"] = gettext_lazy("Fluent type")
TYPED_FLAGS_ARGS["fluent-type"] = single_value_flag(str)

IGNORE_CHECK_FLAGS = {CHECKS[x].ignore_string for x in CHECKS}
IGNORE_CHECK_FLAGS = {check.ignore_string for check in CHECKS.values()} | set(
AUTOFIXES.get_ignore_strings()
)

FLAG_ALIASES = {"markdown-text": "md-text"}

Expand Down
17 changes: 16 additions & 1 deletion weblate/trans/autofixes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,24 @@
Note, unlike checks, using a sortable data object so fixes are applied in desired order.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

from weblate.utils.classloader import ClassLoader

AUTOFIXES = ClassLoader("AUTOFIX_LIST")
if TYPE_CHECKING:
from collections.abc import Iterator


class AutofixLoader(ClassLoader):
def get_ignore_strings(self) -> Iterator[str]:
for fix in self.values():
for check in fix.get_related_checks():
yield check.ignore_string


AUTOFIXES = AutofixLoader("AUTOFIX_LIST")


def fix_target(target, unit):
Expand Down
4 changes: 4 additions & 0 deletions weblate/trans/autofixes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class AutoFix:
def get_identifier(self):
return self.fix_id

@staticmethod
def get_related_checks():
return []

def fix_single_target(self, target, source, unit):
"""Fix a single target, implement this method in subclasses."""
raise NotImplementedError
Expand Down
19 changes: 18 additions & 1 deletion weblate/trans/autofixes/chars.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

from django.utils.translation import gettext_lazy

from weblate.checks.chars import FRENCH_PUNCTUATION_FIXUP_RE
from weblate.checks.chars import (
FRENCH_PUNCTUATION_FIXUP_RE,
EndEllipsisCheck,
PunctuationSpacingCheck,
ZeroWidthSpaceCheck,
)
from weblate.formats.helpers import CONTROLCHARS_TRANS
from weblate.trans.autofixes.base import AutoFix

Expand All @@ -17,6 +22,10 @@ class ReplaceTrailingDotsWithEllipsis(AutoFix):
fix_id = "end-ellipsis"
name = gettext_lazy("Trailing ellipsis")

@staticmethod
def get_related_checks():
return [EndEllipsisCheck()]

def fix_single_target(self, target, source, unit):
if source and source[-1] == "…" and target.endswith("..."):
return f"{target[:-3]}…", True
Expand All @@ -29,6 +38,10 @@ class RemoveZeroSpace(AutoFix):
fix_id = "zero-width-space"
name = gettext_lazy("Zero-width space")

@staticmethod
def get_related_checks():
return [ZeroWidthSpaceCheck()]

def fix_single_target(self, target, source, unit):
if unit.translation.language.base_code == "km":
return target, False
Expand Down Expand Up @@ -70,6 +83,10 @@ class PunctuationSpacing(AutoFix):
fix_id = "punctuation-spacing"
name = gettext_lazy("Punctuation spacing")

@staticmethod
def get_related_checks():
return [PunctuationSpacingCheck()]

def fix_single_target(self, target, source, unit):
if (
unit.translation.language.is_base(("fr", "br"))
Expand Down
5 changes: 5 additions & 0 deletions weblate/trans/autofixes/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from django.utils.translation import gettext_lazy

from weblate.checks.markup import SafeHTMLCheck
from weblate.trans.autofixes.base import AutoFix
from weblate.utils.html import HTMLSanitizer

Expand All @@ -14,6 +15,10 @@ class BleachHTML(AutoFix):
fix_id = "safe-html"
name = gettext_lazy("Unsafe HTML")

@staticmethod
def get_related_checks():
return [SafeHTMLCheck()]

def fix_single_target(self, target: str, source: str, unit):
flags = unit.all_flags
if "safe-html" not in flags:
Expand Down
5 changes: 5 additions & 0 deletions weblate/trans/autofixes/whitespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from django.utils.translation import gettext_lazy

from weblate.checks.chars import BeginSpaceCheck, EndSpaceCheck
from weblate.trans.autofixes.base import AutoFix

NEWLINES = re.compile(r"\r\n|\r|\n")
Expand All @@ -19,6 +20,10 @@ class SameBookendingWhitespace(AutoFix):
fix_id = "end-whitespace"
name = gettext_lazy("Trailing and leading whitespace")

@staticmethod
def get_related_checks():
return [BeginSpaceCheck(), EndSpaceCheck()]

def fix_single_target(self, target, source, unit):
# normalize newlines of source
source = NEWLINES.sub("\n", source)
Expand Down

0 comments on commit a6197e6

Please sign in to comment.