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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
|
# ==================================================================================================================== #
# _____ _ _ ____ _ _ #
# _ __ _ |_ _|__ ___ | (_)_ __ __ _ / ___| _ __ | |__ (_)_ __ __ __ #
# | '_ \| | | || |/ _ \ / _ \| | | '_ \ / _` | \___ \| '_ \| '_ \| | '_ \\ \/ / #
# | |_) | |_| || | (_) | (_) | | | | | | (_| |_ ___) | |_) | | | | | | | |> < #
# | .__/ \__, ||_|\___/ \___/|_|_|_| |_|\__, (_)____/| .__/|_| |_|_|_| |_/_/\_\ #
# |_| |___/ |___/ |_| #
# ==================================================================================================================== #
# Authors: #
# Patrick Lehmann #
# #
# License: #
# ==================================================================================================================== #
# Copyright 2023-2026 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 #
# ==================================================================================================================== #
#
"""
Directives rendering the documentation coverage of a Python package as a table: per package and module; and a page per
package and module.
Unlike the other reports, there is no report file: the package's source directory is analyzed by the Python reader
of :mod:`pyEDAA.Reports` (:class:`~pyEDAA.Reports.DocumentationCoverage.Python.Analyzer`) once per build, when the
builder is initialized. The packages are declared in :file:`conf.py` under ``pyTooling_DocCoverage_Packages``,
each with an identifier a directive names in its ``:reportid:`` option:
.. code-block:: Python
# doc/conf.py
pyTooling_DocCoverage_Packages = {
"src": {
"name": "myPackage",
"directory": "../myPackage",
"pages": "doccoverage/src",
"fail_below": 80,
"levels": "default"
}
}
The coverage levels - limits, descriptions and CSS classes - are declared under ``pyTooling_DocCoverage_Levels``;
:attr:`DocCoverageBase.defaultCoverageDefinitions` holds the ``default`` palette.
A package declared with the key ``pages`` gets a document per package and per module (:class:`DocCoverageReportPages`),
named below that key's document name by its dotted name, e.g. ``<pages>/myPackage/Units/Length``:
* A package's page shows its counters, tables of its sub-packages and modules, each linked to its page, and the
condensed listing of its ``__init__.py``.
* A module's page shows its counters and its condensed listing (see
:class:`~pyTooling.Sphinx.CondensedSource.CondensedSource`): the source condensed to what is documented, syntax
highlighted with the Pygments lexer for Python, each line marked by its documentation state.
The states are those of the items of pyEDAA.Reports' model - the analysis the counters are computed from:
``documented``, ``undocumented``, ``ignored`` or ``excluded``, for a class, a function, a field and a variable.
The pages are built by the machinery of :class:`~pyTooling.Sphinx.Pages.ReportPages`, so they are in the navigation
below the directive ``report:doc-coverage``. Every package and module with a page is an object of domain ``report``,
referred to by the role ``:doccov:`` by its dotted name - e.g. ``:doccov:`myPackage.Units.Length``` - or a suffix of
it.
.. seealso::
:ref:`DIR/DocCoverage`
|rarr| The directives' options and configuration, with examples.
:ref:`DIR/DocCoverage/Pages`
|rarr| The pages per package and module, and the role ``:doccov:``.
:mod:`pyTooling.Sphinx.Pages`
|rarr| The pages of a report: document names, registration, generation and navigation.
:mod:`pyTooling.Sphinx.CondensedSource`
|rarr| The condensed listing of a module's source.
"""
from __future__ import annotations
from pathlib import Path
from tokenize import open as tokenize_open
from typing import TYPE_CHECKING, Any, ClassVar, Iterable, \
Optional as Nullable, TypedDict, Union
from docutils import nodes
from sphinx.application import Sphinx
from sphinx.config import Config
from sphinx.util.logging import getLogger
from pyTooling.Common import getFullyQualifiedName, getTypeName
from pyTooling.Decorators import export
from pyTooling.Warning import WarningCollector
from pyTooling.Sphinx import BaseDirective, LegendStyle, ReportExtensionError, \
ReportsPackageMissingError, SphinxExtensionError, strip, \
stripAndNormalize
from pyTooling.Sphinx.CodeCoveragePages import tokenizeSource
from pyTooling.Sphinx.CondensedSource import CondensedSource, documentationState
from pyTooling.Sphinx.Node import DOCUMENTATION_STATES, DocCoverageListing, ReportIndentation
from pyTooling.Sphinx.Pages import ReportPages
if TYPE_CHECKING: # pragma: no cover
# pyEDAA.Reports is an optional dependency (extra 'reports'), imported where the coverage is computed.
from pyEDAA.Reports.DocumentationCoverage import CoverageCounters, Directory, DocumentationReport, Item
__all__ = ["CONFIG_PREFIX"]
#: Prefix every configuration value of the documentation coverage directives carries in :file:`conf.py`.
CONFIG_PREFIX = "pyTooling_DocCoverage"
@export
def subPackagesAndModules(package: Item) -> tuple[list[Item], list[Item]]:
"""
Return the sub-packages and the modules of a package, each sorted by name.
:param package: The package's item.
:returns: The sub-packages' and the modules' items.
"""
from pyEDAA.Reports.DocumentationCoverage import ItemKind
packages = sorted((item for item in package.Items if item.Kind is ItemKind.Package), key=lambda item: item.Name)
modules = sorted((item for item in package.Items if item.Kind is ItemKind.Module), key=lambda item: item.Name)
return packages, modules
@export
def fileCounters(unit: Item) -> CoverageCounters:
"""
Return the counters of the items declared in a package's ``__init__.py`` or in a module's file.
:param unit: The package's or module's item.
:returns: The file's counters; zero items for a namespace package, which has no ``__init__.py``.
"""
from pyEDAA.Reports.DocumentationCoverage import CoverageCounters
return CoverageCounters() if unit.File is None else unit.File.Counters
@export
def isUnreadable(unit: Item) -> bool:
"""
Check if a package's ``__init__.py`` or a module's file couldn't be read or parsed.
The Python reader excludes such a file's item, without a documentation state.
:param unit: The package's or module's item.
:returns: ``True``, if the file couldn't be read or parsed.
"""
from pyEDAA.Reports.DocumentationCoverage import CoverageState
return unit.File is not None and unit.Status == CoverageState.Excluded
@export
def coverageCell(
unit: Nullable[Item],
coverage: float,
levels: dict[Union[int, str], dict[str, str]]
) -> tuple[str, str]:
"""
Return the text of a row's coverage cell and the CSS class of the row's coverage level.
A package's or module's file, which couldn't be read or parsed, has the text ``can't be read`` and the level
``error``.
:param unit: The package's or module's item, whose row it is; ``None`` for a summary row.
:param coverage: The row's coverage, in range 0.0..1.0.
:param levels: The coverage levels.
:returns: The cell's text and the level's CSS class.
"""
if unit is not None and isUnreadable(unit):
return "can't be read", DocCoverageBase.LevelOf(levels, -1.0, "class")
return f"{coverage:.1%}", DocCoverageBase.LevelOf(levels, coverage, "class")
@export
class PackageConfiguration(TypedDict):
"""An entry of ``pyTooling_DocCoverage_Packages``, after :meth:`DocCoverageBase.CheckConfiguration` read it."""
name: str #: Name of the Python package.
directory: Path #: The package's source directory.
pages: Nullable[str] #: Document name the pages are generated below.
failBelow: float #: Coverage below which the package fails.
levels: Union[str, dict[Union[int, str], dict[str, str]]] #: Coverage levels, or the name of a palette.
@export
class DocCoverageBase(BaseDirective):
"""
Base-class of the documentation coverage directives: the shared options, the configuration and the coverage levels.
The configuration is checked and the packages are analyzed once per build - by :meth:`CheckConfiguration` and
:meth:`AnalyzePackages` -, into class variables every directive reads.
"""
option_spec: dict[str, Any] = { # type: ignore[misc]
"class": strip,
"reportid": stripAndNormalize,
} #: Mapping of option names to validator functions.
defaultCoverageDefinitions: ClassVar[dict[str, dict[Union[int, str], dict[str, str]]]] = {
"default": {
10: {"class": "report-cov-below10", "desc": "almost undocumented"},
20: {"class": "report-cov-below20", "desc": "almost undocumented"},
30: {"class": "report-cov-below30", "desc": "almost undocumented"},
40: {"class": "report-cov-below40", "desc": "poorly documented"},
50: {"class": "report-cov-below50", "desc": "poorly documented"},
60: {"class": "report-cov-below60", "desc": "roughly documented"},
70: {"class": "report-cov-below70", "desc": "roughly documented"},
80: {"class": "report-cov-below80", "desc": "roughly documented"},
85: {"class": "report-cov-below85", "desc": "well documented"},
90: {"class": "report-cov-below90", "desc": "well documented"},
95: {"class": "report-cov-below95", "desc": "well documented"},
100: {"class": "report-cov-below100", "desc": "excellent documented"},
"error": {"class": "report-cov-error", "desc": "internal error"},
}
} #: Predefined palettes of coverage levels, by name: a limit in percent or ``error``, to a CSS class and text.
configValues: ClassVar[dict[str, tuple[Any, str, Any]]] = {
"Packages": ({}, "env", dict),
"Levels": (defaultCoverageDefinitions, "env", dict),
} #: Values added to :file:`conf.py`, as ``name: (default, rebuild, types)``, prefixed by :data:`CONFIG_PREFIX`.
_coverageLevelDefinitions: ClassVar[dict[str, dict[Union[int, str], dict[str, str]]]] = {} #: Palettes, by name.
_packageConfigurations: ClassVar[dict[str, PackageConfiguration]] = {} #: Package configurations, by report ID.
_reports: ClassVar[dict[str, DocumentationReport]] = {} #: Packages analyzed, by report ID.
_analysisErrors: ClassVar[dict[str, Exception]] = {} #: Why a package wasn't analyzed, by ID.
_cssClasses: list[str] #: Additional CSS classes of the table.
_reportID: str #: Identifier of the package in the configuration.
_levels: dict[Union[int, str], dict[str, str]] #: The coverage levels of the package.
def _CheckOptions(self) -> None:
"""
Parse all directive options or use default values.
:raises SphinxExtensionError: If option ``:class:`` isn't a list of CSS class names.
:raises SphinxExtensionError: If option ``:reportid:`` is missing.
"""
cssClasses = self._ParseStringOption("class", "", r"(\w+)?( +\w+)*")
self._reportID = self._ParseStringOption("reportid")
self._cssClasses = [] if cssClasses == "" else cssClasses.split(" ")
@classmethod
def CheckConfiguration(cls, sphinxApplication: Sphinx, sphinxConfiguration: Config) -> None:
"""
Check the configuration values and load the coverage levels and package configurations.
:param sphinxApplication: Sphinx application instance.
:param sphinxConfiguration: Sphinx configuration instance.
"""
cls._CheckLevelsConfiguration(sphinxConfiguration)
cls._CheckPackagesConfiguration(sphinxConfiguration)
@classmethod
def AnalyzePackages(cls, sphinxApplication: Sphinx) -> None:
"""
Analyze every configured package, once per build.
A package that can't be analyzed keeps the exception, which a directive or a package's pages show when they need
it: a :exc:`ReportExtensionError`, e.g. pyEDAA.Reports isn't installed, or the package's directory doesn't exist.
Any other exception is a bug, and ends the build.
:param sphinxApplication: Sphinx application instance.
"""
getLogger(__name__).info("[REPORT] Analyzing documentation coverage ...")
cls._reports = {}
cls._analysisErrors = {}
for reportID, packageConfiguration in cls._packageConfigurations.items():
try:
cls._reports[reportID] = cls._AnalyzePackage(packageConfiguration)
except ReportExtensionError as ex:
cls._analysisErrors[reportID] = ex
@classmethod
def GetReport(cls, reportID: str) -> DocumentationReport:
"""
Return a package analyzed by :meth:`AnalyzePackages`.
:param reportID: Identifier of the package.
:returns: The package's report, aggregated; its top-level item is the package.
:raises ReportExtensionError: If the package wasn't analyzed, chained to the exception analyzing it raised.
"""
try:
return cls._reports[reportID]
except KeyError:
cause = cls._analysisErrors.get(reportID, None)
raise ReportExtensionError(f"Documentation coverage of '{reportID}' wasn't analyzed.") from cause
@staticmethod
def _AnalyzePackage(packageConfiguration: PackageConfiguration) -> DocumentationReport:
"""
Analyze a package's source directory with pyEDAA.Reports' Python reader.
A source file, which can't be read or parsed, is logged as a warning, and its module is excluded from the
analysis.
:param packageConfiguration: The package's configuration.
:returns: The package's report, aggregated; its top-level item is the package.
:raises ReportsPackageMissingError: If pyEDAA.Reports isn't installed.
:raises ReportExtensionError: If pyEDAA.Reports' analysis fails, e.g. the directory doesn't exist.
"""
try:
from pyEDAA.Reports.DocumentationCoverage import DocCoverageError
from pyEDAA.Reports.DocumentationCoverage.Python import Analyzer
except ImportError as cause:
raise ReportsPackageMissingError("Analyzing the documentation coverage", "'pyEDAA.Reports'") from cause
packageName = packageConfiguration["name"]
try:
with WarningCollector() as warnings:
report = Analyzer(packageName, packageConfiguration["directory"]).Analyze()
except DocCoverageError as ex:
raise ReportExtensionError(f"Package '{packageName}' can't be analyzed.") from ex
for warning in warnings:
message = f"Caught {getTypeName(warning)} when analyzing package '{packageName}'.\n {warning}"
if (cause := warning.__cause__) is not None:
message += f"\n {getTypeName(cause)}: {cause}"
getLogger(__name__).warning(message)
getLogger(__name__).verbose(
f"[REPORT] Package '{packageName}': {report.FileCount} files analyzed in "
f"{report.AnalysisDuration.total_seconds():.3f} s."
)
return report
@classmethod
def _CheckLevelsConfiguration(cls, sphinxConfiguration: Config) -> None:
"""
Check and load the palettes of coverage levels from ``pyTooling_DocCoverage_Levels``.
:param sphinxConfiguration: Sphinx configuration instance.
:raises ReportExtensionError: If the configuration value isn't registered.
:raises ReportExtensionError: If a palette has no level ``100`` or no level ``error``.
:raises ReportExtensionError: If a level is a keyword other than ``error``.
:raises ReportExtensionError: If a level is out of range 0..100.
:raises ReportExtensionError: If a level is neither a keyword nor an integer.
:raises ReportExtensionError: If a level has no CSS class or no description.
"""
variableName = f"{CONFIG_PREFIX}_Levels"
try:
coverageLevelDefinitions: dict[str, dict[Union[int, str], dict[str, str]]] = sphinxConfiguration[variableName]
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}]"
cls._coverageLevelDefinitions[key] = cls._CheckLevels(configurationName, coverageLevelDefinition)
@staticmethod
def _CheckLevels(
configurationName: str,
levels: dict[Union[int, str], dict[str, str]]
) -> dict[Union[int, str], dict[str, str]]:
"""
Check coverage levels and return the CSS class and description of each level.
:param configurationName: The levels' name in error messages.
:param levels: The coverage levels: a limit in percent or ``error``, to a CSS class and text.
:returns: The checked coverage levels.
:raises ReportExtensionError: If there is no level ``100`` or no level ``error``.
:raises ReportExtensionError: If a level is a keyword other than ``error``.
:raises ReportExtensionError: If a level is out of range 0..100.
:raises ReportExtensionError: If a level is neither a keyword nor an integer.
:raises ReportExtensionError: If a level has no CSS class or no description.
"""
if 100 not in levels:
raise ReportExtensionError(f"{configurationName}[100]: Configuration is missing.")
elif "error" not in levels:
raise ReportExtensionError(f"{configurationName}[error]: Configuration is missing.")
levelDefinition: dict[Union[int, str], dict[str, str]] = {}
for level, levelConfig in levels.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
levelDefinition[level] = {
"class": cssClass,
"desc": description
}
return levelDefinition
@classmethod
def _CheckPackagesConfiguration(cls, sphinxConfiguration: Config) -> None:
"""
Check and load the package configurations from ``pyTooling_DocCoverage_Packages``.
:param sphinxConfiguration: Sphinx configuration instance.
:raises ReportExtensionError: If the configuration value isn't registered.
:raises ReportExtensionError: If a package configuration has no ``name``.
:raises ReportExtensionError: If a package configuration has no ``directory``.
:raises ReportExtensionError: If the ``directory`` doesn't exist.
:raises ReportExtensionError: If ``pages`` isn't a string.
:raises ReportExtensionError: If ``pages`` isn't a relative document name. |br|
Use a name like 'doccoverage/src', separated by '/', without '.' or '..'.
:raises ReportExtensionError: If a package configuration has no ``fail_below``.
:raises ReportExtensionError: If ``fail_below`` isn't an integer, or is out of range.
:raises ReportExtensionError: If a package configuration has no ``levels``.
:raises ReportExtensionError: If ``levels`` names a palette that isn't defined.
:raises ReportExtensionError: If ``levels`` is a dictionary with a missing or wrong level, CSS class or description.
:raises ReportExtensionError: If ``levels`` is neither a palette's name nor a dictionary.
"""
variableName = f"{CONFIG_PREFIX}_Packages"
cls._packageConfigurations = {}
try:
allPackages: dict[str, PackageConfiguration] = sphinxConfiguration[variableName]
except (KeyError, AttributeError) as ex:
raise ReportExtensionError(f"Configuration option '{variableName}' is not configured.") from ex
for reportID, packageConfiguration in allPackages.items():
configurationName = f"conf.py: {variableName}:[{reportID}]"
try:
packageName = packageConfiguration["name"]
except KeyError as ex:
raise ReportExtensionError(f"{configurationName}.name: Configuration is missing.") from ex
try:
directory = Path(packageConfiguration["directory"])
except KeyError as ex:
raise ReportExtensionError(f"{configurationName}.directory: Configuration is missing.") from ex
if not directory.exists():
raise ReportExtensionError(
f"{configurationName}.directory: Directory '{directory}' doesn't exist."
) from FileNotFoundError(directory)
if (pages := packageConfiguration.get("pages", None)) is not None:
pages = ReportPages.CheckPagesConfiguration(configurationName, pages)
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 "
f"variable '{variableName}'."
) from ex
elif isinstance(levels, dict):
levelDefinition = cls._CheckLevels(f"{configurationName}.levels", levels)
else:
ex = ReportExtensionError(f"{configurationName}.levels: Is neither a palette's name nor a dictionary.")
ex.add_note(f"Got type '{getFullyQualifiedName(levels)}'.")
raise ex
cls._packageConfigurations[reportID] = {
"name": packageName,
"directory": directory,
"pages": pages,
"failBelow": failBelow,
"levels": levelDefinition
}
@staticmethod
def LevelOf(levels: dict[Union[int, str], dict[str, str]], coverage: float, configKey: str) -> str:
"""
Look up the coverage level a coverage falls into, and return one of its fields.
:param levels: The coverage levels.
:param coverage: The coverage, in range 0.0..1.0; a negative value selects level ``error``.
:param configKey: The field to return: ``class`` or ``desc``.
:returns: The field's value of the first level whose limit is above the coverage.
"""
if coverage < 0.0:
return levels["error"][configKey]
for levelLimit, levelConfig in levels.items():
if isinstance(levelLimit, int) and (coverage * 100) < levelLimit:
return levelConfig[configKey]
return levels[100][configKey]
def _ConvertToColor(self, currentLevel: float, configKey: str) -> str:
"""
Look up the coverage level a coverage falls into, in the package's levels, and return one of its fields.
:param currentLevel: The coverage, in range 0.0..1.0; a negative value selects level ``error``.
:param configKey: The field to return: ``class`` or ``desc``.
:returns: The field's value of the first level whose limit is above the coverage.
"""
return self.LevelOf(self._levels, currentLevel, configKey)
@export
class DocCoverage(DocCoverageBase):
"""
Base-class of the ``report:doc-coverage`` directive: the table, without an analyzer.
A package with pages links every package and module to its page.
"""
directiveName: str = "doc-coverage" #: Name the directive is invoked by.
has_content = False #: A boolean; ``True`` if content is allowed.
required_arguments = 0 #: Number of required directive arguments.
optional_arguments = DocCoverageBase.optional_arguments + 0 #: Number of optional arguments.
_packageName: str #: Name of the Python package.
_directory: Path #: The package's source directory.
_failBelow: float #: Coverage below which the package fails.
_report: DocumentationReport #: The package's report, as analyzed.
def _CheckOptions(self) -> None:
"""
Parse all directive options or use default values.
:raises KeyError: If ``pyTooling_DocCoverage_Packages`` has no entry for the report ID.
"""
super()._CheckOptions()
packageConfiguration = self._packageConfigurations[self._reportID]
self._packageName = packageConfiguration["name"]
self._directory = packageConfiguration["directory"]
self._failBelow = packageConfiguration["failBelow"]
self._levels = packageConfiguration["levels"]
def _GenerateCoverageTable(self) -> nodes.table:
"""
Build the table: a row per package and module, and a summary row.
:returns: The table.
"""
cssClasses = ["report-doccov-table", f"report-doccov-{self._reportID}"]
cssClasses.extend(self._cssClasses)
# Create a table and table header with 5 columns
tableGroup = self._CreateSingleRowTableHeader(
identifier=self._reportID,
columns=[
("Filename", 5),
("Total", 1),
("Covered", 1),
("Missing", 1),
("Coverage in %", 1)
],
classes=cssClasses
)
tableBody = nodes.tbody()
tableGroup += tableBody
self._RenderLevel(tableBody, self._report.Items[0])
# Add a summary row
tableBody += nodes.row(
"",
nodes.entry("", nodes.Text(f"Overall ({self._report.FileCount} files):")),
nodes.entry("", nodes.Text(f"{self._report.Counters.Expected}")),
nodes.entry("", nodes.Text(f"{self._report.Counters.Covered}")),
nodes.entry("", nodes.Text(f"{self._report.Counters.Uncovered}")),
nodes.entry("", nodes.Text(f"{self._report.Counters.Coverage:.1%}")),
classes=[
"report-summary",
self._ConvertToColor(self._report.Counters.Coverage, "class")
]
)
return tableGroup.parent
def _RenderLevel(self, tableBody: nodes.tbody, package: Item, level: int = 0) -> None:
"""
Add a row for a package, then - recursively - rows for its sub-packages and modules.
A package's row counts the items declared in its ``__init__.py``, a module's row those declared in the module; a
file, which couldn't be read or parsed, has the coverage level ``error``. The hierarchy is shown by indentation, a
package by 📦 and a module by ⚙️.
:param tableBody: The table body the rows are added to.
:param package: The package's item.
:param level: Optional, the package's depth in the hierarchy. Default: ``0``.
"""
def addRow(unit: Item, indentation: int, icon: str, cssClass: str) -> None:
"""
Nested function adding the row of a package or module.
:param unit: The package's or module's item.
:param indentation: The number of indentation steps.
:param icon: The icon written before the name.
:param cssClass: The CSS class of the row's kind.
"""
counters = fileCounters(unit)
coverage, levelClass = coverageCell(unit, counters.Coverage, self._levels)
tableBody.append(nodes.row(
"",
nodes.entry("", self._NameCell(unit, indentation, icon)),
nodes.entry("", nodes.Text(f"{counters.Expected}")),
nodes.entry("", nodes.Text(f"{counters.Covered}")),
nodes.entry("", nodes.Text(f"{counters.Uncovered}")),
nodes.entry("", nodes.Text(coverage)),
classes=[cssClass, levelClass],
))
packages, modules = subPackagesAndModules(package)
addRow(package, level, "📦", "report-package")
for subPackage in packages:
self._RenderLevel(tableBody, subPackage, level + 1)
for module in modules:
addRow(module, level + 2, "⚙️", "report-module")
def _NameCell(self, unit: Item, level: int, icon: str) -> ReportIndentation:
"""
Create the content of the cell naming a package or module, indented, and linked to its page if the package has
pages.
:param unit: The package's or module's item.
:param level: The number of indentation steps.
:param icon: The icon written before the name.
:returns: The cell's content.
"""
if DocCoverageReportPages.HasPages(self._reportID):
pages = DocCoverageReportPages.GetPages(self._reportID)
if (entry := pages.Entry(unit)) is not None:
# a reference has to be inside a text element; an inline keeps the cell free of a paragraph, as the
# others are
reference = pages.CreateReference("doccov", entry, self.env.docname)
return ReportIndentation("", "", nodes.Text(icon), reference, level=level)
return ReportIndentation("", f"{icon}{unit.Name}", level=level)
@export
class DocStrCoverage(DocCoverage):
"""
The ``report:doc-coverage`` directive: a table of a package's documentation coverage.
"""
def run(self) -> list[nodes.Node]:
"""
Return the configured package's documentation coverage as a table, and - if the package has pages - a hidden
table of contents of its page.
:returns: A container holding the table, or the error message; then the table of contents, if the package has
pages.
"""
container = nodes.container()
try:
self._CheckOptions()
except SphinxExtensionError as ex:
message = f"Caught {getTypeName(ex)} when checking options for directive '{self.directiveName}'."
return self._internalError(container, __name__, message, ex)
try:
self._report = self.GetReport(self._reportID)
except ReportExtensionError as ex:
cause = ex if ex.__cause__ is None else ex.__cause__
message = f"Caught {getTypeName(cause)} when analyzing package '{self._packageName}'."
return self._internalError(container, __name__, message, cause)
container += self._GenerateCoverageTable()
if not DocCoverageReportPages.HasPages(self._reportID):
return [container]
return [container, DocCoverageReportPages.GetPages(self._reportID).TableOfContents(self.env.docname)]
@export
class DocCoverageLegend(DocCoverageBase):
"""
The ``report:doc-coverage-legend`` directive: a table of a package's coverage levels.
"""
directiveName: str = "doc-coverage-legend" #: Name the directive is invoked by.
has_content = False #: A boolean; ``True`` if content is allowed.
required_arguments = 0 #: Number of required directive arguments.
optional_arguments = DocCoverageBase.optional_arguments + 1 #: Number of optional arguments.
option_spec: dict[str, Any] = DocCoverageBase.option_spec | { # type: ignore[misc]
"style": stripAndNormalize
} #: Mapping of option names to validator functions.
_style: LegendStyle #: How the legend is laid out.
def _CheckOptions(self) -> None:
"""
Parse all directive options or use default values.
:raises SphinxExtensionError: If option ``:style:`` names no :class:`~pyTooling.Sphinx.LegendStyle`.
:raises KeyError: If ``pyTooling_DocCoverage_Packages`` has no entry for the report ID.
"""
super()._CheckOptions()
self._style = self._ParseEnumOption("style", LegendStyle, LegendStyle.horizontal_table)
packageConfiguration = self._packageConfigurations[self._reportID]
self._levels = packageConfiguration["levels"]
def _CreateHorizontalLegendTable(self, identifier: str, classes: list[str]) -> nodes.table:
"""
Build the legend as a table with a column per level.
:param identifier: Identifier of the table.
:param classes: CSS classes of the table.
:returns: The table.
"""
columns: list[tuple[str, Union[int, None]]] = [("Documentation Coverage:", 3)]
for level in self._levels:
if isinstance(level, int):
columns.append((f"≤{level} %", 2))
tableGroup = self._CreateSingleRowTableHeader(columns, identifier=identifier, classes=classes)
tableBody = nodes.tbody()
tableGroup += tableBody
legendRow = nodes.row("", classes=["report-doccov-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 tableGroup.parent
def _CreateVerticalLegendTable(self, identifier: str, classes: list[str]) -> nodes.table:
"""
Build the legend as a table with a row per level.
:param identifier: Identifier of the table.
:param classes: CSS classes of the table.
:returns: The table.
"""
tableGroup = self._CreateSingleRowTableHeader([
("Documentation Coverage", 3),
("Coverage Level", 3)
],
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.Text(f"≤{level} %")),
nodes.entry("", nodes.paragraph(text=config["desc"])),
classes=["report-doccov-legend-row", self._ConvertToColor((level - 1) / 100, "class")]
)
return tableGroup.parent
def run(self) -> list[nodes.Node]:
"""
Return the configured package's coverage levels as a table, in the requested style.
:returns: A container holding the table, or the error message.
"""
container = nodes.container()
try:
self._CheckOptions()
except SphinxExtensionError as ex:
message = f"Caught {getTypeName(ex)} 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._reportID}-legend", classes=["report-doccov-legend"]
)
elif LegendStyle.Vertical in self._style:
container += self._CreateVerticalLegendTable(
identifier=f"{self._reportID}-legend", classes=["report-doccov-legend"]
)
else:
container += nodes.paragraph(text="Unsupported legend style.")
else:
container += nodes.paragraph(text="Unsupported legend style.")
return [container]
@export
class DocCoverageReportPages(ReportPages["Item"]):
"""
The pages of one package's documentation coverage: a page per package and per module.
"""
_pages: ClassVar[dict[str, DocCoverageReportPages]] = {} #: The pages of every analyzed package, by report ID.
_separator: ClassVar[str] = "." #: Separator of the names in a dotted name.
_directory: Path #: The package's source directory.
_levels: dict[Union[int, str], dict[str, str]] #: The package's coverage levels, coloring the tables.
def __init__(
self,
reportID: str,
prefix: str,
directory: Path,
package: Item,
levels: dict[Union[int, str], dict[str, str]]
) -> None:
"""
Compute the document names and entries of a package's packages and modules.
:param reportID: Identifier of the package.
:param prefix: Document name the pages are generated below.
:param directory: The package's source directory.
:param package: The package's item, the root of the pages.
:param levels: The package's coverage levels.
"""
super().__init__(reportID, prefix, directory, [("doccov", package)])
self._directory = directory.resolve()
self._levels = levels
@classmethod
def CreatePages(cls, sphinxApplication: Sphinx) -> None:
"""
Call-back for Sphinx' ``builder-inited`` event, after the packages are analyzed: compute and register the pages.
Only a package with the key ``pages`` gets pages. A package that couldn't be analyzed gets none; this is logged
as an error.
:param sphinxApplication: Sphinx application instance.
"""
cls._pages = {}
for reportID, packageConfiguration in DocCoverageBase._packageConfigurations.items():
if (prefix := packageConfiguration["pages"]) is None:
continue
try:
report = DocCoverageBase.GetReport(reportID)
except ReportExtensionError as ex:
cause = ex if ex.__cause__ is None else ex.__cause__
getLogger(__name__).error(
f"Caught {getTypeName(ex)} when generating the pages of documentation coverage '{reportID}'.\n"
f" {getTypeName(cause)}: {cause}"
)
continue
pages = cls(reportID, prefix, packageConfiguration["directory"], report.Items[0], packageConfiguration["levels"])
cls._pages[reportID] = pages
pages.Register(sphinxApplication.env)
def _Name(self, entity: Item) -> str:
"""
Return a package's or module's name; the root package's may be dotted.
The root package has the configured name, e.g. ``pyTooling.Sphinx``, which is one element of a path: a reference
names it in full.
:param entity: The package's or module's item.
:returns: The name.
"""
return entity.Name
def _Title(self, entity: Item) -> Nullable[str]:
"""
Return no title, as a package or module has none.
:param entity: The package's or module's item.
:returns: ``None``
"""
return None
def _Children(self, roleName: str, entity: Item) -> Iterable[tuple[str, Item]]:
"""
Return a package's sub-packages, then its modules, each sorted by name; a module has none.
:param roleName: The role referring to the object: ``doccov``.
:param entity: The package's or module's item.
:returns: The children as ``(role name, object)`` pairs.
"""
packages, modules = subPackagesAndModules(entity)
return [("doccov", child) for child in (*packages, *modules)]
def _Directory(self, package: Item) -> Directory:
"""
Return a package's directory in the report's physical hierarchy.
:param package: The package's item.
:returns: The directory; the report itself for the root package.
"""
names: list[str] = []
unit = package
while unit.Parent is not unit.Root:
names.insert(0, unit.Name)
unit = unit.Parent
directory = package.Root
for name in names:
directory = directory.Directories[Path(name)]
return directory
def _Source(self, entity: Item) -> Path:
"""
Return the source of a package's or module's page: the module's file, the package's ``__init__.py`` - or its
directory, if it has none.
:param entity: The package's or module's item.
:returns: The page's source file.
"""
if entity.File is not None:
return self._directory / entity.File.Path
return self._directory / self._Directory(entity).Path
def _Page(self, docName: str, roleName: str, entity: Item) -> nodes.section:
"""
Build a package's or module's page: its summary, a package's tables of sub-packages and modules, and the
condensed listing.
:param docName: Name of the page's document.
:param roleName: The role referring to the object: ``doccov``.
:param entity: The package's or module's item.
:returns: The page's top section.
"""
page = nodes.section("", nodes.title(entity.Name, entity.Name), ids=["report-doccov-page"])
page += (summarySection := self._SummarySection(docName, entity))
children = self._ChildrenSections(page, docName, entity)
if len(children) > 0:
# body elements precede a section's subsections, so the hidden table of contents goes before 'Summary'
page.insert(page.index(summarySection), self._TableOfContents(docName, children))
if entity.File is not None:
page += self._SourceSection(entity, self._Source(entity))
return page
def _SummarySection(self, docName: str, entity: Item) -> nodes.section:
"""
Create a page's section *Summary*: the parent package, the doc-string's state, and the counters - of the module's
file or the package's ``__init__.py``, and a package's overall -, colored by coverage level. A file, which
couldn't be read or parsed, has the coverage level ``error``.
:param docName: Name of the page's document.
:param entity: The package's or module's item.
:returns: The section.
"""
from pyEDAA.Reports.DocumentationCoverage import ItemKind
section = nodes.section("", nodes.title("Summary", "Summary"), ids=["summary"])
section += (fieldList := nodes.field_list(classes=["report-doccov-fields"]))
if entity.Parent is not entity.Root:
self._Field(fieldList, "Package", self._Reference("doccov", entity.Parent, docName))
if isUnreadable(entity):
self._Field(fieldList, "Doc-string", nodes.Text("can't be read"))
elif entity.File is not None:
self._Field(fieldList, "Doc-string", nodes.Text(DOCUMENTATION_STATES[documentationState(entity.Status)]))
table, tableBody = self._Table(
[("File", 6), ("Expected", 2), ("Covered", 2), ("Uncovered", 2), ("Coverage", 2)],
["report-doccov-table", "report-doccov-summary"]
)
section += table
rows = []
if entity.File is not None:
rows.append((f"{entity.File.Name}", "", entity, fileCounters(entity)))
if entity.Kind is ItemKind.Package:
rows.append((f"Overall ({self._Directory(entity).FileCount} files)", "report-summary", None, entity.Counters))
for name, cssClass, unit, counters in rows:
coverage, level = coverageCell(unit, counters.Coverage, self._levels)
tableBody += (row := nodes.row("", classes=[cssClass, level] if cssClass != "" else [level]))
row += nodes.entry("", nodes.paragraph("", name))
row += nodes.entry("", nodes.paragraph("", f"{counters.Expected}"))
row += nodes.entry("", nodes.paragraph("", f"{counters.Covered}"))
row += nodes.entry("", nodes.paragraph("", f"{counters.Uncovered}"))
row += nodes.entry("", nodes.paragraph("", coverage))
return section
def _ChildrenSections(self, page: nodes.section, docName: str, package: Item) -> list[str]:
"""
Add a package's sections of its sub-packages and modules: a table each, every row linked to its page and colored
by coverage level. A sub-package's row counts it with all its modules; a module has none. A package or module,
whose file couldn't be read or parsed, has the coverage level ``error``.
:param page: The package's page.
:param docName: Name of the page's document.
:param package: The package's or module's item.
:returns: The document names of the listed pages.
"""
packages, modules = subPackagesAndModules(package)
children: list[str] = []
for title, column, identifier, units in (
("Packages", "Package", "packages", packages),
("Modules", "Module", "modules", modules),
):
if len(units) == 0:
continue
page += (section := nodes.section("", nodes.title(title, title), ids=[identifier]))
table, tableBody = self._Table(
[(column, 6), ("Expected", 2), ("Covered", 2), ("Uncovered", 2), ("Coverage", 2)],
["report-doccov-table", "report-doccov-children"]
)
section += table
for unit in units:
coverage, level = coverageCell(unit, unit.Counters.Coverage, self._levels)
tableBody += (row := nodes.row("", classes=[level]))
row += nodes.entry("", nodes.paragraph("", "", self._Reference("doccov", unit, docName)))
row += nodes.entry("", nodes.paragraph("", f"{unit.Counters.Expected}"))
row += nodes.entry("", nodes.paragraph("", f"{unit.Counters.Covered}"))
row += nodes.entry("", nodes.paragraph("", f"{unit.Counters.Uncovered}"))
row += nodes.entry("", nodes.paragraph("", coverage))
if (entry := self.Entry(unit)) is not None:
children.append(entry.docName)
return children
def _SourceSection(self, entity: Item, sourceFile: Path) -> nodes.section:
"""
Create a page's section *Source*: the condensed listing of the module, or of the package's ``__init__.py``.
The source file is read in its declared encoding. A source file, which the analysis couldn't read or parse, isn't
read again; the section says so. A source file, which can't be read or parsed now, is logged as a warning, and
the section says so.
:param entity: The package's or module's item, which has a file.
:param sourceFile: The source file.
:returns: The section.
"""
section = nodes.section("", nodes.title("Source", "Source"), ids=["source"])
path = (entity.Root.Name / entity.File.Path).as_posix()
if isUnreadable(entity):
section += nodes.paragraph(text=f"The source file '{path}' can't be read or parsed.")
return section
try:
with tokenize_open(sourceFile) as file:
source = file.read()
condensedSource = CondensedSource.Parse(source, path, entity)
except (OSError, SyntaxError, ValueError) as ex:
getLogger(__name__).warning(
f"Source file '{sourceFile}' of the documentation coverage can't be read or parsed: {ex}"
)
section += nodes.paragraph(text=f"The source file '{path}' can't be read or parsed.")
return section
sourceLines = source.split("\n")
tokens = tokenizeSource(source, sourceFile.name)
ellipsis = tokenizeSource("...\n", sourceFile.name)[0]
lines: list[tuple[Nullable[int], str, list[tuple[str, str]]]] = []
text: list[str] = []
for number, state, indentation in condensedSource.Lines:
if number is None:
lines.append((None, "", [("", indentation), *ellipsis]))
text.append(f"{indentation}...")
else:
lines.append((number, state, tokens[number - 1]))
text.append(sourceLines[number - 1])
plainText = "\n".join(text)
section += DocCoverageListing(plainText, plainText, lines=lines, path=path)
return section
|