eric7/DebugClients/Python/coverage/annotate.py

branch
eric7
changeset 8312
800c432b34c8
parent 7427
362cd1b6f81a
child 8775
0802ae193343
equal deleted inserted replaced
8311:4e8b98454baa 8312:800c432b34c8
1 # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
2 # For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
3
4 """Source file annotation for coverage.py."""
5
6 import io
7 import os
8 import re
9
10 from coverage.files import flat_rootname
11 from coverage.misc import ensure_dir, isolate_module
12 from coverage.report import get_analysis_to_report
13
14 os = isolate_module(os)
15
16
17 class AnnotateReporter(object):
18 """Generate annotated source files showing line coverage.
19
20 This reporter creates annotated copies of the measured source files. Each
21 .py file is copied as a .py,cover file, with a left-hand margin annotating
22 each line::
23
24 > def h(x):
25 - if 0: #pragma: no cover
26 - pass
27 > if x == 1:
28 ! a = 1
29 > else:
30 > a = 2
31
32 > h(2)
33
34 Executed lines use '>', lines not executed use '!', lines excluded from
35 consideration use '-'.
36
37 """
38
39 def __init__(self, coverage):
40 self.coverage = coverage
41 self.config = self.coverage.config
42 self.directory = None
43
44 blank_re = re.compile(r"\s*(#|$)")
45 else_re = re.compile(r"\s*else\s*:\s*(#|$)")
46
47 def report(self, morfs, directory=None):
48 """Run the report.
49
50 See `coverage.report()` for arguments.
51
52 """
53 self.directory = directory
54 self.coverage.get_data()
55 for fr, analysis in get_analysis_to_report(self.coverage, morfs):
56 self.annotate_file(fr, analysis)
57
58 def annotate_file(self, fr, analysis):
59 """Annotate a single file.
60
61 `fr` is the FileReporter for the file to annotate.
62
63 """
64 statements = sorted(analysis.statements)
65 missing = sorted(analysis.missing)
66 excluded = sorted(analysis.excluded)
67
68 if self.directory:
69 ensure_dir(self.directory)
70 dest_file = os.path.join(self.directory, flat_rootname(fr.relative_filename()))
71 if dest_file.endswith("_py"):
72 dest_file = dest_file[:-3] + ".py"
73 dest_file += ",cover"
74 else:
75 dest_file = fr.filename + ",cover"
76
77 with io.open(dest_file, 'w', encoding='utf8') as dest:
78 i = 0
79 j = 0
80 covered = True
81 source = fr.source()
82 for lineno, line in enumerate(source.splitlines(True), start=1):
83 while i < len(statements) and statements[i] < lineno:
84 i += 1
85 while j < len(missing) and missing[j] < lineno:
86 j += 1
87 if i < len(statements) and statements[i] == lineno:
88 covered = j >= len(missing) or missing[j] > lineno
89 if self.blank_re.match(line):
90 dest.write(u' ')
91 elif self.else_re.match(line):
92 # Special logic for lines containing only 'else:'.
93 if i >= len(statements) and j >= len(missing):
94 dest.write(u'! ')
95 elif i >= len(statements) or j >= len(missing):
96 dest.write(u'> ')
97 elif statements[i] == missing[j]:
98 dest.write(u'! ')
99 else:
100 dest.write(u'> ')
101 elif lineno in excluded:
102 dest.write(u'- ')
103 elif covered:
104 dest.write(u'> ')
105 else:
106 dest.write(u'! ')
107
108 dest.write(line)

eric ide

mercurial