Skip to content

Commit ae73466

Browse files
XhmikosRdanmar
authored andcommitted
Python scripts: PEP8 fixes
1 parent d8fc126 commit ae73466

File tree

5 files changed

+49
-41
lines changed

5 files changed

+49
-41
lines changed

htmlreport/cppcheck-htmlreport

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ HTML_FOOTER = """
148148

149149
HTML_ERROR = "<span style=\"border-width: 2px;border-color: black;border-style: solid;background: #ffaaaa;padding: 3px;\">&lt;--- %s</span>\n"
150150

151+
151152
class AnnotateCodeFormatter(HtmlFormatter):
152153
errors = []
153154

@@ -162,6 +163,7 @@ class AnnotateCodeFormatter(HtmlFormatter):
162163
line_no = line_no + 1
163164
yield i, t
164165

166+
165167
class CppCheckHandler(XmlContentHandler):
166168
"""Parses the cppcheck xml file and produces a list of all its errors."""
167169
errors = []

htmlreport/setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
name="cppcheck",
77
scripts=[
88
"cppcheck-htmlreport",
9-
]
10-
)
9+
]
10+
)

tools/extracttests.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import sys
2626
import re
2727

28+
2829
class Extract:
2930
"""
3031
Read Cppcheck test file and create data
@@ -34,14 +35,14 @@ class Extract:
3435
# array that stores all the test cases
3536
nodes = []
3637

37-
def parseFile(self,filename):
38+
def parseFile(self, filename):
3839
"""
3940
parse test file and add info to the nodes
4041
variable
4142
"""
4243

4344
name = '[0-9a-zA-Z_]+'
44-
string = '\\"(.+)\\"'
45+
string = '\\"(.+)\\"'
4546

4647
testclass = None
4748
functionName = None
@@ -50,50 +51,52 @@ def parseFile(self,filename):
5051
for line in fin:
5152
# testclass starts
5253
res = re.match('class ('+name+')', line)
53-
if res != None:
54+
if res is not None:
5455
testclass = res.group(1)
5556

5657
# end of testclass
57-
if re.match('};', line) != None:
58+
if re.match('};', line) is not None:
5859
testclass = None
5960

6061
# function start
6162
res = re.match('\\s+void ('+name+')\\(\\)', line)
62-
if res != None:
63+
if res is not None:
6364
functionName = res.group(1)
6465

65-
elif re.match('\\s+}', line) != None:
66+
elif re.match('\\s+}', line) is not None:
6667
functionName = None
6768

68-
if functionName == None:
69+
if functionName is None:
6970
continue
7071

7172
# check
7273
res = re.match('\s+check.*\('+string, line)
73-
if res != None:
74+
if res is not None:
7475
code = res.group(1)
7576

7677
# code..
7778
res = re.match('\\s+'+string, line)
78-
if res != None:
79+
if res is not None:
7980
code = code + res.group(1)
8081

8182
# assert
8283
res = re.match('\\s+ASSERT_EQUALS\\(\\"([^"]*)\\",', line)
83-
if res != None and len(code) > 10:
84-
node = { 'testclass':testclass,
85-
'functionName':functionName,
86-
'code':code,
87-
'expected':res.group(1) }
84+
if res is not None and len(code) > 10:
85+
node = {'testclass': testclass,
86+
'functionName': functionName,
87+
'code': code,
88+
'expected': res.group(1)}
8889
self.nodes.append(node)
8990
code = ''
9091

9192
# close test file
9293
fin.close()
9394

95+
9496
def strtoxml(s):
9597
"""Convert string to xml/html format"""
96-
return s.replace('&','&amp;').replace('"', '&quot;').replace('<','&lt;').replace('>','&gt;')
98+
return s.replace('&', '&amp;').replace('"', '&quot;').replace('<', '&lt;').replace('>', '&gt;')
99+
97100

98101
def trimname(name):
99102
"""Trim test name. Trailing underscore and digits are removed"""
@@ -128,7 +131,7 @@ def writeHtmlFile(nodes, functionName, filename, errorsOnly):
128131
testclass = None
129132
num = 0
130133
for node in nodes:
131-
if errorsOnly and node['expected']=='':
134+
if errorsOnly and node['expected'] == '':
132135
continue
133136
if trimname(node['functionName']) == functionName:
134137
num = num + 1
@@ -144,8 +147,8 @@ def writeHtmlFile(nodes, functionName, filename, errorsOnly):
144147
fout.write('<td>' + strtoxml(node['expected']).replace('\\n', '<br>') + '</td>')
145148
fout.write('</tr>\n')
146149

