From ae73466f97a03a1e7975a10b413609ec048eb29a Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Sat, 2 Mar 2013 17:45:26 +0200 Subject: [PATCH] Python scripts: PEP8 fixes --- htmlreport/cppcheck-htmlreport | 2 ++ htmlreport/setup.py | 4 +-- tools/extracttests.py | 45 ++++++++++++++++++---------------- tools/matchcompiler.py | 32 ++++++++++++------------ tools/test_matchcompiler.py | 7 +++--- 5 files changed, 49 insertions(+), 41 deletions(-) diff --git a/htmlreport/cppcheck-htmlreport b/htmlreport/cppcheck-htmlreport index b2c02b120..3037232b6 100755 --- a/htmlreport/cppcheck-htmlreport +++ b/htmlreport/cppcheck-htmlreport @@ -148,6 +148,7 @@ HTML_FOOTER = """ HTML_ERROR = "<--- %s\n" + class AnnotateCodeFormatter(HtmlFormatter): errors = [] @@ -162,6 +163,7 @@ class AnnotateCodeFormatter(HtmlFormatter): line_no = line_no + 1 yield i, t + class CppCheckHandler(XmlContentHandler): """Parses the cppcheck xml file and produces a list of all its errors.""" errors = [] diff --git a/htmlreport/setup.py b/htmlreport/setup.py index 6d2cece9b..26af29311 100755 --- a/htmlreport/setup.py +++ b/htmlreport/setup.py @@ -6,5 +6,5 @@ if __name__ == '__main__': name="cppcheck", scripts=[ "cppcheck-htmlreport", - ] - ) + ] + ) diff --git a/tools/extracttests.py b/tools/extracttests.py index 6d645b48d..640e6dd70 100755 --- a/tools/extracttests.py +++ b/tools/extracttests.py @@ -25,6 +25,7 @@ import os import sys import re + class Extract: """ Read Cppcheck test file and create data @@ -34,14 +35,14 @@ class Extract: # array that stores all the test cases nodes = [] - def parseFile(self,filename): + def parseFile(self, filename): """ parse test file and add info to the nodes variable """ name = '[0-9a-zA-Z_]+' - string = '\\"(.+)\\"' + string = '\\"(.+)\\"' testclass = None functionName = None @@ -50,50 +51,52 @@ class Extract: for line in fin: # testclass starts res = re.match('class ('+name+')', line) - if res != None: + if res is not None: testclass = res.group(1) # end of testclass - if re.match('};', line) != None: + if re.match('};', line) is not None: testclass = None # function start res = re.match('\\s+void ('+name+')\\(\\)', line) - if res != None: + if res is not None: functionName = res.group(1) - elif re.match('\\s+}', line) != None: + elif re.match('\\s+}', line) is not None: functionName = None - if functionName == None: + if functionName is None: continue # check res = re.match('\s+check.*\('+string, line) - if res != None: + if res is not None: code = res.group(1) # code.. res = re.match('\\s+'+string, line) - if res != None: + if res is not None: code = code + res.group(1) # assert res = re.match('\\s+ASSERT_EQUALS\\(\\"([^"]*)\\",', line) - if res != None and len(code) > 10: - node = { 'testclass':testclass, - 'functionName':functionName, - 'code':code, - 'expected':res.group(1) } + if res is not None and len(code) > 10: + node = {'testclass': testclass, + 'functionName': functionName, + 'code': code, + 'expected': res.group(1)} self.nodes.append(node) code = '' # close test file fin.close() + def strtoxml(s): """Convert string to xml/html format""" - return s.replace('&','&').replace('"', '"').replace('<','<').replace('>','>') + return s.replace('&', '&').replace('"', '"').replace('<', '<').replace('>', '>') + def trimname(name): """Trim test name. Trailing underscore and digits are removed""" @@ -128,7 +131,7 @@ def writeHtmlFile(nodes, functionName, filename, errorsOnly): testclass = None num = 0 for node in nodes: - if errorsOnly and node['expected']=='': + if errorsOnly and node['expected'] == '': continue if trimname(node['functionName']) == functionName: num = num + 1 @@ -144,8 +147,8 @@ def writeHtmlFile(nodes, functionName, filename, errorsOnly): fout.write('' + strtoxml(node['expected']).replace('\\n', '
') + '') fout.write('\n') - if testclass != None: - fout.write('\n'); + if testclass is not None: + fout.write('\n') fout.write('\n') fout.close() @@ -175,7 +178,7 @@ for arg in sys.argv[1:]: # extract test cases -if filename != None: +if filename is not None: # parse test file e = Extract() e.parseFile(filename) @@ -193,7 +196,7 @@ if filename != None: s += '/>' print (s) print ('') - elif htmldir != None: + elif htmldir is not None: if not htmldir.endswith('/'): htmldir += '/' if not os.path.exists(htmldir): @@ -280,7 +283,7 @@ if filename != None: filename += functionName + '.cpp' # source code - fout = open(codedir+filename,'w') + fout = open(codedir + filename, 'w') fout.write(code) fout.close() diff --git a/tools/matchcompiler.py b/tools/matchcompiler.py index 6713f86d9..7e3110293 100755 --- a/tools/matchcompiler.py +++ b/tools/matchcompiler.py @@ -22,6 +22,7 @@ import re import glob import argparse + class MatchCompiler: def __init__(self, verify_mode=False): self._verifyMode = verify_mode @@ -105,7 +106,7 @@ class MatchCompiler: return 'tok->isName()' elif tok == '%varid%': return '(tok->isName() && tok->varId()==varid)' - elif (len(tok)>2) and (tok[0]=="%"): + elif (len(tok) > 2) and (tok[0] == "%"): print ("unhandled:" + tok) return '(tok->str()==' + self._insertMatchStr(tok) + ')/* ' + tok + ' */' @@ -136,7 +137,7 @@ class MatchCompiler: gotoNextToken = ' tok = tok->next();\n' # if varid is provided, check that it's non-zero on first use - if varid and tok.find('%varid%') != -1 and checked_varid == False: + if varid and tok.find('%varid%') != -1 and checked_varid is False: ret += ' if (varid==0U)\n' ret += ' throw InternalError(tok, "Internal error. Token::Match called with varid 0. Please report this to Cppcheck developers");\n' checked_varid = True @@ -313,7 +314,7 @@ class MatchCompiler: # Compile function or use previously compiled one patternNumber = self._lookupMatchFunctionId(pattern, None, varId, False) - if patternNumber == None: + if patternNumber is None: patternNumber = len(self._rawMatchFunctions) + 1 self._insertMatchFunctionId(patternNumber, pattern, None, varId, False) self._rawMatchFunctions.append(self._compilePattern(pattern, patternNumber, varId)) @@ -340,10 +341,10 @@ class MatchCompiler: break res = self.parseMatch(line, pos1) - if res == None: + if res is None: break - assert(len(res)==3 or len(res)==4) # assert that Token::Match has either 2 or 3 arguments + assert(len(res) == 3 or len(res) == 4) # assert that Token::Match has either 2 or 3 arguments end_pos = len(res[0]) tok = res[1] @@ -353,7 +354,7 @@ class MatchCompiler: varId = res[3] res = re.match(r'\s*"([^"]*)"\s*$', raw_pattern) - if res == None: + if res is None: break # Non-const pattern - bailout pattern = res.group(1) @@ -410,7 +411,7 @@ class MatchCompiler: # Compile function or use previously compiled one findMatchNumber = self._lookupMatchFunctionId(pattern, endToken, varId, True) - if findMatchNumber == None: + if findMatchNumber is None: findMatchNumber = len(self._rawMatchFunctions) + 1 self._insertMatchFunctionId(findMatchNumber, pattern, endToken, varId, True) self._rawMatchFunctions.append(self._compileFindPattern(pattern, findMatchNumber, endToken, varId)) @@ -438,10 +439,10 @@ class MatchCompiler: break res = self.parseMatch(line, pos1) - if res == None: + if res is None: break - assert(len(res)>=3 or len(res) < 6) # assert that Token::find(simple)match has either 2, 3 or four arguments + assert(len(res) >= 3 or len(res) < 6) # assert that Token::find(simple)match has either 2, 3 or four arguments g0 = res[0] tok = res[1] @@ -462,16 +463,16 @@ class MatchCompiler: # Token *findmatch(const Token *tok, const char pattern[], unsigned int varId = 0); # Token *findmatch(const Token *tok, const char pattern[], const Token *end, unsigned int varId = 0); endToken = None - if is_findsimplematch == True and len(res) == 4: + if is_findsimplematch is True and len(res) == 4: endToken = res[3] - elif is_findsimplematch == False: + elif is_findsimplematch is False: if varId and len(res) == 5: endToken = res[3] - elif varId == None and len(res) == 4: + elif varId is None and len(res) == 4: endToken = res[3] res = re.match(r'\s*"([^"]*)"\s*$', pattern) - if res == None: + if res is None: break # Non-const pattern - bailout pattern = res.group(1) @@ -488,7 +489,7 @@ class MatchCompiler: break res = self._parseStringComparison(line, match.start()) - if res == None: + if res is None: break startPos = res[0] @@ -538,6 +539,7 @@ class MatchCompiler: fout.write(header+stringList+strFunctions+code) fout.close() + def main(): # Main program build_dir = 'build' @@ -556,7 +558,7 @@ def main(): # Argument handling parser = argparse.ArgumentParser(description='Compile Token::Match() calls into native C++ code') parser.add_argument('--verify', action='store_true', default=False, - help='verify compiled matches against on-the-fly parser. Slow!') + help='verify compiled matches against on-the-fly parser. Slow!') args = parser.parse_args() mc = MatchCompiler(verify_mode=args.verify) diff --git a/tools/test_matchcompiler.py b/tools/test_matchcompiler.py index 36fe7896a..daa1d90b8 100755 --- a/tools/test_matchcompiler.py +++ b/tools/test_matchcompiler.py @@ -19,14 +19,15 @@ import unittest import matchcompiler + class MatchCompilerTest(unittest.TestCase): def setUp(self): self.mc = matchcompiler.MatchCompiler(verify_mode=False) def test_parseMatch(self): - self.assertEqual(self.mc.parseMatch(' Token::Match(tok, ";") ',2), ['Token::Match(tok, ";")','tok',' ";"']) - self.assertEqual(self.mc.parseMatch(' Token::Match(tok,', 2), None) # multiline Token::Match is not supported yet - self.assertEqual(self.mc.parseMatch(' Token::Match(Token::findsimplematch(tok,")"), ";")', 2), ['Token::Match(Token::findsimplematch(tok,")"), ";")', 'Token::findsimplematch(tok,")")', ' ";"']) # inner function call + self.assertEqual(self.mc.parseMatch(' Token::Match(tok, ";") ', 2), ['Token::Match(tok, ";")', 'tok', ' ";"']) + self.assertEqual(self.mc.parseMatch(' Token::Match(tok,', 2), None) # multiline Token::Match is not supported yet + self.assertEqual(self.mc.parseMatch(' Token::Match(Token::findsimplematch(tok,")"), ";")', 2), ['Token::Match(Token::findsimplematch(tok,")"), ";")', 'Token::findsimplematch(tok,")")', ' ";"']) # inner function call def test_replaceTokenMatch(self): input = 'if (Token::Match(tok, "foobar")) {'