addons/namingng.py: Fix some PEP 8 and Inspections warnings (#2351)
This commit is contained in:
parent
bd5d82c9bd
commit
4398cf0dec
|
@ -28,13 +28,15 @@ import re
|
||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
|
|
||||||
## Auxiliary class
|
|
||||||
class dataStruct:
|
# Auxiliary class
|
||||||
|
class DataStruct:
|
||||||
def __init__(self, file, linenr, string):
|
def __init__(self, file, linenr, string):
|
||||||
self.file = file
|
self.file = file
|
||||||
self.linenr = linenr
|
self.linenr = linenr
|
||||||
self.str = string
|
self.str = string
|
||||||
|
|
||||||
|
|
||||||
def reportError(filename, linenr, severity, msg):
|
def reportError(filename, linenr, severity, msg):
|
||||||
message = "[{filename}:{linenr}] ( {severity} ) naming.py: {msg}\n".format(
|
message = "[{filename}:{linenr}] ( {severity} ) naming.py: {msg}\n".format(
|
||||||
filename=filename,
|
filename=filename,
|
||||||
|
@ -45,27 +47,31 @@ def reportError(filename, linenr, severity, msg):
|
||||||
sys.stderr.write(message)
|
sys.stderr.write(message)
|
||||||
return message
|
return message
|
||||||
|
|
||||||
|
|
||||||
def loadConfig(configfile):
|
def loadConfig(configfile):
|
||||||
with open(configfile) as fh:
|
with open(configfile) as fh:
|
||||||
data = json.load(fh)
|
data = json.load(fh)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def checkTrueRegex(data, expr, msg, errors):
|
def checkTrueRegex(data, expr, msg, errors):
|
||||||
res = re.match(expr, data.str)
|
res = re.match(expr, data.str)
|
||||||
if res:
|
if res:
|
||||||
errors.append(reportError(data.file, data.linenr, 'style', msg))
|
errors.append(reportError(data.file, data.linenr, 'style', msg))
|
||||||
|
|
||||||
|
|
||||||
def checkFalseRegex(data, expr, msg, errors):
|
def checkFalseRegex(data, expr, msg, errors):
|
||||||
res = re.match(expr, data.str)
|
res = re.match(expr, data.str)
|
||||||
if not res:
|
if not res:
|
||||||
errors.append(reportError(data.file, data.linenr, 'style', msg))
|
errors.append(reportError(data.file, data.linenr, 'style', msg))
|
||||||
|
|
||||||
|
|
||||||
def evalExpr(conf, exp, mockToken, msgType, errors):
|
def evalExpr(conf, exp, mockToken, msgType, errors):
|
||||||
if isinstance(conf, dict):
|
if isinstance(conf, dict):
|
||||||
if (conf[exp][0]):
|
if conf[exp][0]:
|
||||||
msg = msgType + ' ' + mockToken.str + ' violates naming convention : ' + conf[exp][1]
|
msg = msgType + ' ' + mockToken.str + ' violates naming convention : ' + conf[exp][1]
|
||||||
checkTrueRegex(mockToken, exp, msg, errors)
|
checkTrueRegex(mockToken, exp, msg, errors)
|
||||||
elif (~conf[exp][0]):
|
elif ~conf[exp][0]:
|
||||||
msg = msgType + ' ' + mockToken.str + ' violates naming convention : ' + conf[exp][1]
|
msg = msgType + ' ' + mockToken.str + ' violates naming convention : ' + conf[exp][1]
|
||||||
checkFalseRegex(mockToken, exp, msg, errors)
|
checkFalseRegex(mockToken, exp, msg, errors)
|
||||||
else:
|
else:
|
||||||
|
@ -75,6 +81,7 @@ def evalExpr(conf, exp, mockToken, msgType, errors):
|
||||||
msg = msgType + ' ' + mockToken.str + ' violates naming convention'
|
msg = msgType + ' ' + mockToken.str + ' violates naming convention'
|
||||||
checkFalseRegex(mockToken, exp, msg, errors)
|
checkFalseRegex(mockToken, exp, msg, errors)
|
||||||
|
|
||||||
|
|
||||||
def process(dumpfiles, configfile, debugprint=False):
|
def process(dumpfiles, configfile, debugprint=False):
|
||||||
|
|
||||||
errors = []
|
errors = []
|
||||||
|
@ -87,18 +94,18 @@ def process(dumpfiles, configfile, debugprint=False):
|
||||||
print('Checking ' + afile + '...')
|
print('Checking ' + afile + '...')
|
||||||
data = cppcheckdata.parsedump(afile)
|
data = cppcheckdata.parsedump(afile)
|
||||||
|
|
||||||
## Check File naming
|
# Check File naming
|
||||||
if "RE_FILE" in conf and conf["RE_FILE"]:
|
if "RE_FILE" in conf and conf["RE_FILE"]:
|
||||||
mockToken = dataStruct(afile[:-5], "0", afile[afile.rfind('/')+1:-5])
|
mockToken = DataStruct(afile[:-5], "0", afile[afile.rfind('/') + 1:-5])
|
||||||
msgType = 'File name'
|
msgType = 'File name'
|
||||||
for exp in conf["RE_FILE"]:
|
for exp in conf["RE_FILE"]:
|
||||||
evalExpr(conf["RE_FILE"], exp, mockToken, msgType, errors)
|
evalExpr(conf["RE_FILE"], exp, mockToken, msgType, errors)
|
||||||
|
|
||||||
## Check Namespace naming
|
# Check Namespace naming
|
||||||
if "RE_NAMESPACE" in conf and conf["RE_NAMESPACE"]:
|
if "RE_NAMESPACE" in conf and conf["RE_NAMESPACE"]:
|
||||||
for tk in data.rawTokens:
|
for tk in data.rawTokens:
|
||||||
if (tk.str == 'namespace'):
|
if tk.str == 'namespace':
|
||||||
mockToken = dataStruct(tk.next.file, tk.next.linenr, tk.next.str)
|
mockToken = DataStruct(tk.next.file, tk.next.linenr, tk.next.str)
|
||||||
msgType = 'Namespace'
|
msgType = 'Namespace'
|
||||||
for exp in conf["RE_NAMESPACE"]:
|
for exp in conf["RE_NAMESPACE"]:
|
||||||
evalExpr(conf["RE_NAMESPACE"], exp, mockToken, msgType, errors)
|
evalExpr(conf["RE_NAMESPACE"], exp, mockToken, msgType, errors)
|
||||||
|
@ -136,43 +143,43 @@ def process(dumpfiles, configfile, debugprint=False):
|
||||||
var.nameToken.str +
|
var.nameToken.str +
|
||||||
' violates naming convention'))
|
' violates naming convention'))
|
||||||
|
|
||||||
mockToken = dataStruct(var.typeStartToken.file, var.typeStartToken.linenr, var.nameToken.str)
|
mockToken = DataStruct(var.typeStartToken.file, var.typeStartToken.linenr, var.nameToken.str)
|
||||||
msgType = 'Variable'
|
msgType = 'Variable'
|
||||||
for exp in conf["RE_VARNAME"]:
|
for exp in conf["RE_VARNAME"]:
|
||||||
evalExpr(conf["RE_VARNAME"], exp, mockToken, msgType, errors)
|
evalExpr(conf["RE_VARNAME"], exp, mockToken, msgType, errors)
|
||||||
|
|
||||||
## Check Private Variable naming
|
# Check Private Variable naming
|
||||||
if "RE_PRIVATE_MEMBER_VARIABLE" in conf and conf["RE_PRIVATE_MEMBER_VARIABLE"]:
|
if "RE_PRIVATE_MEMBER_VARIABLE" in conf and conf["RE_PRIVATE_MEMBER_VARIABLE"]:
|
||||||
# TODO: Not converted yet
|
# TODO: Not converted yet
|
||||||
for var in cfg.variables:
|
for var in cfg.variables:
|
||||||
if (var.access is None) or var.access != 'Private':
|
if (var.access is None) or var.access != 'Private':
|
||||||
continue
|
continue
|
||||||
mockToken = dataStruct(var.typeStartToken.file, var.typeStartToken.linenr, var.nameToken.str)
|
mockToken = DataStruct(var.typeStartToken.file, var.typeStartToken.linenr, var.nameToken.str)
|
||||||
msgType = 'Private member variable'
|
msgType = 'Private member variable'
|
||||||
for exp in conf["RE_PRIVATE_MEMBER_VARIABLE"]:
|
for exp in conf["RE_PRIVATE_MEMBER_VARIABLE"]:
|
||||||
evalExpr(conf["RE_PRIVATE_MEMBER_VARIABLE"], exp, mockToken, msgType, errors)
|
evalExpr(conf["RE_PRIVATE_MEMBER_VARIABLE"], exp, mockToken, msgType, errors)
|
||||||
|
|
||||||
## Check Public Member Variable naming
|
# Check Public Member Variable naming
|
||||||
if "RE_PUBLIC_MEMBER_VARIABLE" in conf and conf["RE_PUBLIC_MEMBER_VARIABLE"]:
|
if "RE_PUBLIC_MEMBER_VARIABLE" in conf and conf["RE_PUBLIC_MEMBER_VARIABLE"]:
|
||||||
for var in cfg.variables:
|
for var in cfg.variables:
|
||||||
if (var.access is None) or var.access != 'Public':
|
if (var.access is None) or var.access != 'Public':
|
||||||
continue
|
continue
|
||||||
mockToken = dataStruct(var.typeStartToken.file, var.typeStartToken.linenr, var.nameToken.str)
|
mockToken = DataStruct(var.typeStartToken.file, var.typeStartToken.linenr, var.nameToken.str)
|
||||||
msgType = 'Public member variable'
|
msgType = 'Public member variable'
|
||||||
for exp in conf["RE_PUBLIC_MEMBER_VARIABLE"]:
|
for exp in conf["RE_PUBLIC_MEMBER_VARIABLE"]:
|
||||||
evalExpr(conf["RE_PUBLIC_MEMBER_VARIABLE"], exp, mockToken, msgType, errors)
|
evalExpr(conf["RE_PUBLIC_MEMBER_VARIABLE"], exp, mockToken, msgType, errors)
|
||||||
|
|
||||||
## Check Global Variable naming
|
# Check Global Variable naming
|
||||||
if "RE_GLOBAL_VARNAME" in conf and conf["RE_GLOBAL_VARNAME"]:
|
if "RE_GLOBAL_VARNAME" in conf and conf["RE_GLOBAL_VARNAME"]:
|
||||||
for var in cfg.variables:
|
for var in cfg.variables:
|
||||||
if (var.access is None) or var.access != 'Global':
|
if (var.access is None) or var.access != 'Global':
|
||||||
continue
|
continue
|
||||||
mockToken = dataStruct(var.typeStartToken.file, var.typeStartToken.linenr, var.nameToken.str)
|
mockToken = DataStruct(var.typeStartToken.file, var.typeStartToken.linenr, var.nameToken.str)
|
||||||
msgType = 'Public member variable'
|
msgType = 'Public member variable'
|
||||||
for exp in conf["RE_GLOBAL_VARNAME"]:
|
for exp in conf["RE_GLOBAL_VARNAME"]:
|
||||||
evalExpr(conf["RE_GLOBAL_VARNAME"], exp, mockToken, msgType, errors)
|
evalExpr(conf["RE_GLOBAL_VARNAME"], exp, mockToken, msgType, errors)
|
||||||
|
|
||||||
## Check Functions naming
|
# Check Functions naming
|
||||||
if "RE_FUNCTIONNAME" in conf and conf["RE_FUNCTIONNAME"]:
|
if "RE_FUNCTIONNAME" in conf and conf["RE_FUNCTIONNAME"]:
|
||||||
for token in cfg.tokenlist:
|
for token in cfg.tokenlist:
|
||||||
if token.function:
|
if token.function:
|
||||||
|
@ -190,22 +197,23 @@ def process(dumpfiles, configfile, debugprint=False):
|
||||||
if not token.function.name.startswith(conf["function_prefixes"][retval]):
|
if not token.function.name.startswith(conf["function_prefixes"][retval]):
|
||||||
errors.append(reportError(
|
errors.append(reportError(
|
||||||
token.file, token.linenr, 'style', 'Function ' + token.function.name + ' violates naming convention'))
|
token.file, token.linenr, 'style', 'Function ' + token.function.name + ' violates naming convention'))
|
||||||
mockToken = dataStruct(token.file, token.linenr, token.function.name)
|
mockToken = DataStruct(token.file, token.linenr, token.function.name)
|
||||||
msgType = 'Function'
|
msgType = 'Function'
|
||||||
for exp in conf["RE_FUNCTIONNAME"]:
|
for exp in conf["RE_FUNCTIONNAME"]:
|
||||||
evalExpr(conf["RE_FUNCTIONNAME"], exp, mockToken, msgType, errors)
|
evalExpr(conf["RE_FUNCTIONNAME"], exp, mockToken, msgType, errors)
|
||||||
|
|
||||||
## Check Class naming
|
# Check Class naming
|
||||||
if "RE_CLASS_NAME" in conf and conf["RE_CLASS_NAME"]:
|
if "RE_CLASS_NAME" in conf and conf["RE_CLASS_NAME"]:
|
||||||
for fnc in cfg.functions:
|
for fnc in cfg.functions:
|
||||||
# Check if it is Constructor/Destructor
|
# Check if it is Constructor/Destructor
|
||||||
if (fnc.type == 'Constructor' or fnc.type == 'Destructor'):
|
if fnc.type == 'Constructor' or fnc.type == 'Destructor':
|
||||||
mockToken = dataStruct(fnc.tokenDef.file, fnc.tokenDef.linenr, fnc.name)
|
mockToken = DataStruct(fnc.tokenDef.file, fnc.tokenDef.linenr, fnc.name)
|
||||||
msgType = 'Class ' + fnc.type
|
msgType = 'Class ' + fnc.type
|
||||||
for exp in conf["RE_CLASS_NAME"]:
|
for exp in conf["RE_CLASS_NAME"]:
|
||||||
evalExpr(conf["RE_CLASS_NAME"], exp, mockToken, msgType, errors)
|
evalExpr(conf["RE_CLASS_NAME"], exp, mockToken, msgType, errors)
|
||||||
return errors
|
return errors
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser(description='Naming verification')
|
parser = argparse.ArgumentParser(description='Naming verification')
|
||||||
parser.add_argument('dumpfiles', type=str, nargs='+',
|
parser.add_argument('dumpfiles', type=str, nargs='+',
|
||||||
|
|
Loading…
Reference in New Issue