-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathother.py
More file actions
71 lines (55 loc) · 2.36 KB
/
Copy pathother.py
File metadata and controls
71 lines (55 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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
from hacking import core
log_string = re.compile(r".*LOG\.(?:error|warn|warning|info"
r"|critical|exception|debug)")
@core.flake8ext
def hacking_no_cr(physical_line):
r"""Check that we only use newlines not carriage returns.
Okay: import os\nimport sys
# pep8 doesn't yet replace \r in strings, will work on an
# upstream fix
H903 import os\r\nimport sys
"""
if '\r' in physical_line:
yield (0, "H903: Windows style line endings not allowed in code")
@core.flake8ext
@core.off_by_default
def hacking_delayed_string_interpolation(logical_line, noqa):
r"""String interpolation should be delayed at logging calls.
H904: LOG.debug('Example: %s' % 'bad')
Okay: LOG.debug('Example: %s', 'good')
"""
msg = ("H904: String interpolation should be delayed to be "
"handled by the logging code, rather than being done "
"at the point of the logging call. "
"Use ',' instead of '%'.")
if noqa:
return
if log_string.match(logical_line):
# Line is a log statement, strip out strings and see if % is used,
# just to make sure we don't match on a format specifier in a string.
line = re.sub(r"[\"'].+?[\"']", '', logical_line)
# There are some cases where string formatting of the arguments are
# needed, so don't include those when checking.
line = re.sub(r",.*", '', line)
if '%' in line or '.format(' in line:
yield 0, msg
@core.flake8ext
def hacking_no_log_warn(logical_line):
"""Disallow 'LOG.warn('
Use LOG.warning() instead of Deprecated LOG.warn().
https://docs.python.org/3/library/logging.html#logging.warning
"""
if "LOG.warn(" in logical_line:
yield (0, "H906: LOG.warn is deprecated, please use LOG.warning!")