Skip to content

Instantly share code, notes, and snippets.

@mattvonrocketstein
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save mattvonrocketstein/9918077 to your computer and use it in GitHub Desktop.

Select an option

Save mattvonrocketstein/9918077 to your computer and use it in GitHub Desktop.
triage thingy for radon output
#!/usr/bin/env python
""" triage.py reformats radon output
this code anticipates a command line usage as follows:
$ triage.py ./code
it works by reformatting the output of a radon command-line similar to this:
$ radon cc ./code/ --total-average --show-complexity > radon.out
"""
import functools
import os, sys, tempfile
from collections import defaultdict
from pygments.console import colorize
ope = os.path.exists
red = functools.partial(colorize, 'red')
DIV = red('-'*80)
RADON_T = "radon cc {0} --total-average --show-complexity > {1}"
BLOCK_TYPES = dict(M='method', C='class', F='function')
def parse_output(fname):
assert ope(fname), 'file does not exist: '+fname
with open(fname) as fhandle:
lines = fhandle.readlines()
mark = None
tmp_dict = defaultdict(lambda:[])
while lines:
line = lines.pop(0)
is_sub = line.startswith(' ')
if line=='\n':
# when loop exits, what's left is the summary
break
if is_sub:
tmp_dict[mark].append(line.strip())
else:
mark = line.strip()
tmp_dict = sorted(tmp_dict.items())
triaged = defaultdict(lambda:[])
errors = []
for fname,block_list in tmp_dict:
out = []
for line in block_list:
if line.startswith("ERROR:"):
errors.append(line.strip())
continue
_type, index, dotpath, sep, grade, score = line.split()
_type = BLOCK_TYPES[_type]
line = "{0} in {1} @ location {2} (type={3}) ".format(
fname.replace(os.getcwd(),''),
dotpath+('()' if _type in 'function method'.split() else ''),
index, _type)
triaged[grade].append(line)
info=[]
summary = [line.strip() for line in lines]
for grade in triaged:
_list=triaged[grade]
banner = 'score = {0} ({1} blocks) '.format(grade, len(_list)).upper()
print '\n',red(banner)+'\n'+DIV
for line in _list:
print line
breakdown = dict([ [x, len(y)] for x,y in triaged.items() ])
breakdown = 'Breakdown of stats by grade: '+str(breakdown)
print '\n',red("SUMMARY"),'\n',DIV,'\n',breakdown,'\n','\n'.join(summary)
def gen_output(fpath):
tmp = tempfile.mktemp(suffix='radon.out')
cmd = RADON_T.format(fpath, tmp)
if not ope(fpath):
raise SystemExit('path does not exist: '+fpath)
print red("triaging: " + cmd)
stat = os.system(cmd)
return tmp, stat
def main(fpath):
try:
tmp, stat = gen_output(fpath)
parse_output(tmp)
finally:
os.remove(tmp)
if __name__=='__main__':
#parse_output(sys.argv[1])
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment