Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. """ Tiny wrapper for mccabe module.
  2.  
  3. Iterate over all the python sources, and print results in
  4. an easily parseable format
  5.  
  6. If you are already using pyflakes or prospector, you won't need this :)
  7.  
  8. Dependencies: mmcabe, path.py
  9. """
  10.  
  11. import ast
  12. import sys
  13. import textwrap
  14.  
  15. import mccabe
  16. import path
  17.  
  18.  
  19. def ignore(py_source):
  20. parts = py_source.splitall()
  21. # Ignore 'hidden' files
  22. if any(x.startswith(".") for x in parts):
  23. return True
  24. return False
  25.  
  26.  
  27. def yield_sources():
  28. top = path.Path(".")
  29. for py_source in top.walkfiles("*.py"):
  30. py_source = py_source.relpath(top)
  31. if not ignore(py_source):
  32. yield py_source
  33.  
  34.  
  35. def process(py_source, max_complexity):
  36. code = py_source.text()
  37. tree = compile(code, py_source, "exec", ast.PyCF_ONLY_AST)
  38. visitor = mccabe.PathGraphingAstVisitor()
  39. visitor.preorder(tree, visitor)
  40. for graph in visitor.graphs.values():
  41. if graph.complexity() > max_complexity:
  42. text = textwrap.dedent("""\
  43. {}:{}:{} {} {}""")
  44. text = text.format(py_source, graph.lineno, graph.column, graph.entity,
  45. graph.complexity())
  46. print(text)
  47.  
  48.  
  49. def main():
  50. max_complexity = int(sys.argv[1])
  51. print("Looking for code with complexity above", max_complexity)
  52. for py_source in yield_sources():
  53. process(py_source, max_complexity)
  54.  
  55.  
  56. if __name__ == "__main__":
  57. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement