example.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # Copyright 2015 ksyun.com, Inc. or its affiliates. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"). You
  4. # may not use this file except in compliance with the License. A copy of
  5. # the License is located at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # or in the "license" file accompanying this file. This file is
  10. # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  11. # ANY KIND, either express or implied. See the License for the specific
  12. # language governing permissions and limitations under the License.
  13. from kscore.docs.shape import ShapeDocumenter
  14. from kscore.docs.utils import py_default
  15. class BaseExampleDocumenter(ShapeDocumenter):
  16. def document_example(self, section, shape, prefix=None, include=None,
  17. exclude=None):
  18. """Generates an example based on a shape
  19. :param section: The section to write the documentation to.
  20. :param shape: The shape of the operation.
  21. :param prefix: Anything to be included before the example
  22. :type include: Dictionary where keys are parameter names and
  23. values are the shapes of the parameter names.
  24. :param include: The parameter shapes to include in the documentation.
  25. :type exclude: List of the names of the parameters to exclude.
  26. :param exclude: The names of the parameters to exclude from
  27. documentation.
  28. """
  29. history = []
  30. section.style.new_line()
  31. section.style.start_codeblock()
  32. if prefix is not None:
  33. section.write(prefix)
  34. self.traverse_and_document_shape(
  35. section=section, shape=shape, history=history,
  36. include=include, exclude=exclude)
  37. def document_recursive_shape(self, section, shape, **kwargs):
  38. section.write('{\'... recursive ...\'}')
  39. def document_shape_default(self, section, shape, history, include=None,
  40. exclude=None, **kwargs):
  41. py_type = self._get_special_py_default(shape)
  42. if py_type is None:
  43. py_type = py_default(shape.type_name)
  44. if self._context.get('streaming_shape') == shape:
  45. py_type = 'StreamingBody()'
  46. section.write(py_type)
  47. def document_shape_type_string(self, section, shape, history,
  48. include=None, exclude=None, **kwargs):
  49. if 'enum' in shape.metadata:
  50. for i, enum in enumerate(shape.metadata['enum']):
  51. section.write('\'%s\'' % enum)
  52. if i < len(shape.metadata['enum']) - 1:
  53. section.write('|')
  54. else:
  55. self.document_shape_default(section, shape, history)
  56. def document_shape_type_list(self, section, shape, history, include=None,
  57. exclude=None, **kwargs):
  58. param_shape = shape.member
  59. list_section = section.add_new_section('list-value')
  60. self._start_nested_param(list_section, '[')
  61. param_section = list_section.add_new_section(
  62. 'member', context={'shape': param_shape.name})
  63. self.traverse_and_document_shape(
  64. section=param_section, shape=param_shape, history=history)
  65. ending_comma_section = list_section.add_new_section('ending-comma')
  66. ending_comma_section.write(',')
  67. ending_bracket_section = list_section.add_new_section(
  68. 'ending-bracket')
  69. self._end_nested_param(ending_bracket_section, ']')
  70. def document_shape_type_structure(self, section, shape, history,
  71. include=None, exclude=None, **kwargs):
  72. section = section.add_new_section('structure-value')
  73. self._start_nested_param(section, '{')
  74. input_members = self._add_members_to_shape(shape.members, include)
  75. for i, param in enumerate(input_members):
  76. if exclude and param in exclude:
  77. continue
  78. param_section = section.add_new_section(param)
  79. param_section.write('\'%s\': ' % param)
  80. param_shape = input_members[param]
  81. param_value_section = param_section.add_new_section(
  82. 'member-value', context={'shape': param_shape.name})
  83. self.traverse_and_document_shape(
  84. section=param_value_section, shape=param_shape,
  85. history=history, name=param)
  86. if i < len(input_members) - 1:
  87. ending_comma_section = param_section.add_new_section(
  88. 'ending-comma')
  89. ending_comma_section.write(',')
  90. ending_comma_section.style.new_line()
  91. self._end_structure(section, '{', '}')
  92. def document_shape_type_map(self, section, shape, history,
  93. include=None, exclude=None, **kwargs):
  94. map_section = section.add_new_section('map-value')
  95. self._start_nested_param(map_section, '{')
  96. value_shape = shape.value
  97. key_section = map_section.add_new_section(
  98. 'key', context={'shape': shape.key.name})
  99. key_section.write('\'string\': ')
  100. value_section = map_section.add_new_section(
  101. 'value', context={'shape': value_shape.name})
  102. self.traverse_and_document_shape(
  103. section=value_section, shape=value_shape, history=history)
  104. end_bracket_section = map_section.add_new_section('ending-bracket')
  105. self._end_nested_param(end_bracket_section, '}')
  106. def _add_members_to_shape(self, members, include):
  107. if include:
  108. members = members.copy()
  109. for param in include:
  110. members[param.name] = param
  111. return members
  112. def _start_nested_param(self, section, start=None):
  113. if start is not None:
  114. section.write(start)
  115. section.style.indent()
  116. section.style.indent()
  117. section.style.new_line()
  118. def _end_nested_param(self, section, end=None):
  119. section.style.dedent()
  120. section.style.dedent()
  121. section.style.new_line()
  122. if end is not None:
  123. section.write(end)
  124. def _end_structure(self, section, start, end):
  125. # If there are no members in the strucuture, then make sure the
  126. # start and the end bracket are on the same line, by removing all
  127. # previous text and writing the start and end.
  128. if not section.available_sections:
  129. section.clear_text()
  130. section.write(start + end)
  131. self._end_nested_param(section)
  132. else:
  133. end_bracket_section = section.add_new_section('ending-bracket')
  134. self._end_nested_param(end_bracket_section, end)
  135. class ResponseExampleDocumenter(BaseExampleDocumenter):
  136. EVENT_NAME = 'response-example'
  137. class RequestExampleDocumenter(BaseExampleDocumenter):
  138. EVENT_NAME = 'request-example'
  139. def document_shape_type_structure(self, section, shape, history,
  140. include=None, exclude=None, **kwargs):
  141. param_format = '\'%s\''
  142. operator = ': '
  143. start = '{'
  144. end = '}'
  145. if len(history) <= 1:
  146. operator = '='
  147. start = '('
  148. end = ')'
  149. param_format = '%s'
  150. section = section.add_new_section('structure-value')
  151. self._start_nested_param(section, start)
  152. input_members = self._add_members_to_shape(shape.members, include)
  153. for i, param in enumerate(input_members):
  154. if exclude and param in exclude:
  155. continue
  156. param_section = section.add_new_section(param)
  157. param_section.write(param_format % param)
  158. param_section.write(operator)
  159. param_shape = input_members[param]
  160. param_value_section = param_section.add_new_section(
  161. 'member-value', context={'shape': param_shape.name})
  162. self.traverse_and_document_shape(
  163. section=param_value_section, shape=param_shape,
  164. history=history, name=param)
  165. if i < len(input_members) - 1:
  166. ending_comma_section = param_section.add_new_section(
  167. 'ending-comma')
  168. ending_comma_section.write(',')
  169. ending_comma_section.style.new_line()
  170. self._end_structure(section, start, end)