Coverage for app/venv/lib/python3.14/site-packages/weblate/checks/management/commands/list_checks.py: 0%

66 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 07:15 +0000

1# Copyright © Michal Čihař <michal@weblate.org> 

2# 

3# SPDX-License-Identifier: GPL-3.0-or-later 

4 

5from __future__ import annotations 

6 

7from typing import TYPE_CHECKING 

8 

9from weblate.checks.format import BaseFormatCheck 

10from weblate.checks.models import CHECKS 

11from weblate.formats.models import FILE_FORMATS 

12from weblate.utils.management.base import BaseCommand 

13 

14if TYPE_CHECKING: 

15 from django_stubs_ext import StrOrPromise 

16 

17 from weblate.checks.base import BaseCheck 

18 

19 

20def sorter(check: BaseCheck): 

21 if isinstance(check, BaseFormatCheck): 

22 pos = 1 

23 elif check.name < "Formatted strings": 

24 pos = 0 

25 else: 

26 pos = 2 

27 return (check.source, pos, check.name.lower()) 

28 

29 

30def escape(text: StrOrPromise): 

31 return text.replace("\\", "\\\\") 

32 

33 

34class Command(BaseCommand): 

35 help = "List installed checks" 

36 

37 def flush_lines(self, lines: list[str]) -> None: 

38 self.stdout.write("\n".join(lines)) 

39 lines.clear() 

40 

41 def handle(self, *args, **options) -> None: 

42 """List installed checks.""" 

43 self.stdout.write(""".. 

44 Partly generated using ./manage.py list_checks 

45""") 

46 ignores: list[str] = [] 

47 enables: list[str] = [] 

48 lines: list[str] = [] 

49 for check in sorted(CHECKS.values(), key=sorter): 

50 check_class = check.__class__ 

51 is_format = isinstance(check, BaseFormatCheck) 

52 if not lines: 

53 lines.append("\n") 

54 lines.append(f".. _{check.doc_id}:\n") 

55 name = escape(check.name) 

56 lines.append(name) 

57 if is_format: 

58 lines.append("*" * len(name)) 

59 else: 

60 lines.append("~" * len(name)) 

61 lines.extend(("\n", f":Summary: {escape(check.description)}")) 

62 if check.glossary: 

63 lines.append(":Scope: glossary strings") 

64 elif check.target: 

65 if check.ignore_untranslated: 

66 lines.append(":Scope: translated strings") 

67 else: 

68 lines.append(":Scope: all strings") 

69 elif check.source: 

70 lines.append(":Scope: source strings") 

71 lines.extend( 

72 ( 

73 f":Check class: ``{check_class.__module__}.{check_class.__qualname__}``", 

74 f":Check identifier: ``{check.check_id}``", 

75 ) 

76 ) 

77 if check.default_disabled: 

78 enable_flags: list[str] = { 

79 check.enable_string, 

80 *check.extra_enable_strings, 

81 } 

82 flags = ", ".join(f"``{flag}``" for flag in sorted(enable_flags)) 

83 lines.append(":Trigger: This check needs to be enabled using a flag.") 

84 

85 file_formats = ", ".join( 

86 f":ref:`{file_format.format_id}`" 

87 for file_format in FILE_FORMATS.values() 

88 if set(file_format.check_flags) & enable_flags 

89 ) 

90 if file_formats: 

91 lines.append( 

92 f":File formats automatically enabling this check: {file_formats}" 

93 ) 

94 lines.append(f":Flag to enable: {flags}") 

95 else: 

96 lines.append( 

97 ":Trigger: This check is always enabled but can be ignored using a flag." 

98 ) 

99 lines.extend((f":Flag to ignore: ``{check.ignore_string}``", "\n")) 

100 

101 self.flush_lines(lines) 

102 

103 ignores.extend( 

104 ( 

105 f"``{check.ignore_string}``", 

106 f" Skip the :ref:`{check.doc_id}` quality check.", 

107 ) 

108 ) 

109 if check.default_disabled: 

110 enables.extend( 

111 ( 

112 f"``{check.enable_string}``", 

113 f" Enable the :ref:`{check.doc_id}` quality check.", 

114 ) 

115 ) 

116 

117 self.stdout.write("\n") 

118 self.stdout.write("\n".join(enables)) 

119 self.stdout.write("\n".join(ignores))