Debian Codegraph @

src/globus_sdk/_internal/extensions/sphinxext/directives/enumerate_testing_fixtures.py

raw
1 import typing as t
2
3 from docutils.parsers.rst import directives
4
5 import globus_sdk
6
7 from ..utils import classname2methods, locate_class
8 from .add_content_directive import AddContentDirective
9
10
11 class EnumerateTestingFixtures(AddContentDirective):
12 has_content = False
13 required_arguments = 1
14 optional_arguments = 0
15 option_spec = {
16 "header_underline_char": directives.unchanged,
17 }
18
19 def gen_rst(self) -> t.Iterator[str]:
20 from globus_sdk.testing import get_response_set
21
22 underline_char = self.options.get("header_underline_char", "-")
23
24 classname = self.arguments[0]
25 cls = locate_class(classname)
26 if not issubclass(cls, globus_sdk.BaseClient):
27 msg = f"Expected {cls} to be a subclass of BaseClient"
28 raise RuntimeError(msg)
29 service_name = cls.service_name
30
31 yield classname
32 yield underline_char * len(classname)
33 yield ""
34 yield (
35 f":class:`{classname}` has registered responses "
36 "for the following methods:"
37 )
38 yield ""
39 for methodname, method in classname2methods(classname, []):
40 try:
41 rset = get_response_set(method)
42 # success -> has a response
43 except ValueError:
44 continue
45 # error -> has no response, continue
46 for casename in rset.cases():
47 # use "attr" rather than "meth" so that sphinx does not add parens
48 # the use of the method as an attribute of the class or instance better
49 # matches how `testing` handles things
50 yield (
51 ".. dropdown:: "
52 f':py:attr:`~{classname}.{methodname}` (``case="{casename}"``)'
53 )
54 yield ""
55 yield f" .. expandtestfixture:: {service_name}.{methodname}"
56 yield f" :case: {casename}"
57 yield ""
58 yield ""