147-
if testclass != None:
148-
fout.write('</table>\n');
150+
if testclass is not None:
151+
fout.write('</table>\n')
149152
fout.write('</body></html>\n')
150153
fout.close()
151154

@@ -175,7 +178,7 @@ def writeHtmlFile(nodes, functionName, filename, errorsOnly):
175178

176179

177180
# extract test cases
178-
if filename != None:
181+
if filename is not None:
179182
# parse test file
180183
e = Extract()
181184
e.parseFile(filename)
@@ -193,7 +196,7 @@ def writeHtmlFile(nodes, functionName, filename, errorsOnly):
193196
s += '/>'
194197
print (s)
195198
print ('</tree>')
196-
elif htmldir != None:
199+
elif htmldir is not None:
197200
if not htmldir.endswith('/'):
198201
htmldir += '/'
199202
if not os.path.exists(htmldir):
@@ -280,7 +283,7 @@ def writeHtmlFile(nodes, functionName, filename, errorsOnly):
280283
filename += functionName + '.cpp'
281284

282285
# source code
283-
fout = open(codedir+filename,'w')
286+
fout = open(codedir + filename, 'w')
284287
fout.write(code)
285288
fout.close()
286289

tools/matchcompiler.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import glob
2323
import argparse
2424

25+
2526
class MatchCompiler:
2627
def __init__(self, verify_mode=False):
2728
self._verifyMode = verify_mode
@@ -105,7 +106,7 @@ def _compileCmd(self, tok):
105106
return 'tok->isName()'
106107
elif tok == '%varid%':
107108
return '(tok->isName() && tok->varId()==varid)'
108-
elif (len(tok)>2) and (tok[0]=="%"):
109+
elif (len(tok) > 2) and (tok[0] == "%"):
109110
print ("unhandled:" + tok)
110111

111112
return '(tok->str()==' + self._insertMatchStr(tok) + ')/* ' + tok + ' */'
@@ -136,7 +137,7 @@ def _compilePattern(self, pattern, nr, varid, isFindMatch=False):
136137
gotoNextToken = ' tok = tok->next();\n'
137138

