From a6197e66595e236d9a554c391b722e8dc7b03ba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Mon, 29 Jan 2024 14:45:01 +0100 Subject: [PATCH] autofix: expose related checks Used to get list of ignore flags so that these are allowed even if the corresponding check is disabled. Fixes #10880 --- weblate/checks/flags.py | 5 ++++- weblate/trans/autofixes/__init__.py | 17 ++++++++++++++++- weblate/trans/autofixes/base.py | 4 ++++ weblate/trans/autofixes/chars.py | 19 ++++++++++++++++++- weblate/trans/autofixes/html.py | 5 +++++ weblate/trans/autofixes/whitespace.py | 5 +++++ 6 files changed, 52 insertions(+), 3 deletions(-) diff --git a/weblate/checks/flags.py b/weblate/checks/flags.py index af9cdd0527fa..1dff81298eb8 100644 --- a/weblate/checks/flags.py +++ b/weblate/checks/flags.py @@ -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 = { @@ -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"} diff --git a/weblate/trans/autofixes/__init__.py b/weblate/trans/autofixes/__init__.py index 5a7d9e2eb852..71023b8874f2 100644 --- a/weblate/trans/autofixes/__init__.py +++ b/weblate/trans/autofixes/__init__.py @@ -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): diff --git a/weblate/trans/autofixes/base.py b/weblate/trans/autofixes/base.py index 4d2756e08ff0..0c40b3c667fd 100644 --- a/weblate/trans/autofixes/base.py +++ b/weblate/trans/autofixes/base.py @@ -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 diff --git a/weblate/trans/autofixes/chars.py b/weblate/trans/autofixes/chars.py index 8bb2f2e65d9d..dda19dbf18e7 100644 --- a/weblate/trans/autofixes/chars.py +++ b/weblate/trans/autofixes/chars.py @@ -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 @@ -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 @@ -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 @@ -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")) diff --git a/weblate/trans/autofixes/html.py b/weblate/trans/autofixes/html.py index 85fdbdeb14a5..16791bf0e058 100644 --- a/weblate/trans/autofixes/html.py +++ b/weblate/trans/autofixes/html.py @@ -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 @@ -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: diff --git a/weblate/trans/autofixes/whitespace.py b/weblate/trans/autofixes/whitespace.py index ba5309d20534..10c15cd6fd58 100644 --- a/weblate/trans/autofixes/whitespace.py +++ b/weblate/trans/autofixes/whitespace.py @@ -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") @@ -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)