3 # Common lint functions applicable to multiple types of files.
5 from __future__
import print_function
8 def VerifyLineLength(filename
, lines
, max_length
):
9 """Checks to make sure the file has no lines with lines exceeding the length
13 filename: the file under consideration as string
14 lines: contents of the file as string array
15 max_length: maximum acceptable line length as number
18 A list of tuples with format [(filename, line number, msg), ...] with any
24 length
= len(line
.rstrip('\n'))
25 if length
> max_length
:
26 lint
.append((filename
, line_num
,
27 'Line exceeds %d chars (%d)' % (max_length
, length
)))
31 def VerifyTabs(filename
, lines
):
32 """Checks to make sure the file has no tab characters.
35 filename: the file under consideration as string
36 lines: contents of the file as string array
39 A list of tuples with format [(line_number, msg), ...] with any violations
43 tab_re
= re
.compile(r
'\t')
46 if tab_re
.match(line
.rstrip('\n')):
47 lint
.append((filename
, line_num
, 'Tab found instead of whitespace'))
52 def VerifyTrailingWhitespace(filename
, lines
):
53 """Checks to make sure the file has no lines with trailing whitespace.
56 filename: the file under consideration as string
57 lines: contents of the file as string array
60 A list of tuples with format [(filename, line number, msg), ...] with any
64 trailing_whitespace_re
= re
.compile(r
'\s+$')
67 if trailing_whitespace_re
.match(line
.rstrip('\n')):
68 lint
.append((filename
, line_num
, 'Trailing whitespace'))
74 def RunOnFile(filename
, lines
):
75 raise Exception('RunOnFile() unimplemented')
78 def RunLintOverAllFiles(linter
, filenames
):
79 """Runs linter over the contents of all files.
82 lint: subclass of BaseLint, implementing RunOnFile()
83 filenames: list of all files whose contents will be linted
86 A list of tuples with format [(filename, line number, msg), ...] with any
90 for filename
in filenames
:
91 file = open(filename
, 'r')
93 print('Cound not open %s' % filename
)
95 lines
= file.readlines()
96 lint
.extend(linter
.RunOnFile(filename
, lines
))