-
-
Notifications
You must be signed in to change notification settings - Fork 964
/
base.py
209 lines (161 loc) · 6.22 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# -*- coding: utf-8 -*-
#
# Copyright © 2012 - 2020 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <https://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
from django.http import Http404
from weblate.utils.docs import get_doc_url
class Check:
"""Basic class for checks."""
check_id = ''
name = ''
description = ''
target = False
source = False
ignore_untranslated = True
default_disabled = False
severity = 'info'
batch_update = False
param_type = None
def get_identifier(self):
return self.check_id
def __init__(self):
id_dash = self.check_id.replace('_', '-')
self.url_id = 'check:{0}'.format(self.check_id)
self.doc_id = 'check-{0}'.format(id_dash)
self.enable_string = id_dash
self.ignore_string = 'ignore-{0}'.format(id_dash)
def should_skip(self, unit):
"""Check whether we should skip processing this unit."""
# Is this disabled by default
if self.default_disabled and self.enable_string not in unit.all_flags:
return True
# Is this check ignored
if self.ignore_string in unit.all_flags:
return True
return False
def check_target(self, sources, targets, unit):
"""Check target strings."""
# No checking of not translated units (but we do check needs editing ones)
if self.ignore_untranslated and not unit.state:
return False
if self.should_skip(unit):
return False
return self.check_target_unit(sources, targets, unit)
def check_target_unit_with_flag(self, sources, targets, unit):
"""Check flag value."""
raise NotImplementedError()
def check_target_unit(self, sources, targets, unit):
"""Check single unit, handling plurals."""
# Check singular
if self.check_single(sources[0], targets[0], unit):
return True
# Do we have more to check?
if len(sources) == 1:
return False
# Check plurals against plural from source
for target in targets[1:]:
if self.check_single(sources[1], target, unit):
return True
# Check did not fire
return False
def check_single(self, source, target, unit):
"""Check for single phrase, not dealing with plurals."""
raise NotImplementedError()
def check_source(self, source, unit):
"""Check source string."""
raise NotImplementedError()
def check_chars(self, source, target, pos, chars):
"""Generic checker for chars presence."""
try:
src = source[pos]
tgt = target[pos]
except IndexError:
return False
return (src in chars and tgt not in chars) or (
src not in chars and tgt in chars
)
def is_language(self, unit, vals):
"""Detect whether language is in given list, ignores variants."""
return unit.translation.language.base_code in vals
def get_doc_url(self):
"""Return link to documentation."""
return get_doc_url('user/checks', self.doc_id)
def check_highlight(self, source, unit):
"""Return parts of the text that match to hightlight them.
Result is list that contains lists of two elements with start position of the
match and the value of the match
"""
return []
def get_description(self, check_obj):
return self.description
def get_fixup(self, unit):
return None
def render(self, request, unit):
raise Http404('Not supported')
class TargetCheck(Check):
"""Basic class for target checks."""
target = True
def check_target_unit_with_flag(self, sources, targets, unit):
"""We don't check flag value here."""
return False
def check_source(self, source, unit):
"""We don't check source strings here."""
return False
def check_single(self, source, target, unit):
"""Check for single phrase, not dealing with plurals."""
raise NotImplementedError()
class SourceCheck(Check):
"""Basic class for source checks."""
source = True
def check_target_unit_with_flag(self, sources, targets, unit):
"""We don't check flag value here."""
return False
def check_single(self, source, target, unit):
"""We don't check target strings here."""
return False
def check_source(self, source, unit):
"""Check source string."""
raise NotImplementedError()
class TargetCheckParametrized(Check):
"""Basic class for target checks with flag value."""
default_disabled = True
target = True
def get_value(self, unit):
return unit.all_flags.get_value(self.enable_string)
def check_target(self, sources, targets, unit):
"""Check flag value."""
if unit.all_flags.has_value(self.enable_string):
return self.check_target_params(
sources, targets, unit, self.get_value(unit)
)
return False
def check_target_params(self, sources, targets, unit, value):
raise NotImplementedError()
def check_single(self, source, target, unit):
"""We don't check single phrase here."""
return False
def check_source(self, source, unit):
"""We don't check source strings here."""
return False
class CountingCheck(TargetCheck):
"""Check whether there is same count of given string."""
string = None
def check_single(self, source, target, unit):
if not target or not source:
return False
return source.count(self.string) != target.count(self.string)