Debian Codegraph @

src/globus_sdk/_internal/extensions/sphinxext/utils.py

raw
1 from __future__ import annotations
2
3 import functools
4 import inspect
5 import textwrap
6 import types
7 import typing as t
8 from pydoc import locate
9
10
11 def locate_class(classname: str) -> type:
12 cls = locate(classname)
13 if not inspect.isclass(cls):
14 raise RuntimeError(
15 f"uh-oh, {classname} is not a class name? type(classname)={type(cls)}"
16 )
17 return cls
18
19
20 def classname2methods(
21 classname: str, include_methods: t.Sequence[str]
22 ) -> list[tuple[str, types.FunctionType]]:
23 """Resolve a class name to a list of (public) method names + function objects.
24 Takes a classname and a list of method names to avoid filtering out.
25
26 :param classname: The name to resolve to a class
27 :param include_methods: A list or tuple of method names which would normally be
28 excluded as private, but which should be included in the result
29 """
30 cls = locate_class(classname)
31
32 # get methods of the object as [(name, <unbound method>), ...]
33 methods = inspect.getmembers(cls, predicate=inspect.isfunction)
34
35 def methodname_is_good(m: str) -> bool:
36 if m in include_methods:
37 return True
38 # filter out dunder-methods and `_private` methods
39 if m.startswith("_"):
40 return False
41 # filter out any inherited methods which are not overloaded
42 if m not in cls.__dict__:
43 return False
44 return True
45
46 return [(name, value) for name, value in methods if methodname_is_good(name)]
47
48
49 def derive_doc_url_base(service: str | None) -> str:
50 if service is None:
51 return "https://docs.globus.org/api"
52 elif service == "groups":
53 return "https://groups.api.globus.org/redoc#operation"
54 elif service == "gcs":
55 return "https://docs.globus.org/globus-connect-server/v5/api"
56 elif service == "flows":
57 return "https://globusonline.github.io/globus-flows#tag"
58 elif service == "compute":
59 return "https://compute.api.globus.org/redoc#tag"
60 else:
61 raise ValueError(f"Unsupported extdoclink service '{service}'")
62
63
64 @functools.cache
65 def read_sphinx_params(docstring: str) -> tuple[str, ...]:
66 """
67 Given a docstring, extract the `:param:` declarations into a tuple of strings.
68
69 :param docstring: The ``__doc__`` to parse, as it appeared on the original object
70
71 Params start with `:param ...` and end
72 - at the end of the string
73 - at the next param
74 - when a non-indented, non-param line is found
75
76 Whitespace lines within a param doc are supported.
77
78 All produced param strings are dedented.
79 """
80 docstring = textwrap.dedent(docstring)
81
82 result: list[str] = []
83 current: list[str] = []
84 for line in docstring.splitlines():
85 if not current:
86 if line.startswith(":param"):
87 current = [line]
88 else:
89 continue
90 else:
91 # a new param -- flush the current one and restart
92 if line.startswith(":param"):
93 result.append("\n".join(current).strip())
94 current = [line]
95 # a continuation line for the current param (indented)
96 # or a blank line -- it *could* be a continuation of param doc (include it)
97 elif line != line.lstrip() or not line:
98 current.append(line)
99 # otherwise this is a populated line, not indented, and without a `:param`
100 # start -- stop looking for more param doc
101 else:
102 break
103 if current:
104 result.append("\n".join(current).strip())
105
106 return tuple(result)