check-capitalization: Check for banned words.

Fixes #8619
This commit is contained in:
Umair Khan 2018-03-11 10:06:54 +05:00 committed by Tim Abbott
parent e322c2161c
commit 728cd04c4d
3 changed files with 44 additions and 5 deletions

View File

@ -36,12 +36,12 @@ if __name__ == '__main__':
with open('static/locale/en/translations.json') as f:
data = json.load(f)
frontend = check_capitalization(list(data.keys()))
frontend_errors, frontend_ignored = frontend
frontend_errors, frontend_ignored, banned_errors_front = frontend
with open('static/locale/en/LC_MESSAGES/django.po') as f:
rows = [r for r in DJANGO_PO_REGEX.findall(f.read()) if r]
backend = check_capitalization(rows)
backend_errors, backend_ignored = backend
backend_errors, backend_ignored, banned_errors_back = backend
if frontend_errors:
print(WARNING + "Strings not properly capitalized in frontend:" + ENDC)
@ -51,6 +51,14 @@ if __name__ == '__main__':
print(WARNING + "Strings not properly capitalized in backend:" + ENDC)
print('\n'.join(backend_errors))
if banned_errors_front:
print(WARNING + "Found banned words in frontend strings" + ENDC)
print('\n'.join(banned_errors_front))
if banned_errors_back:
print(WARNING + "Found banned words in backend strings" + ENDC)
print('\n'.join(banned_errors_back))
if args.show_ignored:
print(WARNING + "Strings which were ignored: " + ENDC)
print('\n'.join(frontend_ignored + backend_ignored))

View File

@ -130,6 +130,11 @@ DISALLOWED_REGEXES = [re.compile(regex) for regex in [
# after a lower case character when the first character is in upper case.
]]
BANNED_WORDS = {
'realm': ('The term realm should not appear in user-facing strings. '
'Use organization instead.'),
}
def get_safe_phrase(phrase):
# type: (str) -> str
"""
@ -194,10 +199,22 @@ def is_capitalized(safe_text):
return True
def check_banned_words(text: str) -> List[str]:
lower_cased_text = text.lower()
errors = []
for word, reason in BANNED_WORDS.items():
if word in lower_cased_text:
kwargs = dict(word=word, text=text, reason=reason)
msg = "{word} found in '{text}'. {reason}".format(**kwargs)
errors.append(msg)
return errors
def check_capitalization(strings):
# type: (List[str]) -> Tuple[List[str], List[str]]
# type: (List[str]) -> Tuple[List[str], List[str], List[str]]
errors = []
ignored = []
banned_word_errors = []
for text in strings:
text = ' '.join(text.split()) # Remove extra whitespaces.
safe_text = get_safe_text(text)
@ -208,4 +225,6 @@ def check_capitalization(strings):
elif capitalized and has_ignored_phrase:
ignored.append(text)
return sorted(errors), sorted(ignored)
banned_word_errors.extend(check_banned_words(text))
return sorted(errors), sorted(ignored), sorted(banned_word_errors)

View File

@ -129,12 +129,14 @@ class CheckCapitalizationTestCase(TestCase):
"Some number 25MiB",
"Not Ignored Phrase",
"Not ignored phrase",
"Some text with realm in it",
"Realm in capital case",
('<p class="bot-settings-note padded-container"> Looking for our '
'<a href="/integrations" target="_blank">Integrations</a> or '
'<a href="/api" target="_blank">API</a> '
'documentation? </p>'),
]
errored, ignored = check_capitalization(strings)
errored, ignored, banned = check_capitalization(strings)
self.assertEqual(errored, ['Not Ignored Phrase'])
self.assertEqual(
ignored,
@ -148,3 +150,13 @@ class CheckCapitalizationTestCase(TestCase):
'Integrations</a> or <a href="/api" '
'target="_blank">API</a> documentation? </p>'),
]))
self.assertEqual(banned,
sorted(["realm found in 'Some text with realm in it'. "
"The term realm should not appear in user-facing "
"strings. Use organization instead.",
"realm found in 'Realm in capital case'. "
"The term realm should not appear in user-facing "
"strings. Use organization instead.",
]))