Coverage for pyTooling/Documentation/__init__.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-03 23:02 +0000

1# ==================================================================================================================== # 

2# _____ _ _ ____ _ _ _ # 

3# _ __ _ |_ _|__ ___ | (_)_ __ __ _ | _ \ ___ ___ _ _ _ __ ___ ___ _ __ | |_ __ _| |_(_) ___ _ __ # 

4# | '_ \| | | || |/ _ \ / _ \| | | '_ \ / _` | | | | |/ _ \ / __| | | | '_ ` _ \ / _ \ '_ \| __/ _` | __| |/ _ \| '_ \ # 

5# | |_) | |_| || | (_) | (_) | | | | | | (_| |_| |_| | (_) | (__| |_| | | | | | | __/ | | | || (_| | |_| | (_) | | | |# 

6# | .__/ \__, ||_|\___/ \___/|_|_|_| |_|\__, (_)____/ \___/ \___|\__,_|_| |_| |_|\___|_| |_|\__\__,_|\__|_|\___/|_| |_|# 

7# |_| |___/ |___/ # 

8# ==================================================================================================================== # 

9# Authors: # 

10# Patrick Lehmann # 

11# # 

12# License: # 

13# ==================================================================================================================== # 

14# Copyright 2026-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""" 

32Helper functions to work with doc-strings. 

33 

34.. hint:: 

35 

36 See :ref:`high-level help <DOC>` for explanations and usage examples. 

37 

38.. seealso:: 

39 

40 :deco:`pyTooling.Decorators.InheritDocString` 

41 |rarr| Merges a base-class' doc-string into a derived entity, using the split offered here. 

42 :func:`pyTooling.Packaging.extractVersionInformation` 

43 |rarr| Reads a package's short description from the summary of its module doc-string. 

44""" 

45from __future__ import annotations 

46 

47from inspect import cleandoc 

48from typing import Optional as Nullable 

49 

50from pyTooling.Decorators import export 

51from pyTooling.Exceptions import ToolingException 

52 

53 

54__all__ = ["MAXIMUM_SUMMARY_LENGTH"] 

55 

56MAXIMUM_SUMMARY_LENGTH = 200 

57""" 

58Number of characters a doc-string's summary may have by default. 

59 

60A summary is a single sentence, so a line of the usual 120 columns plus room for an embedded link or other markup is 

61a generous bound. A first paragraph longer than that is a body that lost its summary. 

62""" 

63 

64 

65@export 

66class DocumentationError(ToolingException): 

67 """Base-exception of all exceptions raised by :mod:`pyTooling.Documentation`.""" 

68 

69 

70@export 

71def splitDocString( 

72 docString: Nullable[str], 

73 maxSummaryLength: int = MAXIMUM_SUMMARY_LENGTH 

74) -> tuple[str, str]: 

75 """ 

76 Split a doc-string into its summary and its body. 

77 

78 The doc-string is dedented with :func:`inspect.cleandoc` first. The summary is the first paragraph, the body is 

79 whatever follows the first blank line. Both are empty strings if the doc-string is ``None``, and the body is an 

80 empty string if the doc-string is a single paragraph, so a caller needs no special case for either. 

81 

82 A summary is a single sentence, so its length is bounded by ``maxSummaryLength``; everything else belongs behind 

83 a blank line. 

84 

85 :param docString: The doc-string to split, or ``None``. 

86 :param maxSummaryLength: Optional, number of characters the summary may have. Pass ``0`` for no limit. 

87 Default: :data:`MAXIMUM_SUMMARY_LENGTH`. 

88 :returns: A tuple of summary and body. 

89 :raises DocumentationError: If the summary is longer than ``maxSummaryLength`` characters. 

90 """ 

91 if docString is None: 

92 return "", "" 

93 

94 summary, _, body = cleandoc(docString).partition("\n\n") 

95 

96 if 0 < maxSummaryLength < len(summary): 

97 ex = DocumentationError(f"The doc-string's summary is longer than {maxSummaryLength} characters.") 

98 ex.add_note(f"Got {len(summary)} characters.") 

99 ex.add_note("A summary is the first sentence of a doc-string; everything else belongs behind a blank line.") 

100 raise ex 

101 

102 return summary, body