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
|
# ==================================================================================================================== #
# _ _ _ #
# ___ _ __ | |__ (_)_ __ __ __ _ __ ___ _ __ ___ _ __| |_ ___ #
# / __| '_ \| '_ \| | '_ \\ \/ /____| '__/ _ \ '_ \ / _ \| '__| __/ __| #
# \__ \ |_) | | | | | | | |> <_____| | | __/ |_) | (_) | | | |_\__ \ #
# |___/ .__/|_| |_|_|_| |_/_/\_\ |_| \___| .__/ \___/|_| \__|___/ #
# |_| |_| #
# ==================================================================================================================== #
# 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 #
# ==================================================================================================================== #
#
"""
**A Sphinx domain providing directives to add reports to the Sphinx-based documentation.**
Supported reports:
* :ref:`DOCCOV`
* :ref:`CODECOV`
* :ref:`UNITTEST`
* :ref:`DEP`
"""
__author__ = "Patrick Lehmann"
__email__ = "Paebbels@gmail.com"
__copyright__ = "2023-2024, Patrick Lehmann"
__license__ = "Apache License, Version 2.0"
__version__ = "0.7.4"
__keywords__ = ["Python3", "Sphinx", "Extension", "Report", "doc-string", "interrogate"]
from hashlib import md5
from pathlib import Path
from typing import TYPE_CHECKING, Any, Tuple, Dict, Optional as Nullable, TypedDict, List, Callable
from docutils import nodes
from sphinx.addnodes import pending_xref
from sphinx.application import Sphinx
from sphinx.builders import Builder
from sphinx.config import Config
from sphinx.domains import Domain
from sphinx.environment import BuildEnvironment
from pyTooling.Decorators import export
from pyTooling.Common import readResourceFile
from sphinx.util.logging import getLogger
from sphinx_reports.Common import ReportExtensionError
from sphinx_reports import static as ResourcePackage
@export
class ReportDomain(Domain):
"""
A Sphinx extension providing a ``report`` domain to integrate reports and summaries into a Sphinx-based documentation.
.. rubric:: New directives:
* :rst:dir:`report:code-coverage`
* :rst:dir:`report:code-coverage-legend`
* :rst:dir:`report:doc-coverage`
* :rst:dir:`report:doc-coverage-legend`
* :rst:dir:`report:dependency-table`
* :rst:dir:`report:unittest-summary`
.. rubric:: New roles:
* *None*
.. rubric:: New indices:
* *None*
.. rubric:: Configuration variables
All configuration variables in :file:`conf.py` are prefixed with ``report_*``:
* ``report_codecov_packages``
* ``report_doccov_packages``
* ``report_unittest_testsuites``
"""
name = "report" #: The name of this domain
label = "rpt" #: The label of this domain
dependencies: List[str] = [
] #: A list of other extensions this domain depends on.
from sphinx_reports.CodeCoverage import CodeCoverage, CodeCoverageLegend, ModuleCoverage
from sphinx_reports.DocCoverage import DocStrCoverage, DocCoverageLegend
from sphinx_reports.Dependency import DependencyTable
from sphinx_reports.Unittest import UnittestSummary
directives = {
"code-coverage": CodeCoverage,
"code-coverage-legend": CodeCoverageLegend,
"module-coverage": ModuleCoverage,
"doc-coverage": DocStrCoverage,
"doc-coverage-legend": DocCoverageLegend,
"dependency-table": DependencyTable,
"unittest-summary": UnittestSummary,
} #: A dictionary of all directives in this domain.
roles = {
# "design": DesignRole,
} #: A dictionary of all roles in this domain.
indices = [
# LibraryIndex,
] #: A list of all indices in this domain.
from sphinx_reports.CodeCoverage import CodeCoverageBase
from sphinx_reports.DocCoverage import DocCoverageBase
from sphinx_reports.Dependency import DependencyTable
from sphinx_reports.Unittest import UnittestSummary
configValues: Dict[str, Tuple[Any, str, Any]] = {
**CodeCoverageBase.configValues,
**DocCoverageBase.configValues,
**UnittestSummary.configValues,
**DependencyTable.configValues,
} #: A dictionary of all configuration values used by this domain. (name: (default, rebuilt, type))
del CodeCoverageBase
del CodeCoverage
del CodeCoverageLegend
del ModuleCoverage
del DocCoverageBase
del DocStrCoverage
del DocCoverageLegend
del DependencyTable
del UnittestSummary
initial_data = {
# "reports": {}
} #: A dictionary of all global data fields used by this domain.
# @property
# def Reports(self) -> Dict[str, Any]:
# return self.data["reports"]
@staticmethod
def CheckConfigurationVariables(sphinxApplication: Sphinx, config: Config) -> None:
"""
Call back for Sphinx ``config-inited`` event.
This callback will verify configuration variables used by that domain.
.. seealso::
Sphinx *builder-inited* event
See https://www.sphinx-doc.org/en/master/extdev/appapi.html#sphinx-core-events
:param sphinxApplication: The Sphinx application.
:param config: Sphinx configuration parsed from ``conf.py``.
"""
from sphinx_reports.CodeCoverage import CodeCoverageBase
from sphinx_reports.DocCoverage import DocCoverageBase
from sphinx_reports.Unittest import UnittestSummary
checkConfigurations = (
CodeCoverageBase.CheckConfiguration,
DocCoverageBase.CheckConfiguration,
UnittestSummary.CheckConfiguration,
)
for checkConfiguration in checkConfigurations:
try:
checkConfiguration(sphinxApplication, config)
except ReportExtensionError as ex:
logger = getLogger(__name__)
logger.error(f"Caught {ex.__class__.__name__} when checking configuration variables.\n {ex}")
@staticmethod
def AddCSSFiles(sphinxApplication: Sphinx) -> None:
"""
Call back for Sphinx ``builder-inited`` event.
This callback will copy the CSS file(s) to the build directory.
.. seealso::
Sphinx *builder-inited* event
See https://www.sphinx-doc.org/en/master/extdev/appapi.html#sphinx-core-events
:param sphinxApplication: The Sphinx application.
"""
# Create a new static path for this extension
staticDirectory = (Path(sphinxApplication.outdir) / "_report_static").resolve()
staticDirectory.mkdir(exist_ok=True)
sphinxApplication.config.html_static_path.append(str(staticDirectory))
# Read the CSS content from package resources and hash it
cssFilename = "sphinx-reports.css"
cssContent = readResourceFile(ResourcePackage, cssFilename)
# Compute md5 hash of CSS file
hash = md5(cssContent.encode("utf8")).hexdigest()
# Write the CSS file into output directory
cssFile = staticDirectory / f"sphinx-reports.{hash}.css"
sphinxApplication.add_css_file(cssFile.name)
if not cssFile.exists():
# Purge old CSS files
for file in staticDirectory.glob("*.css"):
file.unlink()
# Write CSS content
cssFile.write_text(cssContent, encoding="utf8")
@staticmethod
def ReadReports(sphinxApplication: Sphinx) -> None:
"""
Call back for Sphinx ``builder-inited`` event.
This callback will read the linked report files
.. seealso::
Sphinx *builder-inited* event
See https://www.sphinx-doc.org/en/master/extdev/appapi.html#sphinx-core-events
:param sphinxApplication: The Sphinx application.
"""
from sphinx_reports.CodeCoverage import CodeCoverageBase
from sphinx_reports.Unittest import UnittestSummary
CodeCoverageBase.ReadReports(sphinxApplication)
UnittestSummary.ReadReports(sphinxApplication)
callbacks: Dict[str, List[Callable]] = {
"config-inited": [CheckConfigurationVariables], # (app, config)
"builder-inited": [AddCSSFiles, ReadReports], # (app)
} #: A dictionary of all events/callbacks <https://www.sphinx-doc.org/en/master/extdev/appapi.html#sphinx-core-events>`__ used by this domain.
def resolve_xref(
self,
env: BuildEnvironment,
fromdocname: str,
builder: Builder,
typ: str,
target: str,
node: pending_xref,
contnode: nodes.Element
) -> Nullable[nodes.Element]:
raise NotImplementedError()
if TYPE_CHECKING:
class setup_ReturnType(TypedDict):
version: str
env_version: int
parallel_read_safe: bool
parallel_write_safe: bool
@export
def setup(sphinxApplication: Sphinx) -> "setup_ReturnType":
"""
Extension setup function registering the ``report`` domain in Sphinx.
It will execute these steps:
* register domains, directives and roles.
* connect events (register callbacks)
* register configuration variables for :file:`conf.py`
:param sphinxApplication: The Sphinx application.
:return: Dictionary containing the extension version and some properties.
"""
sphinxApplication.add_domain(ReportDomain)
# Register callbacks
for eventName, callbacks in ReportDomain.callbacks.items():
for callback in callbacks:
sphinxApplication.connect(eventName, callback)
# Register configuration options supported/needed in Sphinx's 'conf.py'
for configName, (configDefault, configRebuilt, configTypes) in ReportDomain.configValues.items():
sphinxApplication.add_config_value(f"{ReportDomain.name}_{configName}", configDefault, configRebuilt, configTypes)
return {
"version": __version__, # version of the extension
"env_version": int(__version__.split(".")[0]), # version of the data structure stored in the environment
'parallel_read_safe': False, # Not yet evaluated, thus false
'parallel_write_safe': True, # Internal data structure is used read-only, thus no problems will occur by parallel writing.
}
|