1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
|
# ==================================================================================================================== #
# _ _ _ #
# ___ _ __ | |__ (_)_ __ __ __ _ __ ___ _ __ ___ _ __| |_ ___ #
# / __| '_ \| '_ \| | '_ \\ \/ /____| '__/ _ \ '_ \ / _ \| '__| __/ __| #
# \__ \ |_) | | | | | | | |> <_____| | | __/ |_) | (_) | | | |_\__ \ #
# |___/ .__/|_| |_|_|_| |_/_/\_\ |_| \___| .__/ \___/|_| \__|___/ #
# |_| |_| #
# ==================================================================================================================== #
# Authors: #
# Patrick Lehmann #
# #
# License: #
# ==================================================================================================================== #
# Copyright 2023-2024 Patrick Lehmann - Bötzingen, Germany #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# #
# SPDX-License-Identifier: Apache-2.0 #
# ==================================================================================================================== #
#
"""
**Report code coverage as Sphinx documentation page(s).**
"""
from pathlib import Path
from typing import Dict, Tuple, Any, List, Iterable, Mapping, Generator, TypedDict, Union, Optional as Nullable, \
ClassVar
from docutils import nodes
from docutils.parsers.rst.directives import flag
from sphinx.addnodes import toctree
from sphinx.application import Sphinx
from sphinx.config import Config
from sphinx.directives.code import LiteralIncludeReader
from sphinx.util.docutils import new_document
from pyTooling.Decorators import export
from sphinx_reports.Common import ReportExtensionError, LegendStyle
from sphinx_reports.Sphinx import strip, BaseDirective
from sphinx_reports.DataModel.CodeCoverage import PackageCoverage, AggregatedCoverage, ModuleCoverage
from sphinx_reports.Adapter.Coverage import Analyzer
class package_DictType(TypedDict):
name: str
json_report: Path
fail_below: int
levels: Union[str, Dict[Union[int, str], Dict[str, str]]]
@export
class CodeCoverageBase(BaseDirective):
option_spec = {
"packageid": strip
}
defaultCoverageDefinitions = {
"default": {
30: {"class": "report-cov-below30", "desc": "almost unused"},
50: {"class": "report-cov-below50", "desc": "poorly used"},
80: {"class": "report-cov-below80", "desc": "somehow used"},
90: {"class": "report-cov-below90", "desc": "well used"},
100: {"class": "report-cov-below100", "desc": "excellently used"},
"error": {"class": "report-cov-error", "desc": "internal error"},
}
}
configPrefix: str = "codecov"
configValues: Dict[str, Tuple[Any, str, Any]] = {
f"{configPrefix}_packages": ({}, "env", Dict),
f"{configPrefix}_levels": (defaultCoverageDefinitions, "env", Dict),
} #: A dictionary of all configuration values used by code coverage directives.
_coverageLevelDefinitions: ClassVar[Dict[str, Dict[Union[int, str], Dict[str, str]]]] = {}
_packageConfigurations: ClassVar[Dict[str, package_DictType]] = {}
_packageID: str
_levels: Dict[Union[int, str], Dict[str, str]]
def _CheckOptions(self) -> None:
# Parse all directive options or use default values
self._packageID = self._ParseStringOption("packageid")
@classmethod
def CheckConfiguration(cls, sphinxApplication: Sphinx, sphinxConfiguration: Config) -> None:
"""
Check configuration fields and load necessary values.
:param sphinxApplication: Sphinx application instance.
:param sphinxConfiguration: Sphinx configuration instance.
"""
cls._CheckLevelsConfiguration(sphinxConfiguration)
cls._CheckPackagesConfiguration(sphinxConfiguration)
@classmethod
def ReadReports(cls, sphinxApplication: Sphinx) -> None:
"""
Read code coverage report files.
:param sphinxApplication: Sphinx application instance.
"""
print(f"[REPORT] Reading code coverage reports ...")
@classmethod
def _CheckLevelsConfiguration(cls, sphinxConfiguration: Config) -> None:
from sphinx_reports import ReportDomain
variableName = f"{ReportDomain.name}_{cls.configPrefix}_levels"
try:
coverageLevelDefinitions: Dict[str, package_DictType] = sphinxConfiguration[f"{ReportDomain.name}_{cls.configPrefix}_levels"]
except (KeyError, AttributeError) as ex:
raise ReportExtensionError(f"Configuration option '{variableName}' is not configured.") from ex
if "default" not in coverageLevelDefinitions:
cls._coverageLevelDefinitions["default"] = cls.defaultCoverageDefinitions["default"]
for key, coverageLevelDefinition in coverageLevelDefinitions.items():
configurationName = f"conf.py: {variableName}:[{key}]"
if 100 not in coverageLevelDefinition:
raise ReportExtensionError(f"{configurationName}[100]: Configuration is missing.")
elif "error" not in coverageLevelDefinition:
raise ReportExtensionError(f"{configurationName}[error]: Configuration is missing.")
cls._coverageLevelDefinitions[key] = {}
for level, levelConfig in coverageLevelDefinition.items():
try:
if isinstance(level, str):
if level != "error":
raise ReportExtensionError(f"{configurationName}[{level}]: Level is a keyword, but not 'error'.")
elif not (0.0 <= int(level) <= 100.0):
raise ReportExtensionError(f"{configurationName}[{level}]: Level is out of range 0..100.")
except ValueError as ex:
raise ReportExtensionError(f"{configurationName}[{level}]: Level is not a keyword or an integer in range 0..100.") from ex
try:
cssClass = levelConfig["class"]
except KeyError as ex:
raise ReportExtensionError(f"{configurationName}[{level}].class: CSS class is missing.") from ex
try:
description = levelConfig["desc"]
except KeyError as ex:
raise ReportExtensionError(f"{configurationName}[{level}].desc: Description is missing.") from ex
cls._coverageLevelDefinitions[key][level] = {
"class": cssClass,
"desc": description
}
@classmethod
def _CheckPackagesConfiguration(cls, sphinxConfiguration: Config) -> None:
from sphinx_reports import ReportDomain
variableName = f"{ReportDomain.name}_{cls.configPrefix}_packages"
try:
allPackages: Dict[str, package_DictType] = sphinxConfiguration[f"{ReportDomain.name}_{cls.configPrefix}_packages"]
except (KeyError, AttributeError) as ex:
raise ReportExtensionError(f"Configuration option '{variableName}' is not configured.") from ex
# try:
# packageConfiguration = allPackages[self._packageID]
# except KeyError as ex:
# raise ReportExtensionError(f"conf.py: {ReportDomain.name}_{cls.configPrefix}_packages: No configuration found for '{self._packageID}'.") from ex
for packageID, packageConfiguration in allPackages.items():
configurationName = f"conf.py: {variableName}:[{packageID}]"
try:
packageName = packageConfiguration["name"]
except KeyError as ex:
raise ReportExtensionError(f"{configurationName}.name: Configuration is missing.") from ex
try:
jsonReport = Path(packageConfiguration["json_report"])
except KeyError as ex:
raise ReportExtensionError(f"{configurationName}.json_report: Configuration is missing.") from ex
if not jsonReport.exists():
raise ReportExtensionError(f"{configurationName}.json_report: Coverage report file '{jsonReport}' doesn't exist.") from FileNotFoundError(jsonReport)
try:
failBelow = int(packageConfiguration["fail_below"]) / 100
except KeyError as ex:
raise ReportExtensionError(f"{configurationName}.fail_below: Configuration is missing.") from ex
except ValueError as ex:
raise ReportExtensionError(f"{configurationName}.fail_below: '{packageConfiguration['fail_below']}' is not an integer in range 0..100.") from ex
if not (0.0 <= failBelow <= 100.0):
raise ReportExtensionError(f"{configurationName}.fail_below: Is out of range 0..100.")
try:
levels = packageConfiguration["levels"]
except KeyError as ex:
raise ReportExtensionError(f"{configurationName}.levels: Configuration is missing.") from ex
if isinstance(levels, str):
try:
levelDefinition = cls._coverageLevelDefinitions[levels]
except KeyError as ex:
raise ReportExtensionError(
f"{configurationName}.levels: Referenced coverage levels '{levels}' are not defined in conf.py variable '{variableName}'.") from ex
elif isinstance(levels, dict):
if 100 not in packageConfiguration["levels"]:
raise ReportExtensionError(f"{configurationName}.levels[100]: Configuration is missing.")
elif "error" not in packageConfiguration["levels"]:
raise ReportExtensionError(f"{configurationName}.levels[error]: Configuration is missing.")
levelDefinition = {}
for x, y in packageConfiguration["levels"].items():
pass
else:
raise ReportExtensionError(f"")
cls._packageConfigurations[packageID] = {
"name": packageName,
"json_report": jsonReport,
"fail_below": failBelow,
"levels": levelDefinition
}
def _ConvertToColor(self, currentLevel: float, configKey: str) -> str:
if currentLevel < 0.0:
return self._levels["error"][configKey]
for levelLimit, levelConfig in self._levels.items():
if isinstance(levelLimit, int) and (currentLevel * 100) < levelLimit:
return levelConfig[configKey]
return self._levels[100][configKey]
@export
class CodeCoverage(CodeCoverageBase):
"""
This directive will be replaced by a table representing code coverage.
"""
directiveName: str = "code-coverage"
has_content = False
required_arguments = 0
optional_arguments = 2
option_spec = CodeCoverageBase.option_spec | {
"no-branch-coverage": flag
}
_noBranchCoverage: bool
_packageName: str
_jsonReport: Path
_failBelow: float
_coverage: PackageCoverage
def _CheckOptions(self) -> None:
"""
Parse all directive options or use default values.
"""
super()._CheckOptions()
self._noBranchCoverage = "no-branch-coverage" in self.options
try:
packageConfiguration = self._packageConfigurations[self._packageID]
except KeyError as ex:
raise ReportExtensionError(f"No configuration for '{self._packageID}'") from ex
self._packageName = packageConfiguration["name"]
self._jsonReport = packageConfiguration["json_report"]
self._failBelow = packageConfiguration["fail_below"]
self._levels = packageConfiguration["levels"]
def _GenerateCoverageTable(self) -> nodes.table:
# Create a table and table header with 10 columns
columns = [
("Package", [(" Module", 500)], None),
("Statments", [("Total", 100), ("Excluded", 100), ("Covered", 100), ("Missing", 100)], None),
("Branches" , [("Total", 100), ("Covered", 100), ("Partial", 100), ("Missing", 100)], None),
("Coverage", [("in %", 100)], None)
]
if self._noBranchCoverage:
columns.pop(2)
table, tableGroup = self._CreateTableHeader(
identifier=self._packageID,
columns=columns,
classes=["report-codecov-table"]
)
tableBody = nodes.tbody()
tableGroup += tableBody
def sortedValues(d: Mapping[str, AggregatedCoverage]) -> Generator[AggregatedCoverage, None, None]:
for key in sorted(d.keys()):
yield d[key]
def renderlevel(tableBody: nodes.tbody, packageCoverage: PackageCoverage, level: int = 0) -> None:
tableRow = nodes.row("", classes=[
"report-codecov-table-row",
"report-codecov-package",
self._ConvertToColor(packageCoverage.Coverage, "class")
])
tableBody += tableRow
tableRow += nodes.entry("", nodes.paragraph(text=f"{' ' * level}📦{packageCoverage.Name}"))
tableRow += nodes.entry("", nodes.paragraph(text=f"{packageCoverage.TotalStatements}"))
tableRow += nodes.entry("", nodes.paragraph(text=f"{packageCoverage.ExcludedStatements}"))
tableRow += nodes.entry("", nodes.paragraph(text=f"{packageCoverage.CoveredStatements}"))
tableRow += nodes.entry("", nodes.paragraph(text=f"{packageCoverage.MissingStatements}"))
if not self._noBranchCoverage:
tableRow += nodes.entry("", nodes.paragraph(text=f"{packageCoverage.TotalBranches}"))
tableRow += nodes.entry("", nodes.paragraph(text=f"{packageCoverage.CoveredBranches}"))
tableRow += nodes.entry("", nodes.paragraph(text=f"{packageCoverage.PartialBranches}"))
tableRow += nodes.entry("", nodes.paragraph(text=f"{packageCoverage.MissingBranches}"))
tableRow += nodes.entry("", nodes.paragraph(text=f"{packageCoverage.Coverage:.1%}"))
for package in sortedValues(packageCoverage._packages):
renderlevel(tableBody, package, level + 1)
for module in sortedValues(packageCoverage._modules):
tableRow = nodes.row("", classes=[
"report-codecov-table-row",
"report-codecov-module",
self._ConvertToColor(module.Coverage, "class")
])
tableBody += tableRow
tableRow += nodes.entry("", nodes.paragraph(text=f"{' ' * (level + 1)} {module.Name}"))
tableRow += nodes.entry("", nodes.paragraph(text=f"{module.TotalStatements}"))
tableRow += nodes.entry("", nodes.paragraph(text=f"{module.ExcludedStatements}"))
tableRow += nodes.entry("", nodes.paragraph(text=f"{module.CoveredStatements}"))
tableRow += nodes.entry("", nodes.paragraph(text=f"{module.MissingStatements}"))
if not self._noBranchCoverage:
tableRow += nodes.entry("", nodes.paragraph(text=f"{module.TotalBranches}"))
tableRow += nodes.entry("", nodes.paragraph(text=f"{module.CoveredBranches}"))
tableRow += nodes.entry("", nodes.paragraph(text=f"{module.PartialBranches}"))
tableRow += nodes.entry("", nodes.paragraph(text=f"{module.MissingBranches}"))
tableRow += nodes.entry("", nodes.paragraph(text=f"{module.Coverage :.1%}"))
renderlevel(tableBody, self._coverage)
# Add a summary row
tableRow = nodes.row("", classes=[
"report-codecov-table-row",
"report-codecov-summary",
self._ConvertToColor(self._coverage.Coverage, "class")
])
tableBody += tableRow
tableRow += nodes.entry("", nodes.paragraph(text=f"Overall ({self._coverage.FileCount} files):"))
tableRow += nodes.entry("", nodes.paragraph(text=f"")) # {self._coverage.AggregatedExpected}")),
tableRow += nodes.entry("", nodes.paragraph(text=f"")) # {self._coverage.AggregatedCovered}")),
tableRow += nodes.entry("", nodes.paragraph(text=f"")) # {self._coverage.AggregatedCovered}")),
tableRow += nodes.entry("", nodes.paragraph(text=f"")) # {self._coverage.AggregatedCovered}")),
if not self._noBranchCoverage:
tableRow += nodes.entry("", nodes.paragraph(text=f"")) # {self._coverage.AggregatedCovered}")),
tableRow += nodes.entry("", nodes.paragraph(text=f"")) # {self._coverage.AggregatedCovered}")),
tableRow += nodes.entry("", nodes.paragraph(text=f"")) # {self._coverage.AggregatedCovered}")),
tableRow += nodes.entry("", nodes.paragraph(text=f"")) # {self._coverage.AggregatedUncovered}")),
tableRow += nodes.entry("", nodes.paragraph(text=f"")) # {self._coverage.AggregatedCoverage:.1%}")),
return table
def _CreatePages(self) -> None:
def handlePackage(package: PackageCoverage) -> None:
for pack in package._packages.values():
if handlePackage(pack):
return
for module in package._modules.values():
if handleModule(module):
return
def handleModule(module: ModuleCoverage) -> None:
doc = new_document("dummy")
rootSection = nodes.section(ids=["foo"])
doc += rootSection
title = nodes.title(text=f"{module.Name}")
rootSection += title
rootSection += nodes.paragraph(text="some text")
docname = f"coverage/{module.Name}"
self.env.titles[docname] = title
self.env.longtitles[docname] = title
return
handlePackage(self._coverage)
def run(self) -> List[nodes.Node]:
container = nodes.container()
try:
self._CheckOptions()
except ReportExtensionError as ex:
message = f"Caught {ex.__class__.__name__} when checking options for directive '{self.directiveName}'."
return self._internalError(container, __name__, message, ex)
# Assemble a list of Python source files
analyzer = Analyzer(self._packageName, self._jsonReport)
self._coverage = analyzer.Convert()
# self._coverage.Aggregate()
self._CreatePages()
container += self._GenerateCoverageTable()
docName = self.env.docname
docParent = docName[:docName.rindex("/")]
subnode = toctree()
subnode['parent'] = docName
# (title, ref) pairs, where ref may be a document, or an external link,
# and title may be None if the document's title is to be used
subnode['entries'] = []
subnode['includefiles'] = []
subnode['maxdepth'] = -1 # self.options.get('maxdepth', -1)
subnode['caption'] = None # self.options.get('caption')
subnode['glob'] = None # 'glob' in self.options
subnode['hidden'] = True # 'hidden' in self.options
subnode['includehidden'] = False # 'includehidden' in self.options
subnode['numbered'] = 0 # self.options.get('numbered', 0)
subnode['titlesonly'] = False # 'titlesonly' in self.options
self.set_source_info(subnode)
wrappernode = nodes.compound(classes=['toctree-wrapper'])
wrappernode.append(subnode)
self.add_name(wrappernode)
for entry in (
"sphinx_reports",
"sphinx_reports.Adapter",
"sphinx_reports.Adapter.Coverage",
"sphinx_reports.Adapter.DocStrCoverage",
"sphinx_reports.Adapter.JUnit",
"sphinx_reports.DataModel",
"sphinx_reports.DataModel.CodeCoverage",
"sphinx_reports.DataModel.DocumentationCoverage",
"sphinx_reports.DataModel.Unittest",
"sphinx_reports.static",
"sphinx_reports.CodeCoverage",
"sphinx_reports.Common",
"sphinx_reports.DocCoverage",
"sphinx_reports.Sphinx",
"sphinx_reports.Unittest",
):
moduleDocumentName = f"{docParent}/{entry}"
moduleDocumentTitle = entry
subnode["entries"].append((moduleDocumentTitle, moduleDocumentName))
subnode["includefiles"].append(moduleDocumentName)
return [container, wrappernode]
@export
class CodeCoverageLegend(CodeCoverageBase):
"""
This directive will be replaced by a legend table representing coverage levels.
"""
has_content = False
required_arguments = 0
optional_arguments = 2
option_spec = CodeCoverageBase.option_spec | {
"style": strip
}
directiveName: str = "code-coverage-legend"
_style: LegendStyle
def _CheckOptions(self) -> None:
# Parse all directive options or use default values
super()._CheckOptions()
self._style = self._ParseLegendStyle("style", LegendStyle.horizontal_table)
try:
packageConfiguration = self._packageConfigurations[self._packageID]
except KeyError as ex:
raise ReportExtensionError(f"No configuration for '{self._packageID}'") from ex
self._levels = packageConfiguration["levels"]
def _CreateHorizontalLegendTable(self, identifier: str, classes: List[str]) -> nodes.table:
columns = [("Code Coverage:", None, 300)]
for level in self._levels:
if isinstance(level, int):
columns.append((f"≤{level} %", None, 200))
table, tableGroup = self._CreateTableHeader(columns, identifier=identifier, classes=classes)
tableBody = nodes.tbody()
tableGroup += tableBody
legendRow = nodes.row("", classes=["report-codecov-legend-row"])
legendRow += nodes.entry("", nodes.paragraph(text="Coverage Level:"))
tableBody += legendRow
for level, config in self._levels.items():
if isinstance(level, int):
legendRow += nodes.entry("", nodes.paragraph(text=config["desc"]), classes=[self._ConvertToColor((level - 1) / 100, "class")])
legendRow = nodes.row("", classes=["report-codecov-legend-row"])
legendRow += nodes.entry("", nodes.paragraph(text="Coverage Level:"))
tableBody += legendRow
for level, config in self._levels.items():
if isinstance(level, int):
legendRow += nodes.entry("", nodes.paragraph(text=config["desc"]), classes=[self._ConvertToColor((level - 1) / 100, "class")])
return table
def _CreateVerticalLegendTable(self, identifier: str, classes: List[str]) -> nodes.table:
table, tableGroup = self._CreateTableHeader([
("Code Coverage", None, 300),
("Coverage Level", None, 300)
],
identifier=identifier,
classes=classes
)
tableBody = nodes.tbody()
tableGroup += tableBody
for level, config in self._levels.items():
if isinstance(level, int):
tableBody += nodes.row(
"",
nodes.entry("", nodes.paragraph(text=f"≤{level} %")),
nodes.entry("", nodes.paragraph(text=config["desc"])),
classes=["report-codecov-legend-row", self._ConvertToColor((level - 1) / 100, "class")]
)
return table
def run(self) -> List[nodes.Node]:
container = nodes.container()
try:
self._CheckOptions()
except ReportExtensionError as ex:
message = f"Caught {ex.__class__.__name__} when checking options for directive '{self.directiveName}'."
return self._internalError(container, __name__, message, ex)
if LegendStyle.Table in self._style:
if LegendStyle.Horizontal in self._style:
container += self._CreateHorizontalLegendTable(identifier=f"{self._packageID}-legend", classes=["report-codecov-legend"])
elif LegendStyle.Vertical in self._style:
container += self._CreateVerticalLegendTable(identifier=f"{self._packageID}-legend", classes=["report-codecov-legend"])
else:
container += nodes.paragraph(text=f"Unsupported legend style.")
else:
container += nodes.paragraph(text=f"Unsupported legend style.")
return [container]
@export
class ModuleCoverage(CodeCoverageBase):
"""
This directive will be replaced by highlighted source code.
"""
directiveName: str = "module-coverage"
has_content = False
required_arguments = 0
optional_arguments = 2
option_spec = CodeCoverageBase.option_spec | {
"module": strip
}
_packageName: str
_moduleName: str
_jsonReport: Path
def _CheckOptions(self) -> None:
"""
Parse all directive options or use default values.
"""
super()._CheckOptions()
self._moduleName = self._ParseStringOption("module")
try:
packageConfiguration = self._packageConfigurations[self._packageID]
except KeyError as ex:
raise ReportExtensionError(f"No configuration for '{self._packageID}'") from ex
self._packageName = packageConfiguration["name"]
self._jsonReport = packageConfiguration["json_report"]
def run(self) -> List[nodes.Node]:
container = nodes.container()
try:
self._CheckOptions()
except ReportExtensionError as ex:
message = f"Caught {ex.__class__.__name__} when checking options for directive '{self.directiveName}'."
return self._internalError(container, __name__, message, ex)
# Assemble a list of Python source files
analyzer = Analyzer(self._packageName, self._jsonReport)
self._coverage = analyzer.Convert()
sourceFile = "../../sphinx_reports/__init__.py"
container += nodes.paragraph(text=f"Code coverage of {self._moduleName}")
# lexer = get_lexer_by_name("python", tabsize=2)
# tokens = lex(code, lexer)
# htmlFormatter = HtmlFormatter(linenos=True, cssclass="source")
# highlight()
location = self.state_machine.get_source_and_line(self.lineno)
rel_filename, filename = self.env.relfn2path(sourceFile)
self.env.note_dependency(rel_filename)
reader = LiteralIncludeReader(filename, {"tab-width": 2}, self.config)
text, lines = reader.read(location=location)
literalBlock: nodes.Element = nodes.literal_block(text, text, source=filename)
literalBlock["language"] = "codecov"
literalBlock['highlight_args'] = extra_args = {}
extra_args['hl_lines'] = [i for i in range(10, 20)]
self.set_source_info(literalBlock)
container += literalBlock
return [container]
|