Workaround.py

Summary

Lines:

17 of 18 covered (94.4%)

Branches:

6 of 8 taken (75.0%), 2 lines partially

Coverage:

88.5%

Directory:

Sphinx

Missed lines:

65

Source

pyTooling/Sphinx/Workaround.pycoveredpartialuncoveredexcludednot executable
1# ==================================================================================================================== #
2#             _____           _ _               ____        _     _                                                    #
3#  _ __  _   |_   _|__   ___ | (_)_ __   __ _  / ___| _ __ | |__ (_)_ __ __  __                                        #
4# | '_ \| | | || |/ _ \ / _ \| | | '_ \ / _` | \___ \| '_ \| '_ \| | '_ \\ \/ /                                        #
5# | |_) | |_| || | (_) | (_) | | | | | | (_| |_ ___) | |_) | | | | | | | |>  <                                         #
6# | .__/ \__, ||_|\___/ \___/|_|_|_| |_|\__, (_)____/| .__/|_| |_|_|_| |_/_/\_\                                        #
7# |_|    |___/                          |___/        |_|                                                               #
8# ==================================================================================================================== #
9# Authors:                                                                                                             #
10#   Patrick Lehmann                                                                                                    #
11#                                                                                                                      #
12# License:                                                                                                             #
13# ==================================================================================================================== #
14# Copyright 2023-2026 Patrick Lehmann - Bötzingen, Germany                                                             #
15#                                                                                                                      #
16# Licensed under the Apache License, Version 2.0 (the "License");                                                      #
17# you may not use this file except in compliance with the License.                                                     #
18# You may obtain a copy of the License at                                                                              #
19#                                                                                                                      #
20#   http://www.apache.org/licenses/LICENSE-2.0                                                                         #
21#                                                                                                                      #
22# Unless required by applicable law or agreed to in writing, software                                                  #
23# distributed under the License is distributed on an "AS IS" BASIS,                                                    #
24# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.                                             #
25# See the License for the specific language governing permissions and                                                  #
26# limitations under the License.                                                                                       #
27#                                                                                                                      #
28# SPDX-License-Identifier: Apache-2.0                                                                                  #
29# ==================================================================================================================== #
30#
31"""
32Workarounds for Sphinx and docutils problems.
33"""
34from typing                            import Any
35
36from docutils.nodes                    import table
37from sphinx.transforms.post_transforms import SphinxPostTransform
38from sphinx.util.logging               import getLogger
39
40from pyTooling.Decorators              import export
41
42
43@export
44class FixLatexTableWidths(SphinxPostTransform):
45	"""
46	A post-transform giving the report tables in LaTeX the column widths their directives state.
47
48	Without class ``colwidths-given``, Sphinx' LaTeX writer computes the widths itself and ignores the ones the table's
49	column specifications state, which squeezes a wide table - such as a unit test or code coverage report - into the
50	page.
51	"""
52
53	default_priority = 500                                                    #: Priority among the post-transforms.
54	formats =          ("latex", )                                            #: Output formats the transform runs for.
55	tableClasses =     ("report-unittest-table", "report-codecov-table")  #: CSS classes of the tables it fixes.
56
57	def run(self, **kwargs: Any) -> None:
58		"""
59		Add class ``colwidths-given`` to every report table that hasn't it yet.
60
61		:param kwargs: Keyword arguments Sphinx passes to a post-transform; none is used.
62		"""
63		for tableNode in self.document.findall(table):
64			if (cssClasses := tableNode.get("classes", None)) is None:
65				continue
66
67			if any(tableClass in cssClasses for tableClass in self.tableClasses):
68				if "colwidths-given" not in cssClasses:
69					cssClasses.append("colwidths-given")
70
71					getLogger(__name__).info(
72						"Applied 'colwidths-given' to a table via FixLatexTableWidths transform.", location=tableNode
73					)