(code, threshold=7, filename='stdin')
| 271 | |
| 272 | |
| 273 | def get_code_complexity(code, threshold=7, filename='stdin'): |
| 274 | try: |
| 275 | tree = compile(code, filename, "exec", ast.PyCF_ONLY_AST) |
| 276 | except SyntaxError: |
| 277 | e = sys.exc_info()[1] |
| 278 | sys.stderr.write("Unable to parse %s: %s\n" % (filename, e)) |
| 279 | return 0 |
| 280 | |
| 281 | complx = [] |
| 282 | McCabeChecker.max_complexity = threshold |
| 283 | for lineno, offset, text, check in McCabeChecker(tree, filename).run(): |
| 284 | complx.append('%s:%d:1: %s' % (filename, lineno, text)) |
| 285 | |
| 286 | if len(complx) == 0: |
| 287 | return 0 |
| 288 | print('\n'.join(complx)) |
| 289 | return len(complx) |
| 290 | |
| 291 | |
| 292 | def get_module_complexity(module_path, threshold=7): |
searching dependent graphs…