| 1 |
# Copyright (C) 2013 Simon Feltman <[email protected]> |
| 2 |
# |
| 3 |
# docstring.py: documentation string generator for gi. |
| 4 |
# |
| 5 |
# This library is free software; you can redistribute it and/or |
| 6 |
# modify it under the terms of the GNU Lesser General Public |
| 7 |
# License as published by the Free Software Foundation; either |
| 8 |
# version 2.1 of the License, or (at your option) any later version. |
| 9 |
# |
| 10 |
# This library is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
# Lesser General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU Lesser General Public |
| 16 |
# License along with this library; if not, write to the Free Software |
| 17 |
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 |
| 18 |
# USA |
| 19 |
|
| 20 |
from ._gi import ( |
| 21 |
VFuncInfo, |
| 22 |
FunctionInfo, |
| 23 |
CallableInfo, |
| 24 |
ObjectInfo, |
| 25 |
StructInfo, |
| 26 |
Direction, |
| 27 |
TypeTag, |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
#: Module storage for currently registered doc string generator function. |
| 32 |
_generate_doc_string_func = None |
| 33 |
|
| 34 |
|
| 35 |
def set_doc_string_generator(func): |
| 36 |
"""Set doc string generator function. |
| 37 |
|
| 38 |
:param callable func: |
| 39 |
Callable which takes a GIInfoStruct and returns documentation for it. |
| 40 |
""" |
| 41 |
global _generate_doc_string_func |
| 42 |
_generate_doc_string_func = func |
| 43 |
|
| 44 |
|
| 45 |
def get_doc_string_generator(): |
| 46 |
"""Returns the currently registered doc string generator.""" |
| 47 |
return _generate_doc_string_func |
| 48 |
|
| 49 |
|
| 50 |
def generate_doc_string(info): |
| 51 |
"""Generate a doc string given a GIInfoStruct. |
| 52 |
|
| 53 |
:param gi.types.BaseInfo info: |
| 54 |
GI info instance to generate documentation for. |
| 55 |
:returns: |
| 56 |
Generated documentation as a string. |
| 57 |
:rtype: str |
| 58 |
|
| 59 |
This passes the info struct to the currently registered doc string |
| 60 |
generator and returns the result. |
| 61 |
""" |
| 62 |
return _generate_doc_string_func(info) |
| 63 |
|
| 64 |
|
| 65 |
_type_tag_to_py_type = { |
| 66 |
TypeTag.BOOLEAN: bool, |
| 67 |
TypeTag.INT8: int, |
| 68 |
TypeTag.UINT8: int, |
| 69 |
TypeTag.INT16: int, |
| 70 |
TypeTag.UINT16: int, |
| 71 |
TypeTag.INT32: int, |
| 72 |
TypeTag.UINT32: int, |
| 73 |
TypeTag.INT64: int, |
| 74 |
TypeTag.UINT64: int, |
| 75 |
TypeTag.FLOAT: float, |
| 76 |
TypeTag.DOUBLE: float, |
| 77 |
TypeTag.GLIST: list, |
| 78 |
TypeTag.GSLIST: list, |
| 79 |
TypeTag.ARRAY: list, |
| 80 |
TypeTag.GHASH: dict, |
| 81 |
TypeTag.UTF8: str, |
| 82 |
TypeTag.FILENAME: str, |
| 83 |
TypeTag.UNICHAR: str, |
| 84 |
TypeTag.INTERFACE: None, |
| 85 |
TypeTag.GTYPE: None, |
| 86 |
TypeTag.ERROR: None, |
| 87 |
TypeTag.VOID: None, |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
def _get_pytype_hint(gi_type): |
| 92 |
type_tag = gi_type.get_tag() |
| 93 |
py_type = _type_tag_to_py_type.get(type_tag) |
| 94 |
|
| 95 |
if py_type and hasattr(py_type, "__name__"): |
| 96 |
return py_type.__name__ |
| 97 |
if type_tag == TypeTag.INTERFACE: |
| 98 |
iface = gi_type.get_interface() |
| 99 |
|
| 100 |
info_name = iface.get_name() |
| 101 |
if not info_name: |
| 102 |
return gi_type.get_tag_as_string() |
| 103 |
|
| 104 |
return f"{iface.get_namespace()}.{info_name}" |
| 105 |
|
| 106 |
return gi_type.get_tag_as_string() |
| 107 |
|
| 108 |
|
| 109 |
def _generate_callable_info_doc(info): |
| 110 |
in_args_strs = [] |
| 111 |
if isinstance(info, VFuncInfo) or ( |
| 112 |
isinstance(info, FunctionInfo) and info.is_method() |
| 113 |
): |
| 114 |
in_args_strs = ["self"] |
| 115 |
|
| 116 |
args = info.get_arguments() |
| 117 |
hint_blacklist = ("void",) |
| 118 |
|
| 119 |
# Build lists of indices prior to adding the docs because it is possible |
| 120 |
# the index retrieved comes before input arguments being used. |
| 121 |
ignore_indices = {info.get_return_type().get_array_length_index()} |
| 122 |
user_data_indices = set() |
| 123 |
for arg in args: |
| 124 |
ignore_indices.add(arg.get_destroy_index()) |
| 125 |
ignore_indices.add(arg.get_type_info().get_array_length_index()) |
| 126 |
user_data_indices.add(arg.get_closure_index()) |
| 127 |
|
| 128 |
# Build input argument strings |
| 129 |
for i, arg in enumerate(args): |
| 130 |
if arg.get_direction() == Direction.OUT: |
| 131 |
continue # skip exclusively output args |
| 132 |
if i in ignore_indices: |
| 133 |
continue |
| 134 |
argstr = arg.get_name() |
| 135 |
hint = _get_pytype_hint(arg.get_type_info()) |
| 136 |
if hint not in hint_blacklist: |
| 137 |
argstr += ":" + hint |
| 138 |
if arg.may_be_null() or i in user_data_indices: |
| 139 |
# allow-none or user_data from a closure |
| 140 |
argstr += "=None" |
| 141 |
elif arg.is_optional(): |
| 142 |
argstr += "=<optional>" |
| 143 |
in_args_strs.append(argstr) |
| 144 |
in_args_str = ", ".join(in_args_strs) |
| 145 |
|
| 146 |
# Build return + output argument strings |
| 147 |
out_args_strs = [] |
| 148 |
return_hint = _get_pytype_hint(info.get_return_type()) |
| 149 |
if not info.skip_return() and return_hint and return_hint not in hint_blacklist: |
| 150 |
argstr = return_hint |
| 151 |
if info.may_return_null(): |
| 152 |
argstr += " or None" |
| 153 |
out_args_strs.append(argstr) |
| 154 |
|
| 155 |
for i, arg in enumerate(args): |
| 156 |
if arg.get_direction() == Direction.IN: |
| 157 |
continue # skip exclusively input args |
| 158 |
if i in ignore_indices: |
| 159 |
continue |
| 160 |
argstr = arg.get_name() |
| 161 |
hint = _get_pytype_hint(arg.get_type_info()) |
| 162 |
if hint not in hint_blacklist: |
| 163 |
argstr += ":" + hint |
| 164 |
out_args_strs.append(argstr) |
| 165 |
|
| 166 |
if out_args_strs: |
| 167 |
return f"{info.__name__}({in_args_str}) -> {', '.join(out_args_strs)}" |
| 168 |
return f"{info.__name__}({in_args_str})" |
| 169 |
|
| 170 |
|
| 171 |
def _generate_class_info_doc(info): |
| 172 |
header = "\n:Constructors:\n\n::\n\n" # start with \n to avoid auto indent of other lines |
| 173 |
doc = "" |
| 174 |
|
| 175 |
if isinstance(info, StructInfo): |
| 176 |
# Don't show default constructor for disguised (0 length) structs |
| 177 |
if info.get_size() > 0: |
| 178 |
doc += " " + info.get_name() + "()\n" |
| 179 |
else: |
| 180 |
doc += " " + info.get_name() + "(**properties)\n" |
| 181 |
|
| 182 |
for method_info in info.get_methods(): |
| 183 |
if method_info.is_constructor(): |
| 184 |
doc += " " + _generate_callable_info_doc(method_info) + "\n" |
| 185 |
|
| 186 |
if doc: |
| 187 |
return header + doc |
| 188 |
return "" |
| 189 |
|
| 190 |
|
| 191 |
def _generate_doc_dispatch(info): |
| 192 |
if isinstance(info, (ObjectInfo, StructInfo)): |
| 193 |
return _generate_class_info_doc(info) |
| 194 |
|
| 195 |
if isinstance(info, CallableInfo): |
| 196 |
return _generate_callable_info_doc(info) |
| 197 |
|
| 198 |
return "" |
| 199 |
|
| 200 |
|
| 201 |
set_doc_string_generator(_generate_doc_dispatch) |