sphinx_reports.Dependency

sphinx_reports/Dependency.py
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
# ==================================================================================================================== #
#            _     _                                           _                                                       #
#  ___ _ __ | |__ (_)_ __ __  __     _ __ ___ _ __   ___  _ __| |_ ___                                                 #
# / __| '_ \| '_ \| | '_ \\ \/ /____| '__/ _ \ '_ \ / _ \| '__| __/ __|                                                #
# \__ \ |_) | | | | | | | |>  <_____| | |  __/ |_) | (_) | |  | |_\__ \                                                #
# |___/ .__/|_| |_|_|_| |_/_/\_\    |_|  \___| .__/ \___/|_|   \__|___/                                                #
#     |_|                                    |_|                                                                       #
# ==================================================================================================================== #
# Authors:                                                                                                             #
#   Patrick Lehmann                                                                                                    #
#                                                                                                                      #
# License:                                                                                                             #
# ==================================================================================================================== #
# Copyright 2023-2025 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 unit test results as Sphinx documentation page(s).**
"""
from typing import Dict, Tuple, Any, List, Mapping, Generator

from docutils                          import nodes
from pyTooling.Decorators              import export
from sphinx.application import Sphinx
from sphinx.config import Config

from sphinx_reports.Common               import ReportExtensionError
from sphinx_reports.Sphinx               import stripAndNormalize, BaseDirective
from sphinx_reports.DataModel.Dependency import Distribution
from sphinx_reports.Adapter.Dependency   import DependencyScanner


@export
class DependencyTable(BaseDirective):
	"""
	This directive will be replaced by a table representing dependencies.
	"""
	has_content = False
	required_arguments = 0
	optional_arguments = 1

	option_spec = {
		"package":       stripAndNormalize
	}

	directiveName: str = "dependency-table"
	configPrefix:  str = "dep"
	configValues:  Dict[str, Tuple[Any, str, Any]] = {
		# f"{configPrefix}_testsuites": ({}, "env", Dict)
	}  #: A dictionary of all configuration values used by unittest directives.

	_packageName:  str

	_distribution: Distribution

	def _CheckOptions(self) -> None:
		"""
		Parse all directive options or use default values.
		"""
		self._packageName = self._ParseStringOption("package")

	@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.
		"""
		pass

	def _GenerateDependencyTable(self) -> nodes.table:
		# Create a table and table header with 8 columns
		columns = [
			("Package", None, 500),
			("Version", None, 100),
			("License", None, 100),
		]

		tableGroup = self._CreateDoubleRowTableHeader(
			identifier=self._packageName,
			columns=columns,
			classes=["report-dependency-table"]
		)
		tableBody = nodes.tbody()
		tableGroup += tableBody

		# def sortedValues(d: Mapping[str, Testsuite]) -> Generator[Testsuite, None, None]:
		# 	for key in sorted(d.keys()):
		# 		yield d[key]

		def renderRoot(tableBody: nodes.tbody, distribution: Distribution) -> None:
			tableRow = nodes.row("", classes=["report-dependency-table-row", "report-dependency"])
			tableBody += tableRow

			tableRow += nodes.entry("", nodes.Text(f"{distribution.Name}"))
			tableRow += nodes.entry("", nodes.Text(f"{distribution.Version}"))
			tableRow += nodes.entry("", nodes.Text(f"{distribution.Licenses}"))

			# for ts in sortedValues(testsuite._testsuites):
			# 	renderTestsuite(tableBody, ts, 0)

		renderRoot(tableBody, self._distribution)

		# # Add a summary row

		return tableGroup.parent

	def run(self) -> List[nodes.Node]:
		self._CheckOptions()

		# Assemble a list of Python source files
		scanner = DependencyScanner(self._packageName)
		self._distribution = scanner.Distribution

		container = nodes.container()
		container += self._GenerateDependencyTable()

		return [container]