3 # Common lint functions applicable to multiple types of files.
5 from __future__
import print_function
9 def VerifyLineLength(filename
, lines
, max_length
):
10 """Checks to make sure the file has no lines with lines exceeding the length
14 filename: the file under consideration as string
15 lines: contents of the file as string array
16 max_length: maximum acceptable line length as number
19 A list of tuples with format [(filename, line number, msg), ...] with any
25 length
= len(line
.rstrip("\n"))
26 if length
> max_length
:
31 "Line exceeds %d chars (%d)" % (max_length
, length
),
38 def VerifyTabs(filename
, lines
):
39 """Checks to make sure the file has no tab characters.
42 filename: the file under consideration as string
43 lines: contents of the file as string array
46 A list of tuples with format [(line_number, msg), ...] with any violations
50 tab_re
= re
.compile(r
"\t")
53 if tab_re
.match(line
.rstrip("\n")):
54 lint
.append((filename
, line_num
, "Tab found instead of whitespace"))
59 def VerifyTrailingWhitespace(filename
, lines
):
60 """Checks to make sure the file has no lines with trailing whitespace.
63 filename: the file under consideration as string
64 lines: contents of the file as string array
67 A list of tuples with format [(filename, line number, msg), ...] with any
71 trailing_whitespace_re
= re
.compile(r
"\s+$")
74 if trailing_whitespace_re
.match(line
.rstrip("\n")):
75 lint
.append((filename
, line_num
, "Trailing whitespace"))
81 def RunOnFile(filename
, lines
):
82 raise Exception("RunOnFile() unimplemented")
85 def RunLintOverAllFiles(linter
, filenames
):
86 """Runs linter over the contents of all files.
89 lint: subclass of BaseLint, implementing RunOnFile()
90 filenames: list of all files whose contents will be linted
93 A list of tuples with format [(filename, line number, msg), ...] with any
97 for filename
in filenames
:
98 file = open(filename
, "r")
100 print("Cound not open %s" % filename
)
102 lines
= file.readlines()
103 lint
.extend(linter
.RunOnFile(filename
, lines
))