Skip to content
This repository has been archived by the owner on Aug 29, 2022. It is now read-only.

Commit

Permalink
pep8.
Browse files Browse the repository at this point in the history
  • Loading branch information
ulif committed Sep 27, 2013
1 parent 1158602 commit f92b404
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
9 changes: 5 additions & 4 deletions src/z3c/coverage/coveragediff.py
Expand Up @@ -72,9 +72,9 @@ def filter_files(files, include=(), exclude=()):
"""
if not include:
include = ['.'] # include everything by default
include = ['.'] # include everything by default
if not exclude:
exclude = [] # exclude nothing by default
exclude = [] # exclude nothing by default
include = list(map(re.compile, include))
exclude = list(map(re.compile, exclude))
return [fn for fn in files
Expand Down Expand Up @@ -136,7 +136,7 @@ def count_coverage(filename):
for line in file:
if line.startswith('>>>>>>'):
uncovered += 1
elif len(line) >= 7 and not line.startswith(' '*7):
elif len(line) >= 7 and not line.startswith(' ' * 7):
covered += 1
return covered, uncovered

Expand Down Expand Up @@ -302,7 +302,8 @@ def main():
parser.error("wrong number of arguments")
olddir, newdir = args
if opts.email:
reporter = ReportEmailer(opts.sender, opts.email, opts.subject, opts.web_url)
reporter = ReportEmailer(
opts.sender, opts.email, opts.subject, opts.web_url)
else:
reporter = ReportPrinter(opts.web_url)
compare_dirs(olddir, newdir, include=opts.include, exclude=opts.exclude,
Expand Down
34 changes: 22 additions & 12 deletions src/z3c/coverage/coveragereport.py
Expand Up @@ -59,6 +59,7 @@
#: Expected encoding of highlight command (enscript).
HIGHLIGHT_CMD_ENCODING = 'latin1'


class Lazy(object):
"""Descriptor for lazy evaluation"""

Expand Down Expand Up @@ -146,7 +147,7 @@ def _parse(self, filename):
total = 0
with open(filename) as file:
for line in file:
if line.startswith(' '*7) or len(line) < 7:
if line.startswith(' ' * 7) or len(line) < 7:
continue
total += 1
if not line.startswith('>>>>>>'):
Expand Down Expand Up @@ -182,8 +183,8 @@ def annotated_source(self):
OTHER = ' '
lines = []
f = open(self.source_filename)
for n, line in enumerate(f):
n += 1 # workaround lack of enumerate(f, start=1) support in 2.4/5
for n, line in enumerate(f, start=1):
n += 1 # workaround: no enumerate(f, start=1) support in 2.4/5

This comment has been minimized.

Copy link
@mgedmin

mgedmin Apr 24, 2017

Member

Whoops, this introduced a bug and I didn't notice: the n += 1 is no longer necessary if you pass start=1 to enumerate().

if n in self._missing: prefix = MISSING
elif n in self._excluded: prefix = EXCLUDED
elif n in self._statements: prefix = STATEMENT
Expand Down Expand Up @@ -275,6 +276,7 @@ def apply_path_aliases(cov, aliases):
# longest key first
aliases = sorted(
aliases.items(), key=lambda i: len(i[0]), reverse=True)

def fixup_filename(filename):
for alias, local in aliases:
return local + filename[len(alias):]
Expand Down Expand Up @@ -405,13 +407,14 @@ def generate_html(output_filename, tree, my_index, info, path, footer=""):
html = open(output_filename, 'w')
print(HEADER % {'name': index_to_name(my_index)}, file=html)
info = [(tree.get_at(node_path), node_path) for node_path in info]

def key(node_info):
(node, node_path) = node_info
return (len(node_path), -node.uncovered, node_path and node_path[-1])
info.sort(key=key)
for node, file_index in info:
if not file_index:
continue # skip root node
continue # skip root node
print_table_row(html, node, file_index)
print('</table><hr/>', file=html)
source = tree.get_at(my_index).html_source
Expand All @@ -438,7 +441,7 @@ def syntax_highlight(filename):
text = cgi.escape(file.read())
else:
text = text.decode(HIGHLIGHT_CMD_ENCODING)
text = text[text.find('<PRE>')+len('<PRE>'):]
text = text[text.find('<PRE>') + len('<PRE>'):]
text = text[:text.find('</PRE>')]
return text

Expand All @@ -448,7 +451,7 @@ def highlight_uncovered_lines(text):
def color_uncov(line):
# The line must start with the missing line indicator or some HTML
# was put in front of it.
if line.startswith('&gt;'*6) or '>'+'&gt;'*6 in line:
if line.startswith('&gt;' * 6) or '>' + '&gt;' * 6 in line:
return ('<div class="notcovered">%s</div>'
% line.rstrip('\n'))
return line
Expand All @@ -467,6 +470,7 @@ def generate_htmls_from_tree(tree, path, report_path, footer=""):
"""
def make_html(node, my_index):
info = []