138139
# if varid is provided, check that it's non-zero on first use
139-
if varid and tok.find('%varid%') != -1 and checked_varid == False:
140+
if varid and tok.find('%varid%') != -1 and checked_varid is False:
140141
ret += ' if (varid==0U)\n'
141142
ret += ' throw InternalError(tok, "Internal error. Token::Match called with varid 0. Please report this to Cppcheck developers");\n'
142143
checked_varid = True
@@ -313,7 +314,7 @@ def _replaceSpecificTokenMatch(self, is_simplematch, line, start_pos, end_pos, p
313314
# Compile function or use previously compiled one
314315
patternNumber = self._lookupMatchFunctionId(pattern, None, varId, False)
315316

316-
if patternNumber == None:
317+
if patternNumber is None:
317318
patternNumber = len(self._rawMatchFunctions) + 1
318319
self._insertMatchFunctionId(patternNumber, pattern, None, varId, False)
319320
self._rawMatchFunctions.append(self._compilePattern(pattern, patternNumber, varId))
@@ -340,10 +341,10 @@ def _replaceTokenMatch(self, line):
340341
break
341342

342343
res = self.parseMatch(line, pos1)
343-
if res == None:
344+
if res is None:
344345
break
345346

346-
assert(len(res)==3 or len(res)==4) # assert that Token::Match has either 2 or 3 arguments
347+
assert(len(res) == 3 or len(res) == 4) # assert that Token::Match has either 2 or 3 arguments
347348

348349
end_pos = len(res[0])
349350
tok = res[1]
@@ -353,7 +354,7 @@ def _replaceTokenMatch(self, line):
353354
varId = res[3]
354355

355356
res = re.match(r'\s*"([^"]*)"\s*$', raw_pattern)
356-
if res == None:
357+
if res is None:
357358
break # Non-const pattern - bailout
358359

359360
pattern = res.group(1)
@@ -410,7 +411,7 @@ def _replaceSpecificFindTokenMatch(self, is_findsimplematch, line, start_pos, en
410411
# Compile function or use previously compiled one
411412
findMatchNumber = self._lookupMatchFunctionId(pattern, endToken, varId, True)
412413

413-
if findMatchNumber == None:
414+
if findMatchNumber is None:
414415
findMatchNumber = len(self._rawMatchFunctions) + 1
415416
self._insertMatchFunctionId(findMatchNumber, pattern, endToken, varId, True)
416417
self._rawMatchFunctions.append(self._compileFindPattern(pattern, findMatchNumber, endToken, varId))
@@ -438,10 +439,10 @@ def _replaceTokenFindMatch(self, line):
438439
break
439440

440441
res = self.parseMatch(line, pos1)
441-
if res == None:
442+
if res is None:
442443
break
443444

444-
assert(len(res)>=3 or len(res) < 6) # assert that Token::find(simple)match has either 2, 3 or four arguments
445+
assert(len(res) >= 3 or len(res) < 6) # assert that Token::find(simple)match has either 2, 3 or four arguments
445446

446447
g0 = res[0]
447448
tok = res[1]
@@ -462,16 +463,16 @@ def _replaceTokenFindMatch(self, line):
462463
# Token *findmatch(const Token *tok, const char pattern[], unsigned int varId = 0);
463464
# Token *findmatch(const Token *tok, const char pattern[], const Token *end, unsigned int varId = 0);
464465
endToken = None
465-
if is_findsimplematch == True and len(res) == 4:
466+
if is_findsimplematch is True and len(res) == 4:
466467
endToken = res[3]
467-
elif is_findsimplematch == False:
468+
elif is_findsimplematch is False:
468469
if varId and len(res) == 5:
469470
endToken = res[3]
470-
elif varId == None and len(res) == 4:
471+
elif varId is None and len(res) == 4:
471472
endToken = res[3]
472473

473474
res = re.match(r'\s*"([^"]*)"\s*$', pattern)
474-
if res == None:
475+
if res is None:
475476
break # Non-const pattern - bailout
476477

477478
pattern = res.group(1)
@@ -488,7 +489,7 @@ def _replaceCStrings(self, line):
488489
break
489490

490491
res = self._parseStringComparison(line, match.start())
491-
if res == None:
492+
if res is None:
492493
break
493494

494495
startPos = res[0]
@@ -538,6 +539,7 @@ def convertFile(self, srcname, destname):
538539
fout.write(header+stringList+strFunctions+code)
539540
fout.close()
540541

542+
541543
def main():
542544
# Main program
543545
build_dir = 'build'
@@ -556,7 +558,7 @@ def main():
556558
# Argument handling
557559
parser = argparse.ArgumentParser(description='Compile Token::Match() calls into native C++ code')
558560
parser.add_argument('--verify', action='store_true', default=False,
559-
help='verify compiled matches against on-the-fly parser. Slow!')
561+
help='verify compiled matches against on-the-fly parser. Slow!')
560562
args = parser.parse_args()
561563

562564
mc = MatchCompiler(verify_mode=args.verify)

tools/test_matchcompiler.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
import unittest
2020
import matchcompiler
2121

22+
2223
class MatchCompilerTest(unittest.TestCase):
2324
def setUp(self):
2425
self.mc = matchcompiler.MatchCompiler(verify_mode=False)
2526

2627
def test_parseMatch(self):
27-
self.assertEqual(self.mc.parseMatch(' Token::Match(tok, ";") ',2), ['Token::Match(tok, ";")','tok',' ";"'])
28-
self.assertEqual(self.mc.parseMatch(' Token::Match(tok,', 2), None) # multiline Token::Match is not supported yet
29-
self.assertEqual(self.mc.parseMatch(' Token::Match(Token::findsimplematch(tok,")"), ";")', 2), ['Token::Match(Token::findsimplematch(tok,")"), ";")', 'Token::findsimplematch(tok,")")', ' ";"']) # inner function call
28+
self.assertEqual(self.mc.parseMatch(' Token::Match(tok, ";") ', 2), ['Token::Match(tok, ";")', 'tok', ' ";"'])
29+
self.assertEqual(self.mc.parseMatch(' Token::Match(tok,', 2), None) # multiline Token::Match is not supported yet
30+
self.assertEqual(self.mc.parseMatch(' Token::Match(Token::findsimplematch(tok,")"), ";")', 2), ['Token::Match(Token::findsimplematch(tok,")"), ";")', 'Token::findsimplematch(tok,")")', ' ";"']) # inner function call
3031

3132
def test_replaceTokenMatch(self):
3233
input = 'if (Token::Match(tok, "foobar")) {'

0 commit comments

Comments
 (0)