summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2017-02-03 10:09:53 +0000
committerGerrit Code Review <review@openstack.org>2017-02-03 10:09:53 +0000
commit55dbc74b303158249128cc2798429c76b52bccdf (patch)
treea5dc5fe653c002326cee46febb47962e670cb0e1
parent482e296fcc38ddf8719c5968a08bf845561abca2 (diff)
parentbe5548cc3ae04410bd10cca1b39ff3dd3c342a4e (diff)
downloadtrove-55dbc74b303158249128cc2798429c76b52bccdf.tar.gz
Merge "Add translation_checks for i18n"
-rw-r--r--HACKING.rst7
-rw-r--r--tox.ini4
-rw-r--r--trove/hacking/__init__.py0
-rw-r--r--trove/hacking/translation_checks.py110
-rw-r--r--trove/tests/unittests/hacking/__init__.py0
-rw-r--r--trove/tests/unittests/hacking/test_translation_checks.py89
6 files changed, 210 insertions, 0 deletions
diff --git a/HACKING.rst b/HACKING.rst
new file mode 100644
index 00000000..ae080f56
--- /dev/null
+++ b/HACKING.rst
@@ -0,0 +1,7 @@
+Trove Library Specific Commandments
+-------------------------------------
+
+- [T101] Validate that LOG messages, except debug ones, are translated
+- [T102] Validate that debug level logs are not translated
+- [T103] Exception messages should be translated
+
diff --git a/tox.ini b/tox.ini
index 81e9aba4..310629db 100644
--- a/tox.ini
+++ b/tox.ini
@@ -78,6 +78,10 @@ builtins = _
exclude=.venv,.tox,.git,dist,doc,*egg,tools,etc,build,*.po,*.pot,integration,releasenotes
filename=*.py,trove-*
+[hacking]
+import_exceptions = trove.common.i18n
+local-check-factory = trove.hacking.translation_checks.factory
+
[testenv:api-ref]
# This environment is called from CI scripts to test and publish
# the API Ref to developer.openstack.org.
diff --git a/trove/hacking/__init__.py b/trove/hacking/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/trove/hacking/__init__.py
diff --git a/trove/hacking/translation_checks.py b/trove/hacking/translation_checks.py
new file mode 100644
index 00000000..9cacd594
--- /dev/null
+++ b/trove/hacking/translation_checks.py
@@ -0,0 +1,110 @@
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import re
+
+import pep8
+
+_all_log_levels = {
+ 'critical': '_',
+ 'error': '_',
+ 'exception': '_',
+ 'info': '_',
+ 'reserved': '_',
+ 'warning': '_',
+}
+_all_hints = set(_all_log_levels.values())
+
+
+def _regex_for_level(level, hint):
+ return r".*LOG\.%(level)s\(\s*((%(wrong_hints)s)\(|'|\")" % {
+ 'level': level,
+ 'wrong_hints': '|'.join(_all_hints - set([hint])),
+ }
+
+
+_log_translation_hint = re.compile(
+ '|'.join('(?:%s)' % _regex_for_level(level, hint)
+ for level, hint in _all_log_levels.items()))
+
+_log_string_interpolation = re.compile(
+ r".*LOG\.(error|warning|info|critical|exception|debug)\([^,]*%[^,]*[,)]")
+
+
+def _translation_is_not_expected(filename):
+ # Do not do these validations on tests
+ return any(pat in filename for pat in ["/tests/"])
+
+
+def validate_log_translations(logical_line, physical_line, filename):
+ """T101 - Log messages require translation hints.
+ :param logical_line: The logical line to check.
+ :param physical_line: The physical line to check.
+ :param filename: The file name where the logical line exists.
+ :returns: None if the logical line passes the check, otherwise a tuple
+ is yielded that contains the offending index in logical line and a
+ message describe the check validation failure.
+ """
+ if _translation_is_not_expected(filename):
+ return
+
+ if pep8.noqa(physical_line):
+ return
+
+ msg = "T101: Untranslated Log message."
+ if _log_translation_hint.match(logical_line):
+ yield (0, msg)
+
+
+def no_translate_debug_logs(logical_line, filename):
+ """T102 - Don't translate debug level logs.
+ Check for 'LOG.debug(_(' and 'LOG.debug(_Lx('
+ As per our translation policy,
+ https://wiki.openstack.org/wiki/LoggingStandards#Log_Translation
+ we shouldn't translate debug level logs.
+ * This check assumes that 'LOG' is a logger.
+ :param logical_line: The logical line to check.
+ :param filename: The file name where the logical line exists.
+ :returns: None if the logical line passes the check, otherwise a tuple
+ is yielded that contains the offending index in logical line and a
+ message describe the check validation failure.
+ """
+ for hint in _all_hints:
+ if logical_line.startswith("LOG.debug(%s(" % hint):
+ yield(0, "T102 Don't translate debug level logs")
+
+
+def check_raised_localized_exceptions(logical_line, filename):
+ """T103 - Untranslated exception message.
+ :param logical_line: The logical line to check.
+ :param filename: The file name where the logical line exists.
+ :returns: None if the logical line passes the check, otherwise a tuple
+ is yielded that contains the offending index in logical line and a
+ message describe the check validation failure.
+ """
+ if _translation_is_not_expected(filename):
+ return
+
+ logical_line = logical_line.strip()
+ raised_search = re.compile(
+ r"raise (?:\w*)\((.*)\)").match(logical_line)
+ if raised_search:
+ exception_msg = raised_search.groups()[0]
+ if exception_msg.startswith("\"") or exception_msg.startswith("\'"):
+ msg = "T103: Untranslated exception message."
+ yield (logical_line.index(exception_msg), msg)
+
+
+def factory(register):
+ register(validate_log_translations)
+ register(no_translate_debug_logs)
+ register(check_raised_localized_exceptions)
diff --git a/trove/tests/unittests/hacking/__init__.py b/trove/tests/unittests/hacking/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/trove/tests/unittests/hacking/__init__.py
diff --git a/trove/tests/unittests/hacking/test_translation_checks.py b/trove/tests/unittests/hacking/test_translation_checks.py
new file mode 100644
index 00000000..bd539f6b
--- /dev/null
+++ b/trove/tests/unittests/hacking/test_translation_checks.py
@@ -0,0 +1,89 @@
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+from trove.hacking import translation_checks as tc
+from trove.tests.unittests import trove_testtools
+
+
+class HackingTestCase(trove_testtools.TestCase):
+
+ def assertLinePasses(self, func, *args):
+ def check_callable(f, *args):
+ return next(f(*args))
+ self.assertRaises(StopIteration, check_callable, func, *args)
+
+ def assertLineFails(self, func, *args):
+ self.assertIsInstance(next(func(*args)), tuple)
+
+ def test_factory(self):
+ def check_callable(fn):
+ self.assertTrue(hasattr(fn, '__call__'))
+ self.assertIsNone(tc.factory(check_callable))
+
+ def test_log_translations(self):
+ expected_marks = {
+ 'error': '_',
+ 'info': '_',
+ 'warning': '_',
+ 'critical': '_',
+ 'exception': '_',
+ }
+ logs = expected_marks.keys()
+ debug = "LOG.debug('OK')"
+ self.assertEqual(
+ 0, len(list(tc.validate_log_translations(debug, debug, 'f'))))
+ for log in logs:
+ bad = 'LOG.%s("Bad")' % log
+ self.assertEqual(
+ 1, len(list(tc.validate_log_translations(bad, bad, 'f'))))
+ ok = 'LOG.%s(_("OK"))' % log
+ self.assertEqual(
+ 0, len(list(tc.validate_log_translations(ok, ok, 'f'))))
+ ok = "LOG.%s('OK') # noqa" % log
+ self.assertEqual(
+ 0, len(list(tc.validate_log_translations(ok, ok, 'f'))))
+ ok = "LOG.%s(variable)" % log
+ self.assertEqual(
+ 0, len(list(tc.validate_log_translations(ok, ok, 'f'))))
+ # Do not do validations in tests
+ ok = 'LOG.%s("OK - unit tests")' % log
+ self.assertEqual(
+ 0, len(list(tc.validate_log_translations(ok, ok,
+ 'f/tests/f'))))
+
+ for mark in tc._all_hints:
+ stmt = "LOG.%s(%s('test'))" % (log, mark)
+ self.assertEqual(
+ 0 if expected_marks[log] == mark else 1,
+ len(list(tc.validate_log_translations(stmt, stmt, 'f'))))
+
+ def test_no_translate_debug_logs(self):
+ for hint in tc._all_hints:
+ bad = "LOG.debug(%s('bad'))" % hint
+ self.assertEqual(
+ 1, len(list(tc.no_translate_debug_logs(bad, 'f'))))
+
+ def test_check_localized_exception_messages(self):
+ f = tc.check_raised_localized_exceptions
+ self.assertLineFails(f, " raise KeyError('Error text')", '')
+ self.assertLineFails(f, ' raise KeyError("Error text")', '')
+ self.assertLinePasses(f, ' raise KeyError(_("Error text"))', '')
+ self.assertLinePasses(f, ' raise KeyError(_ERR("Error text"))', '')
+ self.assertLinePasses(f, " raise KeyError(translated_msg)", '')
+ self.assertLinePasses(f, '# raise KeyError("Not translated")', '')
+ self.assertLinePasses(f, 'print("raise KeyError("Not '
+ 'translated")")', '')
+
+ def test_check_localized_exception_message_skip_tests(self):
+ f = tc.check_raised_localized_exceptions
+ self.assertLinePasses(f, "raise KeyError('Error text')",
+ 'neutron_lib/tests/unit/mytest.py')