def list_parents_and_children(node, index):
position = len(index)
my_position = len(my_index)
Expand All @@ -479,7 +483,7 @@ def list_parents_and_children(node, index):
traverse_tree(tree, [], list_parents_and_children)
output_filename = os.path.join(report_path, index_to_url(my_index))
if not my_index:
return # skip root node
return # skip root node
generate_html(output_filename, tree, my_index, info, path, footer)
traverse_tree(tree, [], make_html)

Expand All @@ -488,12 +492,15 @@ def generate_overall_html_from_tree(tree, output_filename, footer=""):
"""Generate an overall HTML file for all nodes in the tree."""
html = open(output_filename, 'w')
print(HEADER % {'name': ', '.join(sorted(tree.keys()))}, file=html)

def print_node(node, file_index):
if file_index: # skip root node
if file_index: # skip root node
print_table_row(html, node, file_index)

def sort_by(node_info):
(key, node) = node_info
return (-node.uncovered, key)

traverse_tree_in_order(tree, [], print_node, sort_by)
print('</table><hr/>', file=html)
print(FOOTER % footer, file=html)
Expand Down Expand Up @@ -560,7 +567,7 @@ def make_coverage_reports(path, report_path, opts):
if opts.verbose:
print(tree)
rev = get_svn_revision(os.path.join(path, os.path.pardir))
timestamp = str(datetime.datetime.utcnow())+"Z"
timestamp = str(datetime.datetime.utcnow()) + "Z"
footer = "Generated for revision %s on %s" % (rev, timestamp)
create_report_path(report_path)
generate_htmls_from_tree(tree, path, report_path, footer)
Expand Down Expand Up @@ -589,7 +596,7 @@ def main(args=None):

parser = optparse.OptionParser(
"usage: %prog [options] [inputpath [outputdir]]",
description=
description =
'Converts coverage reports to HTML. If the input path is'
' omitted, it defaults to coverage or .coverage, whichever'
' exists. If the output directory is omitted, it defaults to'
Expand All @@ -601,9 +608,12 @@ def main(args=None):
parser.add_option('-v', '--verbose', help='be verbose (default)',
action='store_const', const=1, dest='verbose', default=1)
parser.add_option('--strip-prefix', metavar='PREFIX',
help='strip base directory from filenames loaded from .coverage')
help=('strip base directory from filenames loaded '
'from .coverage'),
)
parser.add_option('--path-alias', metavar='PATH=LOCALPATH',
help='define path mappings for filenames loaded from .coverage',
help=('define path mappings for filenames loaded '
'from .coverage'),
action='append')

if args is None:
Expand Down
11 changes: 8 additions & 3 deletions src/z3c/coverage/tests.py
Expand Up @@ -217,7 +217,8 @@ def doctest_get_svn_revision():
def doctest_syntax_highlight_with_enscript():
"""Test for syntax_highlight
This function takes a cover file, converts it to a nicely colored HTML output:
This function takes a cover file, converts it to a nicely colored
HTML output:
>>> filename = os.path.join(
... os.path.dirname(z3c.coverage.__file__), '__init__.py')
Expand Down Expand Up @@ -264,6 +265,7 @@ def doctest_syntax_highlight_without_enscript():
"""


def doctest_main_default_arguments():
"""Test for main()
Expand Down Expand Up @@ -300,9 +302,11 @@ def doctest_main_default_arguments():
"""


def setUp(test):
test.globs['print_function'] = print_function


def test_suite():
checker = renormalizing.RENormalizing([
# optparse in Python 2.4 prints "usage:" and "options:",
Expand All @@ -315,11 +319,11 @@ def test_suite():
return unittest.TestSuite([
doctest.DocFileSuite(
'README.txt', setUp=setUp, checker=checker,
optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS,

This comment has been minimized.

Copy link
@mgedmin

mgedmin Sep 27, 2013

Member

This is against PEP-8: keyword arguments should not have spaces around the =.

This comment has been minimized.

Copy link
@ulif

ulif Sep 27, 2013

Contributor

You're absolutely right. Sorry. I wonder why the pep8 utility didn't complain.

),
doctest.DocFileSuite(
'coveragediff.txt', setUp=setUp, checker=checker,
optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS,

This comment has been minimized.

Copy link
@mgedmin

mgedmin Sep 27, 2013

Member

Same here.

),
doctest.DocTestSuite(checker=checker),
doctest.DocTestSuite(
Expand All @@ -328,5 +332,6 @@ def test_suite():
'z3c.coverage.coveragereport'),
])


if __name__ == '__main__':
unittest.main(defaultTest='test_suite')

0 comments on commit f92b404

Please sign in to comment.