]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Combine author_tag and log_translation_hint regexes
authorAngus Lees <gus@inodes.org>
Fri, 28 Nov 2014 01:15:15 +0000 (12:15 +1100)
committerAngus Lees <gus@inodes.org>
Tue, 23 Dec 2014 03:52:57 +0000 (14:52 +1100)
author_tag_re and log_translation_hints were lists of regexes, where
each member was then separately matched against each input line.

This change combines each of these lists into an a|b regex and removes
the unnecessary loop.

Change-Id: I8db76862d3f7ef030e31e2e1e6f2fcd8dbd00888

neutron/hacking/checks.py

index e2855e9f429d0b389b5b363c825a176662274178..9a1b40b76b932dc2dd6b6997beb0d7c3d529d16e 100644 (file)
@@ -28,9 +28,8 @@ import pep8
 #  - Add test cases for each new rule to
 #    neutron/tests/unit/test_hacking.py
 
-author_tag_re = (re.compile("^\s*#\s*@?(a|A)uthor"),
-                 re.compile("^\.\.\s+moduleauthor::"))
-_all_hints = set(['_', '_LI', '_LE', '_LW', '_LC'])
+author_tag_re = re.compile(r"^\s*#\s*@?[aA]uthor|^\.\.\s+moduleauthor::")
+
 _all_log_levels = {
     # NOTE(yamamoto): Following nova which uses _() for audit.
     'audit': '_',
@@ -41,13 +40,19 @@ _all_log_levels = {
     'critical': '_LC',
     'exception': '_LE',
 }
-log_translation_hints = []
-for level, hint in _all_log_levels.iteritems():
-    r = "(.)*LOG\.%(level)s\(\s*((%(wrong_hints)s)\(|'|\")" % {
+_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_hints.append(re.compile(r))
+
+
+log_translation_hint = re.compile(
+    '|'.join('(?:%s)' % _regex_for_level(level, hint)
+             for level, hint in _all_log_levels.iteritems()))
 
 
 def validate_log_translations(logical_line, physical_line, filename):
@@ -58,9 +63,8 @@ def validate_log_translations(logical_line, physical_line, filename):
         return
 
     msg = "N320: Log messages require translation hints!"
-    for log_translation_hint in log_translation_hints:
-        if log_translation_hint.match(logical_line):
-            yield (0, msg)
+    if log_translation_hint.match(logical_line):
+        yield (0, msg)
 
 
 def use_jsonutils(logical_line, filename):
@@ -85,13 +89,12 @@ def use_jsonutils(logical_line, filename):
 
 
 def no_author_tags(physical_line):
-    for regex in author_tag_re:
-        if regex.match(physical_line):
-            physical_line = physical_line.lower()
-            pos = physical_line.find('moduleauthor')
-            if pos < 0:
-                pos = physical_line.find('author')
-            return pos, "N322: Don't use author tags"
+    if author_tag_re.match(physical_line):
+        physical_line = physical_line.lower()
+        pos = physical_line.find('moduleauthor')
+        if pos < 0:
+            pos = physical_line.find('author')
+        return pos, "N322: Don't use author tags"
 
 
 def no_translate_debug_logs(logical_line, filename):