Skip to content

Commit

Permalink
Add --no-color option to the parser
Browse files Browse the repository at this point in the history
Sometimes terminals might not support colors, in that case
--no-color should be used to avoid garbled output due to
color formatting strings.
  • Loading branch information
Sanket Agarwal committed Mar 17, 2012
1 parent 3c1b1e6 commit 871567e
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions bin/coverage_doctest.py
Expand Up @@ -50,19 +50,21 @@
c_normal = '\033[0m' c_normal = '\033[0m'
c_color = '\033[%sm' c_color = '\033[%sm'




def print_header(name, underline=None, overline=None): def print_header(name, underline=None, overline=None):


print print
print name print name
if underline: print underline*len(name) if underline: print underline*len(name)


def print_coverage(module_path, c, c_md, c_mdt, c_idt, f, f_md, f_mdt, f_idt, score, total_doctests, total_members, verbose=False): def print_coverage(module_path, c, c_md, c_mdt, c_idt, f, f_md, f_mdt, f_idt, score, total_doctests, total_members, verbose=False, no_color=False):


""" Prints details (depending on verbose) of a module """ """ Prints details (depending on verbose) of a module """


if score < 100: if no_color:
score_string = "%s%% (%s of %s)" % (score, total_doctests, \
total_members)

elif score < 100:
score_string = "%s%s%% (%s of %s)%s" % (c_color % (colors["Red"]) \ score_string = "%s%s%% (%s of %s)%s" % (c_color % (colors["Red"]) \
, score , total_doctests, total_members, c_normal) , score , total_doctests, total_members, c_normal)
else: else:
Expand Down Expand Up @@ -283,7 +285,7 @@ def process_class(c_name, obj, c_sk, c_md, c_mdt, c_idt, c_has_doctest):


return c_dt, c, source return c_dt, c, source


def coverage(module_path, verbose=False): def coverage(module_path, verbose=False, no_color=False):


""" Given a module path, builds an index of all classes and functions """ Given a module path, builds an index of all classes and functions
contained. It then goes through each of the classes/functions to get contained. It then goes through each of the classes/functions to get
Expand Down Expand Up @@ -390,17 +392,17 @@ def coverage(module_path, verbose=False):
f_mdt = sorted(f_mdt, key = lambda x: int(x.split()[1][:-1])) f_mdt = sorted(f_mdt, key = lambda x: int(x.split()[1][:-1]))
f_idt = sorted(f_idt, key = lambda x: int(x.split()[1][:-1])) f_idt = sorted(f_idt, key = lambda x: int(x.split()[1][:-1]))


print_coverage(module_path, classes, c_md, c_mdt, c_idt, functions, f_md, f_mdt, f_idt, score, total_doctests, total_members, verbose) print_coverage(module_path, classes, c_md, c_mdt, c_idt, functions, f_md, f_mdt, f_idt, score, total_doctests, total_members, verbose, no_color)




return total_doctests, total_members return total_doctests, total_members


def go(sympy_top, file, verbose=False, exact=True): def go(sympy_top, file, verbose=False, no_color=False, exact=True):


if os.path.isdir(file): if os.path.isdir(file):
doctests, num_functions = 0, 0 doctests, num_functions = 0, 0
for F in os.listdir(file): for F in os.listdir(file):
_doctests, _num_functions = go(sympy_top, '%s/%s'%(file,F), verbose, exact=False) _doctests, _num_functions = go(sympy_top, '%s/%s'%(file,F), verbose, no_color, exact=False)
doctests += _doctests doctests += _doctests
num_functions += _num_functions num_functions += _num_functions
return doctests, num_functions return doctests, num_functions
Expand All @@ -413,7 +415,7 @@ def go(sympy_top, file, verbose=False, exact=True):
sys.exit(1) sys.exit(1)


# Relpath for constructing the module name # Relpath for constructing the module name
return coverage(get_mod_name(file, sympy_top), verbose) return coverage(get_mod_name(file, sympy_top), verbose, no_color)




if __name__ == "__main__": if __name__ == "__main__":
Expand All @@ -434,6 +436,7 @@ def go(sympy_top, file, verbose=False, exact=True):


parser.add_option("-v", "--verbose", action="store_true", dest="verbose", parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
default=False) default=False)
parser.add_option("--no-color", action="store_true", dest="no_color", help="use no colors", default=False)


options, args = parser.parse_args() options, args = parser.parse_args()


Expand All @@ -446,18 +449,24 @@ def go(sympy_top, file, verbose=False, exact=True):
print 'DOCTEST COVERAGE for %s' % (file) print 'DOCTEST COVERAGE for %s' % (file)
print '='*70 print '='*70
print print
doctests, num_functions = go(sympy_top, file, options.verbose) doctests, num_functions = go(sympy_top, file, options.verbose, options.no_color)
if num_functions == 0: if num_functions == 0:
score = 100 score = 100
else: else:
score = 100 * float(doctests) / num_functions score = 100 * float(doctests) / num_functions
score = int(score) score = int(score)
print print
print '='*70 print '='*70
if score < 100:
if options.no_color:
print "TOTAL SCORE for %s: %s%% (%s of %s)" % \
(get_mod_name(file, sympy_top),score, doctests, num_functions)

elif score < 100:
print "TOTAL SCORE for %s: %s%s%% (%s of %s)%s" % \ print "TOTAL SCORE for %s: %s%s%% (%s of %s)%s" % \
(get_mod_name(file, sympy_top), c_color % (colors["Red"]),\ (get_mod_name(file, sympy_top), c_color % (colors["Red"]),\
score, doctests, num_functions, c_normal) score, doctests, num_functions, c_normal)

else: else:
print "TOTAL SCORE for %s: %s%s%% (%s of %s)%s" % \ print "TOTAL SCORE for %s: %s%s%% (%s of %s)%s" % \
(get_mod_name(file, sympy_top), c_color % (colors["Green"]),\ (get_mod_name(file, sympy_top), c_color % (colors["Green"]),\
Expand Down

0 comments on commit 871567e

Please sign in to comment.