Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def process(py_source, max_complexity):
code = py_source.text()
tree = compile(code, py_source, "exec", ast.PyCF_ONLY_AST)
visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)
for graph in visitor.graphs.values():
if graph.complexity() > max_complexity:
text = "{}:{}:{} {} {}"
return text.format(py_source, graph.lineno, graph.column, graph.entity,
graph.complexity())
def pyls_lint(config, document):
threshold = config.plugin_settings('mccabe').get(THRESHOLD, DEFAULT_THRESHOLD)
log.debug("Running mccabe lint with threshold: %s", threshold)
try:
tree = compile(document.source, document.path, "exec", ast.PyCF_ONLY_AST)
except SyntaxError:
# We'll let the other linters point this one out
return None
visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)
diags = []
for graph in visitor.graphs.values():
if graph.complexity() >= threshold:
diags.append({
'source': 'mccabe',
'range': {
'start': {'line': graph.lineno - 1, 'character': graph.column},
'end': {'line': graph.lineno - 1, 'character': len(document.lines[graph.lineno])},
},
'message': 'Cyclomatic complexity too high: %s (threshold %s)' % (graph.complexity(), threshold),
'severity': lsp.DiagnosticSeverity.Warning
})
return diags
def get_mccabe_violations_for_file(filepath, max_complexity):
code = _read(filepath)
tree = compile(code, filepath, "exec", ast.PyCF_ONLY_AST)
visitor = PathGraphingAstVisitor()
visitor.preorder(tree, visitor)
violations = []
for graph in visitor.graphs.values():
if graph.complexity() >= max_complexity:
complex_function_name = graph.entity
if complex_function_name.startswith('If '):
complex_function_name = 'if __name__ == "__main__"'
violations.append(complex_function_name)
return violations
if not self.__filename or not self.__source:
# don't do anything, if essential data is missing
return
if self.__ignoreCode("C101"):
# don't do anything, if this should be ignored
return
try:
tree = compile(''.join(self.__source), self.__filename, 'exec',
ast.PyCF_ONLY_AST)
except (SyntaxError, TypeError):
self.__reportInvalidSyntax()
return
visitor = PathGraphingAstVisitor()
visitor.preorder(tree, visitor)
for graph in visitor.graphs.values():
if graph.complexity() > self.__maxComplexity:
self.__error(graph.lineno, 0, "C101",
graph.entity, graph.complexity())
def pyls_lint(config, document):
threshold = config.plugin_settings("mccabe").get(THRESHOLD, DEFAULT_THRESHOLD)
log.debug("Running mccabe lint with threshold: %s", threshold)
try:
tree = compile(document.source, document.path, "exec", ast.PyCF_ONLY_AST)
except SyntaxError:
# We'll let the other linters point this one out
return None
visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)
diags = []
for graph in visitor.graphs.values():
if graph.complexity() >= threshold:
diags.append(
{
"source": "mccabe",
"range": {
"start": {"line": graph.lineno, "character": graph.column},
"end": {
"line": graph.lineno,
"character": len(document.lines[graph.lineno]),
},
},
"message": "Cyclomatic complexity too high: %s (threshold %s)"