|
6 | 6 | Utilities for documenting functions, classes and methods
|
7 | 7 | """
|
8 | 8 | #
|
| 9 | +# Copyright © 2020 Dominic Davis-Foster <[email protected]> |
9 | 10 | # Based on https://softwareengineering.stackexchange.com/a/386758
|
10 |
| -# Copyright (c) amon (https://softwareengineering.stackexchange.com/users/60357/amon) |
| 11 | +# Copyright © amon (https://softwareengineering.stackexchange.com/users/60357/amon) |
11 | 12 | # Licensed under CC BY-SA 4.0
|
12 | 13 | #
|
13 | 14 | # This program is free software; you can redistribute it and/or modify
|
|
26 | 27 | # MA 02110-1301, USA.
|
27 | 28 | #
|
28 | 29 |
|
| 30 | +import builtins |
| 31 | + |
| 32 | + |
| 33 | +def document_object_from_another(target, original): |
| 34 | + """ |
| 35 | + Sets the docstring of the `target` function to that of the `original` function. |
| 36 | +
|
| 37 | + This may be useful for subclasses or wrappers that use the same arguments. |
| 38 | +
|
| 39 | + :param target: The object to set the docstring for |
| 40 | + :type target: any |
| 41 | + :param original: The object to copy the docstring from |
| 42 | + :type original: any |
| 43 | + """ |
| 44 | + |
| 45 | + target.__doc__ = original.__doc__ |
| 46 | + |
29 | 47 |
|
30 | 48 | def is_documented_by(original):
|
| 49 | + """ |
| 50 | + Sets the docstring of the `target` function to that of the `original` function. |
| 51 | +
|
| 52 | + This may be useful for subclasses or wrappers that use the same arguments. |
| 53 | + """ |
| 54 | + |
31 | 55 | def wrapper(target):
|
32 |
| - target.__doc__ = original.__doc__ |
| 56 | + document_object_from_another(target, original) |
33 | 57 | return target
|
| 58 | + |
34 | 59 | return wrapper
|
35 | 60 |
|
36 | 61 |
|
| 62 | +def append_doctring_from_another(target, original): |
| 63 | + """ |
| 64 | + Sets the docstring of the `target` function to that of the `original` function. |
| 65 | +
|
| 66 | + This may be useful for subclasses or wrappers that use the same arguments. |
| 67 | +
|
| 68 | + Any indentation in either docstring is removed to |
| 69 | + ensure consistent indentation between the two docstrings. |
| 70 | + Bear this in mind if additional indentation is used in the docstring. |
| 71 | +
|
| 72 | + :param target: The object to append the docstring to |
| 73 | + :type target: any |
| 74 | + :param original: The object to copy the docstring from |
| 75 | + :type original: any |
| 76 | + """ |
| 77 | + |
| 78 | + split_target_doc = target.__doc__.split("\n") |
| 79 | + deindented_target_doc = [line.lstrip("\t ") for line in split_target_doc] |
| 80 | + |
| 81 | + split_original_doc = original.__doc__.split("\n") |
| 82 | + deindented_original_doc = [line.lstrip("\t ") for line in split_original_doc] |
| 83 | + |
| 84 | + target.__doc__ = f"\n".join(deindented_target_doc + deindented_original_doc) |
| 85 | + |
| 86 | + |
37 | 87 | def append_docstring_from(original):
|
| 88 | + """ |
| 89 | + Appends the docstring from the `original` function to the `target` function. |
| 90 | +
|
| 91 | + This may be useful for subclasses or wrappers that use the same arguments. |
| 92 | +
|
| 93 | + Any indentation in either docstring is removed to |
| 94 | + ensure consistent indentation between the two docstrings. |
| 95 | + Bear this in mind if additional indentation is used in the docstring. |
| 96 | + """ |
| 97 | + |
| 98 | + def wrapper(target): |
| 99 | + append_doctring_from_another(target, original) |
| 100 | + return target |
| 101 | + |
| 102 | + return wrapper |
| 103 | + |
| 104 | + |
| 105 | +def make_sphinx_links(input_string, builtins_list=None): |
| 106 | + """ |
| 107 | + Make proper sphinx links out of double-backticked strings in docstring. |
| 108 | +
|
| 109 | + i.e. \`\`str\`\` becomes \:class\:\`~python:str\` |
| 110 | + |
| 111 | + |
| 112 | + Make sure to have `'python': ('https://docs.python.org/3/', None),` in the |
| 113 | + `intersphinx_mapping` dict of your conf.py for sphinx. |
| 114 | + |
| 115 | + :param input_string: The string to process |
| 116 | + :type input_string: str |
| 117 | + :param builtins_list: A list of builtins to make links for |
| 118 | + :type builtins_list: list of str |
| 119 | +
|
| 120 | + :return: processed string with links |
| 121 | + :rtype: str |
| 122 | + """ |
| 123 | + |
| 124 | + if builtins_list is None: |
| 125 | + builtins_list = dir(builtins) |
| 126 | + |
| 127 | + working_string = f"{input_string}" |
| 128 | + |
| 129 | + for builtin in {x for x in builtins_list if not x.startswith("__") and x != "None"}: |
| 130 | + working_string = working_string.replace(f"``{builtin}``", f":class:`~python:{builtin}`") |
| 131 | + |
| 132 | + return working_string |
| 133 | + |
| 134 | + |
| 135 | +def sphinxify_docstring(): |
| 136 | + """ |
| 137 | + Make proper sphinx links out of double-backticked strings in docstring. |
| 138 | +
|
| 139 | + i.e. \`\`str\`\` becomes \:class\:\`~python:str\` |
| 140 | + |
| 141 | + Make sure to have `'python': ('https://docs.python.org/3/', None),` in the |
| 142 | + `intersphinx_mapping` dict of your conf.py for sphinx. |
| 143 | + """ |
| 144 | + |
38 | 145 | def wrapper(target):
|
39 |
| - target.__doc__ = target.__doc__ + "\n" + original.__doc__ |
| 146 | + target.__doc__ = make_sphinx_links(target.__doc__) |
40 | 147 | return target
|
| 148 | + |
41 | 149 | return wrapper
|
0 commit comments