Improve regex for _ import hacking check
Commit 3e2b1117 added a check to make sure that the _ function was being explicitly imported so that translation would work properly. I have discovered that those regexes/code would not work in some cases. Particularly if the import line imported multiple things from gettextutils or i18n. Also the check being used to find lines using the _ function was not right. This commit fixes the issues and adds appropriate tests. It also adds the hacking check to HACKING.rst which should have been done the first time around. Change-Id: I7227bb0051836e537bff2f0f97662c06452d5af6
This commit is contained in:
parent
7a97f5c58a
commit
5347439142
@ -9,7 +9,7 @@ Cinder Specific Commandments
|
||||
----------------------------
|
||||
|
||||
- [N319] Validate that debug level logs are not translated
|
||||
|
||||
- [N323] Add check for explicit import of _() to ensure proper translation.
|
||||
General
|
||||
-------
|
||||
- Use 'raise' instead of 'raise e' to preserve original traceback or exception being reraised::
|
||||
|
@ -31,10 +31,14 @@ Guidelines for writing new hacking checks
|
||||
|
||||
UNDERSCORE_IMPORT_FILES = []
|
||||
|
||||
log_translation = re.compile(
|
||||
r"(.)*LOG\.(audit|error|info|warn|warning|critical|exception)_\(\s*('|\")")
|
||||
translated_log = re.compile(
|
||||
r"(.)*LOG\.(audit|error|info|warn|warning|critical|exception)"
|
||||
"\(\s*_\(\s*('|\")")
|
||||
string_translation = re.compile(r"(.)*_\(\s*('|\")")
|
||||
vi_header_re = re.compile(r"^#\s+vim?:.+")
|
||||
underscore_import_check = re.compile(r"(.)*import _(.)*")
|
||||
# We need this for cases where they have created their own _ function.
|
||||
custom_underscore_check = re.compile(r"(.)*_\s*=\s*(.)*")
|
||||
|
||||
|
||||
def no_vi_headers(physical_line, line_number, lines):
|
||||
@ -87,9 +91,10 @@ def check_explicit_underscore_import(logical_line, filename):
|
||||
# checking needed once it is found.
|
||||
if filename in UNDERSCORE_IMPORT_FILES:
|
||||
pass
|
||||
elif logical_line.endswith("import _"):
|
||||
elif (underscore_import_check.match(logical_line) or
|
||||
custom_underscore_check.match(logical_line)):
|
||||
UNDERSCORE_IMPORT_FILES.append(filename)
|
||||
elif(log_translation.match(logical_line) or
|
||||
elif(translated_log.match(logical_line) or
|
||||
string_translation.match(logical_line)):
|
||||
yield(0, "N323: Found use of _() without explicit import of _ !")
|
||||
|
||||
|
@ -92,3 +92,15 @@ class HackingTestCase(test.TestCase):
|
||||
self.assertEqual(len(list(checks.check_explicit_underscore_import(
|
||||
"msg = _('My message')",
|
||||
"cinder/tests/other_files.py"))), 0)
|
||||
self.assertEqual(len(list(checks.check_explicit_underscore_import(
|
||||
"from cinder.i18n import _, _LW",
|
||||
"cinder/tests/other_files2.py"))), 0)
|
||||
self.assertEqual(len(list(checks.check_explicit_underscore_import(
|
||||
"msg = _('My message')",
|
||||
"cinder/tests/other_files2.py"))), 0)
|
||||
self.assertEqual(len(list(checks.check_explicit_underscore_import(
|
||||
"_ = translations.ugettext",
|
||||
"cinder/tests/other_files3.py"))), 0)
|
||||
self.assertEqual(len(list(checks.check_explicit_underscore_import(
|
||||
"msg = _('My message')",
|
||||
"cinder/tests/other_files3.py"))), 0)
|
||||
|
Loading…
x
Reference in New Issue
Block a user