Debian Codegraph @

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

raw
1 import json
2 import typing as t
3
4 from docutils.parsers.rst import directives
5
6 from globus_sdk.testing import ResponseList
7
8 from .add_content_directive import AddContentDirective
9
10
11 class ExpandTestingFixture(AddContentDirective):
12 has_content = False
13 required_arguments = 1
14 optional_arguments = 0
15 option_spec = {
16 "case": directives.unchanged,
17 }
18
19 def gen_rst(self) -> t.Iterator[str]:
20 from globus_sdk.testing import get_response_set
21
22 response_set_name = self.arguments[0]
23 casename = "default"
24 if "case" in self.options:
25 casename = self.options["case"].strip()
26 response_set = get_response_set(response_set_name)
27 response = response_set.lookup(casename)
28 if isinstance(response, ResponseList):
29 # If the default responses is a list of responses, use the first one
30 response = response.responses[0]
31 if response.json is not None:
32 yield ".. code-block:: json"
33 yield ""
34 output_lines = json.dumps(
35 response.json, indent=2, separators=(",", ": ")
36 ).split("\n")
37 for line in output_lines:
38 yield f" {line}"
39 elif response.body is not None:
40 yield ".. code-block:: text"
41 yield ""
42 output_lines = response.body.split("\n")
43 for line in output_lines:
44 yield f" {line}"
45 else:
46 raise RuntimeError(
47 "Error loading example content for response "
48 f"{response_set_name}:{casename}. Neither JSON nor text body was found."
49 )