Debian Codegraph @

neutron_lib/hacking/translation_checks.py

raw
1 # Copyright (c) 2014 OpenStack Foundation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 import re
16
17 from hacking import core
18
19
20 _all_log_levels = {'critical', 'error', 'exception', 'info',
21 'warning', 'debug'}
22 _all_hints = {'_LC', '_LE', '_LI', '_', '_LW'}
23
24
25 _log_warn = re.compile(
26 r"(.)*LOG\.(warn)\(\s*('|\")")
27
28
29 _log_translation_hint = re.compile(
30 r".*LOG\.({levels})\(\s*({hints})\(".format(
31 levels='|'.join(_all_log_levels),
32 hints='|'.join(_all_hints),
33 ))
34
35
36 def _translation_checks_not_enforced(filename):
37 # Do not do these validations on tests
38 return any(pat in filename for pat in ["/tests/", "rally-jobs/plugins/"])
39
40
41 @core.flake8ext
42 def check_log_warn_deprecated(logical_line, filename):
43 """N532 - Use LOG.warning due to compatibility with py3.
44
45 :param logical_line: The logical line to check.
46 :param filename: The file name where the logical line exists.
47 :returns: None if the logical line passes the check, otherwise a tuple
48 is yielded that contains the offending index in logical line and a
49 message describe the check validation failure.
50 """
51 msg = "N532: Use LOG.warning due to compatibility with py3"
52 if _log_warn.match(logical_line):
53 yield (0, msg)
54
55
56 @core.flake8ext
57 def check_raised_localized_exceptions(logical_line, filename):
58 """N534 - Untranslated exception message.
59
60 :param logical_line: The logical line to check.
61 :param filename: The file name where the logical line exists.
62 :returns: None if the logical line passes the check, otherwise a tuple
63 is yielded that contains the offending index in logical line and a
64 message describe the check validation failure.
65 """
66 if _translation_checks_not_enforced(filename):
67 return
68
69 logical_line = logical_line.strip()
70 raised_search = re.compile(
71 r"raise (?:\w*)\((.*)\)").match(logical_line)
72 if raised_search:
73 exception_msg = raised_search.groups()[0]
74 if exception_msg.startswith("\"") or exception_msg.startswith("\'"):
75 msg = "N534: Untranslated exception message."
76 yield (logical_line.index(exception_msg), msg)
77
78
79 @core.flake8ext
80 def no_translate_logs(logical_line, filename):
81 """N537 - Don't translate logs.
82
83 Check for 'LOG.*(_(' and 'LOG.*(_Lx('
84
85 Translators don't provide translations for log messages, and operators
86 asked not to translate them.
87
88 * This check assumes that 'LOG' is a logger.
89
90 :param logical_line: The logical line to check.
91 :param filename: The file name where the logical line exists.
92 :returns: None if the logical line passes the check, otherwise a tuple
93 is yielded that contains the offending index in logical line and a
94 message describe the check validation failure.
95 """
96 if _translation_checks_not_enforced(filename):
97 return
98
99 msg = "N537: Log messages should not be translated!"
100 match = _log_translation_hint.match(logical_line)
101 if match:
102 yield (logical_line.index(match.group()), msg)